gdombiak / OctoPod

Free open source client for OctoPrint
Apache License 2.0
227 stars 42 forks source link

Implement OctoRelay Settings #476

Closed lkrieger00 closed 2 years ago

lkrieger00 commented 3 years ago

First off all, i love octopod, and in my opinion it is the best App for managing Octoprint on my Smartphone. Just a few days ago a new Plugin called "OctoRelay Settings" was released and i really love it, because it finally allows me to controll the lights of my Printer via Smartphone so i can turn it on if I want to see the camera feed and leave it off the rest of the time. Can you please also implement this option in Octopod? I would love to use this!!

Thanks for your amazing work!

bastienstefani commented 2 years ago

Hello guys, would love to have this too... Currently i control my printer with octorelay, 1 relay for the motherboard so i can turn it down on print finish and one for the lights i can switch on/off when i want to check with the camera. That would be awesome to have to control also on the phone app !

bastienstefani commented 2 years ago

Hello @gdombiak, just wanted to give a hand on this improvement. Octorelay appears to have impemented the rest api plugin capability on /api/plugin/octorelay So 2 commands exists : getStatus to retrieve a relay status with pin value r1 to r8 for the relay identifier {"command":"getStatus","pin":"r1"} and update command with same identifier with toggle on/off the relay {"command":"update","pin":"r1"} I didn't found a way yet to check if a relay is active meaning if the user have set it in the settings as controllable.

gdombiak commented 2 years ago

Thanks @bastienstefani . I just looked at the code of that plugin and I do not see an API that returns list of relays with their labels and status. I only see an API to read the status of a relay or change it. Not efficient to read status of all relays on a single API call and no label would be displayed. :(

Once these other 2 APIs exist, I can see of adding support for this plugin.

Gaston

bastienstefani commented 2 years ago

That’s clearly not efficient I agree, I’m trying to find the motivation to fork octorelay repo or gpio control repo (which has similar api calls) making the changes to have the entry point with all available relays and infos and republish it. I will keep you posted if I can make it. Because that would be awesome to have this in octopod !

bastienstefani commented 2 years ago

@gdombiak just to make sure before sending my pullrequest to octorelay owner, an api call like this one would make it ? request : {"command":"listActives"} response : {"r1": {"name": "Light", "status": false }, "r2": {"name": "Printer", "status": true }} of course, response contains only the relays setup by the user, the ones not activated aren't present

gdombiak commented 2 years ago

Hey @bastienstefani ,

How about the following?

Request

{
  "command": "listStatus"
}

Response

[
  {
    "pin": 1,
    "name": "Light",
    "on": false
  },
  {
    "pin": 2,
    "name": "Printer",
    "on": true
  }
]

Regards, Gaston

bastienstefani commented 2 years ago

@gdombiak I can change the response structure no problem, but the pin identifier is useless as it’s the relay identifier (r1, r2…) that you have to send in the update to toggle on/off. So might be interesting to change the ‘pin’:2 to ‘relay’:’r2’. Is that ok for you ?

gdombiak commented 2 years ago

@bastienstefani, the reason I added a field named pin with an int value is because the update and getStatus commands expect a property called pin that is the pin number. Clients would just take whatever value was returned by listStatus and use it for these other 2 APIs. What do you think?

Regards, Gaston

bastienstefani commented 2 years ago

@bastienstefani, the reason I added a field named pin with an int value is because the update and getStatus commands expect a property called pin that is the pin number. Clients would just take whatever value was returned by listStatus and use it for these other 2 APIs. What do you think?

Regards, Gaston

I made the test yesterday, it’s called pin but it’s the rX (r1, r2…) that it takes, It’s a mistake in the property naming I guess. Took me half an hour to figure this out

gdombiak commented 2 years ago

I did not test this plugin but look at this code

    def on_api_command(self, command, data):
        self._logger.debug("on_api_command {}, some_parameter is {}".format(command,data))
        index = data['pin']

        settings = self.get_settings_defaults()[index]
        settings.update(self._settings.get([index]))

index is used for accessing an array. It seems to expect an int. Passing a string should make the API crash.

Gaston

bastienstefani commented 2 years ago

I did not test this plugin but look at this code

    def on_api_command(self, command, data):
        self._logger.debug("on_api_command {}, some_parameter is {}".format(command,data))
        index = data['pin']

        settings = self.get_settings_defaults()[index]
        settings.update(self._settings.get([index]))

index is used for accessing an array. It seems to expect an int. Passing a string should make the API crash.

Gaston

Get_settings_defaults returns a dictionary. The one declared at the beginning of the script. The variable named index has a misleading name as it’s the dictionary key.

gdombiak commented 2 years ago

I see. That’s an internal method used by OctoPrint. The APIs exposed by this plugin seem to not use that method. I’m trying to be consistent with existing APIs. Might want to check with plugin developer on their new API. I’m just giving feedback.

Regards Gaston

patrickcollins12 commented 1 year ago

Just posting the commands here that worked for me, hopefully it helps someone else...

Get status of Relay: curl -H "X-Api-Key: CDD...4ED2" -d "{\"command\": \"getStatus\", \"pin\":\"r1\" }" -H "Content-Type: application/json" http://octopi.local/api/plugin/octorelay -X POST

{"status":true}

Toggle Relay: curl -H "X-Api-Key: CDD...ED2" -d "{\"command\": \"update\", \"pin\":\"r1\" }" -H "Content-Type: application/json" http://octopi.local/api/plugin/octorelay -X POST

{"status":"ok"}

List all Relays: curl -H "X-Api-Key: CDD...ED2" -d "{\"command\": \"listAllStatus\"}" -H "Content-Type: application/json" http://octopi.local/api/plugin/octorelay -X POST

[{"active":false,"id":"r1","name":"Light"}]