From 14220792cd48d1600c0d4677ce37006fd1adb204 Mon Sep 17 00:00:00 2001 From: Mark Tearle Date: Sat, 30 Apr 2005 09:27:51 +0000 Subject: [PATCH] grandfather clock --- sql-edition/servers/VendServer.py | 111 +++++++++++++++++++++++++++++- 1 file changed, 110 insertions(+), 1 deletion(-) diff --git a/sql-edition/servers/VendServer.py b/sql-edition/servers/VendServer.py index 2adc4e1..ce82535 100755 --- a/sql-edition/servers/VendServer.py +++ b/sql-edition/servers/VendServer.py @@ -8,7 +8,7 @@ import sys, os, string, re, pwd, signal, math import logging, logging.handlers from traceback import format_tb if USE_DB: import pg -from time import time, sleep +from time import time, sleep, mktime, localtime from popen2 import popen2 from LATClient import LATClient, LATClientException from SerialClient import SerialClient, SerialClientException @@ -51,6 +51,7 @@ STATE_DOOR_CLOSING = 3 STATE_GETTING_UID = 4 STATE_GETTING_PIN = 5 STATE_GET_SELECTION = 6 +STATE_GRANDFATHER_CLOCK = 7 class DispenseDatabaseException(Exception): pass @@ -503,7 +504,108 @@ def handle_idle_tick(state, event, params, v, vstatus): ### vstatus.mk.update_display() + + vstatus.state = STATE_GRANDFATHER_CLOCK + run_handler(event, params, v, vstatus) + +def beep_on(when, before=0): + start = int(when - before) + end = int(when) + now = int(time()) + + if now >= start and now <= end: + return 1 + return 0 + +def handle_idle_grandfather_tick(state, event, params, v, vstatus): + ### check for interesting times + now = localtime() + + quarterhour = mktime([now[0],now[1],now[2],now[3],15,0,now[6],now[7],now[8]]) + halfhour = mktime([now[0],now[1],now[2],now[3],30,0,now[6],now[7],now[8]]) + threequarterhour = mktime([now[0],now[1],now[2],now[3],45,0,now[6],now[7],now[8]]) + fivetothehour = mktime([now[0],now[1],now[2],now[3],55,0,now[6],now[7],now[8]]) + + hourfromnow = localtime(time() + 3600) + + #onthehour = mktime([now[0],now[1],now[2],now[3],27,0,now[6],now[7],now[8]]) + onthehour = mktime([hourfromnow[0],hourfromnow[1],hourfromnow[2],hourfromnow[3], \ + 0,0,hourfromnow[6],hourfromnow[7],hourfromnow[8]]) + + ## check for X seconds to the hour + ## if case, update counter to 2 + if beep_on(onthehour,15) \ + or beep_on(halfhour,0) \ + or beep_on(quarterhour,0) \ + or beep_on(threequarterhour,0) \ + or beep_on(fivetothehour,0): + vstatus.state = STATE_GRANDFATHER_CLOCK + vstatus.counter = 2 + run_handler(event, params, v, vstatus) + else: + vstatus.state = STATE_IDLE + +def handle_grandfather_tick(state, event, params, v, vstatus): + go_idle = 1 + msg = [] + ### we live in interesting times + now = localtime() + + quarterhour = mktime([now[0],now[1],now[2],now[3],15,0,now[6],now[7],now[8]]) + halfhour = mktime([now[0],now[1],now[2],now[3],30,0,now[6],now[7],now[8]]) + threequarterhour = mktime([now[0],now[1],now[2],now[3],45,0,now[6],now[7],now[8]]) + fivetothehour = mktime([now[0],now[1],now[2],now[3],55,0,now[6],now[7],now[8]]) + + hourfromnow = localtime(time() + 3600) + + #onthehour = mktime([now[0],now[1],now[2],now[3],27,0,now[6],now[7],now[8]]) + onthehour = mktime([hourfromnow[0],hourfromnow[1],hourfromnow[2],hourfromnow[3], \ + 0,0,hourfromnow[6],hourfromnow[7],hourfromnow[8]]) + + + #print "when it fashionable to wear a onion on your hip" + + if beep_on(onthehour,15): + go_idle = 0 + next_hour=((hourfromnow[3] + 11) % 12) + 1 + if onthehour - time() < next_hour and onthehour - time() > 0: + v.beep(0, False) + msg.append((" DING!", False, None)) + elif int(onthehour - time()) == 0: + v.beep(255, False) + msg.append((" DING!", False, None)) + msg.append((" IT'S "+ str(next_hour) + "O'CLOCK AND ALL IS WELL .....", False, None)) + elif beep_on(halfhour,0): + go_idle = 0 + v.beep(0, False) + msg.append((" HALFHOUR ", False, None)) + elif beep_on(quarterhour,0): + go_idle = 0 + v.beep(0, False) + msg.append((" QTR HOUR ", False, None)) + elif beep_on(threequarterhour,0): + go_idle = 0 + v.beep(0, False) + msg.append((" 3 QTR HOUR ", False, None)) + elif beep_on(fivetothehour,0): + go_idle = 0 + v.beep(0, False) + msg.append(("Quick run to your lectures! Hurry! Hurry!", False, None)) + else: + go_idle = 1 + + ## check for X seconds to the hour + + vstatus.mk.set_messages(msg) + vstatus.mk.update_display() + sleep(1) + ## if no longer case, return to idle + + ## change idler to be clock + if go_idle and vstatus.mk.done(): + vstatus.counter = 1 + vstatus.state = STATE_IDLE def handle_door_idle(state, event, params, v, vstatus): # don't care right now. @@ -564,6 +666,13 @@ def create_state_table(vstatus): vstatus.state_table[(STATE_GET_SELECTION,DOOR,1)] = do_nothing vstatus.state_table[(STATE_GET_SELECTION,KEY,1)] = handle_get_selection_key + vstatus.state_table[(STATE_GRANDFATHER_CLOCK,TICK,1)] = handle_idle_grandfather_tick + vstatus.state_table[(STATE_GRANDFATHER_CLOCK,TICK,2)] = handle_grandfather_tick + vstatus.state_table[(STATE_GRANDFATHER_CLOCK,DOOR,1)] = do_nothing + vstatus.state_table[(STATE_GRANDFATHER_CLOCK,DOOR,2)] = do_nothing + vstatus.state_table[(STATE_GRANDFATHER_CLOCK,KEY,1)] = do_nothing + vstatus.state_table[(STATE_GRANDFATHER_CLOCK,KEY,2)] = do_nothing + def get_state_table_handler(vstatus, state, event, counter): return vstatus.state_table[(state,event,counter)] -- 2.20.1