-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbotpower.py
executable file
·208 lines (154 loc) · 4.74 KB
/
botpower.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/env python
import argparse
import re
from pathlib import Path
import requests
import yaml
def set_outlet(outlet, action):
"""set_outlet
create the parameters for submission to the HTTP endpoint. if this is
a single port, then return a single value in the dict. if outlet =
all, then iterate through the range of outlets
"""
q_params = ""
state = {"off": "0", "on": "1"}
if outlet == "all":
for p in range(1, 5):
key = "p6" + str(p)
q_params += key + "=" + state[action]
# trailing '+' signs drive their parser bonkers
if p < 4:
q_params += "+"
else:
key = "p6" + outlet
q_params += key + "=" + state[action]
return q_params
def parse_response(response_txt):
"""parse_response(string) - parse the request response nicely output
the results of the command execution.
"""
resp = ""
state = {"0": "off", "1": "on"}
outlet_status = re.findall("(p6\d=\d)", response_txt)
if outlet_status:
resp += "current outlet status\n"
resp += "-" * 21 + "\n"
for s in outlet_status:
p = re.match("p6(\d)=(\d)", s)
resp += f"outlet: {p.group(1)} power: {state[p.group(2)]}\n"
return resp
def parse_cli_args():
parser = argparse.ArgumentParser(usage=arg_usage())
parser.add_argument(
"-a",
"--action",
dest="action",
required=True,
choices=["on", "off", "display"],
help="action to take upon the associated outlet(s)",
)
parser.add_argument(
"-o",
"--outlet",
dest="outlet",
choices=["1", "2", "3", "4", "all"],
help="outlet to set the power state on, values: 1-4, all",
)
parser.add_argument(
"--hostname", dest="hostname", help="IP9258 hostname or IP address"
)
parser.add_argument(
"-c",
"--config",
dest="config_file",
help="configuration file to use instead of the default",
)
parser.add_argument(
"-u",
"--username",
dest="http_user",
help="username for authentication to the IP9258",
)
parser.add_argument(
"-p",
"--password",
dest="http_pass",
help="password for authentication to the IP9258",
)
args = parser.parse_args()
if args.action != "display" and not args.outlet:
print(arg_usage())
exit()
return args
def main():
# process CLI args and dependencies
opts = parse_cli_args()
# load default configuration
config_file = str(Path.home()) + "/.config/botpower.cfg"
if opts.config_file:
config_file = opts.config_file
with open(config_file) as yaml_file:
cfg = yaml.load(yaml_file, Loader=yaml.BaseLoader)
# command line argument overrides
if opts.hostname:
cfg["hostname"] = opts.hostname
if opts.http_user:
cfg["username"] = opts.http_user
if opts.http_pass:
cfg["password"] = opts.http_pass
print("outlet:", opts.outlet)
print("action:", opts.action)
query_params = ""
if opts.action != "display":
query_params += "cmd=setpower+"
query_params += set_outlet(opts.outlet, opts.action)
else:
query_params += "cmd=getpower"
url = "http://" + cfg["hostname"] + cfg["api_url"] + query_params
r = requests.get(url, auth=(cfg["username"], cfg["password"]))
if r.status_code != 200:
print("FAILED REQUEST")
print("url:", r.url)
print("status code:", r.status_code)
print("headers\n", "-" * 70, sep="")
print(r.headers)
else:
out = parse_response(r.text)
print(out)
def arg_usage():
return """
botpower.py --action <display,on,off> --outlet <1,2,3,4,all>
turn outlet #1 on.
botpower.py -a on -o 1
outlet: 1
action: on
current outlet status
---------------------
outlet: 1 power: on
display the current state of the outlets.
botpower.py -a display
outlet: 1
action: display
current outlet status
---------------------
outlet: 1 power: on
outlet: 2 power: off
outlet: 3 power: off
outlet: 4 power: off
-o, --outlet
outlet to manipulate, valid values are as follows:
single value from 1 - 4. 1 at a time.
all: execute the associated action on all ports
-a, --action
the action to effect upon an outlet. valid actions are as follows:
on - turn the given outlet on
off - turn the given outlet off
display - display the current state of the outlets
optional arguments
these will override your config file values.
-u, --username (default: admin)
-p, --password (default: 12345678) - this is factory default
-c, --config - alternate configuration file to use.
"""
if __name__ == "__main__":
main()