Apollon77 / hap-controller-node

Node.js library to implement a HAP (HomeKit) controller
Mozilla Public License 2.0
55 stars 15 forks source link

persistence of subscribeCharacteristics after reconnections #21

Closed NebzHB closed 3 years ago

NebzHB commented 3 years ago

Hello,

i'm using a combination of your exemples like that :

const {HttpClient, IPDiscovery} = require('hap-controller');

const discovery = new IPDiscovery();

const pairingData = {
            "AccessoryPairingID": "aaaaaaaaaaaa",
            "AccessoryLTPK": "bbbbbbbbbbbbb",
            "iOSDevicePairingID": "cccccccccccc",
            "iOSDeviceLTSK": "ddddddddddd",
            "iOSDeviceLTPK": "eeeeeeeeeee"
        };

const characteristics = [
  '3.268','2.274'  // aid.iid
];

discovery.on('serviceUp', (service) => {
    //console.log("found : ",service);
    if(service.id != "85:E2:A5:EA:9F:32") return;
 console.log('Found device!');
 console.log(service);

  const client = new HttpClient(
    service.id,
    service.address,
    service.port,
    pairingData
  );

  client.on('event', (ev) => {
    console.log('event:',JSON.stringify(ev, null, 2));
  });  
  client.on('disconnect', (ev) => {
    console.log('disconnect:',JSON.stringify(ev, null, 2));
  });  

  client.getAccessories().then((acc) => {
    //console.log(JSON.stringify(acc, null, 2));
    client.subscribeCharacteristics(characteristics)
    .then((conn) => {
      connection = conn;
      console.log('Subscribed!');
    }).catch((e) => console.error(e));      
  }).catch((e) => console.error(e));
});
discovery.start();

here is the result :

Found device!
{
  name: 'Mi Smart Home Hub-4291',
  address: '192.168.1.32',
  port: 60974,
  'c#': 37,
  ff: 1,
  id: '85:E2:A5:EA:9F:32',
  md: 'DMWG03LM',
  pv: '1.1',
  's#': 1,
  sf: 0,
  ci: 2
}
Subscribed!
event: {
  "characteristics": [
    {
      "aid": 3,
      "iid": 268,
      "value": 1
    }
  ]
}
event: {
  "characteristics": [
    {
      "aid": 3,
      "iid": 268,
      "value": 0
    }
  ]
}

then i add a new accessory to the bridge, and the bridge disconnect then change the port... and send 2 times the discover :

disconnect: {}
Found device!
{
  name: 'Mi Smart Home Hub-4291',
  address: '192.168.1.32',
  port: 59805,
  'c#': 38,
  ff: 1,
  id: '85:E2:A5:EA:9F:32',
  md: 'DMWG03LM',
  pv: '1.1',
  's#': 1,
  sf: 0,
  ci: 2
}
Subscribed!
Found device!
{
  name: 'Mi Smart Home Hub-4291',
  address: '192.168.1.32',
  port: 59805,
  'c#': 38,
  ff: 1,
  id: '85:E2:A5:EA:9F:32',
  md: 'DMWG03LM',
  pv: '1.1',
  's#': 1,
  sf: 0,
  ci: 2
}
Subscribed!

After that, the subscribeCharacteristics have been launched twice... so every event is doubled :

event: {
  "characteristics": [
    {
      "aid": 3,
      "iid": 268,
      "value": 1
    }
  ]
}
event: {
  "characteristics": [
    {
      "aid": 3,
      "iid": 268,
      "value": 1
    }
  ]
}
event: {
  "characteristics": [
    {
      "aid": 3,
      "iid": 268,
      "value": 0
    }
  ]
}
event: {
  "characteristics": [
    {
      "aid": 3,
      "iid": 268,
      "value": 0
    }
  ]
}

do you know a good way to handle that ? how to know there is already subscibed caracteristics ? if i automatically unsubscribeCharacteristics, I got an error if there was no subscribed Characteristics...

mrstegeman commented 3 years ago

It seems like you could just keep a map of devices you've already subscribed to.

NebzHB commented 3 years ago

yep, that's what i did at the end... (and verify the c# change)

thank you for your feedback