예제 #1
0
def run():

  init_GPIO()
  initRadioReceive()

  conn = sqlite3.connect(DBLOCATION)
  curs = conn.cursor()

  newSensors = {}
  initSensors( newSensors ) 

  while True:
    pipe = [1]
    while( radio.available(pipe, False) ):
        recv_buffer = []
        myDateTime = datetime.utcnow().replace(tzinfo=pytz.utc);

        radio.read(recv_buffer)
	#print "[%s]" % recv_buffer


        nodeID = rfbase.getNodeIDFromPayload(recv_buffer)
	#print "Node: [%s]" % nodeID
        if( nodeID not in newSensors ):
            print "No match found for Node {0}".format(nodeID)
            continue

        n = newSensors[nodeID]
        nodeID, seq, tempList = n.parsePayload( recv_buffer )  # Get info from packet
        print "[", nodeID, "] ", seq, "-", myDateTime.astimezone(tzlocal.get_localzone()).strftime("%Y-%m-%d %H:%M:%S %Z"), ": ", tempList,  # trailing comma says no NEWLINE


        if( n.needsPublishing(myDateTime) ):
            print "- Updating"

#	    if( n.getTransmitterID() == "C3" ):
#                try:
#                    # Send proactive ping to a monitoring service
#                    url = PUSHMON_URL+PUSHMON_ID
#                    urlhandle = urllib2.urlopen(url) 
#                    urlhandle.close() 
#                except urllib2.HTTPError as e:
#                    print "PushMon returned error [", e.code, "]"
#                    # Tell an alerting service that PushMon is not accepting our Pings
#                    url = PUSHINGBOX_URL+"?devid="+PUSHINGBOX_ID+"&svc=pushmon.com"+"&rtncode="+str(e.code)
#                    urlhandle = urllib2.urlopen(url) 
#                    urlhandle.close() 
#
#                try:
#                    # Send proactive ping to StatusCake  monitoring service
#                    url = STATUSCAKE_URL+"&time="+str(int(seq)*1000)  # send the sequence number as the time param to track gaps
#                    urlhandle = urllib2.urlopen(url) 
#                    urlhandle.close() 
#                except urllib2.HTTPError as e:
#                    print "StatusCake returned error [", e.code, "]"
#                    # Tell an alerting service that StatusCake is not accepting our Pings
#                    url = PUSHINGBOX_URL+"?devid="+PUSHINGBOX_ID+"&svc=StatusCake"+"&rtncode="+str(e.code)
#                    urlhandle = urllib2.urlopen(url) 
#                    urlhandle.close() 

            numTemps = len(tempList)
            parms = n.getSensorParms()  # Get info from Sensor class
	    metricsString= ""
            for x in range(numTemps):
		_metricguid = parms[x][1]
		_metricname = parms[x][2]
		_metric     = tempList[x]
                if( tempList[x] < 900 and parms[x][0] <> "unused" ):
		    # GroveStream metric string build-up
	            metricsString += "&"+_metricguid+"="+str(tempList[x])
			
#                    try:
#                        xive.xively_update( n.getAPIKey(), n.getFeedID(), parms[x][1], parms[x][2], tempList[x], myDateTime ) 
#                    except( requests.exceptions.ConnectionError, requests.HTTPError, urllib2.URLError) as e:
#                      print "Error updating Xively with RF Data!!({0}): {1}".format(e.errno, e.strerror)

		    # SQLITE DB write
                    curs.execute("INSERT INTO rawdata (nodeid, metricid, metricguid, metricname, metric, metricdt) VALUES (?,?,?,?,?,?) ", \
                       ( nodeID, parms[x][0], parms[x][1], parms[x][2], tempList[x], myDateTime))
                    conn.commit()

	    # GroveStream push for all streams for a node (component)
	    try:
	       url = GROVESTREAMS_URL+"&seq="+str(seq)+"&compId="+n.getComponentID()+metricsString
	       urlhandle = urllib2.urlopen(url) 
	       urlhandle.close() 
	    except( requests.exceptions.ConnectionError, requests.HTTPError, urllib2.URLError) as e:
	      print "Error updating GroveStreams with RF Data!!({0}): {1}".format(e.errno, e.strerror)

	    n.markPublished(myDateTime)

        else:
            print "- TOO SOON" 



    radio.startListening()

    sys.stdout.flush()
    time.sleep(SLEEP_SECONDS)