RESTful url device toggle

Posted on
Sat Jul 17, 2010 11:07 pm
chrisla23 offline
Posts: 97
Joined: Sep 16, 2009

RESTful url device toggle

It has bugged me for a while that the restful interface does not have a toggle, only on/off. I figured out how to add toggle.

In the directory /Library/Application Support/Perceptive Automation/Indigo 4/IndigoWebServer/indigopy

In the file devicesreqhandler.py:

Under the stanza:
Code: Select all
                   #######
                        if device.typeSupportsOnOff:
                                isOn = kw.get('isOn', None)
                                if isOn is not None:
                                        isOn = isOn.lower() in [kTrueStr, u"yes", u"1"]
                                        if not onlyChanges or isOn != device.isOn:
                                                if isOn:
                                                        cherrypy.server.indigoDb.DeviceTurnOn(cherrypy.server.indigoConn, elem_name, cherrypy.request.remote.better_ip)
                                                else:
                                                        cherrypy.server.indigoDb.DeviceTurnOff(cherrypy.server.indigoConn, elem_name, cherrypy.request.remote.better_ip)
 
                        ######



Add the modifed block:

Code: Select all

## chris
                        if device.typeSupportsOnOff:
                                Toggle = kw.get('Toggle', None)
                                if Toggle is not None:
                                        Toggle = Toggle.lower() in [kTrueStr, u"yes", u"1"]
                                        if not onlyChanges or Toggle != device.Toggle:
                                                if Toggle:
                                                        cherrypy.server.indigoDb.DeviceToggleOnOff(cherrypy.server.indigoConn, elem_name, cherrypy.request.remote.better_ip)
                                                else:
                                                        cherrypy.server.indigoDb.DeviceToggleOnOff(cherrypy.server.indigoConn, elem_name, cherrypy.request.remote.better_ip)

                        ######
## chris



and in indigodb.py:

After the stanza:

Code: Select all

        def DeviceTurnOff(self, indigoConn, device, requestOrigin = None):
                if not self._allowControlPages:
                        raise ControlDisabled
                indigoConn.CheckConnection()

                logStr = "request to turn off device \"" + device + "\""
                if requestOrigin:
                        logStr += " from " + requestOrigin
                self._Log(logStr)

                indigoConn.SendCommandPacket(u"TurnOff", dataVal=device)


Add the modified block:

Code: Select all
#### chris
        def DeviceToggleOnOff(self, indigoConn, device, requestOrigin = None):
                if not self._allowControlPages:
                        raise ControlDisabled
                indigoConn.CheckConnection()

                logStr = "request to toggle device \"" + device + "\""
                if requestOrigin:
                        logStr += " from " + requestOrigin
                self._Log(logStr)

                indigoConn.SendCommandPacket(u"ToggleOnOff", dataVal=device)
### chris


Then call it like:

Code: Select all
# curl 'http://10.0.0.32:8176/devices/bedroom%20sparkly%20lights?Toggle=1&_method=put'


It would be really great to see this added to indigo. Most of the heavy lifting (SendCommandPacket(u"ToggleOnOff")) was already baked in, just not exposed to the restful API.

-Chris

Posted on
Sun Jul 18, 2010 6:32 am
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: RESTful url device toggle

With the RESTful API, we've taken a wait and see approach to implementing some functionality. In this particular case, it's usually easily enough implemented in the client so the priority was low. However, this does show the power of the python (IWS) client!

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sun Jul 18, 2010 10:19 am
matt (support) offline
Site Admin
User avatar
Posts: 21417
Joined: Jan 27, 2003
Location: Texas

Re: RESTful url device toggle

I'll get this added to the next 4.0 dot release. I made the arg name all lowercase (toggle) for consistency with the others, and simplified it a bit to:

Code: Select all
            toggle = kw.get('toggle', None)
            if toggle is not None:
               toggle = toggle.lower() in [kTrueStr, u"yes", u"1"]
               if toggle:
                  cherrypy.server.indigoDb.DeviceToggleOnOff(cherrypy.server.indigoConn, elem_name, cherrypy.request.remote.better_ip)

Thanks for the contribution.

Image

Posted on
Sun Jul 18, 2010 11:01 am
chrisla23 offline
Posts: 97
Joined: Sep 16, 2009

Re: RESTful url device toggle

@Jay yeah I was doing it on the client side. For us though this ends up being one of the top ways we interact with Indigo/Insteon so the extra half-second or so it was taking to establish state and use the turnOn=0/1 was noticeable. The python was indeed very clearly written and easy to hook into.

Thanks for including it, will be easier than having to re-modify every time I upgrade.


-Chris

Posted on
Tue Jul 20, 2010 9:38 am
matt (support) offline
Site Admin
User avatar
Posts: 21417
Joined: Jan 27, 2003
Location: Texas

Re: RESTful url device toggle

Indigo 4.1.11, which includes the RESTful toggle functionality, is now available.

Image

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 3 guests