From 6db95ac9449609be4128ebb1a536f0c978543369 Mon Sep 17 00:00:00 2001 From: Mark Tearle Date: Tue, 5 Apr 2005 09:55:01 +0000 Subject: [PATCH] more refactoring --- sql-edition/servers/VendServer.py | 33 ++++++++++++++++++++------- sql-edition/servers/VendingMachine.py | 4 +++- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/sql-edition/servers/VendServer.py b/sql-edition/servers/VendServer.py index e896920..c1ef276 100755 --- a/sql-edition/servers/VendServer.py +++ b/sql-edition/servers/VendServer.py @@ -42,6 +42,7 @@ PIN_LENGTH = 4 DOOR = 1 SWITCH = 2 KEY = 3 +TICK = 4 class DispenseDatabaseException(Exception): pass @@ -147,6 +148,8 @@ def door_open_mode(v): e = v.next_event() if e == None: break (event, params) = e + if event == TICK: break + if event == DOOR: if params == 1: # door closed logging.warning('Leaving open door mode') @@ -256,6 +259,10 @@ def handle_door_event(event, params, v, vstatus): vstatus.cur_pin = '' vstatus.mk.set_message(GREETING) +def handle_tick_event(event, params, v, vstatus): + # don't care right now. + pass + def handle_switch_event(event, params, v, vstatus): # don't care right now. pass @@ -420,8 +427,10 @@ def run_forever(rfh, wfh, options, cf): vstatus.cur_selection = '' vstatus.mk.set_message(GREETING) - if vstatus.time_to_autologout and not vstatus.mk.done(): vstatus.time_to_autologout = None - if vstatus.cur_user == '' and vstatus.time_to_autologout: vstatus.time_to_autologout = None + if vstatus.time_to_autologout and not vstatus.mk.done(): + vstatus.time_to_autologout = None + if vstatus.cur_user == '' and vstatus.time_to_autologout: + vstatus.time_to_autologout = None if len(vstatus.cur_pin) == PIN_LENGTH and vstatus.mk.done() and vstatus.time_to_autologout == None: # start autologout vstatus.time_to_autologout = time() + 15 @@ -430,21 +439,29 @@ def run_forever(rfh, wfh, options, cf): if vstatus.time_to_idle == None and vstatus.cur_user == '': vstatus.time_to_idle = time() + 5 choose_idler() - if vstatus.time_to_idle is not None and vstatus.cur_user != '': vstatus.time_to_idle = None - if vstatus.time_to_idle is not None and time() > vstatus.time_to_idle: idle_step() - if vstatus.time_to_idle is not None and time() > vstatus.time_to_idle + 300: + if vstatus.time_to_idle is not None and vstatus.cur_user != '': + vstatus.time_to_idle = None + + if vstatus.time_to_idle is not None and time() > vstatus.time_to_idle: + idle_step() + + if vstatus.time_to_idle is not None and time() > vstatus.time_to_idle + 30: vstatus.time_to_idle = time() choose_idler() vstatus.mk.update_display() e = v.next_event(0) - if e == None: + (event, params) = e + + if event == TICK: e = v.next_event(0.05) - if e == None: + (event, params) = e + + if event == TICK: + handle_tick_event(event, params, v, vstatus) continue vstatus.time_to_idle = None - (event, params) = e logging.debug('Got event: ' + repr(e)) if event == DOOR: diff --git a/sql-edition/servers/VendingMachine.py b/sql-edition/servers/VendingMachine.py index 26dc0f2..7a06111 100644 --- a/sql-edition/servers/VendingMachine.py +++ b/sql-edition/servers/VendingMachine.py @@ -3,6 +3,7 @@ import re from CRC import do_crc from select import select import socket, logging +from time import time, sleep asynchronous_responses = [ '400', '401', # door open/closed '610', # switches changed @@ -12,6 +13,7 @@ asynchronous_responses = [ '400', '401', # door open/closed DOOR = 1 SWITCH = 2 KEY = 3 +TICK = 4 class VendingException(Exception): pass @@ -160,7 +162,7 @@ class VendingMachine: timeout = 0 else: break - if len(self.events) == 0: return None + if len(self.events) == 0: return (TICK, time()) ret = self.events[0] del self.events[0] return ret -- 2.20.1