-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Using PoE switches as power supply is convenient since a switch is needed in many cases anyway and with the right adapter most embedded devices can be powered. OpenWrt offers `ubus` as micro bus system to control the system, including PoE ports on switches. This commit adds a driver to handle PoE switches running OpenWrt. Signed-off-by: Paul Spooren <[email protected]>
- Loading branch information
Showing
3 changed files
with
124 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
""" UBUS jsonrpc interface for PoE management on OpenWrt devices. This comes in | ||
handy if devices are connected to a PoE switch running OpenWrt. Author: Paul | ||
Spooren <[email protected]> | ||
The URL given in hosts in exporter.yaml must accept unauthenticated UBUS | ||
calls for the two `poe` calls `info` and `manage`. | ||
An ACL example is given below: | ||
root@switch:~# cat /usr/share/rpcd/acl.d/unauthenticated.json | ||
{ | ||
"unauthenticated": { | ||
"description": "Access controls for unauthenticated requests", | ||
"read": { | ||
"ubus": { | ||
"session": [ | ||
"access", | ||
"login" | ||
], | ||
"poe": [ | ||
"info", | ||
"manage" | ||
] | ||
} | ||
} | ||
} | ||
} | ||
Further information is availbe at https://openwrt.org/docs/techref/ubus#acls | ||
NetworkPowerPort: | ||
model: ubus | ||
host: 'http://192.168.1.1/ubus' | ||
index: 1 | ||
""" | ||
|
||
import requests | ||
|
||
|
||
def power_set(host, port, index, value): | ||
assert port is None | ||
|
||
r = requests.post( | ||
host, | ||
json={ | ||
"jsonrpc": "2.0", | ||
"id": 1, | ||
"method": "call", | ||
"params": [ | ||
"00000000000000000000000000000000", | ||
"poe", | ||
"manage", | ||
{"port": f"lan{index}", "enable": value == 1}, | ||
], | ||
}, | ||
) | ||
r.raise_for_status() | ||
|
||
|
||
def power_get(host, port, index): | ||
assert port is None | ||
|
||
r = requests.post( | ||
host, | ||
json={ | ||
"jsonrpc": "2.0", | ||
"id": 1, | ||
"method": "call", | ||
"params": [ | ||
"00000000000000000000000000000000", | ||
"poe", | ||
"info", | ||
{}, | ||
], | ||
}, | ||
) | ||
r.raise_for_status() | ||
assert ( | ||
f"lan{index}" in r.json()["result"]["ports"] | ||
), f"Port lan{index} not found in {r.json()['result']['ports']}" | ||
|
||
return r.json()["result"]["ports"][f"lan{index}"] != "Disabled" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters