Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Visualisierung
    4. socket.io subscribe funktioniert nicht

    NEWS

    • Neuer Blog: Fotos und Eindrücke aus Solingen

    • ioBroker@Smart Living Forum Solingen, 14.06. - Agenda added

    • ioBroker goes Matter ... Matter Adapter in Stable

    socket.io subscribe funktioniert nicht

    This topic has been deleted. Only users with topic management privileges can see it.
    • F
      flohohoho last edited by

      Hallo liebe Community,

      nach dem ich nun mit den Kräften am Ende bin ist dies mein vorerst letzter Versuch. Ich versuche eine Visualisierung ohne Vis aus Schlankheitsgründen auf die Beine zu stellen. Mit socket.io funktioniert es auch soweit. Nur möchte ich die Autosubscription nicht haben und die nötigen states abonnieren.

      Autosubscription habe ich bereits deaktiviert. Ich schaffe es allerdings nicht, dass ich die States abonniere. Meine settings.js sieht so aus:

      servConn.namespace   = 'mobile.0';
      servConn._useStorage = false;
      
          var states = [];
          servConn.init({
              name:          'mobile.0',  // optional - default 'vis.0'
              connLink:      'http://192.168.10.5:8084',  // optional URL of the socket.io adapter
              socketSession: '',           // optional - used by authentication
              
          }, {
              onConnChange: function (isConnected) {
                  if (isConnected) {
                      console.log('connected');
                      servConn.getStates(function (err, _states) {
                          var count = 0;
                          for (var id in _states) {
                              count++;
                          }
                          console.log('Received ' + count + ' states.');
                          states = _states;
                      });
                  } else {
                      console.log('disconnected');
                  }
              },
              onRefresh: function (id, state) {
                  window.location.reload();
              },
              onUpdate: function (id, state) {
                  setTimeout(function () {
                      console.log('NEW VALUE of ' + id + ': ' + JSON.stringify(state));
                      states[id] = state;
                  }, 0);
              },
              onError: function (err) {
                  window.alert(_('Cannot execute %s for %s, because of insufficient permissions', err.command, err.arg), _('Insufficient permissions'), 'alert', 600);
              }    
          }, 
          false, 
          false
      );
       
      
      let helligkeit = 'openknx.0.Sensorik_EG.Helligkeit.Heeligkeit_Essen';
      servConn.subscribe(helligkeit); 
      
      

      Zum Schluss habe ich die Subscrition reingeschrieben. Gibt es hier offensichtliche Fehler? Ich habe schon alle Möglichen Varianten aus dem Forum durch. Die Console zeigt keine Fehler.

      Viele Grüße
      Flo

      R 1 Reply Last reply Reply Quote 0
      • R
        rewenode @flohohoho last edited by

        @flohohoho Also ist ne weile her, dass ich damit mal rumgespielt habe, aber subscribe sah bei mir da ungefähr so aus:

        debugger
        /*
        payload = {fn: 0  //fungtion0..2 
                   subscribePattern: 'zigbee.0'  //subscription    
        }
        
        node functions
        0 - init 
        1 - disconnect
        2 - subscribe
        3 - unsubscribe
        */
        
        let nFunction = msg.payload.fn;
        
        // servConn
        const servConn = global.get('servConn');
        
        // helper
        const mydTime = () => {
            var options = {
                hour: 'numeric',
                minute: 'numeric',
                second: 'numeric'
            };
            return ((new Date()).toLocaleDateString('de-DE', options))
        };
        
        const connStatus  = () => {
            let cTime = mydTime();
            if (servConn._isConnected ) {
                node.status({fill: 'green', shape: "dot", text: 'connected:'+cTime}) ;
            } else {
                node.status({fill: 'red', shape: "dot", text: 'disconnected:'+cTime});
            }
            //debugger;
            console.log('nr connected: '+ servConn._isConnected + ' ' + cTime + ' id:' + servConn._socket.id);
        };
        
        const disconnect = () => {
            //reconnect interval
            servConn._allowReconnection = false;
            servConn._socket.close();
            servConn.logout();
            connStatus();
        };
        
        node.on('close', disconnect);
        
        //servConn._useStorage = false;
        
        var states = [];
        
        // init
        if (nFunction === 0 && !servConn._isConnected ) {
        servConn.init({
                name: 'node-red.' + node.id, // optional - default 'vis.0'
                connLink: 'http://192.168.99.119:8084', // optional URL of the socket.io adapter
                socketSession: ''
            }, {
                onConnChange: function(isConnected) {
                    connStatus();
                    /*
                    if (isConnected) {
                        console.log('connected');
                        node.status({fill: 'green',shape: "dot",text: 'connectet:' + servConn._isConnected});
                        
                        
                        servConn.getStates(function(err, _states) {
                            let count = 0;
                            for (var id in _states) {
                                count++;
                            }
                            node.status({fill: 'green',shape: "dot",text: 'Received ' + count});
        
                            states = _states;
                        });
                       
                    } else {
                        node.status({fill: 'red',shape: "dot",text: 'disconnected'});
                    
                    }
                     */
                },
                // not in use
                onRefresh: function() {
        
                },
                onUpdate: function(id, state) {
                    setTimeout(function() {
                        //console.log('NEW VALUE of ' + id + ': ' + JSON.stringify(state));
                        msg.payload.id = id;
                        msg.payload.state = state;
                        node.send(msg);
                        states[id] = state;
                    }, 0);
                },
                onError: function(err) {
                    //console.log('insufficient permissions' + err.command);
                    node.status({fill: 'red',shape: "dot",text: 'insufficient permissions'});
                }
            },
            false,
            false);
        }
        
        // disconnect
        if (nFunction === 1 && servConn._isConnected) { 
            disconnect();
        }
        //subscribe
        if (nFunction === 2 && servConn._isConnected) { 
            msg.payload.subscribePattern.forEach(
                (subscription) => servConn._socket.emit('subscribe', subscription)
                )
        }
        //unsubscribe
        if (nFunction === 3 && servConn._isConnected) { 
            msg.payload.subscribePattern.forEach(
                (subscription) => servConn._socket.emit('unsubscribe', subscription)
                )
        }
        
        //getstate
        if (nFunction === 4 && servConn._isConnected) { 
            msg.payload.subscribePattern.forEach( 
                (subscription) => servConn.getStates(subscription, function(err, state) {
                    setTimeout(function() {
                        console.log('State ' + subscription + ': ' + JSON.stringify(state));
                        msg.payload = state;
                        node.send(msg);
                    }, 0);
                })
            )
        }
        
        

        Hatte das seinerzeit in einem node-red function-node implementiert und grad noch mal mit einer einzelnen subscription getestet. Läuft noch 😉

        Gruß
        Reiner

        F 1 Reply Last reply Reply Quote 0
        • F
          flohohoho @rewenode last edited by

          @rewenode
          Vielen vielen Dank, das hat geklappt! Habe mir nun ein Array mit den Sates angelegt.

          servConn._socket.emit('subscribe', subscriptions)
          
          1 Reply Last reply Reply Quote 0
          • R
            rewenode last edited by

            @flohohoho :👍

            C 1 Reply Last reply Reply Quote 0
            • C
              chefffe @rewenode last edited by chefffe

              @flohohoho
              Hallo,

              ich bin auch gerade an dem Thema dran per socket.IO dran und möchte bestimmte Datenpunkte abfragen und anzeigen.
              Mir ist noch nicht ganz klar wie ich die abonnierten Datenpunkte (ids) in ein Array packe und einzeln abfragen kann.
              Hast du einen Tipp für mich wie du es bei dir gelöst hast?

              1 Reply Last reply Reply Quote 0
              • First post
                Last post

              Support us

              ioBroker
              Community Adapters
              Donate

              853
              Online

              31.9k
              Users

              80.1k
              Topics

              1.3m
              Posts

              3
              5
              437
              Loading More Posts
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes
              Reply
              • Reply as topic
              Log in to reply
              Community
              Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen
              The ioBroker Community 2014-2023
              logo