async def user_main2(): ans = await command_get_server_info(host="83.220.174.247", port=8888) print(ans.header) print(ans.body) print("Now we have {} tickets on server".format( json.loads(ans.body)["tickets_number"])) # com = Command( # cunit="led_unit", # cfunc="set_current", # cargs={"red":10, "white":100}, # ctype="single" # ) com = Command(cunit="led_unit", cfunc="stop", cargs=None, ctype="single") # com = Command( # cunit="system_unit", # cfunc="get_info", # cargs=None, # ctype="single" #) tick = Ticket(tfrom=10, tto=155167253286217647024261323245457212920, tid=None, tcommand=com.cdict, tresult=None) ans = await command_add_ticket(tick, host="83.220.174.247", port=8888) print(ans.header) print(ans.body)
async def check_server(self): """ do send request to server, parse answer and put tasks to self.tickets (with lock just in case) :return: """ # send request to server logger.debug("check_server: started") res = None try: res = await command_request_ticket(worker_id=self._id, host=self._host, port=self._port) logger.debug("check_server: sent request") except Exception as e: # TODO: what we have to do with this type errors? How to handle logger.debug( "check_server: Error while sending request to server: {}". format(e)) if res: logger.debug("check_server: parsing answer") # parse answer answer = res # its already Message object dicts_list = json.loads(answer.body) # TODO: remove useless print and do something useful # print(answer.header) tickets_list = [Ticket(**t_dict) for t_dict in dicts_list] # add tickets from answer to list async with self._new_tickets_lock: for t in tickets_list: logger.debug(t.id, t.to, t.tfrom) self._new_tickets.append(t) logger.debug("check_server: done")
async def test_tunnel(): ans = await command_get_server_info(host="83.220.174.247", port=8888) print(ans.header) print(ans.body) print("Now we have {} tickets on server".format( json.loads(ans.body)["tickets_number"])) com = Command(cunit="system_unit", cfunc="stop", cargs=None, ctype="single") # com = Command( # cunit="led_unit", # cfunc="set_current", # cargs={"red": 10, "white": 182}, # ctype="single" # ) # com = Command( # cunit="gpio_unit", # cfunc="stop_draining", # cargs=None, # ctype="single" # ) # com = Command( # cunit="gpio_unit", # cfunc="stop_ventilation", # cargs=None, # ctype="single" # ) tick = Ticket(tfrom=10, tto=155167253286217647024261323245457212920, tid=None, tcommand=com.cdict, tresult=None) ans = await command_add_ticket(tick, host="83.220.174.247", port=8888) print(ans.header) print(ans.body) time.sleep(20) ans = await command_get_ticket_result(tick.id, host="83.220.174.247", port=8888) print(ans.header) print(ans.body) ans = await command_delete_ticket(tick.id, host="83.220.174.247", port=8888) print(ans.header) print(ans.body)
async def simple_test(): com = Command(cunit="system_unit", cfunc="get_info", cargs=None, ctype="single") tick = Ticket(tfrom=10, tto=155167253286217647024261323245457212920, tid=None, tcommand=com.cdict, tresult=None) message = Message(header="ADD_TICKET", body=tick.tdict) raw = json.dumps(message.mdict) with open("test.txt", 'w') as file_handler: file_handler.write(raw) print(raw)
async def test_co2(): ans = await command_get_server_info(host="83.220.174.247", port=8888) print(ans.header) print(ans.body) print("Now we have {} tickets on server".format( json.loads(ans.body)["tickets_number"])) # com = Command( # cunit="led_unit", # cfunc="set_current", # cargs={"red":115, "white":58}, # ctype="single" # ) # com = Command( # cunit="co2_sensor_unit", # cfunc="do_command", # cargs={"com": "M\r\n"}, # ctype="single" # ) com = Command(cunit="system_unit", cfunc="do_reconfiguration", cargs=None, ctype="single") tick = Ticket(tfrom=10, tto=155167253286217647024261323245457212920, tid=None, tcommand=com.cdict, tresult=None) # ans = await command_add_ticket(tick, host="83.220.174.247", port=8888) # print(ans.header) # print(ans.body) ans = await command_add_ticket(tick, host="83.220.174.247", port=8888) print(ans.header) print(ans.body) time.sleep(10) ans = await command_get_ticket_result(tick.id, host="83.220.174.247", port=8888) print(ans.header) print(ans.body) ans = await command_delete_ticket(tick.id, host="83.220.174.247", port=8888) print(ans.header) print(ans.body)
async def main(): # example uuid for worker =155167253286217647024261323245457212920 com = Command(cunit="system_unit", cfunc="get_info", cargs=None, ctype="single") ans = await command_get_server_info() print(ans.header) print(ans.body) while True: tick = Ticket(tfrom=10, tto=155167253286217647024261323245457212920, tid=None, tcommand=com.cdict, tresult=None) await asyncio.sleep(5) ans = await command_add_ticket(tick) print(ans.header) print(ans.body) res = await command_get_ticket_result(tick.id) print(res.header) print(res.body)
def handle_message(self, message_: Any): """ Must parse incoming message and return answer message to send response :param message_: Any (raw message dict) :return: Message """ data_dict = json.loads(message_) message = Message(**data_dict) # print(data_dict) # print(type(data_dict)) header = message.header # parse header valid_heads = [ "ADD_TICKET", "DELETE_TICKET", "GET_TICKET_RESULT", "SET_TICKET_RESULT", "REQUEST_TICKETS", "GET_SERVER_INFO" ] if header not in valid_heads: return Message(header="ERROR", body="INVALID_HEADER_ERROR") else: body = message.body if header == "ADD_TICKET": # there must be a dict describes a ticket in body # like that: # body = { # "tfrom": int, # "tto": int, # "tid": UUID = None, # "tcommand": dict = None, # it must be a dict, that describes command # "tresult": Any = None # } try: new_tick = Ticket(**body) self.add_ticket(new_tick) return Message(header="SUCCESS", body="OK") except Exception as e: return Message(header="ERROR", body="INVALID_BODY_ERROR:" + str(e)) elif header == "DELETE_TICKET": # there must be a dict in body # body = { # "id": int # } try: self.delete_ticket(body["id"]) return Message(header="SUCCESS", body="OK") except Exception as e: return Message(header="ERROR", body="INVALID_BODY_ERROR:" + str(e)) elif header == "GET_TICKET_RESULT": # there must be a dict in body # body = { # "id": int # } try: ans_body = self.get_ticket_result(body["id"]) return Message(header="SUCCESS", body=ans_body) except Exception as e: return Message(header="ERROR", body="INVALID_BODY_ERROR:" + str(e)) elif header == "SET_TICKET_RESULT": # there must be a dict in body # body = { # "id": int, # "result": Any # } try: self.set_ticket_result(body["id"], body["result"]) return Message(header="SUCCESS", body="OK") except Exception as e: return Message(header="ERROR", body="INVALID_BODY_ERROR:" + str(e)) elif header == "REQUEST_TICKETS": # there must be a dict in body # body = { # "id": int, # } try: worker_id = body["id"] tickets_list = self.find_tickets_for_worker(worker_id) return Message(header="SUCCESS", body=json.dumps(tickets_list)) except Exception as e: return Message(header="ERROR", body="INVALID_BODY_ERROR:" + str(e)) elif header == "GET_SERVER_INFO": # there must be nothing in body # body = {} try: ans_body = self.server_info() return Message(header="SUCCESS", body=ans_body) except Exception as e: return Message(header="ERROR", body="UNKNOWN_ERROR:" + str(e)) else: return Message(header="ERROR", body="UNKNOWN_ERROR:" + "UNKNOWN")