-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththermoPiClient.py
More file actions
executable file
·121 lines (99 loc) · 3.37 KB
/
thermoPiClient.py
File metadata and controls
executable file
·121 lines (99 loc) · 3.37 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
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
#!/usr/bin/python
import ConfigParser
import datetime
import logging
import os
import Queue
import socket
import string
import sys
import threading
import time
config = ConfigParser.RawConfigParser()
config.read('config/client.cfg')
HOST = config.get('network', 'host')
PORT = config.getint('network', 'port')
w1_path = "/sys/bus/w1/devices/{0}/w1_slave"
sensorsConfig = config.items('sensors')
sensors = []
for sensor in sensorsConfig:
sensors.append(sensor[1])
logging.basicConfig(filename='client.log', level=logging.DEBUG, format='%(asctime)s %(message)s')
class threadedClient (threading.Thread):
wsock = None
commandQueue = None
port = None
def __init__(self, commandQueue=None):
threading.Thread.__init__(self)
if commandQueue is not None:
self.commandQueue = commandQueue
pass
def __del__(self):
print "cowardly dying..."
# TODO: for some reason the threads are never deallocated until the main
# process is killed... maybe setDaemon()
pass
def run(self):
self.open_socket(HOST, PORT)
# sends that i am a logger
self.wsock.send("LOG")
data = self.wsock.recv(128)
while True:
# print "checking queue"
if self.commandQueue is not None:
try:
queueValue = self.commandQueue.get(False, 0)
# print "checking queue, found something:{0}".format(queueValue)
self.wsock.send(queueValue)
data = self.wsock.recv(128)
print data
except Queue.Empty:
# print "nothing in the queue"
pass
time.sleep(1)
def open_socket(self, addr, port):
if self.wsock is None:
print "connecting to {0} port {1}".format(addr, port)
self.wsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
self.wsock.connect((addr, port))
except:
os._exit(1)
else:
print "already connected to {0} port {1}".format(addr, port)
q = Queue.Queue()
client = threadedClient(q)
client.start()
i = 0
while True:
if i % 30 == 0:
logging.debug('sending heartbeat')
q.put('3|KEEPALIVE')
if i == 0:
i = 0
for sensor in sensors:
# 24 00 4b 46 ff ff 0e 10 3d : crc=3d YES
# 24 00 4b 46 ff ff 0e 10 3d t=17875
# data = "24 00 4b 46 ff ff 0e 10 3d t=17875"
try:
wfile = open(w1_path.format(sensor), 'r')
data = wfile.read()
wfile.close()
if string.find(data, '=') != -1:
temp = string.rsplit(data, '=', 1)[1]
q.put('3|SENSOR|{0}|{1}'.format(sensor, temp))
else:
raise IndexError('Data in unexpected format')
except IOError as ioe:
logging.warning("Error polling '{0}': '{1}'".format(sensor, ioe))
print("Error polling '{0}': '{1}'".format(sensor, ioe))
except IndexError as ie:
logging.warning("Error parsing '{0}': '{1}'".format(sensor, ie))
print("Error parsing '{0}': '{1}'".format(sensor, ie))
else:
# q.put("1|PASS")
pass
i = i + 1
if i == 60:
i = 0
time.sleep(1)