-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata_logger.py
49 lines (39 loc) · 1.25 KB
/
data_logger.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
#!/usr/bin/env python
import os,sys
import math, re, time
import py3toolbox as tb
import multiprocessing as mp
import json
MODULE_NAME = "DataLogger"
def get_config():
config = tb.load_json('./config.json')
return config
# =================================================
#
# A simple logging class
#
# =================================================
class DataLogger(mp.Process):
def __init__(self, q_in, q_mon):
mp.Process.__init__(self)
self.config = get_config()[MODULE_NAME]
self.q_in = q_in
self.q_mon = q_mon
self.data_lines = ""
self.last_updated = time.time()
self.log_file = os.path.join(self.config["log_folder"], "data.sensors.log" )
def log(self) :
if get_config()['DataReader']['feed_channel'] == 'FILE' :
return
else:
tb.rm_file(self.log_file)
while True:
self.data_lines = self.data_lines + "\n" + json.dumps(self.q_in.get())
if time.time() - self.last_updated >= 5:
tb.write_file(file_name = self.log_file , text=self.data_lines + "\n" , mode='a')
self.data_lines = ""
self.last_updated = time.time()
def run(self):
self.q_mon.put(MODULE_NAME)
self.log()
pass