def total_used_times(self): # Return time from started return time() - self.__begin_at
def used_times(self): # Return time during running status if self.__start_at: return self.__sum + (time() - self.__start_at) else: return self.__sum
def start_timer(self): if self.__begin_at: self.__start_at = time() else: self.__start_at = self.__begin_at = time()
def pause_timer(self): if self.__start_at: self.__sum = time() - self.__start_at self.__start_at = None
def on_timer(watcher, revent): if time() - shared_st["ts"] > 15 and shared_st["closed"] is False: shared_st["closed"] is True handler.send_text("er " + TIMEOUT) self.stack.exit_task(self, False)
def on_start(self): self.slave_pkey_ts = time() # TODO: for version 1 only self.ipaddress = self.get_last_ipaddr() self.mversion = self.meta.mversion self.setup_upnp_sock()
def send_notify(self): now = time() if now - self.__last_notify > self.__period: self.sock.sendto(self._notify_payload, self.endpoint) self.__last_notify = now
def aws_on_request_callback(self, client, userdata, message): # incommint topic format: "device/{token}/request/{action}" # response topic format: "device/{token}/response/{action}" action = message.topic.split("/", 3)[-1] logger.debug("IoT request: %s", action) response_topic = "device/%s/response/%s" % (self.aws_token, action) try: payload = json.loads(message.payload) except ValueError: logger.error("IoT request payload error: %s", message.payload) client.publish(response_topic, message.payload) return if payload.get("uuid") != self.uuidhex: client.publish( response_topic, json.dumps({ "status": "reject", "cmd_index": payload.get("cmd_index") })) return cmd_index = payload.get("cmd_index") try: if action == "getchu": try: current_hash = metadata.cloud_hash access_id, signature = payload.get("validate_message") client_key = security.get_keyobj(access_id=access_id) if client_key.verify(current_hash, a2b_base64(signature)): client.publish( response_topic, json.dumps({ "status": "ok", "cmd_index": cmd_index })) else: client.publish( response_topic, json.dumps({ "status": "reject", "cmd_index": cmd_index })) finally: metadata.cloud_hash = os.urandom(32) elif action == "monitor": self._notify_aggressive = time() + 180 self._notify_last_st["st_id"] = None client.publish( response_topic, json.dumps({ "status": "ok", "cmd_index": cmd_index })) elif action == "camera": self.require_camera(payload["camera_id"], payload["endpoint"], payload["token"]) client.publish( response_topic, json.dumps({ "status": "ok", "cmd_index": cmd_index })) elif action == "control": self.require_control(payload["endpoint"], payload["token"]) client.publish( response_topic, json.dumps({ "status": "ok", "cmd_index": cmd_index })) else: client.publish( response_topic, json.dumps({ "status": "error", "cmd_index": cmd_index })) except Exception: logger.exception("Handle aws request error") client.publish( response_topic, json.dumps({ "status": "error", "cmd_index": cmd_index }))
def shared_der_rsakey(self, val): h = struct.pack("<BfH", 128, time(), len(val)) self.shm.write(h + val, 1024)