async def get_data(self, tick: Ticket = None): co2, logs = self.sensor.get_data() self.logger.debug("K30 CO2 got results: {}".format(logs)) if tick: tick.result = co2 else: return co2
async def get_data(self, tick: Ticket = None): res = self.hx.get_data() self.logger.debug("Weight unit get data: {}".format(res)) if tick: tick.result = res else: return res
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 do_calibration(self, tick: Ticket = None): self.logger.info("manual do calibration") result = await self.worker.do_calibration() if tick: tick.result = result else: return result
async def start_draining(self, tick: Ticket = None): self.logger.info("Gpio start_draining") res = "" # at first open valves for pin in self.drain_valve_pins: res += self.gpio.write(pin, False) # false - because our relay is low level trigger self.pins[pin] = False self.logger.info("Start opening drain valves") # then wait magic time await asyncio.sleep(self.drain_valve_time) self.logger.info("Drain valves should be open now") # then set up air pumps for pin in self.drain_pump_pins: res += self.gpio.write(pin, False) # false - because our relay is low level trigger self.pins[pin] = False self.logger.info("Start drain pumps") # results self.logger.debug(res) if tick: tick.result = res else: return res
async def do_measure(self, tick: Ticket = None): self.logger.info("manual do measure") result = await self.worker.measure() if tick: tick.result = result else: return result
async def get_info(self, tick: Ticket = None): self.logger.debug("Gpio get_info") res = self.pins if tick: tick.result = res else: return res
async def stop_ventilation(self, tick: Ticket = None): self.logger.info("manual stop ventilation") result = await self.worker.stop_ventilation() if tick: tick.result = result else: return result
async def do_calibration(self, tick: Ticket = None): ans = await self.sensor.send_command("Z\r\n") self.logger.info("Starting calibration of SBA5") if tick: tick.result = ans else: return ans
async def get_info(self, tick: Ticket = None): ans = await self.sensor.send_command("?\r\n") self.logger.debug("Getting info from SBA5") if tick: tick.result = ans else: return ans
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 pause(self, tick: Ticket = None): self.logger.info("manual pause") await self.worker.pause() result = "worker paused" if tick: tick.result = result else: return result
async def do_reconfiguration(self, tick: Ticket = None): self.logger.info("manual do_reconfiguration") await self.worker.do_reconfiguration() result = "worker do_reconfiguration" if tick: tick.result = result else: return result
async def continue_(self, tick: Ticket = None): self.logger.info("manual continue") await self.worker.continue_() result = "worker continued" if tick: tick.result = result else: return result
async def stop(self, tick: Ticket = None): self.logger.info("Gpio stop") self.gpio.deleter() res = "Gpio cleaned up" self.logger.debug(res) if tick: tick.result = res else: return res
async def set_pin(self, pin: int, state: bool, tick: Ticket = None): self.logger.info("Gpio manually set pin") res = self.gpio.write(pin, state) self.pins[pin] = state self.logger.debug(res) if tick: tick.result = res else: return res
async def do_measurement(self, tick: Ticket = None): ans = await self.sensor.send_command("M\r\n") self.logger.debug("Do measure SBA5") self.logger.debug(("SBA5 result is {}".format(ans) )[:-1]) #its try to remove last \n from here if tick: tick.result = ans else: return ans
async def start(self, tick: Ticket = None): self._started = True self.logger.info("Started") res = self.uart_wrapper.START()[1] self.logger.debug(res) if tick: tick.result = res else: return res
async def stop(self, tick: Ticket = None): self.logger.info("manual kill worker") # TODO: check if it really works await self.worker.stop() result = "worker killed" if tick: tick.result = result else: return result
async def get_info(self, tick: Ticket): self.logger.info("get info") proc = await asyncio.create_subprocess_shell( "uname -a", stdout=asyncio.subprocess.PIPE) stdout, stderr = await proc.communicate() content = stdout.decode().strip() if tick: tick.result = content else: return content
async def get_info(self, tick: Ticket = None): self.logger.info( "Info. Unit {}, red current = {}, white current = {}".format( "started" if self._started else "stopped", self._red, self._white)) res = self.uart_wrapper.GET_STATUS()[1] self.logger.debug(res) if tick: tick.result = res else: return res
async def stop_ventilation(self, tick: Ticket = None): self.logger.info("Gpio stop_ventilation") res = "" for i in self.vent_pins: res = self.gpio.write(i, True) self.pins[i] = True self.logger.debug(res) if tick: tick.result = res else: return res
async def stop_coolers(self, tick: Ticket = None): self.logger.info("Gpio stop coolers") for i in self.cooler_pin: res = self.gpio.write(i, True) # false - because our relay is low level trigger self.pins[i] = True self.logger.debug(res) if tick: tick.result = res else: return res
async def get_info(self, tick: Ticket = None): self.logger.debug("Dht get info function") if platf != "RPi": self.logger.error( "We are not on RPi, so this unit will be only a stub") else: self.logger.debug((self.pin, self.dhttype)) if tick: tick.result = (self.pin, self.dhttype) else: return self.pin, self.dhttype
async def start_ventilation(self, tick: Ticket = None): self.logger.info("Gpio start_ventilation") res = "" for i in self.vent_pins: res += self.gpio.write(i, False) # false - because our relay is low level trigger self.pins[i] = False self.logger.debug(res) if tick: tick.result = res else: return res
async def get_data(self, tick: Ticket = None): self.logger.debug("Dht get data function") if platf != "RPi": self.logger.error( "We are not on RPi, so this unit will be only a stub") else: h, t = self.sensor.get_data() self.logger.debug((t, h)) if tick: tick.result = (t, h) else: return t, h
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 create_tunnel(self, tick: Ticket): self.logger.info("manual create tunnel") # TODO: mb remove it ? It is dangerous cmd = 'autossh -M 10984 -N -f -o "PubkeyAuthentication=yes" -o "PasswordAuthentication=no" -i /home/pi/.ssh/id_rsa -R 6666:localhost:22 [email protected] &' proc = await asyncio.create_subprocess_shell( cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE) stdout, stderr = await proc.communicate() content = stdout.decode().strip() if tick: tick.result = content else: return content
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 stop_calibration(self, tick: Ticket = None): self.logger.info("Gpio stop_calibration") for i in self.calibration_pins: res = self.gpio.write(i, True) # false - because our relay is low level trigger self.pins[i] = True # then we need to start pump3 for i in self.measure_pins: res = self.gpio.write(i, False) # false - because our relay is low level trigger self.pins[i] = False self.logger.debug(res) if tick: tick.result = res else: return res