-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnp05.py
57 lines (45 loc) · 1.66 KB
/
np05.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
import sys, time
import requests
class np05(object):
def __init__(self, address, username = "admin", password = "admin"):
self.address = address
self.username = username
self.password = password
self.url = "http://{}/cmd.cgi?".format(address)
def _run_command(self, cmd, args=[]):
argstr = " ".join([str(i) for i in args])
fullurl = "{}{} {}".format(self.url, cmd, argstr)
try:
r = requests.post(fullurl, auth=(self.username, self.password), timeout=10)
except (requests.ConnectTimeout, requests.ConnectionError):
print("ERROR: Unable to connect to NP05b power manager.")
sys.exit(1)
return r
def all_on(self):
cmd = "$A7"
args = [1]
return self._run_command(cmd, args).status_code != 200
def all_off(self):
cmd = "$A7"
args = [0]
return self._run_command(cmd, args).status_code != 200
def power_on(self, outlet):
cmd = "$A3"
args = [outlet, 1]
return self._run_command(cmd, args).status_code != 200
def power_off(self, outlet):
cmd = "$A3"
args = [outlet, 0]
return self._run_command(cmd, args).status_code != 200
def is_on(self, outlet):
return self.status()[outlet - 1] == 1
def status(self):
cmd = "$A5"
status_str = self._run_command(cmd).content.decode('utf-8')
return [i for i in reversed([int(c) for c in status_str])]
def wake_up(self):
try:
requests.get(self.url)
return 0
except (requests.ConnectionError, requests.ConnectTimeout):
return 1