Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Cannot call method 'some' of undefined #276

Closed
ariafloat opened this issue Jan 12, 2016 · 7 comments
Closed

Error: Cannot call method 'some' of undefined #276

ariafloat opened this issue Jan 12, 2016 · 7 comments

Comments

@ariafloat
Copy link

In the following application,

module.exports = function(server) {
  var photocellQuery = server.where({ type: 'photocell' });
  var ledQuery = server.where({ type: 'led' });
  server.observe([photocellQuery, ledQuery], function(photocell, led){
    photocell.streams.intensity.on('data', function(m) {
      if(m.data < 0.5) {
        if (led.available('turn-on')) {
          led.call('turn-on');
        }
      } else {
        if (led.available('turn-off')) {
          led.call('turn-off');
       }
     }
   });
});}

When [led.call('turn-off');] is called after [led.call('turn-on');] was called, the following error is output in Zetta-Server.

TypeError: Cannot call method 'some' of undefined
    at VirtualDevice._getAction (C:\zetta-dev\node_modules\zetta\lib\virtual_device.js:151:17)
    at VirtualDevice.call (C:\zetta-dev\node_modules\zetta\lib\virtual_device.js:105:21)
    at null.<anonymous> (C:\zetta-dev\apps\myapp.js:29:27)
    at EventEmitter.emit (events.js:95:17)
    at null.<anonymous> (_stream_readable.js:745:14)
    at EventEmitter.emit (events.js:92:17)
    at emitReadable_ (_stream_readable.js:407:10)
    at emitReadable (_stream_readable.js:403:5)
    at readableAddChunk (_stream_readable.js:165:9)
    at Readable.push (_stream_readable.js:127:10)
    at C:\zetta-dev\node_modules\zetta-device\node_modules\zetta-streams\consumer_stream.js:12:32
@mdobson
Copy link
Contributor

mdobson commented Jan 12, 2016

Hi @ariafloat! A few questions for you.

  • What version of Zetta are you using?
  • What version of the LED Driver are you using?

Thanks

Matt

@ariafloat
Copy link
Author

・What version of Zetta are you using?
It can't be checked right now. It'll be checked tomorrow.

・What version of the LED Driver are you using?
own driver.

@AdamMagaluk
Copy link
Collaborator

@ariafloat Does your app run on the same zetta process as where the devices run? Or does the zetta process with the app run on a zetta process you link to? Trying to figure out why virtual_device is being used, based on your app you supplied it should only look for local devices and never use a virtual_device.

Maybe provide your complete zetta server file?

This is the file i'm using to reproduce it. Tried with both [email protected] and [email protected]

var zetta = require('zetta');
var Led = require('zetta-led-mock-driver');
var Photocell = require('zetta-photocell-mock-driver');
var MemoryRegistries = require('zetta-memory-registry')(zetta);
var PeerRegistry = MemoryRegistries.PeerRegistry;
var DeviceRegistry = MemoryRegistries.DeviceRegistry;

var app = function(server) {
  var photocellQuery = server.where({ type: 'photocell' });
  var ledQuery = server.where({ type: 'led' });
  server.observe([photocellQuery, ledQuery], function(photocell, led){
    photocell.streams.intensity.on('data', function(m) {
      if(m.data < 0.5) {
        if (led.available('turn-on')) {
          led.call('turn-on', function(){});
        }
      } else {
        if (led.available('turn-off')) {
          led.call('turn-off', function(){});
        }
      }
    });
  });
}

zetta({ registry: new DeviceRegistry(), peerRegistry: new PeerRegistry() })
  .name('Dillon')
  .use(app)
  .use(Led)
  .use(Photocell)
  .listen(0)

Output

Jan-12-2016 10:30:44 [scout] Device (led) 4c4b1373-76c7-4716-bb0e-38c01805605d was discovered
Jan-12-2016 10:30:44 [scout] Device (photocell) 25339e11-59f6-4d48-b203-da79e7afcc27 was discovered
Jan-12-2016 10:30:44 [server] Server (Dillon) Dillon listening on http://127.0.0.1:0
Jan-12-2016 10:30:45 [device] led transition turn-on
Jan-12-2016 10:30:46 [device] led transition turn-off
Jan-12-2016 10:30:48 [device] led transition turn-on
Jan-12-2016 10:30:48 [device] led transition turn-off

@AdamMagaluk
Copy link
Collaborator

It may also help to see what versions of zetta and zetta-device you are using.

Could you give the output of npm ls of your project?

@ariafloat
Copy link
Author

Versions are the following.

  "dependencies": {
    "argv": "0.0.2",
    "zetta": "^1.0.0-beta.5",
    "zetta-device": "^0.17.0",
    "zetta-memory-registry": "^0.1.0"
  }

All codes are the following.

core zetta server

[server.js] is the following.

var zetta = require('zetta');
var myApp = require('./apps/myapp.js');
var argv = require('argv');
var MemoryRegistries = require('zetta-memory-registry')(zetta);
var PeerRegistry = MemoryRegistries.PeerRegistry;
var DeviceRegistry = MemoryRegistries.DeviceRegistry;

zetta({ registry: new DeviceRegistry(), peerRegistry: new PeerRegistry() })
  .name('core')
  .load(myApp)
  .listen(1337);

App code is the following.

module.exports = function (server) {
    var ledQuery = server.from('*').where({
        type: 'led'
    });
    var distanceQuery = server.from('*').where({
        type: 'distance'
    });

    server.observe([ledQuery, distanceQuery], function (led, distance) {
        distance.streams.level.on('data', function (m) {
            console.log('level: ' + m.data + ', ' + 'led: ' + led.state);
            if (m.data >= 30 && led.state === 'off') {
                led.call('turn-on');
            } else if (m.data <= 20 && led.state === 'on') {
                led.call('turn-off');
            }
        });
    });

};

Run the core zetta server

node server.js

end zetta server

[server-end.js] is the following.

var zetta = require('zetta');
var mockLed = require('./drivers/mock-led.js');
var mockDistance = require('./drivers/mock-distance.js');
var argv = require('argv');
var MemoryRegistries = require('zetta-memory-registry')(zetta);
var PeerRegistry = MemoryRegistries.PeerRegistry;
var DeviceRegistry = MemoryRegistries.DeviceRegistry;

var args = argv.option([{
  name: 'link',
  short: 'l',
  type: 'string'
}]).run();

var linkServer = args.options.link || 'http://localhost:1337';

zetta({ registry: new DeviceRegistry(), peerRegistry: new PeerRegistry() })
  .name('end')
  .use(mockLed)
  .use(mockDistance)
  .link(linkServer)
  .listen(1338);

Driver[mock-led.js] is the following.

var Device = require('zetta-device');
var util = require('util');

var LED = module.exports = function () {
  Device.call(this);
};
util.inherits(LED, Device);

LED.prototype.init = function (config) {
    config
    .name('led')
    .type('led')
    .state('off')
    .when('off', { allow: ['turn-on'] })
    .when('on', { allow: ['turn-off'] })
    .map('turn-on', this.turnOn)
    .map('turn-off', this.turnOff);
};

LED.prototype.turnOn = function (cb) {
  this.state = 'on';
  cb();
};

LED.prototype.turnOff = function (cb) {
  this.state = 'off';
  cb();
};

Driver[mock-distance.js] is the following.

var Device = require('zetta-device');
var util = require('util');

var Distance = module.exports = function(distance) {
  Device.call(this);
  this.distance = distance;
};
util.inherits(Distance, Device);

Distance.prototype.init = function (config) {
    config
    .name('distance')
    .type('distance')
    .stream('level', this.streamDistance, 'object');
};

var flag = true;
Distance.prototype.streamDistance = function (stream) {
    setInterval(function () {
        if(flag){
            stream.write(30);
            flag = false;
        } else {
            stream.write(20);
            flag = true;
        }
    }, 1000);
}; 

Run the end zetta server linking the core server

node server-end.js -l http://XXX.XXX.XXX.XXX:1337

[XXX.XXX.XXX.XXX] is the core server address.

@AdamMagaluk
Copy link
Collaborator

Did find the issue:

We call ._update() https://github.com/zettajs/zetta/blob/master/lib/virtual_device.js#L44 when a log messages is received but the format does not seem to match what's expected, not sure when this changed.

What I get back from the log topic

{ topic: 'led/a76ae181-66cf-4bfe-8c7c-6b714ebc1f58/logs',
  timestamp: 1452701209048,
  transition: 'turn-off',
  input: [],
  properties:
   { id: 'a76ae181-66cf-4bfe-8c7c-6b714ebc1f58',
     type: 'led',
     name: 'led',
     state: 'off' },
  transitions: { 'turn-on': {} } }

We expect it to have .actions field like what's returned in a transition. https://github.com/zettajs/zetta/blob/master/lib/virtual_device.js#L137-L147

@AdamMagaluk
Copy link
Collaborator

Ok I think i see what's going on here. In zetta-device we generate that object https://github.com/zettajs/zetta-device/blob/master/device.js#L354-L367 for all external websockets reading the logs stream we rewrite the object to the normal siren format you would see from a transition response. This is done in event_socket https://github.com/zettajs/zetta/blob/master/lib/event_socket.js#L162-L170, we have to do this to format the api hrefs correctly based on where the client is connecting.

For the virtual_device we manually subscribe to the internals https://github.com/zettajs/zetta/blob/master/lib/virtual_device.js#L43-L46 which does not include the reformating. I think we only see this sometimes because generally the virtual_device gets updated by the logs stream and the response from the transition which overwrites the invalid logs one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants