-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataCollector.py
executable file
·190 lines (163 loc) · 6.28 KB
/
dataCollector.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
#!/usr/bin/env python
import socket, json, time, threading, urllib.request, urllib.error, logging.config, sys
from tools import helper
from time import mktime
import dataLogger
#from uuid import getnode as get_mac
MCAST_GRP = '224.1.1.1'
MCAST_PORT_BEAGLE_ALIVE = 5007
MCAST_PORT_DATACOLL_ALIVE = 5008
UDP_PORT = 5010
cache={}
participating=False
logger = logging.getLogger(__name__)
cpath= '../../.oes'
emul_getlog= "/get/log"
emul_url_getlog="to be overwritten in startDataCollector"
area=None
logToDB=False
logEmulatorToDB=False
def periodicAlive(interval):
strMsg = makeAliveMsg()
if logToDB and area:
logger.info("logging data to DB for area "+area)
else :
logger.info("not logging data to DB")
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
#send periodically (inifite loop)
i=0
while True:
#logger.debug( "sending alive multicast message"+json.dumps(msg))
sock.sendto(strMsg, (MCAST_GRP, MCAST_PORT_DATACOLL_ALIVE))
time.sleep(interval)
removeOldOesunits()
#logging to db
if logToDB and area:
i=i+1
if i==3: # only save to db every 3*5s
dataLogger.dbLogNow(area, json.dumps(cache))
i=0
def makeAliveMsg():
msg=helper.getInfo()
#overwrite some file infos
msg["display"]="dataCollector"
msg["id"]="dataColl"
msg["port"]=UDP_PORT
msg["on"]=participating
if "area" in msg :
global area
area=msg['area']
return str(json.dumps(msg))
def dataCollector() :
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(("", UDP_PORT))
logger.debug( "waiting on port:"+str( UDP_PORT))
while 1:
#data = s.recvfrom(2048)
try:
data, addr = s.recvfrom(2048)
#print data
#print addr[0]
#seems like we need to convert twice: string -> unicode -> dict
jsondata =json.loads(json.loads(data), object_hook=helper.convert)
if jsondata["oesunit"]["id"] not in cache:
logger.info("New unit discovered : "+ jsondata["oesunit"]["id"])
cache[jsondata["oesunit"]["id"]] = jsondata
except TypeError:
logger.error( "Datacollector could not decode json "+ str(data))
except:
logger.error("Unexpected error:"+ str(sys.exc_info()[0]))
def removeOldOesunits():
todelete=[]
for oesunit in cache:
now=time.time()
last = mktime(time.strptime(cache[oesunit]["time"], "%Y/%m/%d-%H:%M:%S"))
if now-last > 20 :
logger.warning("deleting oesunit from cache because no reply to datacollector for "+str(now-last)+" seconds. Oesid: "+oesunit)
todelete.append(oesunit)
#if there is any unit that should be deleted
if todelete :
for i in todelete:
del cache[i]
def forceCacheUpdate(emulator):
try:
global cache
logger.warning("Clear cache and make single call to dataCollector")
cache.clear()
if emulator :
logger.warning("forced cache update "+emul_url_getlog)
jsonrsp = urllib.request.urlopen(emul_url_getlog, timeout=2).read()
jsonout = json.loads(jsonrsp, object_hook=helper.convert)
for i in jsonout :
cache[jsonout[i]["oesunit"]["id"]] =jsonout[i]
logger.warning(cache);
else :
strMsg = makeAliveMsg()
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
sock.sendto(strMsg, (MCAST_GRP, MCAST_PORT_DATACOLL_ALIVE))
except urllib.error.URLError as e:
logging.error(e.reason)
def emulatedDataCollector(interval):
i=0
while 1:
# log to db every 10 requests if logEmulatorToDB=True
if logEmulatorToDB and i>=10 :
callToEmulator(True)
i=0
else:
callToEmulator(False)
time.sleep(interval)
i+=1
def callToEmulator(logToDB=False):
global cache
try:
jsonrsp = urllib.request.urlopen(emul_url_getlog, timeout=2).read()
jsonout = json.loads(jsonrsp, object_hook=helper.convert)
if len(jsonout)==0:
logging.error("empty json received "+str(jsonout))
else:
cache.clear()
now="empty"
for i in jsonout :
cache[jsonout[i]["oesunit"]["id"]] =jsonout[i]
now = jsonout[i]["time"]
logging.debug("emulDataCollectorCall: " +now)
if logToDB :
#logging.debug("logging to db")
dataLogger.dbLogNow(area, json.dumps(cache))
#else :
# logging.debug("not logging to db")
except ValueError as e:
logging.error(" json could not be decoded+ "+e.reason)
logging.error(jsonout)
except socket.timeout:
logging.error("Socket timeout for "+ emul_url_getlog)
except urllib.error.URLError as e:
logging.error("urllib.error.URLError in data collector"+str(e.reason))
except :
logging.exception('')
logging.error("unkown error in data collector")
def startDataCollector(interval, emulator, emul_url):
global emul_url_getlog
emul_url_getlog=emul_url+emul_getlog
logger.info("starting datacollector "+emul_url_getlog)
if emulator :
logger.info("starting emulated datacollector")
emulatedDataCollector(interval)
else :
logger.info("starting datacollector")
#sending alive messages to trigger log data
t = threading.Thread(target=periodicAlive, args=(interval,), name="alive-messages")
t.daemon = True
t.start()
#sleep for one second => there is a strange global import lock on strptime hold by another thread. sleeping for 1s seems to solve it
#time.sleep(10)
#listen to responses
dataCollector()
# if no other main is around use this one
if __name__ == "__main__":
logging.config.fileConfig("config/logger.conf",disable_existing_loggers=0)
logger = logging.getLogger(__name__)
startDataCollector(5, False)