-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlugPoller.py
More file actions
53 lines (48 loc) · 1.49 KB
/
PlugPoller.py
File metadata and controls
53 lines (48 loc) · 1.49 KB
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
__author__ = 'nrees'
import threading
import socket
class PlugPoller(threading.Thread):
def __init__(self, ip, port):
threading.Thread.__init__(self)
self.ip = ip
self.port = port
self.connect(ip, port)
self.status = '0000'
self.command = '!getstat\r'
self.loop = True
self.statusChangeCallback = None
def connect(self, ip, port):
try:
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.s.settimeout(1)
self.s.connect((ip, port))
except:
pass
def run(self):
while self.loop:
try:
sleep(.1)
self.s.send(self.command)
self.status = str(self.s.recv(4))
# print "data:{"+self.command,self.status+"}"
except:
# print 'plug connection error'
sleep(5.0)
self.connect(self.ip, self.port)
pass
if (self.command is not '!getstat\r'):
# print self.command
self.command = '!getstat\r'
def getstatus(self):
return self.status
def toggleAll(self):
self.sendcmd('!toggle1\r')
self.sendcmd('!toggle2\r')
if self.statusChangeCallback:
self.statusChangeCallback('{"Lamps":"'+self.getstatus()+'"}')
def sendcmd(self, c):
try:
self.s.send(c)
except:
pass
return self.s.recv(4)