def _status_changed(self): self.speed = abs(self.slave_actuator.status - self.status) / self.change_time if self._changethread and self._changethread.is_alive(): return self._changethread = Thread(target=threaded(self.system, self.statuschanger), name="Changethread for " + self.name) self._changethread.start()
def start_ioloop(self): global web_thread ioloop = tornado.ioloop.IOLoop.instance() if not ioloop._running: web_thread = threading.Thread(target=threaded(self.system, ioloop.start), name="%s::%s" % (self.system.name, self.__class__.__name__)) web_thread.start()
def setup(self): tornado_app = tornado.web.Application(self.get_tornado_handlers()) if self.ssl_certificate and self.ssl_private_key: ssl_options = { "certfile": self.ssl_certificate, "keyfile": self.ssl_private_key, } else: ssl_options = None server = tornado.httpserver.HTTPServer(tornado_app, ssl_options=ssl_options) try: server.listen(self.http_port, self.http_ipaddr) except socket.error as e: self.logger.error('Could not start server: %s', e) self._server = tornado.ioloop.IOLoop.instance() if not self._server._running: self._web_thread = threading.Thread(target=threaded(self._server.start), name="%s::%s" % (self.system.name, self.__class__.__name__)) self._web_thread.start() else: self.logger.debug('Tornado IOLoop already running, no need to start new')
def start_ioloop(self): global web_thread ioloop = tornado.ioloop.IOLoop.instance() if not ioloop._running: web_thread = threading.Thread( target=threaded(self.system, ioloop.start), name="%s::%s" % (self.system.name, self.__class__.__name__)) web_thread.start()
def _keep_alive(self): if self.keep_alive: self.logger.debug('Sending keep-alive message to Arduino') self._board.send_sysex(SYSEX_KEEP_ALIVE, [0]) interval = 60 self._keepalive_thread = threading.Timer( interval, threaded(self.system, self._keep_alive)) self._keepalive_thread.name = "Arduino keepalive (60s)" self._keepalive_thread.start()
def enable_input_port(self, port, callback, pull_up_down): pud = {"down": self._hw.PUD_DOWN, "up": self._hw.PUD_UP, "none": self._hw.PUD_OFF} if self.rpio: self._hw.setup(port, self._hw.IN) self._hw.add_interrupt_callback(port, callback, edge="both", pull_up_down=pud[pull_up_down]) else: self._hw.setup(port, self._hw.IN, pull_up_down=pud[pull_up_down]) self._hw.add_event_detect(port, self._hw.BOTH, lambda _port: threaded(self.system, callback, _port, self._hw.input(_port))())
def _setup_next_update(self, next_update_time): now = self._now() if self._update_timer and self._update_timer.is_alive(): self._update_timer.cancel() delay = next_update_time - now + timedelta(seconds=5) self.logger.info('Setting timer to %s, %s seconds, at %s', delay, delay.seconds, now+delay) self._update_timer = threading.Timer(delay.seconds, threaded(self.system, self.update_status,)) self._update_timer.name = ("Timer for TimerSensor %s at %s (%s seconds)" % (self.name, now + delay, delay.seconds)) self._update_timer.start()
def call(self, caller, **kwargs): with self._lock: state = self.get_state(caller) threads = state.get_or_create('threads', []) t = threading.Timer(0., None) t.function = threaded(self.system, self._run, caller, t, **kwargs) t.name = 'Thread for %s' % self t._cancel_while = False threads.append(t) t.start() return True
def _restart(self): if self._stop: return if self._pollthread and self._pollthread.is_alive(): self._pollthread.cancel() if self.poll_active: self.update_status() self._pollthread = threading.Timer(self.interval, threaded(self.system, self._restart)) time_after_interval = datetime.now() + timedelta(seconds=self.interval) self._pollthread.name = "PollingSensor: %s next poll at %s (%.2f sek)" % (self.name, time_after_interval, self.interval) self._pollthread.start()
def _restart(self): if self._stop: return if self._pollthread and self._pollthread.is_alive(): self._pollthread.cancel() if self.poll_active: self.update_status() self._pollthread = threading.Timer( self.interval, threaded(self.system, self._restart)) time_after_interval = datetime.now() + timedelta( seconds=self.interval) self._pollthread.name = "PollingSensor: %s next poll at %s (%.2f sek)" % ( self.name, time_after_interval, self.interval) self._pollthread.start()
def _setup_next_update(self, next_update_time): now = self._now() if self._update_timer and self._update_timer.is_alive(): self._update_timer.cancel() delay = next_update_time - now + timedelta(seconds=5) self.logger.info('Setting timer to %s, %s seconds, at %s', delay, delay.seconds, now + delay) self._update_timer = threading.Timer( delay.seconds, threaded( self.system, self.update_status, )) self._update_timer.name = ( "Timer for TimerSensor %s at %s (%s seconds)" % (self.name, now + delay, delay.seconds)) self._update_timer.start()
def enable_input_port(self, port, callback, pull_up_down): pud = { "down": self._hw.PUD_DOWN, "up": self._hw.PUD_UP, "none": self._hw.PUD_OFF } if self.rpio: self._hw.setup(port, self._hw.IN) self._hw.add_interrupt_callback(port, callback, edge="both", pull_up_down=pud[pull_up_down]) else: self._hw.setup(port, self._hw.IN, pull_up_down=pud[pull_up_down]) self._hw.add_event_detect( port, self._hw.BOTH, lambda _port: threaded( self.system, callback, _port, self._hw.input(_port))())
def call(self, caller, **kwargs): if not caller: return with self._lock: state = self.get_state(caller) timers = state.get_or_create('timers', []) self.logger.info("Scheduling %s", self) delay = self.call_eval(self.delay, caller, **kwargs) timer = threading.Timer(delay, None) timer.function = threaded(self.system, self._run, caller, timer, **kwargs) time_after_delay = datetime.datetime.now() + datetime.timedelta(seconds=delay) timer.name = "Timer for %s timed at %s (%d sek)" % (self, time_after_delay, delay) timer.start() timers.append(timer)
def call(self, caller, **kwargs): if not caller: return with self._lock: state = self.get_state(caller) timers = state.get_or_create('timers', []) self.logger.info("Scheduling %s", self) delay = self.call_eval(self.delay, caller, **kwargs) timer = threading.Timer(delay, None) timer.function = threaded(self.system, self._run, caller, timer, **kwargs) time_after_delay = datetime.datetime.now() + datetime.timedelta( seconds=delay) timer.name = "Timer for %s timed at %s (%d sek)" % ( self, time_after_delay, delay) timer.start() timers.append(timer)
def setup(self): self.logger.debug("Initializing Arduino subsystem") # Initialize configured self.boards try: if not os.access(self.device, os.R_OK): raise self.FileNotReadableError board = pyfirmata.Board(self.device, layout=pyfirmata.BOARDS[self.device_type]) self._board = board self.is_mocked = False except (self.FileNotReadableError, OSError) as e: if isinstance( e, self.FileNotReadableError) or e.errno == os.errno.ENOENT: self.logger.warning( 'Your arduino device %s is not available. Arduino will be mocked.', self.device) self._board = None self.is_mocked = True else: raise self._lock = Lock() if self._board: self._board.add_cmd_handler(pyfirmata.STRING_DATA, self._string_data_handler) self._iterator_thread = it = threading.Thread( target=threaded(self.system, iterate_serial, self._board)) it.daemon = True it.name = "PyFirmata thread for {dev}".format(dev=self.device) it.start() self.reset() self.configure_sample_rate() self.configure_instant_digital_reporting() self.configure_virtualwire() self.configure_lcd() self.configure_analog_reference() self._keep_alive()
def _status_changed(self): if self._changethread and self._changethread.is_alive(): return self._changethread = Thread(target=threaded(self.system, self.statuschanger), name="Changethread for " + self.name) self._changethread.start()