<filename>berkeley/acme/tools/readACmeX2UDPSQL.py

import socket
import ACmeX2Report
import re
import sys
import MySQLdb

#host = 'fc00:e968:6179::de52:7100'
#host = 'fd00:c2b6:b24b:be67:2827:688d:e6a1:6a3b'
host = ''
port = 7001

if __name__ == '__main__':
    
    conn = MySQLdb.connect (host = "172.16.17.32",
                            user = "acme",
                            db = "acmex2",
                            passwd = "<PASSWORD>")

    cursor = conn.cursor();

    s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
    s.bind((host, port))

    while True:
        data, addr = s.recvfrom(1024)
        if (len(data) > 0):
            rpt = ACmeX2Report.AcReport(data=data, data_length=len(data))
            #myaddr = str(addr[0]).split(":")
            #moteid = int(myaddr[5],16)
            myaddr = addr[0].split("::")
            moteid = int(myaddr[1],16)
            averagePower = rpt.get_averagePower();
            apparentPower = rpt.get_apparentPower();
            cumulativeEnergy = rpt.get_cumulativeEnergy();
            maxPower = rpt.get_maxPower();
            minPower = rpt.get_minPower();
            seq = rpt.get_seq();

            try:
                hop_limit = rpt.get_route_hop_limit();
                parent = rpt.get_route_parent();
                parent_metric = rpt.get_route_parent_metric();
                parent_etx = rpt.get_route_parent_etx();
            except:
                parent = -1
                hop_limit = -1
                parent_metric = -1
                parent_etx = -1
                

#            print rpt

            
            print "moteid=%d, seq=%d, averagePower=%d, cumulativeEnergy=%ld" % (moteid, seq, averagePower, cumulativeEnergy)

#            print energy
#            print parent
#            print totalEnergy


            insert = "INSERT INTO energy VALUES ("
            insert += str(moteid) + "," + "NOW()," + str(seq) + ","
            insert += str(averagePower) + "," + str(apparentPower) + ","
            insert += str(maxPower) + "," + str(minPower) + ","
            insert += str(cumulativeEnergy)
            insert += ");"

            print insert
            try:
                cursor.execute(insert)
            except cursor.Error, e:
                # print "Data duplicate detected"
                print "Error %d: %s" % (e.args[0], e.args[1])

            debug = "INSERT INTO debug VALUES ("
            debug += str(moteid) + "," + "NOW()," + str(seq) + ","
            debug += str(parent) + "," + str(parent_metric) + ","
            debug += str(parent_etx) + "," + str(hop_limit) + ","
            debug += "0);"

            print debug

            try:
              cursor.execute(debug)
            except cursor.Error, e:
                # print "Data duplicate detected"
                print "Error %d: %s" % (e.args[0], e.args[1])

    conn.close()


<filename>berkeley/acme/tools/readACmeX2UDPSQL.py

import socket
import ACmeX2Report
import re
import sys
import MySQLdb

#host = 'fc00:e968:6179::de52:7100'
#host = 'fd00:c2b6:b24b:be67:2827:688d:e6a1:6a3b'
host = ''
port = 7001

if __name__ == '__main__':
    
    conn = MySQLdb.connect (host = "172.16.17.32",
                            user = "acme",
                            db = "acmex2",
                            passwd = "<PASSWORD>")

    cursor = conn.cursor();

    s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
    s.bind((host, port))

    while True:
        data, addr = s.recvfrom(1024)
        if (len(data) > 0):
            rpt = ACmeX2Report.AcReport(data=data, data_length=len(data))
            #myaddr = str(addr[0]).split(":")
            #moteid = int(myaddr[5],16)
            myaddr = addr[0].split("::")
            moteid = int(myaddr[1],16)
            averagePower = rpt.get_averagePower();
            apparentPower = rpt.get_apparentPower();
            cumulativeEnergy = rpt.get_cumulativeEnergy();
            maxPower = rpt.get_maxPower();
            minPower = rpt.get_minPower();
            seq = rpt.get_seq();

            try:
                hop_limit = rpt.get_route_hop_limit();
                parent = rpt.get_route_parent();
                parent_metric = rpt.get_route_parent_metric();
                parent_etx = rpt.get_route_parent_etx();
            except:
                parent = -1
                hop_limit = -1
                parent_metric = -1
                parent_etx = -1
                

#            print rpt

            
            print "moteid=%d, seq=%d, averagePower=%d, cumulativeEnergy=%ld" % (moteid, seq, averagePower, cumulativeEnergy)

#            print energy
#            print parent
#            print totalEnergy


            insert = "INSERT INTO energy VALUES ("
            insert += str(moteid) + "," + "NOW()," + str(seq) + ","
            insert += str(averagePower) + "," + str(apparentPower) + ","
            insert += str(maxPower) + "," + str(minPower) + ","
            insert += str(cumulativeEnergy)
            insert += ");"

            print insert
            try:
                cursor.execute(insert)
            except cursor.Error, e:
                # print "Data duplicate detected"
                print "Error %d: %s" % (e.args[0], e.args[1])

            debug = "INSERT INTO debug VALUES ("
            debug += str(moteid) + "," + "NOW()," + str(seq) + ","
            debug += str(parent) + "," + str(parent_metric) + ","
            debug += str(parent_etx) + "," + str(hop_limit) + ","
            debug += "0);"

            print debug

            try:
              cursor.execute(debug)
            except cursor.Error, e:
                # print "Data duplicate detected"
                print "Error %d: %s" % (e.args[0], e.args[1])

    conn.close()


#host = 'fc00:e968:6179::de52:7100' #host = 'fd00:c2b6:b24b:be67:2827:688d:e6a1:6a3b' #myaddr = str(addr[0]).split(":") #moteid = int(myaddr[5],16) #            print rpt #            print energy #            print parent #            print totalEnergy # print "Data duplicate detected" # print "Data duplicate detected"