def get(self, uid): """get all temp-io devices of specific user""" if self.req_user and self.req_user['myself'] is True: wio = Wio(self.req_token) try: thing_list = yield wio.get_all_thing() except Exception as e: gen_log.error(e) raise HTTPError(400, "Get temp is failure, {}".format(e.message)) if thing_list is None: self.set_status(200) self.finish({"temps": []}) return docs = yield self.db_temp.get_all_temp_by_uid(uid) temp_list = [jsonify(doc) for doc in docs] for temp in temp_list[:]: # FIXME, need more efficiency for thing in thing_list: if thing['id'] == temp['id']: temp['online'] = thing['online'] self.finish({"temps": temp_list}) else: docs = yield self.db_temp.get_all_temp_by_uid(uid) temp_list = [] for doc in docs: if doc['private'] is False: temp_list.append(jsonify(doc)) self.finish({"temps": temp_list})
def get(self, uid, tid): if self.req_user and self.req_user['myself'] is True: data = jsonify(self.temp) wio = Wio(self.req_token) try: thing_list = yield wio.get_all_thing() except Exception as e: gen_log.error(e) raise HTTPError( 400, "Get thing is failure from built-in Wio server, {}".format( str(e))) for thing in thing_list: if thing["id"] == data["id"]: data["online"] = thing["online"] self.finish(data) else: if self.temp['private'] is True: raise HTTPError(400, "The device is private") data = jsonify(self.temp) # value = {} # TODO, filter output value # for key in ['temperature', 'temperature_f', 'temperature_updated_at', 'updated_at', 'created_at']: # value[key] = data[key] # self.finish(value) self.finish(data)
def delete(self, uid, tid): if self.req_user and self.req_user['myself'] is False: raise HTTPError(400, "No operation permission") yield self.db_temp.del_temp(tid) wio = Wio(self.req_token) try: yield wio.del_thing(tid) except Exception as e: gen_log.error(e) raise HTTPError(400, "del_thing error, {}".format(str(e))) self.set_status(204) self.finish()
def delete(self, uid, tid): # TODO: (ten) authenticated uid is correct? yield self.db_temp.del_temp(tid) wio = Wio(self.current_user['token']) try: yield wio.del_thing(tid) except Exception as e: gen_log.error(e) self.set_status(400) self.finish({"error": "del_thing error"}) return self.set_status(204) self.finish()
def post(self, uid, tid): wio = Wio(self.current_user['token']) try: activated = yield wio.get_activation(tid) except Exception as e: gen_log.error(e) self.set_status(400) self.finish({"error": "Get activation is failure on Wio, {}".format(e.message)}) return if activated is True: yield self.db_temp.update_temp(tid, {"activated": activated}) self.set_status(204) self.finish() else: self.set_status(400) self.finish({"error": "Verify activation failure."})
def get_ota(self, tid, thing_key): """Long polling on Wio v1""" wio = Wio(thing_key) while 1: try: result = yield wio.get_ota() except httpclient.HTTPError as e: if e.code == 599: continue elif e.code == 400: gen_log.error(e) break except Exception as e: gen_log.error(e) break yield self.db_temp.update_ota(tid, {"status": result['status'], "status_text": result['status_text']}) if result['status'] == "error" or result['status'] == "done": break
def post(self, uid, tid): if self.req_user and self.req_user['myself'] is False: raise HTTPError(400, "No operation permission") wio = Wio(self.req_token) try: activated = yield wio.get_activation(tid) except Exception as e: gen_log.error(e) raise HTTPError( 400, "Get activation is failure on built-in Wio server, {}".format( str(e))) if activated is True: yield self.db_temp.update_temp(tid, {"activated": activated}) self.set_status(204) self.finish() else: self.set_status(400) self.finish({"error": "Verify activation failure."})
def get(self, uid): wio = Wio(self.current_user['token']) try: thing_list = yield wio.get_all_thing() except Exception as e: gen_log.error(e) self.set_status(400) self.finish({"error": "Get temp is failure, {}".format(e.message)}) return if thing_list is None: self.set_status(400) self.finish({"temps": []}) return docs = yield self.db_temp.get_all_temp_by_uid(uid) temp_list = [jsonify(doc) for doc in docs] for temp in temp_list[:]: # FIXME, need more efficiency for thing in thing_list: if thing['id'] == temp['id']: temp['online'] = thing['online'] self.finish({"temps": temp_list})
def post(self, uid): """create a temp-io device on server""" if self.req_user and self.req_user['myself'] is False: raise HTTPError(400, "No operation permission") wio = Wio(self.req_token) try: thing = yield wio.add_thing() except Exception as e: gen_log.error(e) raise HTTPError( 400, "Create temp-io is failure on built-in Wio server, {}".format( str(e))) cur_time = datetime.utcnow() document = { "uid": uid, "id": thing['id'], "key": thing['key'], # "online": False, "board_type_id": 1, "temperature": None, "temperature_f": None, "temperature_updated_at": None, "read_period": 60, "has_sleep": True, "status": "", "status_text": "", "open": False, "activated": False, "name": "", "description": "", "private": False, "gps": "", "picture_url": "", "updated_at": cur_time, "created_at": cur_time } result = yield self.db_temp.add_temp(document) data = jsonify(result) self.set_status(201) self.finish(data)
def get_ota(self, tid, thing_key): """Long polling on Wio v1""" wio = Wio(thing_key) while 1: try: result = yield wio.get_ota() except httpclient.HTTPError as e: if e.code == 599: continue elif e.code == 400: gen_log.error(e) break except Exception as e: gen_log.error(e) break yield self.db_temp.update_ota(tid, { "status": result['status'], "status_text": result['status_text'] }) if result['status'] == "error" or result['status'] == "done": break
def post(self, uid, tid): if self.req_user and self.req_user['myself'] is False: raise HTTPError(400, "No operation permission") thing_key = self.temp['key'] wio = Wio(thing_key) try: result = yield wio.add_ota(self.temp['board_type_id']) except Exception as e: gen_log.error(e) raise HTTPError(400, "Trigger OTA is failure, {}".format(str(e))) ota = yield self.db_temp.update_ota( tid, { "status": result['status'], "status_text": result['status_text'] }) self.set_status(202) self.add_header("Location", "") # TODO, get status url self.finish(jsonify(ota)) IOLoop.current().add_callback(self.get_ota, tid, thing_key)
def get(self, uid, tid): # TODO: (ten) authenticated uid is correct? result = yield self.db_temp.get_temp(tid) if result is None: gen_log.error("Not this temp") self.set_status(400) self.finish({"error": "Not this temp"}) return wio = Wio(self.current_user['token']) try: thing_list = yield wio.get_all_thing() except Exception as e: gen_log.error(e) self.set_status(400) self.finish({"error": "Get thing is failure on Wio, {}".format(e.message)}) return data = jsonify(result) for thing in thing_list: if thing["id"] == data["id"]: data["online"] = thing["online"] self.finish(data)
def post(self, uid): wio = Wio(self.current_user['token']) try: thing = yield wio.add_thing() except Exception as e: gen_log.error(e) self.set_status(400) self.finish({"error": "Create thing is failure on Wio, {}".format(e.message)}) return cur_time = datetime.utcnow() document = { "uid": uid, "id": thing['id'], "key": thing['key'], # "online": False, "board_type": 1, "temperature": None, "temperature_f": None, "temperature_updated_at": None, "read_period": 60, "has_sleep": True, "status": "", "status_text": "", "open": False, "activated": False, "name": "", "description": "", "private": False, "gps": "", "picture_url": "", "updated_at": cur_time, "created_at": cur_time } result = yield self.db_temp.add_temp(document) data = jsonify(result) self.set_status(201) self.finish(data)
def post(self, uid, tid): temp = yield self.db_temp.get_temp(tid) if temp is None: gen_log.error("Not this temp") self.set_status(400) self.finish({"error": "Not this temp"}) return thing_key = temp['key'] wio = Wio(thing_key) try: result = yield wio.add_ota() except Exception as e: gen_log.error(e) self.set_status(400) self.finish({"error": "Trigger OTA is failure."}) return ota = yield self.db_temp.update_ota(tid, {"status": result['status'], "status_text": result['status_text']}) self.set_status(202) self.add_header("Location", "") # TODO, get status url self.finish(jsonify(ota)) IOLoop.current().add_callback(self.get_ota, tid, thing_key)
def task(self, *args): temp_id = args[0] gen_log.info("=== Do task id({})====".format(temp_id)) doc = yield Temp().get_temp(temp_id) if doc is None: gen_log.info("Temp({}) be deleted!".format(temp_id)) yield self.remove_from_tasks(temp_id) raise gen.Return() access_token = doc.get('key') period = doc.get('read_period') temp_open = doc.get('open') has_sleep = doc.get('has_sleep') board_type_id = doc.get('board_type_id') if not temp_open: gen_log.info("Temp({}) be closed!".format(temp_id)) yield self.remove_from_tasks(temp_id, "normal", "The temp is closed.") raise gen.Return() end_time = time.time() + (3 * period) while True: try: temps = [] wio = Wio(access_token) for i in range(4): result = yield wio.get_temp(board_type_id) temps.append(result) yield gen.sleep(1) temp = round(sum(temps[1:]) / (len(temps) - 1), 1) except Exception as e: # TODO, if not pulgin grove temp, will gen error if time.time() > end_time: gen_log.error("Temp({}) {}".format(temp_id, e)) yield self.remove_from_tasks( temp_id, "error", "The node is not wake up on three period.") yield self.close_temp(temp_id) raise gen.Return() yield gen.sleep(5) gen_log.info("Temp({}) {}".format(temp_id, e)) continue gen_log.info("{} ==> {}".format(temps, temp)) self.update_temp(temp_id, temp) break if has_sleep is True: wio = Wio(access_token) try: yield wio.sleep(period, board_type_id) self.update_status(temp_id, "normal", "The node is sleep mode.") except Exception as e: gen_log.error(e) else: self.update_status(temp_id, "normal", "The node is online mode.") IOLoop.current().add_timeout(time.time() + (period or 60), self.task, temp_id) gen_log.info("task ids: ({})".format(self.tasks)) gen_log.info("===End task and restart({})====".format(temp_id))