Пример #1
0
 def _run(self):
     cmd = Command(self.device_id, "1", [], self.task_id)
     self.device.post_command(cmd)
     while self.is_active:
         cmd = Command(self.device_id, "9", [], self.task_id)
         self.device.post_command(cmd)
         sleep(int(self.sleep_period))
Пример #2
0
    def get_od_for_init(self):
        cmd = Command(self.device_id,
                      "5", [self.od_channel],
                      self.task_id,
                      is_awaited=True)

        self.device.post_command(cmd)
        cmd.await_cmd()
        if cmd.is_valid:
            return cmd.response
Пример #3
0
 def get_pump_command(self, state: bool) -> Command:
     if state:
         return Command(self.device_id,
                        self.pump_on_command.get("command_id"),
                        eval(self.pump_on_command.get("arguments", "[]")),
                        self.task_id)
     else:
         return Command(self.device_id,
                        self.pump_off_command.get("command_id"),
                        eval(self.pump_off_command.get("arguments", "[]")),
                        self.task_id)
Пример #4
0
    def _execute_command(self, command: Command):
        try:
            validity = True
            response = self.get_command_reference(
                command.command_id)(*command.args)
        except Exception as e:
            validity = False
            response = e
            Log.error(e)

        command.response = response
        command.is_valid = validity
        command.executed_on = (self.device_class, self.device_id)

        command.resolve()
        return command
Пример #5
0
    def _run(self):
        self.average_od = self.measure_initial_od_average()
        od_variant = 'od_1' if self.od_channel == 1 else 'od_0'

        while self.is_active:
            commands = []

            for _name, _command in self.commands_to_execute.items():
                command = Command(self.device_id,
                                  _command.get("id"),
                                  _command.get("args", []),
                                  self.task_id,
                                  is_awaited=True)
                commands.append((_name, command))
                self.device.post_command(command, 1)

            for name, command in commands:
                command.await_cmd()
                if command.is_valid and name == od_variant:
                    od = command.response['od']
                    self.latest_values.appendleft(od)
                    od_is_outlier = self.handle_outlier(od)
                    if not od_is_outlier:
                        self.od.value = od
                    command.response = {
                        'od': od,
                        'outlier': od_is_outlier,
                        'channel': self.od_channel
                    }
                command.save_data_to_db()

            sleep(self.sleep_period)
Пример #6
0
    def command(self, config) -> Response:
        try:
            validate_attributes(['device_id', 'command_id'], config, 'Command')

            device_id = config.get('device_id')
            command_id = config.get('command_id')
            args = config.get('arguments', '[]')
            source = config.get('source', 'external')
            priority = 1 if config.get('priority', False) else 2

            cmd = Command(device_id, command_id, eval(args), source)
            self.deviceManager.get_device(device_id).post_command(
                cmd, priority=priority)
            cmd.save_command_to_db()
            return Response(True, None)
        except AttributeError as e:
            Log.error(e)
            return Response(False, None, e)
Пример #7
0
    def _run(self):
        while self.is_active:
            command_msg = {
                "device_id": self.device_id,
                "command_id": "6",
                "source": self.task_id
            }

            cmd = Command(self.device_id, "6", [], self.task_id)
            self.device.post_command(cmd)
            sleep(int(self.sleep_period))
Пример #8
0
 def post_command(self, cmd: Command, priority=2):
     cmd.device_id = self.device_id
     self._queue.put(cmd, priority)
     if not self._is_queue_check_running:
         t = Thread(target=self._queue_new_item)
         t.start()
Пример #9
0
 def _post_command(self, cmd: Command, priority=0):
     cmd.device_id = self.device_id
     job = Job(task=self._execute_command, args=[cmd])
     self.scheduler.schedule_job(job)