156 lines
4.0 KiB
Python
Executable File
156 lines
4.0 KiB
Python
Executable File
import sys
|
|
import serial
|
|
import time
|
|
import traceback
|
|
#from CommonCode.db_conn import connect
|
|
import dateutil.parser
|
|
from datetime import timedelta, datetime
|
|
from pytz import timezone
|
|
from CommonCode.track import Temperature, Odometer, BaseSensorPost
|
|
import os
|
|
import threading
|
|
import redis
|
|
|
|
#sq = connect()
|
|
r = redis.StrictRedis(host='192.168.1.242', port=6379, db=1)
|
|
|
|
def now():
|
|
return datetime.now()
|
|
# return datetime.now(timezone('US/Eastern'))
|
|
|
|
commit_interval = 60
|
|
code_heartbeat = 10*60
|
|
|
|
last_commit = now()
|
|
last_heartbeat = now()
|
|
|
|
#func_ref = {'T:':Temperature(sq, 'floor2temperature'), 'V:':VerticalWheel(sq, 'verticalwheel'), 'M:':ActivityLogger(sq,'activities'),
|
|
# 'W':Weight(sq,'food_weight'), 'S:':HorizontalWheel(sq, 'horizontalwheel_sonic'), 'D':ActivityLogger(sq,'food_dispenser'), 'A':Paired(sq,'food_forager')};
|
|
|
|
|
|
from clickhouse_driver import connect
|
|
from datetime import datetime
|
|
conn = connect('clickhouse://192.168.1.242');
|
|
sq = conn.cursor();
|
|
|
|
|
|
func_ref = {'T':Temperature(sq, 'floor_3_temperature','temperature',where=1),'S':Odometer(sq,'i2c_odometer','odometer',who=4)}
|
|
code_start = BaseSensorPost(sq, 'started','heartbeats')
|
|
code_start.insert_heartbeat(now())
|
|
|
|
|
|
heartbeat = BaseSensorPost(sq, 'code','heartbeats')
|
|
|
|
ser = serial.Serial('/dev/serial_i2c', 9600, timeout=None)
|
|
|
|
def write():
|
|
while True:
|
|
time.sleep(0.2)
|
|
try:
|
|
if os.path.exists('/dev/shm/mealies_open.txt'):
|
|
os.remove('/dev/shm/mealies_open.txt')
|
|
ser.write(b'n')
|
|
|
|
if os.path.exists('/dev/shm/mealies_close.txt'):
|
|
os.remove('/dev/shm/mealies_close.txt')
|
|
ser.write(b'o')
|
|
|
|
if os.path.exists('/dev/shm/mealies_next.txt'):
|
|
os.remove('/dev/shm/mealies_next.txt')
|
|
ser.write(b'p')
|
|
|
|
|
|
|
|
|
|
|
|
if os.path.exists('/dev/shm/winslow/mealies2_open.txt'):
|
|
os.remove('/dev/shm/winslow/mealies2_open.txt')
|
|
ser.write(b'c')
|
|
|
|
if os.path.exists('/dev/shm/mealies2_reset.txt'):
|
|
os.remove('/dev/shm/mealies2_reset.txt')
|
|
ser.write(b'd')
|
|
|
|
if os.path.exists('/dev/shm/mealies2_open_all.txt'):
|
|
os.remove('/dev/shm/mealies2_open_all.txt')
|
|
ser.write(b'e')
|
|
|
|
|
|
|
|
|
|
if os.path.exists('/dev/shm/mealies_reinit.txt'):
|
|
os.remove('/dev/shm/mealies_reinit.txt')
|
|
ser.write(b'q')
|
|
|
|
|
|
if os.path.exists('/dev/shm/i2c_reinit.txt'):
|
|
os.remove('/dev/shm/i2c_reinit.txt')
|
|
ser.write(b'r')
|
|
|
|
if os.path.exists('/dev/shm/winslow/mealies2_reset.txt'):
|
|
os.remove('/dev/shm/winslow/mealies2_reset.txt')
|
|
ser.write(b'd')
|
|
|
|
except:
|
|
e = traceback.format_exc()
|
|
print(e)
|
|
|
|
|
|
#t1 = threading.Thread(target=write, args=())
|
|
#t1.start()
|
|
|
|
|
|
while True:
|
|
|
|
# try:
|
|
if True:
|
|
time.sleep(0.2)
|
|
data = ser.readline()
|
|
string = data.decode('UTF-8')
|
|
print(string)
|
|
parse_str = string.strip()
|
|
time_init = now()
|
|
for startchars, function in func_ref.items():
|
|
if parse_str.startswith(startchars):
|
|
r.set('i2c_'+startchars, str(time.time()) + ':' + parse_str)
|
|
try:
|
|
function.parse(time_init,parse_str)
|
|
except IndexError:
|
|
print('Index error')
|
|
except Exception as ff:
|
|
raise Exception(ff)
|
|
|
|
if (time_init - last_commit).seconds > commit_interval:
|
|
# sq['s'].commit()
|
|
last_commit = time_init
|
|
|
|
# sq['s'].commit()
|
|
curr_time = now()
|
|
if (curr_time - last_heartbeat).seconds > code_heartbeat:
|
|
heartbeat.insert_heartbeat(curr_time)
|
|
last_heartbeat = curr_time
|
|
|
|
last_heartbeat = curr_time
|
|
|
|
|
|
|
|
# except:
|
|
# e = traceback.format_exc()
|
|
# print(e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|