|
| 1 | +"""Power backend for the Minuteman RPM1521 networked power controller. |
| 2 | +
|
| 3 | +The RPM1521 is controlled via an HTTP CGI endpoint using HTTP basic auth:: |
| 4 | +
|
| 5 | + curl --user user:pwd \\ |
| 6 | + "http://192.168.1.100/nagios_powerctrl.csp?slave_id=1&port=<PORT>&ctrl_kind=<KIND>" |
| 7 | +
|
| 8 | +where ``ctrl_kind=1`` turns the outlet on and ``ctrl_kind=2`` turns it off. |
| 9 | +
|
| 10 | +The outlet state is read back from a separate status CGI:: |
| 11 | +
|
| 12 | + curl --user user:pwd "http://192.168.1.100/nagios_power_status.csp" |
| 13 | +
|
| 14 | +which returns a list whose second to last element holds the on/off state of each |
| 15 | +outlet (0 = off, 1 = on). |
| 16 | +
|
| 17 | + NetworkPowerPort: |
| 18 | + model: rpm1521 |
| 19 | + host: 'http://admin:secret@192.168.1.10' |
| 20 | + index: 1 |
| 21 | +""" |
| 22 | + |
| 23 | +import json |
| 24 | + |
| 25 | +import requests |
| 26 | + |
| 27 | +SLAVE_ID = 1 |
| 28 | +CTRL_KIND_ON = 1 |
| 29 | +CTRL_KIND_OFF = 2 |
| 30 | + |
| 31 | + |
| 32 | +def power_set(host, port, index, value): |
| 33 | + index = int(index) |
| 34 | + ctrl_kind = CTRL_KIND_ON if value else CTRL_KIND_OFF |
| 35 | + params = { |
| 36 | + "slave_id": SLAVE_ID, |
| 37 | + "port": index, |
| 38 | + "ctrl_kind": ctrl_kind, |
| 39 | + } |
| 40 | + r = requests.get(f"{host}/nagios_powerctrl.csp", params=params) |
| 41 | + r.raise_for_status() |
| 42 | + |
| 43 | + |
| 44 | +def power_get(host, port, index): |
| 45 | + index = int(index) |
| 46 | + params = { |
| 47 | + "slave_id": SLAVE_ID, |
| 48 | + } |
| 49 | + r = requests.get(f"{host}/nagios_power_status.csp", params=params) |
| 50 | + r.raise_for_status() |
| 51 | + |
| 52 | + # The status CGI returns a list literal, e.g. |
| 53 | + # ['RPM1521E','0.0','NULL','1','1','109.9',['1','0'],['0','1'],['0.0','0.0']] |
| 54 | + # It contains several bracketed sub-lists; the second to last one holds the |
| 55 | + # on/off status of each outlet. |
| 56 | + data = json.loads(r.text.replace("'", '"')) |
| 57 | + socket_states = data[-2] |
| 58 | + return int(socket_states[index - 1]) == 1 |
0 commit comments