def do_POST(self): try: length = int(self.headers['Content-length']) m2x_post_contents = self.rfile.read(length) self.send_response(200, "OK") self.end_headers() print 'm2xCallback.py: received a POST at time ', datetime.datetime.now( ), length payload = json.loads(m2x_post_contents) print payload node_id_hex = payload['device']['serial'] (match_found, dev_info) = getDeviceInfo(int(node_id_hex, 16), devices) # FUTURE IMPROVEMENT: add supported alarm types to dev_info for each device if match_found: try: # The trigger names below match what you MUST name them in m2x if payload['trigger'] == 'serial_trig': if payload['custom_data'] != None: data_string = payload['custom_data'] + payload[ 'values']['serial']['value'] else: data_string = payload['values']['serial']['value'] # Robustness, try 3 times, if needed for i in range(3): retval = sendSerialText2Rest( data_string, node_id_hex) if retval == 'OK': break else: print 'ERROR: sendSerialText2Rest cannot send to REST retval =', retval, i elif payload['trigger'] == 'intrusion_trig': subject = 'Alarm: 0x' + node_id_hex if payload['values']['intrusion']['value'] == 1: alarm_cond = 'Set' elif payload['values']['intrusion']['value'] == 0: alarm_cond = 'Cleared' message_body = 'Sensor:%s Alarm:%s Timestamp:%s' % ( payload['device']['name'], alarm_cond, payload['timestamp']) sendEmails(dev_info['alarm_email_list'], subject, message_body) except: traceback.print_exc() print 'm2xCallback.py: Exception(1): ', sys.exc_info()[0] else: print 'm2xCallback.py: no match found for node ID 0x', node_id_hex, payload except: traceback.print_exc() print 'lew 2 m2xCallback.py: Exception(2): ', sys.exc_info()[0]
def do_POST(self): try: length = int(self.headers['Content-length']) m2x_post_contents = self.rfile.read(length) self.send_response(200, "OK") self.end_headers() print 'm2xCallback.py: received a POST at time ', datetime.datetime.now(), length payload = json.loads(m2x_post_contents) print payload node_id_hex = payload['device']['serial'] (match_found, dev_info) = getDeviceInfo(int(node_id_hex, 16), devices) # FUTURE IMPROVEMENT: add supported alarm types to dev_info for each device if match_found: try: # The trigger names below match what you MUST name them in m2x if payload['trigger'] == 'serial_trig': if payload['custom_data'] != None: data_string = payload['custom_data'] + payload['values']['serial']['value'] else: data_string = payload['values']['serial']['value'] # Robustness, try 3 times, if needed for i in range (3): retval = sendSerialText2Rest(data_string, node_id_hex) if retval == 'OK': break else: print 'ERROR: sendSerialText2Rest cannot send to REST retval =', retval, i elif payload['trigger'] == 'intrusion_trig': subject = 'Alarm: 0x' + node_id_hex if payload['values']['intrusion']['value'] == 1: alarm_cond = 'Set' elif payload['values']['intrusion']['value'] == 0: alarm_cond = 'Cleared' message_body = 'Sensor:%s Alarm:%s Timestamp:%s' %(payload['device']['name'], alarm_cond, payload['timestamp']) sendEmails(dev_info['alarm_email_list'], subject, message_body) except: traceback.print_exc() print 'm2xCallback.py: Exception(1): ', sys.exc_info()[0] else: print 'm2xCallback.py: no match found for node ID 0x', node_id_hex, payload except: traceback.print_exc() print 'lew 2 m2xCallback.py: Exception(2): ', sys.exc_info()[0]
while 1: try: (status, last_sdu_id, size, sdu_list) = getNextUlSdu(nextSduFile) if status != 'OK': print 'sleep on error' time.sleep(10) # problem, wait and try again print 'done sleep on error' elif size == 0 and last_sdu_id == '': time.sleep(1) # queue is empty, attempt to get latest SDUs every 1 second elif size == 0: print 'rest2m2x.py: assuming downlinkDatagramResponse (or non-uplink mssg), incrementing nextUlSdu' incrementNextUlSduPointer(nextSduFile, last_sdu_id) else: (sdu_id, node_id_hex, rx_timestamp, payload) = getSduInfo(sdu_list) (match_found, dev_info) = getDeviceInfo(node_id_hex, devices) if match_found: # Call a parser with the payload # --------------------------------------------------------------------------------------------------- # Serial (You Must set M2X stream data type to alphanumeric when creating device in M2X) # --------------------------------------------------------------------------------------------------- if dev_info['parser'] == 'serial_1': print '\nBegin parsing ULSDU at %s with: serial_1, sdu_id %s \nnodeId %s rx_timestamp %s payload %s'\ % (datetime.datetime.now(), sdu_id, hex(int(node_id_hex, 16)), rx_timestamp, payload) (msgType, data) = parsers.parser_serial_1(payload) # Note we do not send the test button alarm to M2x, so we only accept one message type if msgType == 'Serial': allMeasAccepted = 1 # default means M2X accepted all measurements for i in range(len(data)):
while 1: try: (status, last_sdu_id, size, sdu_list) = getNextUlSdu(nextSduFile) if status != 'OK': print 'sleep on error' time.sleep(10) # problem, wait and try again print 'done sleep on error' elif size == 0 and last_sdu_id == '': time.sleep(1) # queue is empty, attempt to get latest SDUs every 1 second elif size == 0: print 'rest2console.py: assuming downlinkDatagramResponse (or non-uplink mssg), incrementing nextUlSdu' incrementNextUlSduPointer(nextSduFile, last_sdu_id) else: (sdu_id, node_id_hex, rx_timestamp, payload) = getSduInfo(sdu_list) (match_found, dev_info) = getDeviceInfo(node_id_hex, devices) # Veto match if only a specified device is called for. if match_found: if (int(options.nodeIdToDisplay, 16) == 0): pass elif int(node_id_hex, 16) != int(options.nodeIdToDisplay, 16): # Veto case only single node filter is enabled. # increment if we don't send to Console incrementNextUlSduPointer(nextSduFile, last_sdu_id) continue else: pass if match_found: # Call a parser with the payload