11import requests
22
3- from ..exception import ExecutionError
3+
4+ # Driver has been tested with:
5+ # * Gude Expert Power Control 8031-1
6+ # * Gude Expert Power Control 8316-1
7+ # * Gude Expert Power Control 87-1210-18
8+ # According to the manual it also should work with:
9+ # * Gude Expert Power Control 8316
10+ #
11+ # This device needs to be used in 'Basic Compatible' mode for HTTP GET
12+ # to be usable. Do not turn on session authentication.
13+ #
14+ # HTTP-GET API is defined in the Gude EPC-HTTP-Interface specification:
15+ # http://wiki.gude.info/EPC_HTTP_Interface
16+ #
17+ # The `components=<N>` parameter defines which status information are
18+ # included into the returned JSON.
19+ # * `components=0` happily returns an empty response but still switches the
20+ # outputs as requested.
21+ # * `components=1` only includes the output's state into the JSON.
422
523PORT = 80
624
25+
726def power_set (host , port , index , value ):
827 index = int (index )
9- assert 1 <= index <= 8
28+ upper_limit = count_ports (host , port )
29+ assert 1 <= index <= upper_limit , f'index ({ index } ) out of port range (1-{ upper_limit } )'
1030 # access the web interface...
1131 value = 1 if value else 0
12- r = requests .get (
13- f"http://{ host } :{ port } /switch.html?cmd=1&p={ index } &s={ value } "
14- )
32+ r = requests .get (f"http://{ host } :{ port } /status.json?components=0&cmd=1&p={ index } &s={ value } " )
1533 r .raise_for_status ()
1634
1735
1836def power_get (host , port , index ):
1937 index = int (index )
20- assert 1 <= index <= 8
21- # get the contents of the main page
22- r = requests .get (f"http://{ host } :{ port } /" )
38+
39+ # get the component status
40+ r = requests .get (f"http://{ host } :{ port } /status.json?components=1" )
41+ r .raise_for_status ()
42+
43+ body = r .json ()
44+ assert 1 <= index <= len (body ["outputs" ]), f'index ({ index } ) out of port range (1-{ len (body ["outputs" ])} )'
45+ state = body ["outputs" ][index - 1 ]["state" ]
46+
47+ return state
48+
49+
50+ def count_ports (host , port ):
51+ r = requests .get (f"http://{ host } :{ port } /status.json?components=1" )
2352 r .raise_for_status ()
24- for line in r .text .splitlines ():
25- power_pattern = f"Power Port { index } </td>"
26- switch_patern = f"SwitchPort { index } </td>"
27- if line .find (power_pattern ) > 0 or line .find (switch_patern ) > 0 :
28- if line .find ("OFF" ) > 0 :
29- return False
30- if line .find ("ON" ) > 0 :
31- return True
32-
33- raise ExecutionError ("failed to parse the port status" )
34- # if we got this far, something is wrong with the website
35- raise ExecutionError ("failed to find the port" )
53+ return len (r .json ()["outputs" ])
0 commit comments