import sys import serial import time import math import traceback from subprocess import check_output, call import syslog from db_conn import connect import dateutil.parser from datetime import timedelta, datetime sys.path.append('/home/thebears/Nextcloud/Designs/NuggetTracker/CommonCode/') from track import Temperature, VerticalWheel, ActivityLogger, Weight, BaseSensorPost, HorizontalWheel import os from sqlalchemy import func import syslog import redis do_break = False while True: try: sq = connect() do_break = True; except: syslog.syslog("Failed to connect, waiting 1 second and trying again") time.sleep(1) if do_break: break curr_time = datetime.now() last_date = datetime(curr_time.year, curr_time.month, curr_time.day, 18, 0, 0) if last_date > curr_time: curr_time = datetime.now() - timedelta(days=1) last_date = datetime(curr_time.year, curr_time.month, curr_time.day, 18, 0, 0) r = redis.StrictRedis(host='192.168.1.242', port=6379, db=1) os.makedirs('/dev/shm/winslow/', exist_ok=True) call(['chmod','777','/dev/shm/winslow/']) res = sq['s'].query(func.count(sq['t']['horizontalwheel'].c.transittime)).filter(sq['t']['horizontalwheel'].c.timestamp > last_date).all() total_counts = res[0][0] r.set('nugget_run_counts', total_counts) #1500 turns is 1/4 mile threshold_for_next_mealie = 1500 flipped_stuff = list() bins_per_day = 18 last_iter = min([int(math.floor(total_counts / threshold_for_next_mealie)),bins_per_day]) while True: do_rotate = False next_date = last_date + timedelta(days = 1) if datetime.now() > next_date: if next_date not in flipped_stuff: do_rotate = True if do_rotate is True: curr_time = datetime.now() last_date = datetime(curr_time.year, curr_time.month, curr_time.day, 18, 0, 0) flipped_stuff.append(last_date) syslog.syslog('Resetting time to '+str(last_date)) last_iter = 0 r.set('nugget_run_counts', 0) # res = sq['s'].query(func.count(sq['t']['horizontalwheel'].c.transittime)).filter(sq['t']['horizontalwheel'].c.timestamp > last_date).all() # total_counts = res[0][0] total_counts = int(r.get('nugget_run_counts')) curr_iter = min([int(math.floor(total_counts / threshold_for_next_mealie)),bins_per_day]) syslog.syslog('Total: '+str(total_counts)+' Current bin: '+str(curr_iter)+ ' Redis Count:' + str(total_counts)) if curr_iter != last_iter: last_iter = curr_iter open('/dev/shm/winslow/mealies2_open.txt','w').close() call(['chmod','777','/dev/shm/winslow']) syslog.syslog("OPENING MEALIES") time.sleep(5)