def test(self, cmd=None): if cmd == 'self': return 'OK' if cmd == 'get': return self.data if cmd == 'critical': self.log_critical('test') return True try: port, val = cmd.split('=') try: val = float(val) eva.benchmark.report('UPDATE', val) self.data[port] = val except: self.data[port] = val self.log_debug( '%s test completed, set port %s=%s' % (self.phi_id, port, val)) if self.phi_cfg.get('event_on_test_set'): handle_phi_event(self, port, self.data) return self.data except: return { 'get': 'get sensors values', 'X=S': 'set sensor port X to S' }
def set(self, port=None, data=None, cfg=None, timeout=0): if isinstance(port, list): ports = port multi = True else: ports = [port] multi = False for i in range(0, len(ports)): p = ports[i] _port = str(p) if multi: d = data[i] else: d = data if self._is_required.value: d, value = d try: _data = int(d) except: return False if not _port in self.data: return False self.data[_port] = (_data, value) if self._is_required.value else _data eva.benchmark.report('ACTION', _data, end=True) if self.phi_cfg.get('event_on_set'): handle_phi_event(self.phi_id, port, self.data) return True
def _perform_update(self): self.log_debug('performing update') to.init(timeout=get_timeout()) state = self.get(timeout=get_timeout()) if not state: return if self._last_update_state: stu = {} for x, v in state.items(): if v != self._last_update_state.get(x): stu[x] = v else: if self.__benchmark: self.__last_update_reset = time() self.log_warning('benchmark mode') stu = state if self.__benchmark: self.__update_count += 1 self._last_update_state = state.copy() if stu: handle_phi_event(self, 'scheduler', stu) if self.__benchmark and self.__update_count > 1 / self._update_interval: self.log_warning('update benchmark: {}/s'.format( round(self.__update_count / (time() - self.__last_update_reset)))) self.__update_count = 0 self.__last_update_reset = time()
def _t_exp(self): while self.is_active: time.sleep(sleep_delay) if self.current_state and \ self.state_set_time + self.expires < time.time(): with self.state_lock: self.current_state = 0 handle_phi_event(self, 1, self.get())
def mqtt_handler(self, data, topic=None, qos=None, retain=None): try: port = int(topic.split('/')[-1][5:]) if port < 0 or port > self.channels: raise Exception except: return self.current_status[str(port)] = 1 if data == 'ON' else 0 handle_phi_event(self, port, {str(port): 1 if data == 'ON' else 0})
def mqtt_state_handler(self, data, topic, qos, retain): try: state = json.loads(data) if state.get('RfReceived', {}).get('Data') == self.sensor_id: with self.state_lock: self.current_state = 1 self.state_set_time = time.time() handle_phi_event(self, 1, self.get()) except: log_traceback()
def process_snmp_trap(self, host, data): if host != self.snmp_host: return if data.get('1.3.6.1.4.1.3854.1.7.4.0') != str(self.sensor_port - 1): return d = data.get('1.3.6.1.4.1.3854.1.7.1.0') if d == '7': handle_phi_event(self, ['t', 'h'], {'t': False, 'h': False}) elif d == '2': t = self.get('t', timeout=get_timeout()) h = self.get('h', timeout=get_timeout()) handle_phi_event(self, ['t', 'h'], {'t': t, 'h': h}) return
def process_snmp_trap(self, host, data): if host != self.snmp_host: return if data.get('1.3.6.1.6.3.1.1.4.1.0') != '1.3.6.1.4.1.42505.7.0.1': return for i in range(16): value = data.get('1.3.6.1.4.1.42505.7.2.1.1.7.{}'.format(i)) if value: port = 'din{}'.format(i + 1) self.log_debug('event {} = {}'.format(port, value)) self.port_state[port] = value handle_phi_event(self, port, {port: value}) return
def mqtt_state_handler(self, data, topic, qos, retain): try: obtained_status = {} state = json.loads(data) for ch in range(1, self.channels + 1): st = state.get('POWER%u' % ch) if st is not None: st = 1 if st == 'ON' else 0 self.current_status[str(ch)] = st obtained_status[str(ch)] = st handle_phi_event(self, 'all', obtained_status) except: pass
def push_state(self, payload): try: data = payload['data'] self.data['bat'] = int(data[4:8], 16) / 100 self.data['hum'] = int(data[12:14], 16) * 0.5 self.data['pres'] = int(data[18:22], 16) / 10 self.data['temp'] = int(data[26:30], 16) / 10 self.data['gas'] = int(data[34:38], 16) / 100 handle_phi_event(self, data=self.data) return True except: log_traceback() return False
def start(self): if self.ports: ports = self.ports if not isinstance(ports, list): ports = [ports] for p in ports: try: _p = int(p) pf = lambda a=str(_p): \ handle_phi_event(self, a, {str(a): '1'}) rf = lambda a=str(_p): \ handle_phi_event(self, a, {str(a): '0'}) d = self.gpiozero.Button(_p, pull_up=not self.no_pullup) d.when_pressed = pf d.when_released = rf self.devices.append(d) except: log_traceback() self.log_error('can not assign button to gpio port %s' % p)
def test(self, cmd=None): if cmd == 'self': return 'OK' if cmd == 'get': return self.data if cmd == 'critical': self.log_critical('test') return True if cmd == 'start_benchmark': eva.benchmark.enabled = True register_benchmark_handler() eva.benchmark.reset() return 'OK' if cmd == 'stop_benchmark': eva.benchmark.enabled = False unregister_benchmark_handler() return 'OK' try: port, val = cmd.split('=') port = int(port) if self._is_required.value: if val.find(',') != -1: val, value = val.split(',', 1) else: value = '' val = int(val) if self._is_required.value: state = val, value else: state = val if port < 1 or port > 16 or val < -1 or val > 1: return None self.set(port=str(port), data=state, cfg={'skip_timeout': True}) self.log_debug('test set port %s=%s' % (port, state)) if self.phi_cfg.get('event_on_test_set'): handle_phi_event(self, port, self.data) return self.data except: return {'get': 'get relay ports status', 'X=S': 'set port X to S'}
def process_snmp_trap(self, host, data): if host != self.snmp_host: return if data.get('1.3.6.1.4.1.3854.1.7.4.0') not in self.sensor_port: return try: port = str(int(data.get('1.3.6.1.4.1.3854.1.7.4.0')) + 1) except: return d = data.get('1.3.6.1.4.1.3854.1.7.1.0') if d == '7': handle_phi_event(self, self.sensor_port, {port: False}) elif d == '2': handle_phi_event(self, self.sensor_port, {port: 0}) elif d == '4': handle_phi_event(self, self.sensor_port, {port: 1}) return
def mqtt_handler(self, data, topic=None, qos=None, retain=None): self.current_status = 1 if data == 'ON' else 0 handle_phi_event(self, 1, self.get())