def test_start(self): timer = PeriodicTimer(3, self.foo) timer.start() try: while time < 11: time.sleep(1) finally: timer.cancel()
def panic(self): if not self.timeout: self.realPanic() else: self.timePassed = 0 self.panicTimer = PeriodicTimer(1.0, self.updateState, self.timeout, self.realPanic) self.panicTimer.start()
def start(self): # start periodic checks if self.timer: return self.timer = PeriodicTimer(self.delay, self.handleData) self.timer.start()
class BaseSensor(): def __init__(self, options, panicCallback, stopPanicCallback): # you should probably save the callbacks and do something with the options # if options are wrong, raise WrongOptionsError # if the sensor is not suported (i.e. a required dll isn't installed), raise NotSupportedError self.delay = 1 self.timer = None self.panicking = False self.panicCallback = panicCallback self.stopPanicCallback = stopPanicCallback def checkIfWeAreScrewed(self): # this function is NOT required and will not be called by the program itself # however, functions start() and stop() below use it, so if you're too lazy to write your own start() and stop(), # you can simply redefine this function return False def handleData(self): # same as above, basically res = self.checkIfWeAreScrewed() if res and (not self.panicking): self.panicking = True self.panicCallback() elif (not res) and self.panicking: self.panicking = False self.stopPanicCallback() def start(self): # start periodic checks if self.timer: return self.timer = PeriodicTimer(self.delay, self.handleData) self.timer.start() def stop(self): # stop periodic checks if not self.timer: return self.timer.cancel() self.timer = None def close(self): # close all the handles and shit pass
def control_temperature_thread(): global process command = json.loads(request.data)['thread_command'] if command == 'Start' and process : print 'process is already on!' if command == 'Start' and not process: time_update=1 # This starts the process that runs control_gpio_state periodically. process = PeriodicTimer(time_update, control_gpio_state) process.start() else: try: process.cancel() process = None except: print "Process did not exist yet" return "OK", 200
def run(self): # todo: make it automatic what table columns are supposed to be there, derived from contents of measurement object? self.db.create_database_table(self.table_name, timeStamp='REAL', value='REAL', units='TEXT') timerDataAquisition = PeriodicTimer(self.timeStep, self.appendMeasurement) timerWriteToFile = PeriodicTimer(self.writeInterval, self.writeMeasurementsToDatabase) timerDataAquisition.start() time.sleep(1) timerWriteToFile.start() try: while True: pass finally: self.writeMeasurementsToDatabase() timerDataAquisition.cancel() timerWriteToFile.cancel()
class Alarmist(): def __init__(self, panic, panicOptions, sensor, sensorOptions, UI, UIOptions, initState, timeout): self.state = initState self.panicM = panic(panicOptions) self.sensor = sensor(sensorOptions, self.panic, self.stopPanic) self.UI = UI(UIOptions, self.toggle, self.exit, self.state) self.panicTimer = None self.timeout = timeout self.timePassed = 0 if (self.state): self.sensor.start() def exit(self): self.sensor.stop() self.sensor.close() self.UI.close() os._exit(0) def realPanic(self): self.panicM.panic() self.exit() def updateState(self): self.timePassed += 1 self.UI.changePanicState(float(self.timePassed) / self.timeout) def panic(self): if not self.timeout: self.realPanic() else: self.timePassed = 0 self.panicTimer = PeriodicTimer(1.0, self.updateState, self.timeout, self.realPanic) self.panicTimer.start() def stopPanic(self): if not self.panicTimer: return self.panicTimer.cancel() self.panicTimer = None self.state = True self.UI.changePanicState(0) self.UI.changeState(self.state) def toggle(self): self.state = not self.state if (self.state): self.sensor.start() else: self.sensor.stop() if (self.panicTimer): self.panicTimer.cancel() self.panicTimer = None self.UI.changeState(self.state)