forked from netik/rpc3control
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrpc3Control.py
executable file
·224 lines (180 loc) · 7.13 KB
/
rpc3Control.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/python
# Control Class for the Baytech RPC3
# J. Adams <[email protected]>
from pexpect import *
import sys
import re
import syslog
import time
import os
import errno
import datetime
import fcntl
class rpc3ControlError(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
class rpc3Control:
# Class to control a Baytech RPC-3
child = None
status = {}
name = {}
statuscached = False
unitid = ""
def __init__(self, hostname, user, password=None, debug=False):
self.hostname = hostname
self.user = user
self.password = password
self.debug = debug
self.connect()
self.unitid = ""
self.child.delaybeforesend = .05
# the RPC supports username, or password, or both
# so we support both of those cases in this code.
# get unit id
check = self.child.expect([".*Unit ID: (.*)", EOF, TIMEOUT], timeout=3)
if check == 0:
self.unitid = self.child.match.group(1)
if check > 0:
if check == 1:
raise rpc3ControlError("EOF during read")
else:
raise rpc3ControlError("Timeout during read in init function")
# print(str(self.unitid))
if user != None:
self.es("Enter username>", user)
if password != None:
self.es("Enter password>", password)
def file_exists(self, path, filename):
for file_or_folder in os.listdir(path):
if file_or_folder == filename:
return True
return False
def es(self,str_expect,str_send):
# a pexpect helper method; expect and send with error monitoring
result = self.child.expect([str_expect, EOF, TIMEOUT], timeout=3)
# print(str(self.child))
if result == 0:
self.child.send("%s\r" % str_send)
return
if result > 0:
if result == 1:
raise rpc3ControlError("EOF during read")
else:
raise rpc3ControlError("Timeout during read in ES function")
def connect(self):
if self.child == None:
self.child = spawn("telnet " + self.hostname)
result = self.child.expect(["Connected to", EOF, TIMEOUT], timeout=3)
#print (self.child)
#self.child.send("\r")
if self.debug == True:
self.child.logfile = sys.stdout
def outlet(self,outlet_number,state):
# control an outlet
# state is one of on, off,or reboot
# outlet_number is an integer.
if state not in ("on", "off", "reboot"):
self.child.send("6\r")
self.child.terminate()
raise rpc3ControlError('Invalid outlet state')
if int(outlet_number) > 8 or int(outlet_number) < 1:
self.child.send("6\r")
self.child.terminate()
return None
self.es("Enter Selection>", "1")
self.es("RPC-3>", "%s %d\rY" % (state, outlet_number) )
self.es("RPC-3>", "MENU")
self.statuscached = False
self.child.send("6\r")
self.child.terminate()
return True
def outlet_status(self, outlet_number, ignore_cache = True):
# Get the status of an outlet
if self.statuscached == True and ignore_cache != True:
if self.user == "admin" or self.user == None:
self.child.send("6\r")
else:
self.child.send("LOGOUT\r")
return (self.status[outlet_number],self.name[outlet_number])
if int(outlet_number) > 8 or int(outlet_number) < 1:
if self.user == "admin" or self.user == None:
self.child.send("6\r")
else:
self.child.send("LOGOUT\r")
return None
if self.user == "admin" or self.user == None:
self.es("Enter Selection>", "1")
self.es("RPC-3>", "MENU")
# parse the output
inlist = False
for line in self.child.before.split('\n'):
if line.rstrip() == "" and inlist:
inlist = False
#without the below check i would get parse errors randomly when checking state
if line.rstrip() == " 5)...Reset Unit" and inlist:
inlist = False
if inlist:
m = re.match('^ .....([0-9]) ..... (...........) ([0-9]) ..... (On|Off)',line)
self.name[int(m.group(1))] = m.group(2).rstrip()
if m.group(4) == "On":
self.status[int(m.group(1))] = True
else:
self.status[int(m.group(1))] = False
if line.find("Status") != -1:
inlist = True
self.statuscached = True
if self.user == "admin" or self.user == None:
self.child.send("6\r")
self.child.terminate()
else:
self.child.send("LOGOUT\r")
self.child.terminate()
#Write all outlet status to file and lock the file. Doing this because i use this script with homebridge and siri executes the script to get status for each outloet. The RPC unit allows only 4 telnet sesions for user admin at a time. I have homebridge use the this status file instead of logging in if the status file is recent.
lock_filename = 'telnetrunning.txt'
frunning = open(lock_filename,'w+')
while True:
try:
fcntl.flock(frunning, fcntl.LOCK_EX | fcntl.LOCK_NB)
break
except IOError as e:
# raise on unrelated IOErrors
if e.errno != errno.EAGAIN:
raise
else:
time.sleep(0.05)
i=1
sttime = str(int(round(time.time() * 1000)))
frunning.write(sttime + "," + str(outlet_number) + ",\r\n")
while (i <= 8):
writeText = "%d," % (i)
stringOutletStatus = "%s," % self.status[i]
stringOutletName = "%s" % self.name[i]
frunning.write(writeText + stringOutletStatus + stringOutletName + "\r\n")
i=i+1
fcntl.flock(frunning, fcntl.LOCK_UN)
frunning.close()
return (self.status[outlet_number],self.name[outlet_number])
# fetch credentials
# the credentials should be in a file called ".credentials" and in the form "hostname:user:pass" on one line.
def load_credentials(credentials):
user = None
pw = None
rpc = None
err = None
whitelist = None
try:
f = open(credentials, 'r')
(rpc, user, pw, w) = f.readline().rstrip().split(":",3)
whitelist = w.split(",")
f.close()
except IOError:
err = "FATAL: Couldn't open credentials file"
except ValueError:
err = "FATAL: Malformed %s file. Credentails should be in the form \"host:user:pw\"" % credentials
if err != None:
syslog.syslog(syslog.LOG_ERR, err)
print >> sys.stderr, err
sys.exit(1)
return (rpc,user,pw,whitelist)