The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

power-outlet-json.cgi - Control Power::Outlet device with JSON web service (e.g. Node-Red)

DESCRIPTION

power-outlet-json.cgi is a CGI application to control a Power::Outlet device with a web service.

API

The script is called over HTTP with name and action parameters. The name is the Section Name from the INI file and the action is one of on, off, query, or switch.

  http://localhost/power-outlet/power-outlet-json.cgi?name=Lamp;action=off
  http://localhost/power-outlet/power-outlet-json.cgi?name=Lamp;action=on
  http://localhost/power-outlet/power-outlet-json.cgi?name=Lamp;action=query
  http://localhost/power-outlet/power-outlet-json.cgi?name=Lamp;action=switch

Return is a JSON hash with keys status and state. status is OK if there are no errors, state is the state of the switch after command either ON or OFF.

  {"status":"OK","state":"ON"}

Node-Red Integration

Use three nodes: inject, http request, and debug.

  • In the inject node

    • Set the "Topic" to the desired INI config file [section] name.

    • Set the "Payload" to one of "ON", "OFF", "QUERY" or "SWITCH"

  • In the http request node

    • Set the "Method" to GET (script also supports POST)

    • Set the "URL" to https://127.0.0.1/power-outlet/power-outlet-json.cgi?name={{topic}};action={{payload}}

    • Set the "Return" to a parsed JSON Object

  • In the debug node

    • Set the "Output" to msg.payload.state which returns "ON" or "OFF"

Node Red Example

  [{"id":"736cc2df.cc616c","type":"inject","z":"bbbcee28.8891c","name":"","topic":"Christmas Tree","payload":"On","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":330,"y":1480,"wires":[["6f024760.ea5058"]]},{"id":"6f024760.ea5058","type":"http request","z":"bbbcee28.8891c","name":"power-outlet-json.cgi","method":"GET","ret":"obj","paytoqs":false,"url":"https://127.0.0.1/power-outlet/power-outlet-json.cgi?name={{topic}};action={{payload}}","tls":"","persist":false,"proxy":"","authType":"","x":560,"y":1480,"wires":[["2673faca.21f8d6"]],"inputLabels":["Topic=>name, Payload=>action"]},{"id":"2673faca.21f8d6","type":"debug","z":"bbbcee28.8891c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.state","targetType":"msg","x":790,"y":1480,"wires":[]}]

CONFIGURATION

To add an outlet for the web service, add a new INI section to the power-outlet.ini file.

  [Unique_Section_Name]
  type=iBoot
  host=Lamp

If you need to override the defaults

  [Unique_Section_Name]
  type=iBoot
  host=Lamp
  port=80
  pass=PASS
  name=My iBoot Description

WeMo device

  [WeMo]
  type=WeMo
  host=mywemo

Default Location: /etc/power-outlet.ini

BUILD

  rpmbuild -ta Power-Outlet-*.tar.gz

INSTALLATION

I recommend installation with the provided RPM package perl-Power-Outlet-application-cgi which installs to /usr/share/power-outlet.

  sudo yum install perl-Power-Outlet-application-cgi