ivesdebruycker / maxcube

eQ-3 Max! Cube interface
Other
22 stars 23 forks source link

Getting null for getDeviceInfo #13

Closed asantaga closed 7 years ago

asantaga commented 8 years ago

Ok, this is doing my head in.. Ive had this working before and now when I issue myMaxCube.getDeviceStatus I ALMOST constantly get null returned...

My code is simple

var MaxCube = require('maxcube'); var myMaxCube = new MaxCube('192.168.0.222', 62910); myMaxCube.on('connected', function () { console.log('Connected'); myMaxCube.getDeviceStatus().then(function (devices) { console.log(JSON.stringify((devices))); devices.forEach( function (device) { var deviceInfo = myMaxCube.getDeviceInfo(device.rf_address); console.log(JSON.stringify(deviceInfo)); } ); }); });

And the output is

Connected [{"rf_address":"13856e","initialized":true,"fromCmd":false,"error":false,"valid":true,"mode":"AUTO","dst_active":true,"gateway_known":true,"panel_locked":false,"link_error":false,"battery_low":false,"valve":0,"setpoint":16,"temp":21.3},{"rf_address":"1385f3","initialized":true,"fromCmd":false,"error":false,"valid":true,"mode":"AUTO","dst_active":true,"gateway_known":true,"panel_locked":true,"link_error":false,"battery_low":false,"valve":0,"setpoint":16,"temp":21.1},{"rf_address":"12e5fc","initialized":true,"fromCmd":false,"error":false,"valid":true,"mode":"MANUAL","dst_active":true,"gateway_known":true,"panel_locked":false,"link_error":false,"battery_low":false,"valve":100,"setpoint":22,"temp":21.1},{"rf_address":"1385de","initialized":true,"fromCmd":false,"error":false,"valid":true,"mode":"AUTO","dst_active":true,"gateway_known":true,"panel_locked":true,"link_error":false,"battery_low":false,"valve":0,"setpoint":16,"temp":20.1},{"rf_address":"138561","initialized":true,"fromCmd":false,"error":false,"valid":true,"mode":"AUTO","dst_active":true,"gateway_known":true,"panel_locked":true,"link_error":false,"battery_low":false,"valve":56,"setpoint":17.5,"temp":16.3},{"rf_address":"12e5c7","initialized":true,"fromCmd":false,"error":false,"valid":true,"mode":"MANUAL","dst_active":true,"gateway_known":true,"panel_locked":false,"link_error":false,"battery_low":false,"valve":100,"setpoint":22,"temp":21},{"rf_address":"0dd84e","initialized":true,"fromCmd":false,"error":false,"valid":true,"mode":"MANUAL","dst_active":true,"gateway_known":true,"panel_locked":false,"link_error":false,"battery_low":false,"valve":100,"setpoint":24,"temp":0},{"rf_address":"1381f8","initialized":true,"fromCmd":false,"error":false,"valid":true,"mode":"AUTO","dst_active":true,"gateway_known":true,"panel_locked":false,"link_error":false,"battery_low":false,"valve":0,"setpoint":16,"temp":20},{"rf_address":"12e5f4","initialized":true,"fromCmd":false,"error":false,"valid":true,"mode":"AUTO","dst_active":true,"gateway_known":true,"panel_locked":true,"link_error":false,"battery_low":false,"valve":0,"setpoint":15,"temp":0}] {"device_type":null,"device_name":null,"room_name":null,"room_id":null} {"device_type":null,"device_name":null,"room_name":null,"room_id":null} {"device_type":null,"device_name":null,"room_name":null,"room_id":null} {"device_type":null,"device_name":null,"room_name":null,"room_id":null} {"device_type":null,"device_name":null,"room_name":null,"room_id":null} {"device_type":null,"device_name":null,"room_name":null,"room_id":null} {"device_type":null,"device_name":null,"room_name":null,"room_id":null} {"device_type":null,"device_name":null,"room_name":null,"room_id":null} {"device_type":null,"device_name":null,"room_name":null,"room_id":null}

Strangely, maxcube-cli works perfectly everytime.. Im baffled.. any ideas?

asantaga commented 8 years ago

Done some debugging and I think I know whats going wrong.. The maxcube-lowlevel.js (connect) promise is returning too soon. From debugging Ive discovered that if you wait a second or so the data is retrieved correctly.. My gut feeling is that the initial exchange isnt completing to populate the cache and is why the rooms arent populated.. (basically command M is being processed AFTER the connect promise resolves... Im guessing this shouldnt happen?

NB, Im a JS Noob, and just starting to understand promises.

asantaga commented 8 years ago

issue identified : Cache being populated AFTER connect promise is fulfilled.

nasty workaround is to wait 500ms for the promises in the connection to complete before returning.. e.g. var MaxCube = require('maxcube'); var myMaxCube = new MaxCube('192.168.0.222', 62910); myMaxCube.on('connected', function () { console.log('Connected'); // wait 500ms, then query rooms setTimeout( function() { myMaxCube.getDeviceStatus().then(function (devices) { devices.forEach( function (device) { var deviceInfo = myMaxCube.getDeviceInfo(device.rf_address); console.log(JSON.stringify(deviceInfo)); } ); }); },500); });

I think the bug is in the maxcube-lowlevel.js(connect) method, its emitting the connected message too soon

ivesdebruycker commented 8 years ago

You're right, maxcube is only really ready when the device cache is populated, and this is done with the M-response received after connecting. So, fix is waiting for this response before emitting connected-event.

Could you check if b33969d fixes this issue?

asantaga commented 7 years ago

yup, b33969d fixes the issue.. Like the way youve structured the code BTW, alas Im new to JS/Node(especially promises) and its taken me a while to work out your code! Java is a doddle!