class WDEventHandler(FileSystemEventHandler): def __init__(self, gus): self.timer = None self.gus = gus def timer_callback(self): self.gus.render_site() self.timer = None def do_something(self, event): if re.match('.*~$', event.src_path): return if re.match("^.*%s\..*\.sw.$" % os.path.sep, event.src_path): return if self.timer is None: self.timer = ResettableTimer(0.5, self.timer_callback) else: self.timer.reset() print("mod or create %s" % event.src_path) def on_created(self, event): self.do_something(event) def on_modified(self, event): self.do_something(event)
def test_timer_supports_args_and_kwargs(self): m = Mock(return_value=None) t = ResettableTimer(3, m, ["args"], {"kwarg": "kwarg"}) t.start() t._timer.pass_time(3) m.assert_called_once_with("args", kwarg="kwarg")
def test_timer_can_be_started_with_reset(self): m = Mock(return_value=None) t = ResettableTimer(3, m) t._timer.pass_time(3) m.assert_not_called() t.reset(start=True) t._timer.pass_time(3) m.assert_called_once_with()
class ServoControl(object): def __init__(self, pan_pin=12, tilt_pin=18, pan_limits=(500, 2500), tilt_limits=(500, 2500), initial_position=(90, 90), auto_off=1.5): self.__pi = pigpio.pi() self.__pan = ServoDetails(pan_pin, *pan_limits) self.__tilt = ServoDetails(tilt_pin, *tilt_limits) if auto_off: self.__auto_off_timer = ResettableTimer(auto_off, self.off) self.__auto_off_timer.start() else: self.__auto_off_timer = None # Move to initial position to have servos in known position self.move_to(*initial_position) def __set_pw(self, servo, pw=None): # TODO: check input pw validity if pw is None: pw = servo.pulsewidth return self.__pi.set_servo_pulsewidth(servo.pin, pw) @property def position(self): return {"pan": self.__pan.pos, "tilt": self.__tilt.pos} def move_to(self, pan=None, tilt=None): if pan is not None: self.__pan.pos = pan self.__set_pw(self.__pan) if tilt is not None: self.__tilt.pos = tilt self.__set_pw(self.__tilt) if self.__auto_off_timer is not None: self.__auto_off_timer.reset() def off(self): for servo in [self.__pan, self.__tilt]: self.__set_pw(servo, 0) def min(self): self.move_to(0, 0) def max(self): self.move_to(180, 180)
def test_timer_can_be_reset_after_cancel(self): m = Mock(return_value=None) t = ResettableTimer(5, m) t.start() t._timer.pass_time(3) m.assert_not_called() t.cancel() t.reset() t.start() t._timer.pass_time(6) m.assert_called_once_with()
def _main(): i = inotify.adapters.Inotify() i.add_watch(TARGET_DIR) global running_ti, files, cre ti = ResettableTimer(TIMER_TO, expired) cre = re.compile(REGEX) # watch directory for new file creation while 1 > 0 : for event in i.event_gen(yield_nones=False, timeout_s=1): (_, type_names, path, filename) = event for type_name in type_names: if (type_name == "IN_CREATE") : # start a new timer if ( running_ti ) : ti.reset() files.append(filename) if ( not running_ti ) : running_ti = True ti = ResettableTimer(TIMER_TO, expired) ti.start() files = [] files.append(filename)
def __init__(self, pan_pin=12, tilt_pin=18, pan_limits=(500, 2500), tilt_limits=(500, 2500), initial_position=(90, 90), auto_off=1.5): self.__pi = pigpio.pi() self.__pan = ServoDetails(pan_pin, *pan_limits) self.__tilt = ServoDetails(tilt_pin, *tilt_limits) if auto_off: self.__auto_off_timer = ResettableTimer(auto_off, self.off) self.__auto_off_timer.start() else: self.__auto_off_timer = None # Move to initial position to have servos in known position self.move_to(*initial_position)
def do_something(self, event): if re.match('.*~$', event.src_path): return if re.match("^.*%s\..*\.sw.$" % os.path.sep, event.src_path): return if self.timer is None: self.timer = ResettableTimer(0.5, self.timer_callback) else: self.timer.reset() print "mod or create %s" % event.src_path
class PiScreenOnOff(object): def __init__(self, timeout=15 * 60, pin=17): self.__sensor = HcSr501(pin) self.__timer = ResettableTimer(timeout, screen, [False]) self.__timer.start() def __run(self): while True: if self.__sensor.active: screen(True) self.__timer.reset(start=True) sleep(0.5) def run(self): try: self.__run() except KeyboardInterrupt: self.__timer.cancel()
def datagramReceived(self, data, addr): # HomeBrew Protocol Commands DMRD = b'DMRD' DMRA = b'DMRA' MSTCL = b'MSTCL' MSTNAK = b'MSTNAK' MSTPONG = b'MSTPONG' MSTN = b'MSTN' MSTP = b'MSTP' MSTC = b'MSTC' RPTL = b'RPTL' RPTPING = b'RPTPING' RPTCL = b'RPTCL' RPTL = b'RPTL' RPTACK = b'RPTACK' RPTK = b'RPTK' RPTC = b'RPTC' RPTP = b'RPTP' RPTA = b'RPTA' RPTO = b'RPTO' host, port = addr nowtime = time() Debug = self.debug #If the packet comes from the master if host == self.master: _command = data[:4] if _command == DMRD: _peer_id = data[11:15] elif _command == RPTA: if data[6:10] in self.peerTrack: _peer_id = data[6:10] else: _peer_id = self.connTrack[port] elif _command == MSTN: _peer_id = data[6:10] self.peerTrack[_peer_id]['timer'].cancel() self.reaper(_peer_id) return elif _command == MSTP: _peer_id = data[7:11] elif _command == MSTC: _peer_id = data[5:9] self.peerTrack[_peer_id]['timer'].cancel() self.reaper(_peer_id) return # _peer_id = self.connTrack[port] if self.debug: print(data) if _peer_id and _peer_id in self.peerTrack: self.transport.write(data, (self.peerTrack[_peer_id]['shost'], self.peerTrack[_peer_id]['sport'])) #self.peerTrack[_peer_id]['timer'].reset() return else: _command = data[:4] if _command == DMRD: # DMRData -- encapsulated DMR data frame _peer_id = data[11:15] elif _command == DMRA: # DMRAlias -- Talker Alias information _peer_id = _data[4:8] elif _command == RPTL: # RPTLogin -- a repeater wants to login _peer_id = data[4:8] elif _command == RPTK: # Repeater has answered our login challenge _peer_id = data[4:8] elif _command == RPTC: # Repeater is sending it's configuraiton OR disconnecting if data[:5] == RPTCL: # Disconnect command _peer_id = data[5:9] else: _peer_id = data[4:8] # Configure Command elif _command == RPTO: # options _peer_id = data[4:8] elif _command == RPTP: # RPTPing -- peer is pinging us _peer_id = data[7:11] else: return if _peer_id in self.peerTrack: _dport = self.peerTrack[_peer_id]['dport'] self.peerTrack[_peer_id]['sport'] = port self.peerTrack[_peer_id]['shost'] = host self.transport.write(data, ('127.0.0.1', _dport)) self.peerTrack[_peer_id]['timer'].reset() if self.debug: print(data) return else: if int_id(_peer_id) in self.blackList: return #for _dport in self.connTrack: while True: _dport = random.randint(1, (self.numPorts - 1)) _dport = _dport + self.destPortStart if not self.connTrack[_dport]: break self.connTrack[_dport] = _peer_id self.peerTrack[_peer_id] = {} self.peerTrack[_peer_id]['dport'] = _dport self.peerTrack[_peer_id]['sport'] = port self.peerTrack[_peer_id]['shost'] = host self.peerTrack[_peer_id]['timer'] = ResettableTimer( self.timeout, self.reaper, [_peer_id]) self.peerTrack[_peer_id]['timer'].start() self.transport.write(data, (self.master, _dport)) if self.debug: print(data) return
print('Error moving to slide', x) def timeout_schedule_close(): global driver_for_shown_schedule if driver_for_shown_schedule != None: close_schedule() def timeout_command(): m.listen_input() cp.copy('refresh_command') go_to_x_slide('1') timer_command = ResettableTimer(45, timeout_command) timer_command.start() class NLPController(WebSocket): def __init__(self, *args, **kwargs): WebSocket.__init__(self, *args, **kwargs) _thread.start_new_thread(self.listen, ()) def listen(self): global BUFFER while True: try: if BUFFER: BUFFER = list(OrderedDict.fromkeys(BUFFER)) cmd = BUFFER.pop()
def __init__(self, timeout=15 * 60, pin=17): self.__sensor = HcSr501(pin) self.__timer = ResettableTimer(timeout, screen, [False]) self.__timer.start()
backend_connected = False user_logged_in = False user_name = "" last_object = "" def timeout(): global isObjectPresent timer.cancel() if isObjectPresent: print("object out") isObjectPresent = False db.set_led_state(not isObjectPresent, not backend_connected) timer = ResettableTimer(0.5, timeout) def dummy_callback(param): global isObjectPresent global objCount global backend_connected global last_object timer.reset() if not isObjectPresent: sound.play() timer.start() objCount += 1 m_relay.set_monoflop(1, True, 200) print("object in, count: " + str(objCount)) isObjectPresent = True