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: A status page to return a simple status response 0 or 1 #3

Closed peros550 closed 6 years ago

peros550 commented 6 years ago

Hi Many thanks for your efforts on this project.

Would it be possible to create a status page for example:

http://[IPoftheServer]:[Port]/devices/DeviceID/status

that would return a simple 1 or 0 based on the status (ON/OFF) of the Sonoff?

Having this functionality would allow me to import the sonoff to homebridge using HTTP plugin and monitor actual status.

Many Thanks in advance,

mdopp commented 6 years ago

Hi @peros550,

first draft of a solution is in pull-request #4

But I am currently not at home, so I can not test this, and I am sure i missed something about http vs. https. Either you can test it on your own, or I will do this in 2 or 3 days.

http://[IPoftheServer]:[Port]/devices/[DeviceID]/status

If you test it, pls let me know the outcome.

Best regards Michael

peros550 commented 6 years ago

Hi , it does not look to work. Here is what I get when I request the status page:

RangeError: Invalid status code: 0
    at ServerResponse.writeHead (_http_server.js:195:11)
    at ServerResponse._implicitHeader (_http_server.js:159:8)
    at ServerResponse.end (_http_outgoing.js:621:10)
    at ServerResponse.send (/var/lib/simple-sonoff-server/node_modules/express/lib/response.js:221:10)
    at /var/lib/simple-sonoff-server/sonoff.server.js:94:25
    at Layer.handle [as handle_request] (/var/lib/simple-sonoff-server/node_modules/express/lib/router/layer.js:95:5)
    at next (/var/lib/simple-sonoff-server/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/var/lib/simple-sonoff-server/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/var/lib/simple-sonoff-server/node_modules/express/lib/router/layer.js:95:5)
    at /var/lib/simple-sonoff-server/node_modules/express/lib/router/index.js:281:22
peros550 commented 6 years ago

I thing you need to replace the following line of code:

res.status(200).send(((d.state == 'ON') ? 1 : 0));

with this one:

res.status(200).send(((d.state == 'on') ? '1' : '0'));

I tried the above change locally and it worked!

mdopp commented 6 years ago

sorry for the long delay. Its now merged along with other changes (refactoring + modularization + docker)