mdopp / simple-sonoff-server

Emulates the original sonoff-cloud-servers within your local network.
BSD 2-Clause "Simplified" License
144 stars 47 forks source link

Feature Request: Preserve additional device parameters #9

Closed PKGeorgiev closed 6 years ago

PKGeorgiev commented 6 years ago

Hi,

Looking at the log it seems sonoffs send additional interesting information that is not persisted. For example: MAC, RSSI, startup state of the switch. Additionally TH10/TH16 will send sensor's data (if connected).

Here are device update requests:

// sonoff (update req #1) > server
{  
   "userAgent":"device",
   "apikey":"111111111-1111-1111-1111-11111111111",
   "deviceid":"10000xxxx",
   "action":"update",
   "params":{  
      "switch":"off",
      "fwVersion":"2.0.4",
      "rssi":-52,
      "staMac":"5C:CF:7F:42:90:EF",
      "startup":"off"
   }
}

// sonoff (update req #1) > server
{  
   "userAgent":"device",
   "apikey":"111111111-1111-1111-1111-11111111111",
   "deviceid":"10000dba6b",
   "action":"update",
   "params":{  
      "currentTemperature":"21",
      "currentHumidity":"28",
      "sensorType":"AM2301"
   }
}

Can you please preserve these fields in device's object? Because these fields may vary from device to device, one possible solution will be to store them (as they appear) under a new property "params" in the WS:Update code. For example:

device.params = device.params || {};

Object.keys(data.params).forEach(function(p) {
    device.params[p] = data.params[p];
});

Of course other suggestions are welcome :)

Also it will be useful to include a property that keeps the date/time of last contact - like a timestamp. It will allow us to detect if the device haven't reported for xyz minutes.

mdopp commented 6 years ago

isAlive and isConnected are now part of the device information.

For isAlive a ping is send every 3000ms

About the params: As I do not have a full list, and they also change for different messages, I added two rawMessage fields => rawMessageRegister and rawMessageLastUpdate. There you can find now the original message. Maybe that will help. Whenever I get a clear list of parameters, that should be preserved, I can move them to special variables for you. Hope this helps.

PKGeorgiev commented 6 years ago

Many thanks! I'll give it a try in the next few days.

PKGeorgiev commented 6 years ago

I get plenty of errors when running the new version. This seem to be a result of recent modularization. The log is defined in the config and is not accessible directly from within sonoff.server.js:

starting the server ReferenceError: log is not defined at Server. (sonoff.server.js:39:5)

HTTP GET /devices ReferenceError: log is not defined at /home/pi/sonoff/simple-sonoff-server-master/sonoff.server.js:94:5

.trace() is missing: at /home/pi/sonoff/simple-sonoff-server-master/sonoff.server.module.js:125:13

Two other problems: 1) The other thing is that node-red-contrib-sonoff-server will not work with this setup. 2) HTTP GET /devices returns empty array despite of my sonoffs register successfully:

SONOFF Server Started On Port 1080
API Server Started On Port 1081
REQ | POST | /dispatch/device
REQ | {"accept":"ws;2","version":2,"ts":727,"deviceid":"10000db6be","apikey":"hidden_text","model":"PSA-BHA-GL","romVersion":"2.0.4"}
REQ | POST | /dispatch/device
REQ | {"accept":"ws;2","version":2,"ts":8566,"deviceid":"1000088e5c","apikey":"hidden_text","model":"PSB-B04-GL","romVersion":"1.3.0"}
REQ | POST | /dispatch/device
REQ | {"accept":"ws;2","version":2,"ts":347,"deviceid":"10000d76a4","apikey":"hidden_text","model":"PSA-B01-GL","romVersion":"1.5.5"}
GET | GET | /devices

I'm using version 1.1.1 with config:

{
    "router": {
        "SSID": "##########",
        "password": "###########"
    },
    "server": {
        "IP": "0.0.0.0",
        "httpPort": 1081,
        "httpsPort": 1080,
        "websocketPort": 4443
    }
}
mdopp commented 6 years ago

Hi, I will have a closer look at this this weekend. And maybe build some tests.

mdopp commented 6 years ago

starting the server

ReferenceError: log is not defined at Server. (sonoff.server.js:39:5)

could be reproduced and is now solved

HTTP GET /devices

ReferenceError: log is not defined at /home/pi/sonoff/simple-sonoff-server-master/sonoff.server.js:94:5

is now solved, was the same problem (missing "const log = config.logger")

.trace() is missing:

at /home/pi/sonoff/simple-sonoff-server-master/sonoff.server.module.js:125:13

I was unable to reproduce this one. Still I changed all "traces" to "info" logs. As the output of trace is more confusing.

Two other problems:

The other thing is that node-red-contrib-sonoff-server will not work with this setup.

How is your setup with node-red-contrib-sonoff-server? Basically it will be running only inside node-red, and not be started seperatly anymore. And as node-red is capable of publishing information in any form, i removed the 2nd http-server. Do you need this one?

HTTP GET /devices returns empty array despite of my sonoffs register successfully:

Can you give me a log with the current version (1.1.2)? As your Log shows that the sonoff devices are triggering the https server, but not that they are connecting to the websockets later.

SONOFF Server Started On Port 1081
API Server Started On Port 1080
REQ | POST | /dispatch/device
REQ | {"accept":"ws;2","version":2,"ts":652,"deviceid":"10001428ec","apikey":"0a0fbcab-6258-4c59-b548-ac456debc97f","model":"ITA-GZ1-GL","romVersion":"1.5.5"}
REQ | POST | /dispatch/device
REQ | {"accept":"ws;2","version":2,"ts":396,"deviceid":"1000145cbc","apikey":"435edbda-c0b5-418f-a4f9-360c4569a300","model":"ITA-GZ1-GL","romVersion":"1.5.5"}
WS | Server is up 192.168.178.31:1443 to ::ffff:192.168.178.39:11385
REQ | WS | DEV | {"userAgent":"device","apikey":"0a0fbcab-6258-4c59-b548-ac456debc97f","deviceid":"10001428ec","action":"register","version":2,"romVersion":"1.5.5","model":"ITA-GZ1-GL","ts":404}
INFO | WS | Device 10001428ec registered
RES | WS | DEV | {"error":0,"deviceid":"10001428ec","apikey":"111111111-1111-1111-1111-111111111111"}
WS | Server is up 192.168.178.31:1443 to ::ffff:192.168.178.38:24023
REQ | WS | DEV | {"userAgent":"device","apikey":"111111111-1111-1111-1111-11111111111","deviceid":"10001428ec","action":"date"}
RES | WS | DEV | {"error":0,"deviceid":"10001428ec","apikey":"111111111-1111-1111-1111-111111111111","date":"2018-02-17T12:00:41.514Z"}
REQ | WS | DEV | {"userAgent":"device","apikey":"435edbda-c0b5-418f-a4f9-360c4569a300","deviceid":"1000145cbc","action":"register","version":2,"romVersion":"1.5.5","model":"ITA-GZ1-GL","ts":911}
INFO | WS | Device 1000145cbc registered
RES | WS | DEV | {"error":0,"deviceid":"1000145cbc","apikey":"111111111-1111-1111-1111-111111111111"}
REQ | WS | DEV | {"userAgent":"device","apikey":"111111111-1111-1111-1111-11111111111","deviceid":"10001428ec","action":"update","params":{"switch":"on","fwVersion":"1.5.5","rssi":-75,"staMac":"2C:3A:E8:11:62:00","startup":"off"}}
RES | WS | DEV | {"error":0,"deviceid":"10001428ec","apikey":"111111111-1111-1111-1111-111111111111"}
REQ | WS | DEV | {"userAgent":"device","apikey":"111111111-1111-1111-1111-11111111111","deviceid":"1000145cbc","action":"date"}
RES | WS | DEV | {"error":0,"deviceid":"1000145cbc","apikey":"111111111-1111-1111-1111-111111111111","date":"2018-02-17T12:00:41.538Z"}
REQ | WS | DEV | {"userAgent":"device","apikey":"111111111-1111-1111-1111-11111111111","deviceid":"10001428ec","action":"query","params":["timers"]}
RES | WS | DEV | {"error":0,"deviceid":"10001428ec","apikey":"111111111-1111-1111-1111-111111111111","params":{}}
REQ | WS | DEV | {"userAgent":"device","apikey":"111111111-1111-1111-1111-11111111111","deviceid":"1000145cbc","action":"update","params":{"switch":"off","fwVersion":"1.5.5","rssi":-72,"staMac":"5C:CF:7F:BD:6E:8C","startup":"off"}}
RES | WS | DEV | {"error":0,"deviceid":"1000145cbc","apikey":"111111111-1111-1111-1111-111111111111"}
REQ | WS | DEV | {"userAgent":"device","apikey":"111111111-1111-1111-1111-11111111111","deviceid":"1000145cbc","action":"query","params":["timers"]}
RES | WS | DEV | {"error":0,"deviceid":"1000145cbc","apikey":"111111111-1111-1111-1111-111111111111","params":{}}
GET | GET | /devices
mdopp commented 6 years ago

@PKGeorgiev any news?

PKGeorgiev commented 6 years ago

I'd tried 1.1.2 and it works. Here is the device list (via API server):

image

In my case I have Slampher, Sonoff TH, Sonoff Dual. Here is the log from these devices (see how switches are described):

SONOFF Server Started On Port 1080
API Server Started On Port 1081
WS | Server is up 192.168.178.31:4443 to ::ffff:192.168.88.84:30408
REQ | WS | DEV | {"userAgent":"device","apikey":"3e559968-96a1-4ac9-8944-f09198eaea1e","deviceid":"10000d76a4","action":"register","version":2,"romVersion":"1.5.5","model":"PSA-B01-GL","ts":650}
INFO | WS | Device 10000d76a4 registered
RES | WS | DEV | {"error":0,"deviceid":"10000d76a4","apikey":"111111111-1111-1111-1111-111111111111"}
REQ | WS | DEV | {"userAgent":"device","apikey":"111111111-1111-1111-1111-11111111111","deviceid":"10000d76a4","action":"date"}
RES | WS | DEV | {"error":0,"deviceid":"10000d76a4","apikey":"111111111-1111-1111-1111-111111111111","date":"2018-02-20T08:55:48.231Z"}
REQ | WS | DEV | {"userAgent":"device","apikey":"111111111-1111-1111-1111-11111111111","deviceid":"10000d76a4","action":"update","params":{"switch":"on","fwVersion":"1.5.5","rssi":-69,"staMac":"2C:3A:E8:05:7F:CF","startup":"off"}}
RES | WS | DEV | {"error":0,"deviceid":"10000d76a4","apikey":"111111111-1111-1111-1111-111111111111"}
REQ | WS | DEV | {"userAgent":"device","apikey":"111111111-1111-1111-1111-11111111111","deviceid":"10000d76a4","action":"query","params":["timers"]}
RES | WS | DEV | {"error":0,"deviceid":"10000d76a4","apikey":"111111111-1111-1111-1111-111111111111","params":{}}
WS | Server is up 192.168.178.31:4443 to ::ffff:192.168.88.80:8214
WS | Server is up 192.168.178.31:4443 to ::ffff:192.168.88.82:16856
REQ | WS | DEV | {"userAgent":"device","apikey":"569fc747-9178-46a1-953f-cf1416a6480b","deviceid":"10000db6be","action":"register","version":2,"romVersion":"2.0.4","model":"PSA-BHA-GL","ts":674}
INFO | WS | Device 10000db6be registered
RES | WS | DEV | {"error":0,"deviceid":"10000db6be","apikey":"111111111-1111-1111-1111-111111111111"}
REQ | WS | DEV | {"userAgent":"device","apikey":"81f4e8f4-fb09-4618-bbc2-9fd29ed9d90d","deviceid":"100010a270","action":"register","version":2,"romVersion":"1.6.2","model":"ITA-GZ1-GL","ts":465}
INFO | WS | Device 100010a270 registered
RES | WS | DEV | {"error":0,"deviceid":"100010a270","apikey":"111111111-1111-1111-1111-111111111111"}
REQ | WS | DEV | {"userAgent":"device","apikey":"111111111-1111-1111-1111-11111111111","deviceid":"10000db6be","action":"date"}
RES | WS | DEV | {"error":0,"deviceid":"10000db6be","apikey":"111111111-1111-1111-1111-111111111111","date":"2018-02-20T08:55:49.197Z"}
REQ | WS | DEV | {"userAgent":"device","apikey":"111111111-1111-1111-1111-11111111111","deviceid":"100010a270","action":"date"}
RES | WS | DEV | {"error":0,"deviceid":"100010a270","apikey":"111111111-1111-1111-1111-111111111111","date":"2018-02-20T08:55:49.205Z"}
REQ | WS | DEV | {"userAgent":"device","apikey":"111111111-1111-1111-1111-11111111111","deviceid":"10000db6be","action":"update","params":{"switch":"off","fwVersion":"2.0.4","rssi":-69,"staMac":"5C:CF:7F:40:CE:B4","startup":"off"}}
RES | WS | DEV | {"error":0,"deviceid":"10000db6be","apikey":"111111111-1111-1111-1111-111111111111"}
REQ | WS | DEV | {"userAgent":"device","apikey":"111111111-1111-1111-1111-11111111111","deviceid":"10000db6be","action":"query","params":["timers","targets","mainSwitch","deviceType"]}
RES | WS | DEV | {"error":0,"deviceid":"10000db6be","apikey":"111111111-1111-1111-1111-111111111111","params":{}}
REQ | WS | DEV | {"userAgent":"device","apikey":"111111111-1111-1111-1111-11111111111","deviceid":"100010a270","action":"update","params":{"switch":"off","fwVersion":"1.6.2","rssi":-70,"staMac":"60:01:94:84:78:49","startup":"off"}}
RES | WS | DEV | {"error":0,"deviceid":"100010a270","apikey":"111111111-1111-1111-1111-111111111111"}
REQ | WS | DEV | {"userAgent":"device","apikey":"111111111-1111-1111-1111-11111111111","deviceid":"100010a270","action":"query","params":["timers"]}
RES | WS | DEV | {"error":0,"deviceid":"100010a270","apikey":"111111111-1111-1111-1111-111111111111","params":{}}
REQ | WS | DEV | {"userAgent":"device","apikey":"111111111-1111-1111-1111-11111111111","deviceid":"10000db6be","action":"update","params":{"currentTemperature":"21","currentHumidity":"36","sensorType":"AM2301"}}
RES | WS | DEV | {"error":0,"deviceid":"10000db6be","apikey":"111111111-1111-1111-1111-111111111111"}
WS | Server is up 192.168.178.31:4443 to ::ffff:192.168.88.95:5029
REQ | WS | DEV | {"userAgent":"device","apikey":"26968f11-aab5-41c4-8968-324bb5e95865","deviceid":"1000088e5c","action":"register","version":2,"romVersion":"1.3.0","model":"PSB-B04-GL","ts":1143}
INFO | WS | Device 1000088e5c registered
RES | WS | DEV | {"error":0,"deviceid":"1000088e5c","apikey":"111111111-1111-1111-1111-111111111111"}
REQ | WS | DEV | {"userAgent":"device","apikey":"111111111-1111-1111-1111-11111111111","deviceid":"1000088e5c","action":"date"}
RES | WS | DEV | {"error":0,"deviceid":"1000088e5c","apikey":"111111111-1111-1111-1111-111111111111","date":"2018-02-20T08:55:50.157Z"}
REQ | WS | DEV | {"userAgent":"device","apikey":"111111111-1111-1111-1111-11111111111","deviceid":"1000088e5c","action":"update","params":{"switches":[{"switch":"off","outlet":0},{"switch":"off","outlet":1},{"switch":"off","outlet":2},{"switch":"off","outlet":3}],"configure":[{"startup":"off","outlet":0},{"startup":"off","outlet":1},{"startup":"off","outlet":2},{"startup":"off","outlet":3}],"fwVersion":"1.3.0","rssi":-65}}
RES | WS | DEV | {"error":0,"deviceid":"1000088e5c","apikey":"111111111-1111-1111-1111-111111111111"}
REQ | WS | DEV | {"userAgent":"device","apikey":"111111111-1111-1111-1111-11111111111","deviceid":"1000088e5c","action":"query","params":["timers"]}
RES | WS | DEV | {"error":0,"deviceid":"1000088e5c","apikey":"111111111-1111-1111-1111-111111111111","params":{}}
WS | Server is up 192.168.178.31:4443 to ::ffff:192.168.88.102:27223
REQ | WS | DEV | {"userAgent":"device","apikey":"ce7dbcdb-f82d-46ec-b739-05ae5324703a","deviceid":"100010a704","action":"register","version":2,"romVersion":"1.6.2","model":"ITA-GZ1-GL","ts":882}
INFO | WS | Device 100010a704 registered
RES | WS | DEV | {"error":0,"deviceid":"100010a704","apikey":"111111111-1111-1111-1111-111111111111"}
REQ | WS | DEV | {"userAgent":"device","apikey":"111111111-1111-1111-1111-11111111111","deviceid":"100010a704","action":"date"}
RES | WS | DEV | {"error":0,"deviceid":"100010a704","apikey":"111111111-1111-1111-1111-111111111111","date":"2018-02-20T08:55:53.203Z"}
REQ | WS | DEV | {"userAgent":"device","apikey":"111111111-1111-1111-1111-11111111111","deviceid":"100010a704","action":"update","params":{"switch":"off","fwVersion":"1.6.2","rssi":-77,"staMac":"60:01:94:84:77:47","startup":"off"}}
RES | WS | DEV | {"error":0,"deviceid":"100010a704","apikey":"111111111-1111-1111-1111-111111111111"}
REQ | WS | DEV | {"userAgent":"device","apikey":"111111111-1111-1111-1111-11111111111","deviceid":"100010a704","action":"query","params":["timers"]}
RES | WS | DEV | {"error":0,"deviceid":"100010a704","apikey":"111111111-1111-1111-1111-111111111111","params":{}}
REQ | WS | DEV | {"userAgent":"device","apikey":"111111111-1111-1111-1111-11111111111","deviceid":"100010a270","action":"update","params":{"switch":"off","fwVersion":"1.6.2","rssi":-67,"staMac":"60:01:94:84:78:49","startup":"off"}}
RES | WS | DEV | {"error":0,"deviceid":"100010a270","apikey":"111111111-1111-1111-1111-111111111111"}
REQ | WS | DEV | {"userAgent":"device","apikey":"111111111-1111-1111-1111-11111111111","deviceid":"100010a704","action":"update","params":{"switch":"off","fwVersion":"1.6.2","rssi":-74,"staMac":"60:01:94:84:77:47","startup":"off"}}
RES | WS | DEV | {"error":0,"deviceid":"100010a704","apikey":"111111111-1111-1111-1111-111111111111"}
Connection closed: connection timed out (408)
Device 1000088e5c disconnected
WS | Server is up 192.168.178.31:4443 to ::ffff:192.168.88.95:5030
REQ | WS | DEV | {"userAgent":"device","apikey":"26968f11-aab5-41c4-8968-324bb5e95865","deviceid":"1000088e5c","action":"register","version":2,"romVersion":"1.3.0","model":"PSB-B04-GL","ts":1289}
INFO | WS | Device 1000088e5c registered
RES | WS | DEV | {"error":0,"deviceid":"1000088e5c","apikey":"111111111-1111-1111-1111-111111111111"}
REQ | WS | DEV | {"userAgent":"device","apikey":"111111111-1111-1111-1111-11111111111","deviceid":"1000088e5c","action":"date"}
RES | WS | DEV | {"error":0,"deviceid":"1000088e5c","apikey":"111111111-1111-1111-1111-111111111111","date":"2018-02-20T08:57:14.396Z"}
REQ | WS | DEV | {"userAgent":"device","apikey":"111111111-1111-1111-1111-11111111111","deviceid":"1000088e5c","action":"update","params":{"switches":[{"switch":"off","outlet":0},{"switch":"off","outlet":1},{"switch":"off","outlet":2},{"switch":"off","outlet":3}],"configure":[{"startup":"off","outlet":0},{"startup":"off","outlet":1},{"startup":"off","outlet":2},{"startup":"off","outlet":3}],"fwVersion":"1.3.0","rssi":-65}}
RES | WS | DEV | {"error":0,"deviceid":"1000088e5c","apikey":"111111111-1111-1111-1111-111111111111"}
REQ | WS | DEV | {"userAgent":"device","apikey":"111111111-1111-1111-1111-11111111111","deviceid":"1000088e5c","action":"query","params":["timers"]}
RES | WS | DEV | {"error":0,"deviceid":"1000088e5c","apikey":"111111111-1111-1111-1111-111111111111","params":{}}
Connection closed: connection timed out (408)
Device 1000088e5c disconnected

IsAlive works for me but it will be nice to have datetime stamp in rawMessageRegister and rawMessageLastUpdate. This way I can keep track of devices directly in NodeRED instead of parsing the log file for dates/times.

About node-red-contrib-sonoff-server - it seems to be developed in a private repo (couldn't find it on GitHub) and it uses a modified version of your "simple-sonoff-server" - his version 1.1.0 was different from your 1.1.0. I'll test it these days.

Btw in npm the current version of simple-sonoff-server is still 1.1.0.

mdopp commented 6 years ago

Hi, the repo can be found here => https://github.com/saryn/node-red-contrib-sonoff-server I also created a pull request a week ago, to switch to a newer version, where I merged some of his changes into my version. So version 1.1.1 should contain all of his changes. 1.1.2 should also fix some problems that were still in his version. For npm => I do not know how to change that. Its belonging to @saryn

PKGeorgiev commented 6 years ago

Aha! I found it: https://github.com/saryn/node-red-contrib-sonoff-server and now know the full story about these two modules. It will be helpful if you add GIT repo URL in project.json so it will appear in npmjs.com :)

mdopp commented 6 years ago

for git repo url it would be

"repository": "github:mdopp/simple-sonoff-server"

Sorry, I really haven't done that before, and want to make it right

mdopp commented 6 years ago

@PKGeorgiev i added an timestamp to the raw-message as date.Now()

PKGeorgiev commented 6 years ago

I've tried it - just what I wanted. Thanks a lot!