def testCmp(self): #"""Test Comparison function item1 = models.Node(id=1, locationId = 2, nodeTypeId = 3) item2 = models.Node(id=1, locationId = 2, nodeTypeId = 3) self.assertEqual(item1, item2) #Order on id item2.id = 0 self.assertGreater(item1, item2) self.assertNotEqual(item1, item2) item2.id = 2 self.assertLess(item1, item2)
def testEq(self): #"""Test for Equality""" item1 = models.Node(id=1, locationId = 2, nodeTypeId = 3) item2 = models.Node(id=1, locationId = 2, nodeTypeId = 3) #Now a node needs both id and location Id to be equal self.assertEqual(item1, item2) self.assertReallyEqual(item1, item2) #So this test should pass (as type Id differs) item2.nodeTypeId = 10 self.assertEqual(item1, item2) self.assertReallyEqual(item1, item2)
def testNEQ(self): item1 = models.Node(id=1, locationId = 2, nodeTypeId = 3) item2 = models.Node(id=1, locationId = 2, nodeTypeId = 3) self.assertEqual(item1, item2) #Should fail on id differing item2.id = 2 self.assertNotEqual(item1, item2) self.assertReallyNotEqual(item1, item2) #and also location Id item2.id = 1 item2.locationId = 1 self.assertNotEqual(item1, item2) self.assertReallyNotEqual(item1, item2)
def populatedata(): """Populate our testing database with an example deployment etc""" LOG = logging.getLogger(__name__) LOG.setLevel(logging.DEBUG) LOG.debug("Populating Testing Data") #The Deployment session = meta.Session() #now = datetime.datetime.now() now = datetime.datetime(2013, 01, 01, 00, 00, 00) thedeployment = models.Deployment(id=1, name="testing", description="a testing deployment", startDate=now) session.merge(thedeployment) session.flush() #We also add a testing house thehouse = models.House(id=1, address="testing house", deploymentId=thedeployment.id, startDate=now) session.merge(thehouse) session.flush() #Lets add two locations thebedroom = models.Location(id=1, houseId=thehouse.id, roomId=1) thebathroom = models.Location(id=2, houseId=thehouse.id, roomId=6) session.merge(thebedroom) session.merge(thebathroom) session.flush() #update the nodes so they have the correct locations thenode = session.query(models.Node).filter_by(id=837).first() if thenode is None: thenode = models.Node(id=837) session.add(thenode) thenode.locationId = 1 thenode = session.query(models.Node).filter_by(id=838).first() if thenode is None: thenode = models.Node(id=838) session.add(thenode) thenode.locationId = 2 session.flush() transaction.commit() #Now we want to add a load of readings thetime = now # - datetime.timedelta(days = 10) endtime = now + datetime.timedelta(days=10) #print "START TIME {0}".format(starttime) transaction.commit() #To test the Yields I also want an incomplte database thehouse = models.House(id=2, address="Poor House", deploymentId=thedeployment.id, startDate=now) session.merge(thehouse) #Add a couple of locations #Lets add two locations thebedroom = models.Location(id=3, houseId=thehouse.id, roomId=1) session.merge(thebedroom) thebathroom = models.Location(id=4, houseId=thehouse.id, roomId=6) session.merge(thebathroom) #And More Nodes thenode = session.query(models.Node).filter_by(id=1061).first() if thenode is None: thenode = models.Node(id=1061) session.add(thenode) thenode.locationId = 3 thenode = session.query(models.Node).filter_by(id=1063).first() if thenode is None: thenode = models.Node(id=1063) session.add(thenode) thenode.locationId = 4 session.flush() transaction.commit() #Add Nodestates thecount = 0.0 seqnum = -1 while thetime < endtime: #Increment and roll over the sequence number seqnum += 1 if seqnum > 255: seqnum = seqnum - 255 for nid in [837, 838, 1061, 1063]: locationid = 1 if nid == 838: locationid = 2 elif nid == 1061: locationid = 3 #Sample very 10 minutes (50% Yield) if thetime.minute % 10 == 0: continue elif nid == 1063: locationid = 4 #And remove every 3rd sample if thetime.minute % 15 == 0: continue ns = models.NodeState(nodeId=nid, parent=1, time=thetime, localtime=seqnum, seq_num=seqnum) session.add(ns) reading = models.Reading( nodeId=nid, typeId=0, time=thetime, locationId=locationid, value=18.0 + (2.0 * math.sin(thecount)), ) session.add(reading) #Increment the time thetime = thetime + datetime.timedelta(minutes=5) thecount = thecount + (3.14 / 144) transaction.commit()
def _serialobj(self): """Helper Method to provide an object to serialise""" theItem = models.Node(id=1, locationId = 2, nodeTypeId = 3) return theItem
def populatedata(session): """Populate with some testing data""" now = datetime.datetime.utcnow() startdate = now - datetime.timedelta(days=14) cutdate = now - datetime.timedelta(days=7) enddate = now - datetime.timedelta(hours=1) #Add A Deployment thedeployment = models.Deployment(id=1, name="Testing Deployments") session.add(thedeployment) session.flush() #And three servers (However only two are actually deployed) theserver = models.Server(id=1, hostname="server1", baseid=41) session.add(theserver) theserver = models.Server(id=2, hostname="server2", baseid=42) session.add(theserver) theserver = models.Server(id=3, hostname="server3", baseid=44) session.add(theserver) session.flush() #Add some houses thehouse = models.House(id=1, address="address1", deploymentId=1, serverid=1) session.add(thehouse) thehouse = models.House(id=2, address="address2", deploymentId=1, serverid=1) session.add(thehouse) thehouse = models.House(id=3, address="address3", deploymentId=1, serverid=1) session.add(thehouse) thehouse = models.House(id=4, address="address4", deploymentId=1, serverid=2) session.add(thehouse) #House that is out of date thehouse = models.House(id=5, address="address5", deploymentId=1, serverid=2) session.add(thehouse) #House missing one node thehouse = models.House(id=6, address="address6", deploymentId=1) session.add(thehouse) #Next nodes and locations (two for each house) locidx = 1 allnodes = [] for house in range(5): for room in range(2): thisloc = models.Location(id=locidx, houseId=house + 1, roomId=room + 1) session.add(thisloc) nid = house * 10 + room thisnode = models.Node(id=nid, locationId=locidx) session.add(thisnode) allnodes.append(nid) session.flush() locidx += 1 nidx = 1000 for x in range(2): thisnode = models.Node(id=nidx) session.add(thisnode) nidx += 1 session.flush() session.commit() #STUFF FOR PUSH STATUS currenttime = startdate while currenttime <= cutdate: for server in ["server1", "server2", "server3"]: pstat = models.PushStatus(time=currenttime, hostname=server) session.add(pstat) currenttime += datetime.timedelta(hours=2) session.flush() session.commit() while currenttime <= enddate: for server in ["server1", "server2"]: pstat = models.PushStatus(time=currenttime, hostname=server) session.add(pstat) currenttime += datetime.timedelta(hours=2) #Add readings / nodestates every two hours for two weeks #But stop 3 days ago currenttime = startdate seq_num = 1 while currenttime <= cutdate: for nid in allnodes: thereading = models.Reading(time=currenttime, nodeId=nid, typeId=0, value=0) thestate = models.NodeState(time=currenttime, nodeId=nid, seq_num=seq_num) session.add(thereading) session.add(thestate) session.flush() currenttime = currenttime + datetime.timedelta(hours=2) seq_num += 1 session.flush() session.commit() logging.debug("ALL NODES {0}".format(allnodes)) #And then for all but the nodes assocated with house 1 while currenttime <= enddate: for nid in allnodes[3:]: thereading = models.Reading(time=currenttime, nodeId=nid, typeId=0, value=0) thestate = models.NodeState(time=currenttime, nodeId=nid, seq_num=seq_num) session.add(thereading) session.add(thestate) session.flush() currenttime = currenttime + datetime.timedelta(hours=2) seq_num += 1 session.flush() session.commit() #STUFF FOR PUSLE COUNT currenttime = startdate pcount = 0 qry = session.query(models.SensorType).filter_by(name="Gas Pulse Count") gas_sensor = qry.first() while currenttime <= cutdate: #Let node 10, 11 work thereading = models.Reading(time=currenttime, nodeId=40, typeId=gas_sensor.id, value=pcount) session.add(thereading) thereading = models.Reading(time=currenttime, nodeId=41, typeId=gas_sensor.id, value=pcount) session.add(thereading) pcount += 1 currenttime += datetime.timedelta(hours=2) while currenttime <= enddate: thereading = models.Reading(time=currenttime, nodeId=40, typeId=gas_sensor.id, value=pcount) session.add(thereading) thereading = models.Reading(time=currenttime, nodeId=41, typeId=gas_sensor.id, value=100) session.add(thereading) pcount += 1 currenttime += datetime.timedelta(hours=2) session.flush() session.commit()
def run(self): """Single iteration of the mainloop""" #Wait for print "Waiting on Data" data = self.con.readline() log = self.log print "Recv ", data if data: session = self.Session() log.debug("Pkt Received> {0}".format(data.strip())) if "FOO:" in data: now = datetime.datetime.utcnow() pktdata = data[4:].strip().replace("'", "\"") #Json only likes "'s js = json.loads(pktdata) #First Part is to update the Node / Node State nodeid = js["nodeid"] msg_seq = js["msg_seq"] localtime = js["localtime"] parent = js["parent"] qry = session.query(models.Node).filter_by(id=nodeid) thenode = qry.first() if thenode is None: thenode = models.Node(id=nodeid) session.add(thenode) session.flush() #Check for duplicate packet pktlist = self.recentpackets.get(nodeid, SequenceList()) prev = pktlist.contains(msg_seq) if prev: log.warning("Duplicate Packet Detected") return else: pktlist.add(msg_seq) self.recentpackets[nodeid] = pktlist #Then we can create a nodestate ns = models.NodeState(time=now, nodeId=nodeid, localtime=localtime, seq_num=msg_seq, parent=parent) session.add(ns) # ------------- WORK THROUGH READINGS ------------------ # == Stuff that needs converting == val = js.get("temp", None) #Falls over on return of 0 if val: val = self._convert_temp(val) rdg = models.Reading(time=now, nodeId=nodeid, typeId=0, locationId=thenode.locationId, value=val) log.debug("Temperature {0}".format(rdg)) session.add(rdg) val = js.get("hum", None) if val: val = self._convert_hum(val) rdg = models.Reading(time=now, nodeId=nodeid, typeId=2, locationId=thenode.locationId, value=val) #log.debug("Humidity {0}".format(rdg)) session.add(rdg) val = js.get("battery", None) if val: val = self._convert_batt(val) rdg = models.Reading(time=now, nodeId=nodeid, typeId=6, locationId=thenode.locationId, value=val) #log.debug("Battery {0}".format(rdg)) session.add(rdg) # ======== AND SOME AUTOMAGIC CONVERTIONS for item in READLIST: val = js.get(item[0], None) #log.debug("Getting item {0} = {1}".format(item,val)) if val is None: log.warning("No Such Item") continue rdg = models.Reading(time=now, nodeId=nodeid, typeId=item[1], locationId=thenode.locationId, value=val) #log.debug("Reading {0}".format(rdg)) session.add(rdg) session.flush() #Finally deal with any neighbors nb = js.get("neighbors", None) if nb: for idx, item in enumerate(nb): val = item["address"] rdg = models.Reading(time=now, nodeId=nodeid, typeId=2000 + idx, locationId=thenode.locationId, value=val) session.add(rdg) session.flush() try: session.flush() session.commit() except sqlalchemy.exc.IntegrityError, e: log.warning("Error saving item {0}".format(e)) session.close()