YACWC
This commit is contained in:
129
trackSerial.py
Executable file
129
trackSerial.py
Executable file
@@ -0,0 +1,129 @@
|
||||
import sys
|
||||
import serial
|
||||
import time
|
||||
import traceback
|
||||
import syslog
|
||||
from db_conn import connect
|
||||
import dateutil.parser
|
||||
from datetime import timedelta, datetime, timezone
|
||||
sys.path.append('/home/thebears/Nextcloud/Designs/NuggetTracker/CommonCode/')
|
||||
from track import Temperature, VerticalWheel, ActivityLogger, Weight, BaseSensorPost, HorizontalWheel, Paired
|
||||
import os
|
||||
|
||||
sq = connect()
|
||||
|
||||
commit_interval = 60
|
||||
code_heartbeat = 10*60
|
||||
|
||||
def now():
|
||||
return datetime.now(timezone.utc)
|
||||
|
||||
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'), 'D':ActivityLogger(sq,'food_dispenser'), 'A':Paired(sq,'food_forager')};
|
||||
|
||||
code_start = BaseSensorPost(sq, 'started')
|
||||
code_start.insert_heartbeat(now())
|
||||
|
||||
|
||||
heartbeat = BaseSensorPost(sq, 'code')
|
||||
|
||||
ser = serial.Serial('/dev/winslowMONITOR', 9600, timeout=None)
|
||||
#ser = serial.Serial('/dev/ttyACM0',57600,timeout=None)
|
||||
while True:
|
||||
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/mealies2_open.txt'):
|
||||
os.remove('/dev/shm/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)
|
||||
syslog.syslog(syslog.LOG_ERR,e)
|
||||
|
||||
|
||||
|
||||
|
||||
try:
|
||||
|
||||
data = ser.readline()
|
||||
string = data.decode('UTF-8')
|
||||
print(string)
|
||||
syslog.syslog(syslog.LOG_INFO,string)
|
||||
parse_str = string.strip()
|
||||
time_init = now()
|
||||
for startchars, function in func_ref.items():
|
||||
if parse_str.startswith(startchars):
|
||||
print(parse_str, function)
|
||||
function.parse(time_init,parse_str)
|
||||
|
||||
|
||||
sq['s'].commit()
|
||||
curr_time = now()
|
||||
if (curr_time - last_heartbeat).seconds > code_heartbeat:
|
||||
heartbeat.insert_heartbeat(curr_time)
|
||||
|
||||
|
||||
|
||||
except:
|
||||
e = traceback.format_exc()
|
||||
print(e)
|
||||
syslog.syslog(syslog.LOG_ERR,e)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user