def close(self): """ Close the serial port """ try: if self.is_open() is True: fct.log("Closing " + self.node_name) self.fd_port.close() except Exception as ex: fct.log_exception(ex)
def run(self): """ Cyclic execution to poll for received characters """ loop_nb = 1 while self.is_loop_enabled is True: try: #fct.log("DEBUG: " + self.node_name + " loop " + str(loop_nb)) if self.is_open() is False: self.open() time.sleep(1.0) if self.is_open() is True: line = "" cserial = " " read_iter_ = 0 while (len(cserial) > 0) and (self.is_loop_enabled is True): try: cserial = self.fd_port.read(1) if cserial is None: cserial = "" else: #if len(cserial) > 0: # print(cserial.hex(), end='') cserial = cserial.decode(encoding='utf-8', errors='ignore') if len(cserial) > 0: read_iter_ = read_iter_ + 1 if ord(cserial) == 0: cserial = "" print(cserial, end='') else: cserial = "" if (self.line != "") and (cserial == "\n" or cserial == "\r"): line = self.line self.line = "" # fct.log("DEBUG New line create=" + line) break else: if (cserial != "\n") and (cserial != "\r"): self.line = self.line + cserial except Exception as ex: self.line = "" cserial = "" fct.log_exception( ex, msg="ERROR while decoding data on " + self.node_name) self.close() if read_iter_ > self.read_iter: self.read_iter = read_iter_ if line != "": line_array = line.split(" ") fct.log("DEBUG: line_array=" + str(line_array)) except Exception as ex: fct.log_exception(ex) self.close() loop_nb += 1 time.sleep(0.001)
def run(): """ Cycle execution to poll on sensors """ try: alarm_sum = 1 for node_name, node_value in settings.acq.items(): for sensor_name, sensor_value in node_value.items(): if 'type' in sensor_value: if "alarm" in sensor_value['type']: if sensor_value['val'] == 0: alarm_sum = 0 settings.alarm['contacts'] = alarm_sum if settings.alarm['is_enabled'] is True: if settings.alarm['triggered'] is True: if 10 * 60 < settings.alarm['timeout']: settings.node_list["safety"].write("buzzerRelay set 0") if settings.alarm['stopped'] is False: fct.send_alert("BUZZER stopped") settings.alarm['stopped'] = True else: settings.alarm['timeout'] = settings.alarm['timeout'] + 1 # Turn on the buzzer settings.node_list["safety"].write("buzzerRelay set 1") # Close all the shutters settings.rts.write("OFF ID 22 RTS") else: msg = "" for node_name, node_value in settings.acq.items(): for sensor_name, sensor_value in node_value.items(): if 'type' in sensor_value: if "alarm" in sensor_value['type']: #fct.log("DEBUG: checking alarm: " + node_name + "." + sensor_name + ": " + str(sensor_value['val']) + " / " + str(settings.alarm['initial_status'][node_name][sensor_name]['val'])) if sensor_value['val'] != settings.alarm[ 'initial_status'][node_name][ sensor_name]['val']: msg = msg + " " + node_name + "." + sensor_name settings.alarm['triggered'] = True if "move" in sensor_value['type']: if sensor_value['val'] == 0: msg = msg + " " + node_name + "." + sensor_name settings.alarm['triggered'] = True if settings.alarm['triggered'] is True: settings.alarm['timeout'] = 0 settings.alarm['stopped'] = False settings.node_list["safety"].write("buzzerRelay set 1") fct.send_alert("ALARM started:" + msg) # Turn ON the music # requests.post("http://osmc:8080/jsonrpc?Player.Stop", '{"jsonrpc":"2.0","method":"Player.Stop","params":[0],"id":1}') # requests.post("http://osmc:8080/jsonrpc?Player.Open", '{"jsonrpc":"2.0","method":"Player.Open","params":{"item":{"file":"/home/osmc/alarm.mp3"},"options":{"repeat":"all"}},"id":2}') else: settings.node_list["safety"].write("buzzerRelay set 0") else: #fct.log("DEBUG: alarm is not enabled. Copying acq to alarm.initial_status") settings.alarm['initial_status'] = copy.deepcopy(settings.acq) settings.node_list["safety"].write("buzzerRelay set 0") except Exception as ex: fct.log_exception(ex)
def error404(self, resp): """ Return page not found """ try: self.send_response(404) self.end_headers() self.wfile.write( (time.strftime('%Y/%m/%d %H:%M:%S: ') + resp).encode()) except Exception as ex: fct.log_exception(ex)
def open(self): """ Open the serial port """ try: fct.log("Opening " + self.node_name) self.fd_port = open(self.port, "rb+", buffering=0) fd_port = self.fd_port.fileno() flag = fcntl.fcntl(fd_port, fcntl.F_GETFL) fcntl.fcntl(fd_port, fcntl.F_SETFL, flag | os.O_NONBLOCK) self.open_cnt += 1 except Exception as ex: fct.log_exception(ex)
def ok200(self, resp, content_type='text/plain'): """ Return OK page """ try: self.send_response(200) self.send_header('Content-Type', content_type) self.end_headers() if content_type == 'text/plain': self.wfile.write( (time.strftime('%Y/%m/%d %H:%M:%S: ') + resp).encode()) else: self.wfile.write((resp).encode()) except Exception as ex: fct.log_exception(ex)
def open(self): """ Open the serial port """ try: fct.log("Opening " + self.node_name) self.fd_port = open(self.port, "rb+", buffering=0) fd_port = self.fd_port.fileno() flag = fcntl.fcntl(fd_port, fcntl.F_GETFL) fcntl.fcntl(fd_port, fcntl.F_SETFL, flag | os.O_NONBLOCK) self.write("ATZ") time.sleep(0.1) self.write("ATE0") time.sleep(0.1) self.write("AT+CFUN=1") time.sleep(0.1) self.write("AT+CMGF=1") except Exception as ex: fct.log_exception(ex)
def run(): """ Cycle execution to update log file """ global run_loop global log_msg try: flog = open("/dev/shm/lbGate.settings", "w") msg = "###########################\n" msg = msg + "### " + time.strftime('%Y/%m/%d %H:%M:%S') + " ###\n" msg = msg + "# contact_status =\n" msg = msg + " # node contact = current value | alarm value\n" for node_name, node_value in acq.items(): for sensor_name, sensor_value in node_value.items(): if 'type' in sensor_value: if "alarm" in sensor_value['type']: msg = msg + " " + node_name.rjust( 7) + " " + sensor_name.rjust(22) + " = " + str( sensor_value['val']) + " | " + str( alarm['initial_status'][node_name] [sensor_name]['val']) + "\n" msg = msg + "# move_status =\n" for node_name, node_value in acq.items(): for sensor_name, sensor_value in node_value.items(): if 'type' in sensor_value: if "move" in sensor_value['type']: msg = msg + " " + node_name.rjust( 7) + " " + sensor_name.rjust(22) + " = " + str( sensor_value['val']) + "\n" msg = msg + "# alarm: is_enabled=" + str( alarm['is_enabled']) + " triggered=" + str( alarm['triggered']) + " timeout=" + str( alarm['timeout']) + " stopped=" + str( alarm['stopped']) + "\n" msg = msg + "# presence_is_enabled=" + str( presence_is_enabled) + " move_is_enabled=" + str( move_is_enabled) + "\n" msg = msg + "# node_list =\n" msg = msg + " # node = is_open | open_cnt | cmd_rx | ping_tx | ping_rx | wd | Max/" + str( MAX_NODE_ERRORS) + " | read_iter\n" for key, value in node_list.items(): msg = msg + " " + key.rjust(7) + " = " + str(value.is_open( )).rjust(9) + " | " + str(value.open_cnt).rjust(9) + " | " + str( value.cmd_rx_cnt).rjust(9) + " | " + str( value.ping_tx_cnt).rjust(9) + " | " + str( value.ping_rx_cnt).rjust(9) + " | " + str( value.error_cnt).rjust(9) + " | " + str( value.error_cnt_max).rjust(9) + " | " + str( value.read_iter).rjust(9) + "\n" for key, value in acq.items(): msg_temp = print_temp(value) if msg_temp != '': msg = msg + key + " temp:" + msg_temp + "\n" msg = msg + "# weather = " + "rain=" + str( acq['ext']['rainFlow']['val']) + " wind=" + str( acq['ext']['windSpeed']['val']) + "\n" msg = msg + "# ups = " + "port=" + str(ups.port) + " is_open=" + str( ups.is_open()) + " open_cnt=" + str(ups.open_cnt) + " iter=" + str( ups.read_iter) + " loop=" + str(ups.is_loop_enabled) + "\n" msg = msg + "- run_loop = " + str(run_loop) + "\n" log_msg = msg flog.write(msg) flog.close() except Exception as ex: fct.log_exception(ex) run_loop = run_loop + 1
def run(self): """ Cyclic execution to poll for received characters """ loop_nb = 1 while self.is_loop_enabled is True: try: #fct.log("DEBUG: " + self.node_name + " loop " + str(loop_nb)) if self.is_open() is False: self.open() time.sleep(1.0) if self.is_open() is True: line = "" cserial = " " read_iter_ = 0 while (len(cserial) > 0) and (self.is_loop_enabled is True): try: cserial = self.fd_port.read(1) if cserial is None: cserial = "" else: cserial = cserial.decode(encoding='utf-8', errors='ignore') if len(cserial) > 0: read_iter_ = read_iter_ + 1 if ord(cserial) == 0: cserial = "" else: cserial = "" if (self.line != "") and (cserial == "\n" or cserial == "\r"): line = self.line self.line = "" # fct.log("DEBUG New line create=" + line) break else: if (cserial != "\n") and (cserial != "\r"): self.line = self.line + cserial except Exception as ex: self.line = "" cserial = "" fct.log_exception(ex, msg="ERROR while decoding data on " + self.node_name) self.close() if read_iter_ > self.read_iter: self.read_iter = read_iter_ if line != "": line_array = line.split(" ") # fct.log("DEBUG: line_array=" + str(line_array)) if len(line_array) > 2: node = line_array[0] cmd = line_array[1] if node in settings.acq: if cmd in settings.acq[node]: arg_map = line_array[2:] if 'fct' in settings.acq[node][cmd]: try: fct_to_run = getattr(fct, settings.acq[node][cmd]['fct']) fct_to_run(node, cmd, arg_map) except Exception as ex: fct.log_exception(ex) else: if len(arg_map) == 2: if arg_map[0] in settings.acq[node][cmd]: settings.acq[node][cmd][arg_map[0]] = type(settings.acq[node][cmd][arg_map[0]])(arg_map[1]) else: fct.log("ERROR: " + arg_map[0] + " is not in cmd " + node + "." + cmd) else: if len(arg_map) == 3: if arg_map[0] in settings.acq[node][cmd]: if arg_map[1] in settings.acq[node][cmd][arg_map[0]]: settings.acq[node][cmd][arg_map[0]][arg_map[1]] = type(settings.acq[node][cmd][arg_map[0]][arg_map[1]])(arg_map[2]) else: fct.log("ERROR: " + arg_map[1] + " is not in cmd " + node + "." + cmd + "." + arg_map[0]) else: fct.log("ERROR: " + arg_map[0] + " is not in cmd " + node + "." + cmd) else: fct.log("ERROR: incorrect number of arguments in '" + str(arg_map) + "'. Got " + str(len(arg_map)) + ", expected 2") self.cmd_rx_cnt += 1 else: fct.log("ERROR: " + cmd + " is not in node " + node) else: fct.log("ERROR: node '" + node + "' is unknown") else: fct.log("ERROR: line '" + line + "' is too short") if loop_nb % 500 == 0: self.write("ping get") # fct.log("DEBUG PING to node " + node) self.ping_tx_cnt += 1 except Exception as ex: fct.log_exception(ex) self.close() self.timeout_check() loop_nb += 1 if loop_nb >= 1000000: loop_nb = 0 time.sleep(0.001)
def run(self): """ Cyclic execution to poll for received characters """ loop_nb = 1 while self.is_loop_enabled is True: try: #fct.log("DEBUG: " + self.node_name + " loop " + str(loop_nb)) if self.is_open() is False: self.open() time.sleep(1.0) if self.is_open() is True: line = "" cserial = " " read_iter_ = 0 while (len(cserial) > 0) and (self.is_loop_enabled is True): try: cserial = self.fd_port.read(1) if cserial is None: cserial = "" else: cserial = cserial.decode(encoding='utf-8', errors='ignore') if len(cserial) > 0: read_iter_ = read_iter_ + 1 if ord(cserial) == 0: cserial = "" else: cserial = "" if (self.line != "") and (cserial == "\n" or cserial == "\r"): line = self.line self.line = "" # fct.log("DEBUG New line create=" + line) break else: if (cserial != "\n") and (cserial != "\r"): self.line = self.line + cserial except Exception as ex: self.line = "" cserial = "" fct.log_exception( ex, msg="ERROR while decoding data on " + self.node_name) self.close() if read_iter_ > self.read_iter: self.read_iter = read_iter_ if line != "": if line.startswith("ZIA33"): try: resp = json.loads(line[5:]) #fct.log("DEBUG: line_array=" + str(json.dumps(resp))) if resp["frame"]["header"][ "protocolMeaning"] == "CHACON": if resp["frame"]["infos"][ "id"] == "1060074882": if resp["frame"]["infos"][ "subTypeMeaning"] == "ON": #fct.log("DEBUG: CHACON cmd ON") fct.http_request( 'http://localhost:' + str(settings.HTTPD_PORT) + '/api/ext/waterMainRelay/set/1' ) fct.http_request( 'http://localhost:' + str(settings.HTTPD_PORT) + '/api/ext/waterGardenRelay/set/1' ) elif resp["frame"]["infos"][ "subTypeMeaning"] == "OFF": #fct.log("DEBUG: CHACON cmd OFF") fct.http_request( 'http://localhost:' + str(settings.HTTPD_PORT) + '/api/ext/waterMainRelay/set/0' ) fct.http_request( 'http://localhost:' + str(settings.HTTPD_PORT) + '/api/ext/waterGardenRelay/set/0' ) else: fct.log( "WARNING: CHACON type unknown") else: fct.log("WARNING: CHACON id unknown") else: fct.log( "WARNING: RfPlayer protocol unknown") except Exception as ex: fct.log_exception(ex) else: fct.log( "WARNING: RfPlayer line does not start with 'ZIA33': " + str(line)) except Exception as ex: fct.log_exception(ex) self.close() loop_nb += 1 time.sleep(0.001)