def open_connections(self, host, user, password, db, poolsize=None): # pt: persist timer global conns global cursors global ps_socket curs = [] self.db = db # Default connection pool = 10 if not poolsize: poolsize = 10 for _ in range(poolsize): con = mdb.connect(host, user, password, db) conns.append(con); curs.append(con.cursor(mdb.cursors.DictCursor)) cursors = itertools.cycle(curs) dtd = Timer(5.0, self.__dump_to_database__,[self.persist_timer]) dtd.daemon = True dtd.start() ka = Timer(18000.0, self.keepalive) ka.daemon = True ka.start()
def execute(self): """ Queue all Jobs for execution """ status, created = models.Cron.objects.get_or_create(pk=1) # This is important for 2 reasons: # 1. It keeps us for running more than one instance of the # same job at a time # 2. It reduces the number of polling threads because they # get killed off if they happen to check while another # one is already executing a job (only occurs with # multi-threaded servers) if status.executing: return status.executing = True try: status.save() except: # this will fail if you're debugging, so we want it # to fail silently and start the timer again so we # can pick up where we left off once debugging is done thread = Timer(polling_frequency, self.execute) thread.daemon = True thread.start() return jobs = models.Job.objects.all() for job in jobs: if job.queued: time_delta = timezone.now() - job.last_run if (time_delta.seconds + 86400 * time_delta.days) > job.run_frequency: inst = cPickle.loads(str(job.instance)) args = cPickle.loads(str(job.args)) kwargs = cPickle.loads(str(job.kwargs)) try: inst.run(*args, **kwargs) job.last_run = timezone.now() job.save() except Exception: # if the job throws an error, just remove it from # the queue. That way we can find/fix the error and # requeue the job manually job.queued = False job.save() status.executing = False status.save() # Set up for this function to run again thread = Timer(polling_frequency, self.execute) thread.daemon = True thread.start()
def check_time_server(self): """Sets Timers to repeatedly set offsets (if leader) or check time server.""" ack = self.check_server_activity() if self.am_leader: t = Timer(5, self.set_offset_for_processes) t.daemon = True t.start() else: t = Timer(10, self.check_time_server) t.daemon = True t.start() return ack
def update_prices(self, app): price_timer = Timer( 60.0, self.update_prices, kwargs={'app': app} ) price_timer.name = 'price_timer' price_timer.daemon = True price_timer.start() conn = database.get_db(app) db = conn.cursor() for unit in app.config['units']: self.log.info('fetching price for {}'.format(unit)) streamer_price = self.streamer.get_price(unit) if streamer_price is None: price = self.standard.get_price(unit) self.log.warn('price streamer offline!') else: price = streamer_price db.execute( "INSERT INTO prices (unit, price) VALUES (%s,%s)", ( unit, price ) ) self.log.info('{} price set to {}'.format(unit, price)) conn.commit() conn.close()
def check_drivers_alive(): """Checks all drivers and reloads those that crashed. This method is executed automatically at the given interval. """ if lock.acquire(False): LOG.info('Checks driver threads') for index, driver_thread in enumerate(threads): if not driver_thread.is_alive(): LOG.warning('%s(probe_ids=%s, kwargs=%s) is crashed' % (driver_thread.__class__.__name__, driver_thread.probe_ids, driver_thread.kwargs)) new_thread = load_driver(driver_thread.__class__.__name__, driver_thread.probe_ids, driver_thread.kwargs ) if new_thread is not None: threads[index] = new_thread # Schedule periodic execution of this function if cfg.CONF.check_drivers_interval > 0: timer = Timer(cfg.CONF.check_drivers_interval, check_drivers_alive) timer.daemon = True timer.start() lock.release()
def broadcast_dis(self): """Broadcast a DIS message on all interfaces 1.build_dis 2. broadcast_backet (dis) Called by: rpl_node on init """ logger.debug("checking if a DIS broadcast is required") if self.dodag_cache.is_empty(): logger.debug("broadcasting DIS") # Create RPL_DIO message dismsg = icmpv6_rpl.rpl_dis() # Create Dodag RPL Control message rplmsg = icmpv6_rpl.rpl_control(code=icmpv6_rpl.RPL_DIO, rplctrl=dismsg) # Create ICMPv6 protocol message rpl = icmpv6.icmpv6(type_= icmpv6_rpl.ICMPv6_RPL, code=icmpv6_rpl.RPL_DIS, csum=0, data= rplmsg) self._broadcast_rpl_pkt(rpl) else: logger.debug("no DIS is required") dis_timer = Timer(DEFAULT_INTERVAL_BETWEEN_DIS, broadcast_dis, kwargs= {}) dis_timer.daemon = True dis_timer.start()
def reset_timer(sprout, pool, timeout): global timer if timer: timer.cancel() timer = Timer((timeout / 2) * 60, lambda: ping_pool(sprout, pool, timeout)) timer.daemon = True timer.start()
def scan_for_contests(): # Setup and start the threading v = Timer(60.0 * 10.0, scan_for_contests) v.daemon = True v.start() mutex.acquire() try: out("Scanning For Contests...") last_twitter_id = p.get_last_twitter_id() try: #"RT to win" music OR sound OR speaker OR dj OR mixer OR gear' results = t.api.GetSearch( term='"RT to win"', since_id=last_twitter_id ) for status in results: item = status.AsDict() if 'retweet_count' in item and item['retweet_count'] > 0: if item['id'] > last_twitter_id: p.set_last_twitter_id(item['id']) contests.append(item) except Exception as e: err("[scan_for_contests] Search Error: %s" % e) finally: mutex.release()
def run_defence_planner(): global defence_timer delay = defence_planner.plan_and_act(latest_world) defence_timer.cancel() defence_timer = Timer(delay, run_defence_planner) defence_timer.daemon = True defence_timer.start()
def scan_for_contests(): time = get_fuzzed_time(s['search']['base'], s['search']['fuzz']) minutes, seconds = divmod(time, 60) hours, minutes = divmod(minutes, 60) out("scan_for_contests random time: %sh%sm%ss" % ( int(hours), int(minutes), int(seconds) )) # Setup and start the threading v = Timer(time, scan_for_contests) v.daemon = True v.start() mutex.acquire() try: out("Scanning For Contests...") last_twitter_id = p.get_last_twitter_id() try: #"RT to win" music OR sound OR speaker OR dj OR mixer OR gear' results = t.api.GetSearch( term='"RT to win"', since_id=last_twitter_id ) for status in results: item = status.AsDict() if 'retweet_count' in item and item['retweet_count'] > 0: if item['id'] > last_twitter_id: p.set_last_twitter_id(item['id']) contests.append(item) except Exception as e: err("[scan_for_contests] Search Error: %s" % e) finally: mutex.release()
def update_queue(): time = get_fuzzed_time(s['retweet']['base'], s['retweet']['fuzz']) minutes, seconds = divmod(time, 60) hours, minutes = divmod(minutes, 60) out("update_queue random time: %sh%sm%ss" % ( int(hours), int(minutes), int(seconds) )) # Setup and start the threading u = Timer(time, update_queue) u.daemon = True u.start() mutex.acquire() try: if len(contests) > 0: contest = contests[0] out("Contest: %s" % contest['text']) followed = check_for_follow_request(contest, t, p, s) favourited = check_for_favourite_request(contest, t, p, s) retweet_post(contest, t, p, s, followed, favourited) contests.pop(0) finally: mutex.release()
def _update_time(self): """Update the Aquaero real time clock and then wait for the next turn.""" self.device.set_time() if not self.shutdown: timer = Timer(self.frequency, lambda: self._update_time()) timer.daemon = True timer.start()
def _check_streams(self): if not self.stream_names: return global _timer output = [] try: data = self.retrieve_stream_data(self.stream_names) except: data = dict() for stream, params in data.iteritems(): if not params: self.cache.set(stream, STATUS_OFFLINE) continue try: previous_state = int(self.cache.get(stream)) except (TypeError, ValueError): previous_state = None if previous_state in (None, STATUS_OFFLINE): output.append(MESSAGE_TEMPLATE.format(**params)) self.cache.set(stream, STATUS_ONLINE) if self.whitelist and output: message = "\n".join(output) responses = dispatcher.send(signals.REQUEST_CHATS) chats = responses[0] for chat in chats: if chat_is_whitelisted(chat, self.whitelist): self.output.append(ChatMessage(chat, message)) _timer = Timer(self.check_interval, self._check_streams) _timer.daemon = True _timer.start()
def run(self): with app.app_context(): print("running task {time}".format(time=datetime.datetime.now().time().isoformat())) self.callback(**self.kwargs) t = Timer(self.interval, self.run) t.daemon = self.daemon t.start()
def play(args): """Handles the 'play' command.""" from threading import Timer from lib.configuration import Configuration from lib.task import Task config = utils.parse_config(args, Configuration()) tasks = [] for task, items in config.tasks.items(): t = Timer(items['timing'], Task.run_task, args=(task, len(tasks) + 1, items, config)) t.daemon = True t.start() tasks.append(t) duration = config.duration if duration == 0: for t in tasks: t.join() else: start = time.time() while time.time() < start + duration: finished = True for t in tasks: if not t.finished.is_set(): finished = False break if finished: break time.sleep(1)
def _poll_skill_settings(self): """ If identifier exists for this skill poll to backend to request settings and store it if it changes TODO: implement as websocket Args: hashed_meta (int): the hashed identifier """ try: if not self._complete_intialization: self.initialize_remote_settings() if not self._complete_intialization: return # unable to do remote sync else: original = hash(str(self)) self.update_remote() # Call callback for updated settings if self.changed_callback and hash(str(self)) != original: self.changed_callback() except Exception as e: LOG.error(e) LOG.exception("") # this is used in core so do not delete! if self.is_alive: # continues to poll settings every 60 seconds t = Timer(60, self._poll_skill_settings) t.daemon = True t.start()
def run(self): while self.is_running: try: super(SerializeThread, self).run() except Exception, e: log.error("Exception in thread %s: %s", self.name, e) delay = TIMER_DELAYS[self.name] if delay > MAX_DELAY: num_attempts = int(math.log((delay/10), 2)); log.error("Giving up re-spawning serialization " "worker after %d seconds (%d attempts).", delay, num_attempts ) notify_serialization_failure(None, subject="Notice - Bungeni Serialization Workers", body="""Unable to restart serialization worker. Please check the Bungeni logs.""" ) else: log.info("Attempting to respawn serialization " "consumer in %d seconds", delay ) next_delay = delay * 2 timer = Timer(delay, init_thread, [], {"delay": next_delay }) timer.daemon = True timer.start() self.is_running = False del TIMER_DELAYS[self.name]
def idle(self, bounce_timer = (29 * 60) ): ''' Use this method to initiate IDLE mode. bounce_timer: The IDLE spec suggests stopping and restarting IDLE every 29 minutes in order to avoid timing out. This is the default, and there should be no need to change it. If you find that your connection is timing out anyway however change this value to something lower. Note that this method is blocking and should be run in a thread. ''' name = 'IDLE' if name not in self.capabilities: raise self.Abort('Server does not support IDLE') tag = self._new_tag() data = '%s %s' % (tag, name) self.send('%s%s' % (data, CRLF)) response = self._get_line() if 'accepted, awaiting DONE command' in response or 'idling' in response: self.state = name tmr = Timer(bounce_timer, self._bounce_idle) tmr.daemon = True tmr.start() final_response = self._coreader(tag) tmr.cancel() return final_response else: self.Abort('Failed to initiate IDLE! Got error %s' % response)
def _handle_wake_up(self,socket,network,wake_up_fun): socket.recv() #timeout events are used for pupil sync. #annouce masterhood every interval time: if isinstance(self.time_sync_node,Clock_Sync_Master): network.shouts(self.group, SYNC_TIME_MASTER_ANNOUNCE+"%s"%self.clock_master_worthiness()+msg_delimeter+'%s'%self.time_sync_node.port) elif self._time_grandmaster: self.time_sync_node = Clock_Sync_Master(time_fn=self.get_time) network.shouts(self.group, SYNC_TIME_MASTER_ANNOUNCE+"%s"%self.clock_master_worthiness()+msg_delimeter+'%s'%self.time_sync_node.port) # synced slave: see if we should become master if we dont hear annoncement within time. elif isinstance(self.time_sync_node,Clock_Sync_Follower) and not self.time_sync_node.offset_remains: if self.get_unadjusted_time()-self.last_master_announce > self.time_sync_wait_interval_short: self.time_sync_node.terminate() self.time_sync_node = Clock_Sync_Master(time_fn=self.get_time) network.shouts(self.group, SYNC_TIME_MASTER_ANNOUNCE+"%s"%self.clock_master_worthiness()+msg_delimeter+'%s'%self.time_sync_node.port) # unsynced slave or none should wait longer but eventually take over elif self.get_unadjusted_time()-self.last_master_announce > self.time_sync_wait_interval_long: if self.time_sync_node: self.time_sync_node.terminate() self.time_sync_node = Clock_Sync_Master(time_fn=self.get_time) network.shouts(self.group, SYNC_TIME_MASTER_ANNOUNCE+"%s"%self.clock_master_worthiness()+msg_delimeter+'%s'%self.time_sync_node.port) t = Timer(self.time_sync_announce_interval, wake_up_fun) t.daemon = True t.start()
def is_alive(self, destination, primary=False): with requests.session() as session: while True: response = session.request("GET", "{0}/databases/{1}/replication/topology?check-server-reachable".format( destination["url"], destination["database"]), headers=self.headers) if response.status_code == 412 or response.status_code == 401: try: try: oauth_source = response.headers.__getitem__("OAuth-Source") except KeyError: raise exceptions.InvalidOperationException( "Something is not right please check your server settings") self.do_auth_request(self.api_key, oauth_source) except exceptions.ErrorResponseException: break if response.status_code == 200: if primary: self.primary = True else: self.replication_topology.put(destination) return else: break is_alive_timer = Timer(5, lambda: self.is_alive(destination, primary)) is_alive_timer.daemon = True is_alive_timer.start()
def run_attack_planner(): global attack_timer delay = attack_planner.plan_and_act(latest_world) attack_timer.cancel() attack_timer = Timer(delay, run_attack_planner) attack_timer.daemon = True attack_timer.start()
def scheduleFlight(res, flight, blocking=False, scheduler=None): flight_time = time_module.mktime(flight.legs[0].depart.dt_utc.utctimetuple()) - time_module.timezone seconds_before = config["CHECKIN_WINDOW"] + 24*60*60 # how many seconds before the flight time do we check in flight.sched_time = flight_time - seconds_before flight.sched_time_formatted = DateTimeToString(flight.legs[0].depart.dt_utc.replace(tzinfo=utc) - timedelta(seconds=seconds_before)) flight.seconds = flight.sched_time - time_module.time() # Retrieve timezone and apply it because datetimes are stored as naive (no timezone information) tz = airport_timezone_map[flight.legs[0].depart.airport] flight.sched_time_local_formatted = DateTimeToString(flight.legs[0].depart.dt_utc.replace(tzinfo=utc).astimezone(tz) - timedelta(seconds=seconds_before)) db.Session.commit() dlog("Flight time: %s" % flight.legs[0].depart.dt_formatted) if config["CELERY"]: from tasks import check_in_flight result = check_in_flight.apply_async([res.id, flight.id], countdown=flight.seconds) flight.task_uuid = result.id db.Session.commit() elif not blocking: result = "Scheduling check in for flight at", flight.legs[0].depart.dt_formatted, "(local), ", flight.legs[0].depart.dt_utc_formatted, "(UTC) in", int(flight.seconds/60/60), "hrs", int(flight.seconds/60%60), "mins from now..." t = Timer(flight.seconds, TryCheckinFlight, (res.id, flight.id, None, 1)) t.daemon = True t.start() # DEBUG # if flight == res.flights[0]: # Timer(5, TryCheckinFlight, (res, flight, None, 1)).start() else: scheduler.enterabs(flight.sched_time, 1, TryCheckinFlight, (res.id, flight.id, scheduler, 1)) result = "Scheduling check in for flight at", flight.legs[0].depart.dt_formatted, "(local), ", flight.legs[0].depart.dt_utc_formatted, "(UTC)" print result dlog('Checkin scheduled at (UTC): %s' % flight.sched_time_formatted) dlog('Checkin scheduled at (local): %s' % flight.sched_time_local_formatted) dlog('Flights scheduled. Waiting...') return result
def print_every_min(): try: timer = Timer(60,print_every_min) timer.daemon = True timer.start() threads.append(timer) global total_links global domains global unique_words ti_now = datetime.now() for entry in dict.keys(): no_of_times = 0 index_to_del = [] for i in xrange(0,len(dict[entry])): diff = divmod((ti_now - (dict[entry][i][0])).total_seconds(),60) td = (diff[0]*60)+ diff[1] #print td if td <= 300: no_of_times += 1 total_links += len(dict[entry][i][1]) for u in dict[entry][i][1]: domains.append(u) for w in dict[entry][i][2]: unique_words.append(w) # make a list of indeces to delete from user entry else: index_to_del.append(i) # modify the user entry to hold the values only inside 5 minutes dict[entry][:] = [dict[entry][i] for i in xrange(0,len(dict[entry])) if i not in index_to_del] # if no tweets from a user in last 5 miutes delete the entry if no_of_times == 0: del dict[entry] else: print entry , no_of_times print "\n" print "total number of links posted by the same users in the last 5 minutes is : {0}".format(total_links) print "\n" print "Top 10 domains are : {0}".format([str(name) for name,frequency in Counter(domains).most_common(10)]) print "\n" print "Total number of unique words in the last 5 minutes is : {0} ".format(len(set(unique_words))) print "\n" print "Top 10 most common words are : {0}".format([str(word) for word,frequency in Counter(unique_words).most_common(10)]) print "\nNext data will be displayed in a minute\n" total_links = 0 domains = [] unique_words = [] except TypeError: print " TypeError occurred"
def cleaner_thread(): # Call itself again after the interval cleaner = Timer(config["CLEAN_INTERVAL"], cleaner_thread) cleaner.daemon = True # Daemons will attempt to exit cleanly along with the main process, which we want cleaner.start() # Actual function delete_old()
def _generate_cleanup_thread(self, start=True): cleanup_thread = Timer(CLEANUP_INTERVAL, self.cleanup) cleanup_thread.daemon = True if start: cleanup_thread.start() return cleanup_thread
def update(): try: x = Timer(900.0, update) x.daemon=True x.start() feed_refresh() except (KeyboardInterrupt, SystemExit): x.cancel()
def updateThread (): global updating while updating: time.sleep(0.2) updateTime() thread = Timer(0.2,updateThread) thread.daemon = True thread.start()
def loop_assume_role(role_arn, timeout=DEFAULT_TIMER_LOOP_SECONDS): # Do an initial assume role assume_role(role_arn, True) # Create timer to loop through function t = Timer(timeout, loop_assume_role, args=[role_arn, timeout]) t.daemon = True t.start()
def Lock(self): if not self.Locked: if self.Time > 0: self.Locked = True t = Timer(self.Time, self.Unlock, ()) t.daemon = True t.start() return self.Locked
def stat(): global count, manager eps = count / 20 count = 0 print 'eps:%d' % eps t = Timer(20, stat) t.daemon = True t.start()
def _app_deploy(client, resource_group, service, app, name, version, path, runtime_version, jvm_options, cpu, memory, instance_count, env, target_module=None, no_wait=False, file_type="Jar", update=False): upload_url = None relative_path = None logger.warning("[1/3] Requesting for upload URL") try: response = client.apps.get_resource_upload_url(resource_group, service, app, None, None) upload_url = response.upload_url relative_path = response.relative_path except (AttributeError, CloudError) as e: raise CLIError( "Failed to get a SAS URL to upload context. Error: {}".format(e.message)) if not upload_url: raise CLIError("Failed to get a SAS URL to upload context.") prase_result = parse.urlparse(upload_url) storage_name = prase_result.netloc.split('.')[0] split_path = prase_result.path.split('/')[1:3] share_name = split_path[0] sas_token = "?" + prase_result.query deployment_settings = models.DeploymentSettings( cpu=cpu, memory_in_gb=memory, environment_variables=env, jvm_options=jvm_options, runtime_version=runtime_version, instance_count=instance_count,) user_source_info = models.UserSourceInfo( version=version, relative_path=relative_path, type=file_type, artifact_selector=target_module) properties = models.DeploymentResourceProperties( deployment_settings=deployment_settings, source=user_source_info) # upload file logger.warning("[2/3] Uploading package to blob") file_service = FileService(storage_name, sas_token=sas_token) file_service.create_file_from_path(share_name, None, relative_path, path) if file_type == "Source" and not no_wait: def get_log_url(): try: log_file_url_response = client.deployments.get_log_file_url( resource_group_name=resource_group, service_name=service, app_name=app, deployment_name=name) if not log_file_url_response: return None return log_file_url_response.url except CloudError: return None def get_logs_loop(): log_url = None while not log_url or log_url == old_log_url: log_url = get_log_url() sleep(10) logger.info("Trying to fetch build logs") stream_logs(client.deployments, resource_group, service, app, name, logger_level_func=logger.info) old_log_url = get_log_url() timer = Timer(3, get_logs_loop) timer.daemon = True timer.start() # create deployment logger.warning( "[3/3] Updating deployment in app '{}' (this operation can take a while to complete)".format(app)) if update: return sdk_no_wait(no_wait, client.deployments.update, resource_group, service, app, name, properties) return sdk_no_wait(no_wait, client.deployments.create_or_update, resource_group, service, app, name, properties)
def check_connection(): """ Check for network connection. If not paired trigger pairing. Runs as a Timer every second until connection is detected. """ if connected(): enclosure = EnclosureAPI(ws) if is_paired(): # Skip the sync message when unpaired because the prompt to go to # home.mycrof.ai will be displayed by the pairing skill enclosure.mouth_text(mycroft.dialog.get("message_synching.clock")) # Force a sync of the local clock with the internet config = Configuration.get() platform = config['enclosure'].get("platform", "unknown") if platform in ['mycroft_mark_1', 'picroft']: ws.emit(Message("system.ntp.sync")) time.sleep(15) # TODO: Generate/listen for a message response... # Check if the time skewed significantly. If so, reboot skew = abs((monotonic.monotonic() - start_ticks) - (time.time() - start_clock)) if skew > 60 * 60: # Time moved by over an hour in the NTP sync. Force a reboot to # prevent weird things from occcurring due to the 'time warp'. # ws.emit( Message( "speak", {'utterance': mycroft.dialog.get("time.changed.reboot")})) wait_while_speaking() # provide visual indicators of the reboot enclosure.mouth_text(mycroft.dialog.get("message_rebooting")) enclosure.eyes_color(70, 65, 69) # soft gray enclosure.eyes_spin() # give the system time to finish processing enclosure messages time.sleep(1.0) # reboot ws.emit(Message("system.reboot")) return else: ws.emit(Message("enclosure.mouth.reset")) time.sleep(0.5) ws.emit(Message('mycroft.internet.connected')) # check for pairing, if not automatically start pairing if not is_paired(): # begin the process payload = {'utterances': ["pair my device"], 'lang': "en-us"} ws.emit(Message("recognizer_loop:utterance", payload)) else: from mycroft.api import DeviceApi api = DeviceApi() api.update_version() else: thread = Timer(1, check_connection) thread.daemon = True thread.start()
def _job_popen( commands: List[str], stdin_path: Optional[str], stdout_path: Optional[str], stderr_path: Optional[str], env: MutableMapping[str, str], cwd: str, job_dir: str, job_script_contents: Optional[str] = None, timelimit: Optional[int] = None, name: Optional[str] = None, monitor_function=None, # type: Optional[Callable[[subprocess.Popen[str]], None]] ) -> int: if job_script_contents is None and not FORCE_SHELLED_POPEN: stdin = subprocess.PIPE # type: Union[IO[Any], int] if stdin_path is not None: stdin = open(stdin_path, "rb") stdout = sys.stderr # type: IO[Any] if stdout_path is not None: stdout = open(stdout_path, "wb") stderr = sys.stderr # type: IO[Any] if stderr_path is not None: stderr = open(stderr_path, "wb") sproc = subprocess.Popen( commands, shell=False, # nosec close_fds=not onWindows(), stdin=stdin, stdout=stdout, stderr=stderr, env=env, cwd=cwd, universal_newlines=True, ) processes_to_kill.append(sproc) if sproc.stdin is not None: sproc.stdin.close() tm = None if timelimit is not None and timelimit > 0: def terminate(): # type: () -> None try: _logger.warning( "[job %s] exceeded time limit of %d seconds and will" "be terminated", name, timelimit, ) sproc.terminate() except OSError: pass tm = Timer(timelimit, terminate) tm.daemon = True tm.start() if monitor_function: monitor_function(sproc) rcode = sproc.wait() if tm is not None: tm.cancel() if isinstance(stdin, IOBase): stdin.close() if stdout is not sys.stderr: stdout.close() if stderr is not sys.stderr: stderr.close() return rcode else: if job_script_contents is None: job_script_contents = SHELL_COMMAND_TEMPLATE env_copy = {} key = None # type: Any for key in env: env_copy[key] = env[key] job_description = { "commands": commands, "cwd": cwd, "env": env_copy, "stdout_path": stdout_path, "stderr_path": stderr_path, "stdin_path": stdin_path, } with open(os.path.join(job_dir, "job.json"), mode="w", encoding="utf-8") as job_file: json_dump(job_description, job_file, ensure_ascii=False) try: job_script = os.path.join(job_dir, "run_job.bash") with open(job_script, "wb") as _: _.write(job_script_contents.encode("utf-8")) job_run = os.path.join(job_dir, "run_job.py") with open(job_run, "wb") as _: _.write(PYTHON_RUN_SCRIPT.encode("utf-8")) sproc = subprocess.Popen( # nosec ["bash", job_script.encode("utf-8")], shell=False, # nosec cwd=job_dir, # The nested script will output the paths to the correct files if they need # to be captured. Else just write everything to stderr (same as above). stdout=sys.stderr, stderr=sys.stderr, stdin=subprocess.PIPE, universal_newlines=True, ) processes_to_kill.append(sproc) if sproc.stdin is not None: sproc.stdin.close() rcode = sproc.wait() return rcode finally: shutil.rmtree(job_dir)
def check_connection(): """ Check for network connection. If not paired trigger pairing. Runs as a Timer every second until connection is detected. """ if connected(): enclosure = EnclosureAPI(bus) if is_paired(): # Skip the sync message when unpaired because the prompt to go to # home.mycrof.ai will be displayed by the pairing skill enclosure.mouth_text(dialog.get("message_synching.clock")) # Force a sync of the local clock with the internet config = Configuration.get() platform = config['enclosure'].get("platform", "unknown") if platform in ['mycroft_mark_1', 'picroft', 'mycroft_mark_2pi']: bus.wait_for_response(Message('system.ntp.sync'), 'system.ntp.sync.complete', 15) if not is_paired(): try_update_system(platform) # Check if the time skewed significantly. If so, reboot skew = abs((time.monotonic() - start_ticks) - (time.time() - start_clock)) if skew > 60 * 60: # Time moved by over an hour in the NTP sync. Force a reboot to # prevent weird things from occcurring due to the 'time warp'. # data = {'utterance': dialog.get("time.changed.reboot")} bus.emit(Message("speak", data)) wait_while_speaking() # provide visual indicators of the reboot enclosure.mouth_text(dialog.get("message_rebooting")) enclosure.eyes_color(70, 65, 69) # soft gray enclosure.eyes_spin() # give the system time to finish processing enclosure messages time.sleep(1.0) # reboot bus.emit(Message("system.reboot")) return else: bus.emit(Message("enclosure.mouth.reset")) time.sleep(0.5) enclosure.eyes_color(189, 183, 107) # dark khaki enclosure.mouth_text(dialog.get("message_loading.skills")) bus.emit(Message('mycroft.internet.connected')) # check for pairing, if not automatically start pairing try: if not is_paired(ignore_errors=False): payload = {'utterances': ["pair my device"], 'lang': "en-us"} bus.emit(Message("recognizer_loop:utterance", payload)) else: api = DeviceApi() api.update_version() except BackendDown: data = {'utterance': dialog.get("backend.down")} bus.emit(Message("speak", data)) bus.emit(Message("backend.down")) else: thread = Timer(1, check_connection) thread.daemon = True thread.start()
def woofer_timers(self): # Check if overlay is connected if self.overlay.active < 1: timer = Timer(30, self.woofer_timers) timer.daemon = True timer.start() return # Check if timer is enabled min_lines_timer = "" for action in self.settings.ScheduledMessages: if not action["Enabled"]: continue current_epoch = int(time()) if (current_epoch - self.settings.scheduleTable[action["Name"]] ) >= (action["Timer"] * 60): # Timers without MinLines limits if action["MinLines"] == 0: self.settings.scheduleTable[action["Name"]] = current_epoch if "Command" in action: self.woofer_commands({ "command": action["Command"], "broadcaster": 1, "sender": self.settings.TwitchChannel.lower(), "display-name": self.settings.TwitchChannel, "custom-tag": "command" }) else: self.woofer_addtoqueue({ "message": SystemRandom().choice( self.settings.Messages[action["Name"]]), "image": "{}{}images{}{}".format(self.settings.pathRoot, self.settings.slash, self.settings.slash, action["Image"]), "sender": "", "customtag": "ScheduledMessage", "id": action["Name"] }) # Check if timer with MinLines limits is executable elif action["MinLines"] > 0: if self.settings.scheduleLines < action["MinLines"]: continue if min_lines_timer == "" or \ self.settings.scheduleTable[action["Name"]] < self.settings.scheduleTable[min_lines_timer]: min_lines_timer = action["Name"] # Timers with MinLines limits if min_lines_timer != "": for action in self.settings.ScheduledMessages: if action["Name"] != min_lines_timer: continue self.settings.scheduleLines = 0 self.settings.scheduleTable[action["Name"]] = int(time()) if "Command" in action: self.woofer_commands({ "command": action["Command"], "broadcaster": 1, "sender": self.settings.TwitchChannel.lower(), "display-name": self.settings.TwitchChannel, "custom-tag": "command" }) else: self.woofer_addtoqueue({ "message": SystemRandom().choice( self.settings.Messages[action["Name"]]), "image": "{}{}images{}{}".format(self.settings.pathRoot, self.settings.slash, self.settings.slash, action["Image"]), "sender": "", "customtag": "ScheduledMessage", "id": action["Name"] }) # Reset to default after X seconds timer = Timer(30, self.woofer_timers) timer.daemon = True timer.start()
def woofer_alert(self, json_data): custom_id = json_data["custom-tag"] if not self.settings.Enabled[custom_id]: return json_feed = {"sender": json_data["display-name"]} # # sub/resub # if custom_id in ("sub", "resub"): for customObj in self.settings.CustomSubs: if customObj["Tier"] == "" and \ int(customObj["From"]) <= int(json_data["months"]) <= int(customObj["To"]): custom_id = customObj["Name"] sub_tier = "" if json_data["sub_tier"] == "Tier 1": sub_tier = "1" if json_data["sub_tier"] == "Tier 2": sub_tier = "2" if json_data["sub_tier"] == "Tier 3": sub_tier = "3" if json_data["sub_tier"] == "Prime": sub_tier = "prime" for customObj in self.settings.CustomSubs: if sub_tier == customObj["Tier"] and \ int(customObj["From"]) <= int(json_data["months"]) <= int(customObj["To"]): custom_id = customObj["Name"] json_feed["months"] = json_data["months"] json_feed["months_streak"] = json_data["months_streak"] json_feed["sub_tier"] = json_data["sub_tier"] # # subgift/anonsubgift # if custom_id in ("subgift", "anonsubgift"): if json_data["custom-tag"] == "anonsubgift": json_data["display-name"] = "anonymous" json_feed["recipient"] = json_data[ "msg-param-recipient-display-name"] json_feed["sub_tier"] = json_data["sub_tier"] # # bits # if custom_id == "bits": for customObj in self.settings.CustomBits: if int(customObj["From"]) <= int(json_data["bits"]) <= int( customObj["To"]): custom_id = customObj["Name"] json_feed["bits"] = json_data["bits"] # # host/raid # if custom_id in ("host", "raid"): # Check if user has already raided/hosted s = set(self.hostingUsers) if json_data["sender"] in s: return self.hostingUsers.append(json_data["sender"]) if custom_id == "host": json_feed["sender"] = json_data["sender"] if custom_id == "raid": json_feed["viewers"] = json_data["viewers"] # # Send data # json_feed["id"] = custom_id self.woofer_addtoqueue(json_feed) # Trigger autoshoutout if enabled if custom_id in ("host", "raid") and self.settings.AutoShoutout: json_data["subscriber"] = "1" json_data["vip"] = "1" json_data["moderator"] = "1" json_data["broadcaster"] = "1" json_data["command_parameter"] = json_data["display-name"] json_data["custom-tag"] = "shoutout" timer = Timer(self.settings.AutoShoutoutTime, self.woofer_shoutout, args=[json_data]) timer.daemon = True timer.start()
def start(): global snake_previous global change global from_up global from_left snake["back"] = copy.deepcopy(snake["front"]) front = [snake["front"][1], snake["front"][2]] if key == "d": if change: if front[1] == 2: snake["front"][1] = (front[0] + 1) % 4 + add_data[front[0]] if from_up: snake["front"][2] = (front[1] + 1) else: snake["front"][2] = (front[1] - 1) if front[1] == 0: if from_up: snake["front"][2] = (front[1] + 3) else: snake["front"][2] = (front[1] + 1) from_left = True change = False else: snake["front"][1] = (front[0] + 1) % 4 + add_data[front[0]] if key == "a": if change: if front[1] == 0: snake["front"][1] = (front[0] - 1) % 4 + add_data[front[0]] if from_up: snake["front"][2] = (front[1] + 3) else: snake["front"][2] = (front[1] + 1) if front[1] == 2: if from_up: snake["front"][2] = (front[1] - 1) else: snake["front"][2] = (front[1] + 1) from_left = False change = False else: snake["front"][1] = (front[0] - 1) % 4 + add_data[front[0]] if key == "w": if change: if front[1] == 1: snake["front"][1] = (front[0] - 4) % 24 if from_left: snake["front"][2] = (front[1] + 1) else: snake["front"][2] = (front[1] - 1) if front[1] == 3: if from_left: snake["front"][2] = (front[1] - 1) else: snake["front"][2] = (front[1] - 3) from_up = True change = False else: snake["front"][1] = (front[0] - 4) % 24 if key == "s": if change: if front[1] == 3: snake["front"][1] = (front[0] + 4) % 24 if from_left: snake["front"][2] = (front[1] - 1) else: snake["front"][2] = (front[1] - 3) if front[1] == 1: if from_left: snake["front"][2] = (front[1] + 1) else: snake["front"][2] = (front[1] - 1) from_up = False change = False else: snake["front"][1] = (front[0] + 4) % 24 print(snake_previous) print(snake) print(key) snake_previous = copy.deepcopy(snake) snake_previous["front"][3] = 0 snake_previous["back"][3] = 0 __clock = Timer(2, start) __clock.daemon = True __clock.start()
def woofer_queue(self, queue_id, json_data): # # Check if there is somethign in queue # if not self.queue: return # # Check if overlay is connected # if self.overlay.active < 1: print("waiting for overlay") timer = Timer(3, self.woofer_queue, args=(queue_id, json_data)) timer.daemon = True timer.start() return # # Check if our turn in queue # if self.queue[0] != queue_id: timer = Timer(0.5, self.woofer_queue, args=(queue_id, json_data)) timer.daemon = True timer.start() return # # Send to overlay, retry later if overlay buffer is full # if self.overlay.send("EVENT_WOOFERBOT", json_data) == 1: timer = Timer(1, self.woofer_queue, args=(queue_id, json_data)) timer.daemon = True timer.start() return # # Execute custom scripts # if "script" in json_data and json_data["script"] != "": system("\"{}\"".format(json_data["script"])) # # Execute hotkey # if "hotkey" in json_data and json_data["hotkey"] != "": for key in json_data["hotkey"]: if key in KEYLIST: try: self.keyboard.press(KEYLIST[key]) except: print("Invalid hotkey in {}".format(json_data["id"])) else: try: self.keyboard.press(key) except: print("Invalid hotkey in {}".format(json_data["id"])) sleep(0.05) for key in reversed(json_data["hotkey"]): if key in KEYLIST: try: self.keyboard.release(KEYLIST[key]) except: print("Invalid hotkey in {}".format(json_data["id"])) else: try: self.keyboard.release(key) except: print("Invalid hotkey in {}".format(json_data["id"])) # # Turn on Nanoleaf # if "nanoleaf" in json_data and json_data["nanoleaf"] != "": self.nanoleaf.scene(json_data["nanoleaf"]) if "nanoleafpersistent" in json_data and json_data[ "nanoleafpersistent"]: self.changedLightsNanoleaf = json_data["nanoleaf"] # # Turn on Hue # if "hue" in json_data: for device in json_data["hue"]: pose_light = json_data["hue"][device] if "Brightness" in pose_light and \ pose_light["Brightness"] >= 1 and \ "Color" in pose_light and \ 6 <= len(pose_light["Color"]) <= 7: self.hue.state(device=device, bri=pose_light["Brightness"], col=pose_light["Color"]) if "huepersistent" in json_data and json_data["huepersistent"]: self.changedLightsHue = json_data["hue"] # # Turn on Yeelight # if "yeelight" in json_data: for device in json_data["yeelight"]: pose_light = json_data["yeelight"][device] if "Brightness" in pose_light and \ pose_light["Brightness"] >= 1 and \ "Color" in pose_light and \ 6 <= len(pose_light["Color"]) <= 7: self.yeelight.state( device=device, brightness=pose_light["Brightness"], color=pose_light["Color"], transition=pose_light["Transition"], transitionTime=pose_light["TransitionTime"]) if "yeelightpersistent" in json_data and json_data[ "yeelightpersistent"]: self.changedLightsYeelight = json_data["yeelight"] # # Reset to default after X seconds # timer = Timer(json_data["time"] / 1000, self.woofer_queue_default, args=(queue_id, json_data)) timer.daemon = True timer.start()
def process_preprepare(self, req, fd): _logger.info("Node: [%s], Phase: [PRE-PREPARE], Event FD: [%s]" % (self._id, fd)) if req.inner.seq in self.node_message_log["PRPR"]: return None # the msg field for a preprepare should be the digest of the original client request # TODO: make it clearer that req.outer stands for the message that the replica in subject # is going down.. if req.outer != b'': # req.outer should be same format as req.inner (bytes) try: # import pdb; pdb.set_trace() client_req = request_pb2.Request() # client_req.ParseFromString(req.outer) client_req.ParseFromString(req) record_pbft(self.debuglog, client_req) client_key = get_asymm_key(client_req.inner.id, ktype="sign") if not bool(client_key): _logger.error( "Node: [%s], ErrorMsg => {get_asymm_key(): -> returned empty key}" % (self._id)) return client_req = message.check(client_key, client_req) if client_req == None or req.inner.msg != client_req.dig: _logger.warn("FAILED PRPR OUTER SIGCHECK") return except: _logger.error("ERROR IN PRPR OUTER PROTOBUFF") raise # return # TODO: remove replica from replica map as it has by this time probably gone down or is corrupt. else: client_req = None if req.inner.msg not in self.active: # self.active[req.inner.msg] = (client_req, Timer(self.timeout, self.handle_timeout), fd) request_timer = Timer(self.timeout, self.handle_timeout, [req.inner.msg, req.inner.view]) request_timer.daemon = True request_timer.start() self.active[req.inner.msg] = (client_req, request_timer, fd) self.add_node_history(req) m = self.create_request("PREP", req.inner.seq, req.inner.msg) self.add_node_history(m) record_pbft(self.debuglog, m) self.inc_prep_dict(req.inner.msg) self.broadcast_to_nodes(m) if self.check_prepared_margin(req.inner.msg, req.inner.seq): record(self.debuglog, "PREPARED sequence number " + str(req.inner.seq)) m = self.create_request("COMM", req.inner.seq, req.inner.msg) self.broadcast_to_nodes(m) self.add_node_history(m) self.inc_comm_dict(m.inner.msg) record_pbft(self.debuglog, m) self.prepared[req.inner.seq] = req.inner.msg if self.check_committed_margin(m.inner.msg, m): record(self.debuglog, "COMMITTED sequence number " + str(m.inner.seq)) record_pbft(self.commitlog, m) self.execute_in_order(m)
def __init__(self): thread = Timer(0.68, _sync_dir) thread.daemon = True thread.start() self.thread = thread
def __startMonitoring(self, delay=0): thread = Timer(delay, self.__waitForActivation) thread.daemon = True thread.start()
timeout_scan.start() # Default master address master_address = '5d:36:ac:90:0b:22' access_address = 0x9a328370 # Open serial port of NRF52 Dongle driver = NRF52Dongle(serial_port, '115200') # Send scan request scan_req = BTLE() / BTLE_ADV(RxAdd=slave_addr_type) / BTLE_SCAN_REQ( ScanA=master_address, AdvA=advertiser_address) send(scan_req) # Start the scan timeout to resend packets timeout_scan = Timer(5, scan_timeout) timeout_scan.daemon = True timeout_scan.start() timeout = Timer(5.0, crash_timeout) timeout.daemon = True timeout.start() c = False print(Fore.YELLOW + 'Waiting advertisements from ' + advertiser_address) while True: pkt = None # Receive packet from the NRF52 Dongle data = driver.raw_receive() if data: # Decode Bluetooth Low Energy Data pkt = BTLE(data) # if packet is incorrectly decoded, you may not be using the dongle
def set_timer(): t = Timer(1.0, unset) t.name = "UnsetScrollingTimer" t.daemon = True return t
def refresh_wrapper(self): self.refresh() t = Timer(60, self.refresh_wrapper) t.daemon = True t.start()
subprocess.Popen(["xdotool", "click", "3"], stdout=subprocess.PIPE) elif right_click_method == "pymouse": mouse.click(mouse.position()[0], mouse.position()[1], 2) def dummy(time): pass for event in device.read_loop(): # print(evdev.util.categorize(event)) if event.code == 330 and event.value == 1: touch_time = Decimal(str(event.sec) + "." + str(event.usec)) # print touch_time t = Timer(minimum_long_press_time, dummy, [touch_time]) t.daemon = True t.start() if event.code == 330 and event.value == 0: lift_time = Decimal(str(event.sec) + "." + str(event.usec)) # print lift_time else: lift_time = None if event.code == 47 and event.value == 0: finger0_time = Decimal(str(event.sec) + "." + str(event.usec)) # print finger0_time if event.code == 47 and event.value == 1: finger1_time = Decimal(str(event.sec) + "." + str(event.usec)) # print finger1_time
def run_after_async(seconds, func, *args, **kwargs): """Run the function after seconds asynchronously.""" t = Timer(seconds, func, args, kwargs) t.daemon = True t.start() return t
def _start_timer(self): timer = Timer(11, self._reboot, ()) timer.daemon = True timer.start()
def thread_waitingTimerCalculation(self): th_timer = Timer(0, self.waitingTimeCalculation) th_timer.daemon = True th_timer.start()
def _job_popen( commands, # type: List[Text] stdin_path, # type: Optional[Text] stdout_path, # type: Optional[Text] stderr_path, # type: Optional[Text] env, # type: MutableMapping[AnyStr, AnyStr] cwd, # type: Text job_dir, # type: Text job_script_contents=None, # type: Text timelimit=None, # type: int name=None # type: Text ): # type: (...) -> int if not job_script_contents and not FORCE_SHELLED_POPEN: stdin = subprocess.PIPE # type: Union[IO[Any], int] if stdin_path is not None: stdin = open(stdin_path, "rb") stdout = sys.stderr # type: IO[Any] if stdout_path is not None: stdout = open(stdout_path, "wb") stderr = sys.stderr # type: IO[Any] if stderr_path is not None: stderr = open(stderr_path, "wb") sproc = subprocess.Popen(commands, shell=False, close_fds=not onWindows(), stdin=stdin, stdout=stdout, stderr=stderr, env=env, cwd=cwd) processes_to_kill.append(sproc) if sproc.stdin: sproc.stdin.close() tm = None if timelimit: def terminate(): try: _logger.warn( u"[job %s] exceeded time limit of %d seconds and will be terminated", name, timelimit) sproc.terminate() except OSError: pass tm = Timer(timelimit, terminate) tm.daemon = True tm.start() rcode = sproc.wait() if tm: tm.cancel() if isinstance(stdin, IOBase): stdin.close() if stdout is not sys.stderr: stdout.close() if stderr is not sys.stderr: stderr.close() return rcode else: if not job_script_contents: job_script_contents = SHELL_COMMAND_TEMPLATE env_copy = {} key = None # type: Any for key in env: env_copy[key] = env[key] job_description = dict( commands=commands, cwd=cwd, env=env_copy, stdout_path=stdout_path, stderr_path=stderr_path, stdin_path=stdin_path, ) with open(os.path.join(job_dir, "job.json"), encoding='utf-8', mode="wb") as job_file: json_dump(job_description, job_file, ensure_ascii=False) try: job_script = os.path.join(job_dir, "run_job.bash") with open(job_script, "wb") as _: _.write(job_script_contents.encode('utf-8')) job_run = os.path.join(job_dir, "run_job.py") with open(job_run, "wb") as _: _.write(PYTHON_RUN_SCRIPT.encode('utf-8')) sproc = subprocess.Popen( ["bash", job_script.encode("utf-8")], shell=False, cwd=job_dir, # The nested script will output the paths to the correct files if they need # to be captured. Else just write everything to stderr (same as above). stdout=sys.stderr, stderr=sys.stderr, stdin=subprocess.PIPE, ) processes_to_kill.append(sproc) if sproc.stdin: sproc.stdin.close() rcode = sproc.wait() return rcode finally: shutil.rmtree(job_dir)
def auto_wifi_task(): if secret_key is not None: configWifiLogin(secret_key) t = Timer(10, auto_wifi_task) t.daemon = True t.start()
def threadTimer(self): th_training_time = Timer(0, self.trainingTime) th_training_time.daemon = True th_training_time.start()
def check_later(self, method: str, exc: CouchbaseError): timer = Timer(self.QUIET_PERIOD, self.maybe_warn, args=[method, exc]) timer.daemon = True timer.start()
def create_timer(self): t = Timer(self.interval, self.func, self.args) t.daemon = True return t
def _start_timer(self): timer = Timer(11, self._finish, ()) timer.daemon = True timer.start()
def hammett_tests_pass(config, callback): # noinspection PyUnresolvedReferences from hammett import main_cli modules_before = set(sys.modules.keys()) # set up timeout import _thread from threading import ( Timer, current_thread, main_thread, ) timed_out = False def timeout(): _thread.interrupt_main() nonlocal timed_out timed_out = True assert current_thread() is main_thread() timer = Timer(config.baseline_time_elapsed * 10, timeout) timer.daemon = True timer.start() # Run tests try: class StdOutRedirect(TextIOBase): def write(self, s): callback(s) return len(s) redirect = StdOutRedirect() sys.stdout = redirect sys.stderr = redirect returncode = main_cli( shlex.split(config.test_command[len(hammett_prefix):])) sys.stdout = sys.__stdout__ sys.stderr = sys.__stderr__ timer.cancel() except KeyboardInterrupt: timer.cancel() if timed_out: raise TimeoutError('In process tests timed out') raise modules_to_force_unload = { x.partition(os.sep)[0].replace('.py', '') for x in config.paths_to_mutate } for module_name in list( sorted(set(sys.modules.keys()) - set(modules_before), reverse=True)): if any(module_name.startswith(x) for x in modules_to_force_unload) or module_name.startswith( 'tests') or module_name.startswith('django'): del sys.modules[module_name] return returncode == 0
def sleep_and_set_stop_signal_task(stop_signal, wait_seconds): """Waits for wait_seconds seconds before setting stop_signal.""" timer = Timer(wait_seconds, stop_signal.set) timer.daemon = True timer.start()
variant.get("conditional-configure-args", {}).get(variant_platform, ""), CONFIGURE_ARGS, ) # Timeouts. ACTIVE_PROCESSES = set() def killall(): for proc in ACTIVE_PROCESSES: proc.kill() ACTIVE_PROCESSES.clear() timer = Timer(args.timeout, killall) timer.daemon = True timer.start() ensure_dir_exists(OBJDIR, clobber=not args.dep and not args.nobuild) ensure_dir_exists(OUTDIR, clobber=not args.keep) # Any jobs that wish to produce additional output can save them into the upload # directory if there is such a thing, falling back to OBJDIR. env.setdefault("MOZ_UPLOAD_DIR", OBJDIR) ensure_dir_exists(env["MOZ_UPLOAD_DIR"], clobber=False, creation_marker_filename=None) info("MOZ_UPLOAD_DIR = {}".format(env["MOZ_UPLOAD_DIR"])) def run_command(command, check=False, **kwargs):
def staging_timer_for_task(self, timeout_value, driver, task_id): timer = Timer(timeout_value, lambda: self.log_and_kill(driver, task_id)) timer.daemon = True return timer
def woofer_queue_default(self, queue_id, old_json_data): # # Set default Idle image # mascot_idle_image = self.settings.mascotImages["Idle"]["Image"] if not path.isfile(mascot_idle_image): mascot_idle_image = "" # # Check mapping for custom Idle image # if "Idle" in self.settings.PoseMapping and \ "Image" in self.settings.PoseMapping["Idle"] and \ self.settings.PoseMapping["Idle"]["Image"] in self.settings.mascotImages: tmp = self.settings.mascotImages[self.settings.PoseMapping["Idle"] ["Image"]]["Image"] if path.isfile(tmp): mascot_idle_image = tmp # # Send to overlay, retry later if overlay buffer is full # json_data = {"mascot": mascot_idle_image} if self.overlay.send("EVENT_WOOFERBOT", json_data) == 1: timer = Timer(1, self.woofer_queue_default, args=(queue_id, old_json_data)) timer.daemon = True timer.start() return # # Reset Nanoleaf to Idle # if "nanoleaf" in old_json_data and old_json_data["nanoleaf"]: # Reset to persistent lights if self.changedLightsNanoleaf: self.nanoleaf.scene(self.changedLightsNanoleaf) # Reset to Idle lights elif "Idle" in self.settings.PoseMapping and "Nanoleaf" in self.settings.PoseMapping[ "Idle"]: self.nanoleaf.scene( self.settings.PoseMapping["Idle"]["Nanoleaf"]) # Turn off lights else: self.nanoleaf.scene() # # Reset Hue to Idle # if "hue" in old_json_data: # Reset to persistent lights if self.changedLightsHue: for device in self.changedLightsHue: pose_light = self.changedLightsHue[device] if "Brightness" in pose_light and \ pose_light["Brightness"] >= 1 and \ "Color" in pose_light and \ 6 <= len(pose_light["Color"]) <= 7: self.hue.state(device=device, bri=pose_light["Brightness"], col=pose_light["Color"]) for device in old_json_data["hue"]: pose_light = old_json_data["hue"][device] if "Brightness" in pose_light and \ pose_light["Brightness"] >= 1 and \ "Color" in pose_light and \ 6 <= len(pose_light["Color"]) <= 7 and \ device not in self.changedLightsHue: self.hue.state(device=device) # Reset to Idle lights elif "Idle" in self.settings.PoseMapping and "Hue" in self.settings.PoseMapping[ "Idle"]: for device in self.settings.PoseMapping["Idle"]["Hue"]: pose_light = self.settings.PoseMapping["Idle"]["Hue"][ device] if "Brightness" in pose_light and \ pose_light["Brightness"] >= 1 and \ "Color" in pose_light and \ 6 <= len(pose_light["Color"]) <= 7: self.hue.state(device=device, bri=pose_light["Brightness"], col=pose_light["Color"]) for device in old_json_data["hue"]: pose_light = old_json_data["hue"][device] if "Brightness" in pose_light and \ pose_light["Brightness"] >= 1 and \ "Color" in pose_light and \ 6 <= len(pose_light["Color"]) <= 7 and \ device not in self.settings.PoseMapping["Idle"]["Hue"]: self.hue.state(device=device) # Turn off lights else: for device in old_json_data["hue"]: pose_light = old_json_data["hue"][device] if "Brightness" in pose_light and \ pose_light["Brightness"] >= 1 and \ "Color" in pose_light and \ 6 <= len(pose_light["Color"]) <= 7: self.hue.state(device=device) # # Reset Yeelight to Idle # if "yeelight" in old_json_data: # Reset to persistent lights if self.changedLightsYeelight: for device in self.changedLightsYeelight: pose_light = self.changedLightsYeelight[device] if "Brightness" in pose_light and \ pose_light["Brightness"] >= 1 and \ "Color" in pose_light and \ 6 <= len(pose_light["Color"]) <= 7: self.yeelight.state( device=device, brightness=pose_light["Brightness"], color=pose_light["Color"], transition=pose_light["Transition"], transitionTime=pose_light["TransitionTime"]) for device in old_json_data["yeelight"]: pose_light = old_json_data["yeelight"][device] if "Brightness" in pose_light and \ pose_light["Brightness"] >= 1 and \ "Color" in pose_light and \ 6 <= len(pose_light["Color"]) <= 7 and \ device not in self.changedLightsYeelight: self.yeelight.state(device=device) # Reset to Idle lights elif "Idle" in self.settings.PoseMapping and "Yeelight" in self.settings.PoseMapping[ "Idle"]: for device in self.settings.PoseMapping["Idle"]["Yeelight"]: pose_light = self.settings.PoseMapping["Idle"]["Yeelight"][ device] if "Brightness" in pose_light and \ pose_light["Brightness"] >= 1 and \ "Color" in pose_light and \ 6 <= len(pose_light["Color"]) <= 7: self.yeelight.state( device=device, brightness=pose_light["Brightness"], color=pose_light["Color"], transition=pose_light["Transition"], transitionTime=pose_light["TransitionTime"]) for device in old_json_data["yeelight"]: pose_light = old_json_data["yeelight"][device] if "Brightness" in pose_light and \ pose_light["Brightness"] >= 1 and \ "Color" in pose_light and \ 6 <= len(pose_light["Color"]) <= 7 and \ device not in self.settings.PoseMapping["Idle"]["Yeelight"]: self.yeelight.state(device=device) # Turn off lights else: for device in old_json_data["yeelight"]: pose_light = old_json_data["yeelight"][device] if "Brightness" in pose_light and \ pose_light["Brightness"] >= 1 and \ "Color" in pose_light and \ 6 <= len(pose_light["Color"]) <= 7: self.yeelight.state(device=device) # # Remove notification from queue # if self.queue: self.queue.remove(queue_id)
def TrainingLoop(self): thread_train = Timer(1, self.do_training_loop) thread_train.daemon = True thread_train.start()