Skip to content

Instantly share code, notes, and snippets.

@maxgibbons
Last active August 24, 2016 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxgibbons/d0d91523132040083400ed6627a6d50a to your computer and use it in GitHub Desktop.
Save maxgibbons/d0d91523132040083400ed6627a6d50a to your computer and use it in GitHub Desktop.
rest2m2x.py Additional Parser
# ---------------------------------------------------------------------------------------------------
# Temp/Humidity (ARDUINO Serial)
# This bit calls the new parser and also parses the serial payload into segments for M2X. It grabs the serial
# string from an arduino and parses it into chunks delineated by the '_' character. Then it formats/sanitizes
# the separate strings into numeric characters for M2X ingest.
# Each segment can be called by using text[0] (first segment) text[1] (second segment) and so on.
# NOTE: You can set the M2X stream data type to numeric with this parser.
# ---------------------------------------------------------------------------------------------------
elif dev_info['parser'] == 'serial_m2x': # Referencing the appropriate parser over in /parsers.py
print '\nBegin parsing ULSDU at %s with: serial_m2x, 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_m2x(payload)
# Note we do not send the test button alarm to M2x, so we only accept one message type
if msgType == 'Serial': # We're still pulling a serial-type message from Intellect
allMeasAccepted = 1 # Default means M2X accepted all measurements
for i in range(len(data)):
timestamp = convertToDateTime(rx_timestamp).strftime("%Y-%m-%dT%H:%M:%SZ") # Timestamp format m2x expects
serialDATA = hex2text(data[i]['data']) # Converting hex to ASCII.
text = serialDATA.split("_"); # Splitting ASCII string into chunks
# Replace 'temp_f' below with the name of your first M2X stream
# 're.sub("[^0-9^.]", "", text[0])' below sanitizes string for M2X by stripping out any non-numeric characters. M2X is picky.
res = sendStream2M2x(dev_info['m2x_primary_key'], dev_info['m2x_device_id'],\
'temp_f', timestamp, re.sub("[^0-9^.]", "", text[0])) # Replace 'temp_f' with name of your first M2X stream
if res == '{"status":"accepted"}':
print "OK: M2X Stream Updated on ATT Servers", 'temp_f',datetime.datetime.now(),\
'\n text = ', text[0], hex(int(node_id_hex, 16))
else:
print "ERROR: M2X Stream Failed to Update on ATT Servers reason: %s" % (res)
print data[i]['sensorName'], timestamp, data[i]['data'], text[0], hex(int(node_id_hex, 16)),\
datetime.datetime.now()
# We can't let a misconfigured device block all other UL SDUs
# so we drop this SDU and must go back and push it to M2x in the future
incrementNextUlSduPointer(nextSduFile, last_sdu_id)
allMeasAccepted = 0
# Now we run the same sequence again for our second M2X stream 'humidity'. Rinse and repeat for each additional stream you want data sent to.
res = sendStream2M2x(dev_info['m2x_primary_key'], dev_info['m2x_device_id'],\
'humidity', timestamp, re.sub("[^0-9^.]", "", text[1])) # Replace 'humidity' with name of your second M2X stream
if res == '{"status":"accepted"}':
print "OK: M2X Stream Updated on ATT Servers", 'humidity',datetime.datetime.now(),\
'\n text = ', text[1], hex(int(node_id_hex, 16))
else:
print "ERROR: M2X Stream Failed to Update on ATT Servers reason: %s" % (res)
print data[i]['sensorName'], timestamp, data[i]['data'], text[1], hex(int(node_id_hex, 16)),\
datetime.datetime.now()
# We can't let a misconfigured device block all other UL SDUs
# so we drop this SDU and must go back and push it to M2x in the future
incrementNextUlSduPointer(nextSduFile, last_sdu_id)
allMeasAccepted = 0
# We only increment next SDU if the current SDU made it to M2X completely
# or in the case of certain errors
if allMeasAccepted:
incrementNextUlSduPointer(nextSduFile, last_sdu_id)
else:
print 'INFO: msgType will not be sent to M2x: ', msgType
print 'Data: ', data
incrementNextUlSduPointer(nextSduFile, last_sdu_id) # increment if we discard and don't send to M2X
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment