some cleanups

try to detect closed longpoll connection
added last event timestamp
This commit is contained in:
Andre Schröter
2015-08-01 17:47:42 +02:00
parent ab71dc352f
commit 939f79b2b0

View File

@@ -40,6 +40,7 @@ FHEM_update(inform_id, value, no_update) {
return; return;
FHEM_cached[inform_id] = value; FHEM_cached[inform_id] = value;
//FHEM_cached[inform_id] = { 'value': value, 'timestamp': Date.now() };
console.log(" caching: " + inform_id + ": " + value + " as " + typeof(value) ); console.log(" caching: " + inform_id + ": " + value + " as " + typeof(value) );
if( !no_update ) if( !no_update )
@@ -48,6 +49,7 @@ FHEM_update(inform_id, value, no_update) {
} }
var FHEM_lastEventTimestamp;
var FHEM_longpoll_running = false; var FHEM_longpoll_running = false;
//FIXME: force reconnect on xxx bytes received ?, add filter, add since //FIXME: force reconnect on xxx bytes received ?, add filter, add since
function FHEM_startLongpoll(connection) { function FHEM_startLongpoll(connection) {
@@ -59,7 +61,7 @@ function FHEM_startLongpoll(connection) {
var since = "null"; var since = "null";
var query = "/fhem.pl?XHR=1"+ var query = "/fhem.pl?XHR=1"+
"&inform=type=status;filter="+filter+";since="+since+";fmt=JSON"+ "&inform=type=status;filter="+filter+";since="+since+";fmt=JSON"+
"&timestamp="+new Date().getTime(); "&timestamp="+Date.now()
var url = encodeURI( connection.base_url + query ); var url = encodeURI( connection.base_url + query );
console.log( 'starting longpoll: ' + url ); console.log( 'starting longpoll: ' + url );
@@ -100,6 +102,7 @@ function FHEM_startLongpoll(connection) {
var subscription = FHEM_subscriptions[d[0]]; var subscription = FHEM_subscriptions[d[0]];
if( subscription != undefined ) { if( subscription != undefined ) {
//console.log( "Rcvd: "+(l.length>132 ? l.substring(0,132)+"...("+l.length+")":l) ); //console.log( "Rcvd: "+(l.length>132 ? l.substring(0,132)+"...("+l.length+")":l) );
FHEM_lastEventTimestamp = Date.now();
var accessory = subscription.accessory; var accessory = subscription.accessory;
var value = d[1]; var value = d[1];
@@ -135,6 +138,7 @@ function FHEM_startLongpoll(connection) {
value = accessory.reading2homekit(reading, value); value = accessory.reading2homekit(reading, value);
FHEM_update( device+'-'+reading, value ); FHEM_update( device+'-'+reading, value );
} else {
} }
} }
@@ -142,6 +146,23 @@ function FHEM_startLongpoll(connection) {
input = input.substr(FHEM_longpollOffset); input = input.substr(FHEM_longpollOffset);
FHEM_longpollOffset = 0; FHEM_longpollOffset = 0;
} ).on( 'end', function() {
console.log( "longpoll ended" );
FHEM_longpoll_running = false;
setTimeout( function(){FHEM_startLongpoll(connection)}, 5000 );
} ).on( 'close', function() {
console.log( "longpoll closed" );
FHEM_longpoll_running = false;
setTimeout( function(){FHEM_startLongpoll(connection)}, 5000 );
} ).on( 'finish', function() {
console.log( "longpoll finished" );
FHEM_longpoll_running = false;
setTimeout( function(){FHEM_startLongpoll(connection)}, 5000 );
} ).on( 'error', function(err) { } ).on( 'error', function(err) {
console.log( "longpoll error: " + err ); console.log( "longpoll error: " + err );
@@ -437,10 +458,6 @@ FHEMAccessory(log, connection, s) {
s.isOccupancySensor = true; s.isOccupancySensor = true;
else if( s.Attributes.model == 'fs20di' ) else if( s.Attributes.model == 'fs20di' )
s.isLight = true; s.isLight = true;
//FIXME: set isSwitch to reading: state/transportState/... (on/off / play/pause)
//else if( s.PossibleSets.match(/\bon\b/)
// && s.PossibleSets.match(/\boff\b/) )
// s.isSwitch = true;
if( s.PossibleSets.match(/\bdesired-temp\b/) ) if( s.PossibleSets.match(/\bdesired-temp\b/) )
s.isThermostat = 'desired-temp'; s.isThermostat = 'desired-temp';
@@ -451,11 +468,12 @@ FHEMAccessory(log, connection, s) {
log( s.Internals.NAME + ' is NOT a thermostat. set for target temperature missing' ); log( s.Internals.NAME + ' is NOT a thermostat. set for target temperature missing' );
} }
this.endpoints = {};
if( s.Internals.TYPE == 'SONOSPLAYER' ) if( s.Internals.TYPE == 'SONOSPLAYER' )
this.hasOnOff = { reading: 'transportState', cmdOn: 'play', cmdOff: 'pause' }; this.endpoints.onOff = { reading: 'transportState', cmdOn: 'play', cmdOff: 'pause' };
else if( s.PossibleSets.match(/\bon\b/) else if( s.PossibleSets.match(/\bon\b/)
&& s.PossibleSets.match(/\boff\b/) ) && s.PossibleSets.match(/\boff\b/) )
this.hasOnOff = { reading: 'state', cmdOn: 'on', cmdOff: 'off' }; this.endpoints.onOff = { reading: 'state', cmdOn: 'on', cmdOff: 'off' };
var event_map = s.Attributes.eventMap; var event_map = s.Attributes.eventMap;
if( event_map ) { if( event_map ) {
@@ -692,13 +710,13 @@ FHEMAccessory.prototype = {
//this.log( this.name + ' old : [' + h + ',' + s + ',' + v + ']' ); //this.log( this.name + ' old : [' + h + ',' + s + ',' + v + ']' );
if( c == 'H-rgb' ) { if( c == 'H-rgb' ) {
FHEM_cached[this.device + '-hue' ] = value; FHEM_update(this.device + '-hue', value, false );
h = value / 360; h = value / 360;
} else if( c == 'S-rgb' ) { } else if( c == 'S-rgb' ) {
FHEM_cached[this.device + '-sat' ] = value; FHEM_update(this.device + '-sat', value, false );
s = value / 100; s = value / 100;
} else if( c == 'B-rgb' ) { } else if( c == 'B-rgb' ) {
FHEM_cached[this.device + '-bri' ] = value; FHEM_update(this.device + '-bri', value, false );
v = value / 100; v = value / 100;
} }
//this.log( this.name + ' new : [' + h + ',' + s + ',' + v + ']' ); //this.log( this.name + ' new : [' + h + ',' + s + ',' + v + ']' );
@@ -908,32 +926,30 @@ FHEMAccessory.prototype = {
designedMaxLength: 255 designedMaxLength: 255
}] }]
if( this.name != undefined //if( this.name != undefined
&& !this.hasTemperature // && !this.hasTemperature
&& !this.hasHumidity // && !this.hasHumidity
&& !this.isBlind // && !this.isBlind
&& !this.isThermostat // && !this.isThermostat
&& !this.isContactSensor // && !this.isContactSensor
&& !this.isOccupancySensor ) { // && !this.isOccupancySensor ) {
if( this.endpoints.onOff ) {
cTypes.push({ cTypes.push({
cType: types.POWER_STATE_CTYPE, cType: types.POWER_STATE_CTYPE,
onRegister: function(characteristic) { onRegister: function(characteristic) {
characteristic.eventEnabled = true; characteristic.eventEnabled = true;
if( that.type == 'SONOSPLAYER' ) FHEM_subscribe(characteristic, that.name+'-'+that.endpoints.onOff.reading, that);
FHEM_subscribe(characteristic, that.name+'-transportState', that);
else
FHEM_subscribe(characteristic, that.name+'-state', that);
}, },
onUpdate: function(value) { onUpdate: function(value) {
that.command( value == 0 ? 'off' : 'on' ); that.command( value == 0 ? 'off' : 'on' );
}, },
onRead: function(callback) { onRead: function(callback) {
that.query( that.type == 'SONOSPLAYER' ? 'transportState' : 'state', function(state){ callback(state) } ); that.query( that.endpoints.onOff.reading, function(state){ callback(state) } );
}, },
perms: ["pw","pr","ev"], perms: ["pw","pr","ev"],
format: "bool", format: "bool",
initialValue: 0, initialValue: 0,
//initialValue: that.query( that.type == 'SONOSPLAYER' ? 'transportState' : 'state' ), //initialValue: that.query( that.endpoints.onOff.reading );
supportEvents: true, supportEvents: true,
supportBonjour: false, supportBonjour: false,
manfDescription: "Change the power state", manfDescription: "Change the power state",
@@ -1460,6 +1476,8 @@ function FHEMdebug_handleRequest(request, response){
if( request.url == "/cached" ) { if( request.url == "/cached" ) {
response.write( "<a href='/'>home</a><br>" ); response.write( "<a href='/'>home</a><br>" );
if( FHEM_lastEventTimestamp )
response.write( "FHEM_lastEventTime: "+ new Date(FHEM_lastEventTimestamp) +"<br>" );
response.end( "cached: " + util.inspect(FHEM_cached).replace(/\n/g, '<br>') ); response.end( "cached: " + util.inspect(FHEM_cached).replace(/\n/g, '<br>') );
} else if( request.url == "/subscriptions" ) { } else if( request.url == "/subscriptions" ) {