function FHEM_startLongpoll(connection) {
  if( FHEM_longpoll_running )
    return;
  FHEM_longpoll_running = true;

  var filter = ".*";
  var since = "null";
  var query = "/fhem.pl?XHR=1"+
              "&inform=type=status;filter="+filter+";since="+since+";fmt=JSON"+
              "&timestamp="+Date.now()

  var url = encodeURI( connection.base_url + query );
  console.log( 'starting longpoll: ' + url );

  var FHEM_longpollOffset = 0;
  var input = "";
  connection.request.get( { url: url } ).on( 'data', function(data) {
//console.log( 'data: '+ data );
                 if( !data )
                   return;

                 input += data;
                 for(;;) {
                   var nOff = input.indexOf("\n", FHEM_longpollOffset);
                   if(nOff < 0)
                     break;
                   var l = input.substr(FHEM_longpollOffset, nOff-FHEM_longpollOffset);
                   FHEM_longpollOffset = nOff+1;
//console.log( "Rcvd: "+(l.length>132 ? l.substring(0,132)+"...("+l.length+")":l) );
                   if(!l.length)
                     continue;

                   var d;
                   if( l.substr(0,1) == '[' )
                     d = JSON.parse(l);
                   else
                     d = l.split("<<", 3);

                   //console.log(d);

                   if(d.length != 3)
                     continue;
                   if(d[0].match(/-ts$/))
                     continue;

//console.log( "Rcvd: "+(l.length>132 ? l.substring(0,132)+"...("+l.length+")":l) );

                   var subscription = FHEM_subscriptions[d[0]];
                   if( subscription != undefined ) {
//console.log( "Rcvd: "+(l.length>132 ? l.substring(0,132)+"...("+l.length+")":l) );
                     FHEM_lastEventTimestamp = Date.now();
                     var accessory = subscription.accessory;

                     var value = d[1];
                     if( value.match( /^set-/ ) )
                       continue;
                     if( value.match( /^set_/ ) )
                       continue;

                     var match = d[0].match(/([^-]*)-(.*)/);
                     var device = match[1];
                     var reading = match[2];
                     if( reading == undefined )
                       continue;

                     if( reading == 'state') {
                       if( accessory.mappings.window ) {
                         var level = 50;
                         if( match = value.match(/^(\d+)/ ) )
                           level = parseInt( match[1] );
                         else if( value == 'locked' )
                           level = 0;

                         FHEM_update( accessory.mappings.window.informId, level );
                         continue;

                       } else if( accessory.mappings.lock ) {
                         var lock = 0;
                         if( value.match( /^locked/ ) )
                           lock = 1;

                         if( value.match( /uncertain/ ) )
                           level = 4;

                         FHEM_update( accessory.mappings.lock.informId, lock );
                         continue;

                       } else if( match = value.match(/dim(\d+)%/ ) ) {
                         var pct = parseInt( match[1] );

                         FHEM_update( device+'-pct', pct );
                       }

                     } else if(accessory.mappings.rgb && reading == accessory.mappings.rgb.reading) {
                       var hsv = FHEM_rgb2hsv(value);
                       var hue = parseInt( hsv[0] * 360 );
                       var sat = parseInt( hsv[1] * 100 );
                       var bri = parseInt( hsv[2] * 100 );

                       //FHEM_update( device+'-'+reading, value, false );
                       FHEM_update( device+'-hue', hue );
                       FHEM_update( device+'-sat', sat );
                       FHEM_update( device+'-bri', bri );
                       continue;
                     }

                     value = accessory.reading2homekit(reading, value);
                     FHEM_update( device+'-'+reading, value );

                   } else {
                   }

                 }

                 input = input.substr(FHEM_longpollOffset);
                 FHEM_longpollOffset = 0;

               } ).on( 'end', function() {
                 console.log( "longpoll ended" );

                 FHEM_longpoll_running = false;
                 setTimeout( function(){FHEM_startLongpoll(connection)}, 2000 );

               } ).on( 'error', function(err) {
                 console.log( "longpoll error: " + err );

                 FHEM_longpoll_running = false;
                 setTimeout( function(){FHEM_startLongpoll(connection)}, 5000 );

               } );
}
//FIXME: force reconnect on xxx bytes received ?, add filter, add since
force reconnect on xxx bytes received ?, add filter, add since
force reconnect on xxx bytes received ? add filter add since
function FHEM_startLongpoll(connection) {
  if( FHEM_longpoll_running )
    return;
  FHEM_longpoll_running = true;
  var filter = ".*";
  var since = "null";
  var query = "/fhem.pl?XHR=1"+
              "&inform=type=status;filter="+filter+";since="+since+";fmt=JSON"+
              "&timestamp="+Date.now()
  var url = encodeURI( connection.base_url + query );
  console.log( 'starting longpoll: ' + url );
  var FHEM_longpollOffset = 0;
  var input = "";
  connection.request.get( { url: url } ).on( 'data', function(data) {
                 if( !data )
                   return;
                 input += data;
                 for(;;) {
                   var nOff = input.indexOf("\n", FHEM_longpollOffset);
                   if(nOff < 0)
                     break;
                   var l = input.substr(FHEM_longpollOffset, nOff-FHEM_longpollOffset);
                   FHEM_longpollOffset = nOff+1;
                   if(!l.length)
                     continue;
                   var d;
                   if( l.substr(0,1) == '[' )
                     d = JSON.parse(l);
                   else
                     d = l.split("<<", 3);
                   if(d.length != 3)
                     continue;
                   if(d[0].match(/-ts$/))
                     continue;
                   var subscription = FHEM_subscriptions[d[0]];
                   if( subscription != undefined ) {
                     FHEM_lastEventTimestamp = Date.now();
                     var accessory = subscription.accessory;
                     var value = d[1];
                     if( value.match( /^set-/ ) )
                       continue;
                     if( value.match( /^set_/ ) )
                       continue;
                     var match = d[0].match(/([^-]*)-(.*)/);
                     var device = match[1];
                     var reading = match[2];
                     if( reading == undefined )
                       continue;
                     if( reading == 'state') {
                       if( accessory.mappings.window ) {
                         var level = 50;
                         if( match = value.match(/^(\d+)/ ) )
                           level = parseInt( match[1] );
                         else if( value == 'locked' )
                           level = 0;
                         FHEM_update( accessory.mappings.window.informId, level );
                         continue;
                       } else if( accessory.mappings.lock ) {
                         var lock = 0;
                         if( value.match( /^locked/ ) )
                           lock = 1;
                         if( value.match( /uncertain/ ) )
                           level = 4;
                         FHEM_update( accessory.mappings.lock.informId, lock );
                         continue;
                       } else if( match = value.match(/dim(\d+)%/ ) ) {
                         var pct = parseInt( match[1] );
                         FHEM_update( device+'-pct', pct );
                       }
                     } else if(accessory.mappings.rgb && reading == accessory.mappings.rgb.reading) {
                       var hsv = FHEM_rgb2hsv(value);
                       var hue = parseInt( hsv[0] * 360 );
                       var sat = parseInt( hsv[1] * 100 );
                       var bri = parseInt( hsv[2] * 100 );
                       FHEM_update( device+'-hue', hue );
                       FHEM_update( device+'-sat', sat );
                       FHEM_update( device+'-bri', bri );
                       continue;
                     }
                     value = accessory.reading2homekit(reading, value);
                     FHEM_update( device+'-'+reading, value );
                   } else {
                   }
                 }
                 input = input.substr(FHEM_longpollOffset);
                 FHEM_longpollOffset = 0;
               } ).on( 'end', function() {
                 console.log( "longpoll ended" );
                 FHEM_longpoll_running = false;
                 setTimeout( function(){FHEM_startLongpoll(connection)}, 2000 );
               } ).on( 'error', function(err) {
                 console.log( "longpoll error: " + err );
                 FHEM_longpoll_running = false;
                 setTimeout( function(){FHEM_startLongpoll(connection)}, 5000 );
               } );
}
function FHEM_startLongpoll ( connection ) { if ( FHEM_longpoll_running ) return ; FHEM_longpoll_running = true ; var filter = ".*" ; var since = "null" ; var query = "/fhem.pl?XHR=1" + "&inform=type=status;filter=" + filter + ";since=" + since + ";fmt=JSON" + "&timestamp=" + Date . now ( ) var url = encodeURI ( connection . base_url + query ) ; console . log ( 'starting longpoll: ' + url ) ; var FHEM_longpollOffset = 0 ; var input = "" ; connection . request . get ( { url : url } ) . on ( 'data' , function ( data ) { if ( ! data ) return ; input += data ; for ( ; ; ) { var nOff = input . indexOf ( "\n" , FHEM_longpollOffset ) ; if ( nOff < 0 ) break ; var l = input . substr ( FHEM_longpollOffset , nOff - FHEM_longpollOffset ) ; FHEM_longpollOffset = nOff + 1 ; if ( ! l . length ) continue ; var d ; if ( l . substr ( 0 , 1 ) == '[' ) d = JSON . parse ( l ) ; else d = l . split ( "<<" , 3 ) ; if ( d . length != 3 ) continue ; if ( d [ 0 ] . match ( / -ts$ / ) ) continue ; var subscription = FHEM_subscriptions [ d [ 0 ] ] ; if ( subscription != undefined ) { FHEM_lastEventTimestamp = Date . now ( ) ; var accessory = subscription . accessory ; var value = d [ 1 ] ; if ( value . match ( / ^set- / ) ) continue ; if ( value . match ( / ^set_ / ) ) continue ; var match = d [ 0 ] . match ( / ([^-]*)-(.*) / ) ; var device = match [ 1 ] ; var reading = match [ 2 ] ; if ( reading == undefined ) continue ; if ( reading == 'state' ) { if ( accessory . mappings . window ) { var level = 50 ; if ( match = value . match ( / ^(\d+) / ) ) level = parseInt ( match [ 1 ] ) ; else if ( value == 'locked' ) level = 0 ; FHEM_update ( accessory . mappings . window . informId , level ) ; continue ; } else if ( accessory . mappings . lock ) { var lock = 0 ; if ( value . match ( / ^locked / ) ) lock = 1 ; if ( value . match ( / uncertain / ) ) level = 4 ; FHEM_update ( accessory . mappings . lock . informId , lock ) ; continue ; } else if ( match = value . match ( / dim(\d+)% / ) ) { var pct = parseInt ( match [ 1 ] ) ; FHEM_update ( device + '-pct' , pct ) ; } } else if ( accessory . mappings . rgb && reading == accessory . mappings . rgb . reading ) { var hsv = FHEM_rgb2hsv ( value ) ; var hue = parseInt ( hsv [ 0 ] * 360 ) ; var sat = parseInt ( hsv [ 1 ] * 100 ) ; var bri = parseInt ( hsv [ 2 ] * 100 ) ; FHEM_update ( device + '-hue' , hue ) ; FHEM_update ( device + '-sat' , sat ) ; FHEM_update ( device + '-bri' , bri ) ; continue ; } value = accessory . reading2homekit ( reading , value ) ; FHEM_update ( device + '-' + reading , value ) ; } else { } } input = input . substr ( FHEM_longpollOffset ) ; FHEM_longpollOffset = 0 ; } ) . on ( 'end' , function ( ) { console . log ( "longpoll ended" ) ; FHEM_longpoll_running = false ; setTimeout ( function ( ) { FHEM_startLongpoll ( connection ) } , 2000 ) ; } ) . on ( 'error' , function ( err ) { console . log ( "longpoll error: " + err ) ; FHEM_longpoll_running = false ; setTimeout ( function ( ) { FHEM_startLongpoll ( connection ) } , 5000 ) ; } ) ; }
FIXME: force reconnect on xxx bytes received ?, add filter, add since
FIXME : force reconnect on xxx bytes received ? add filter add since
//console.log( 'data: '+ data ); //console.log( "Rcvd: "+(l.length>132 ? l.substring(0,132)+"...("+l.length+")":l) ); //console.log(d); //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_update( device+'-'+reading, value, false );