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 testCmp(self): """Test Comparison function (actually __lt__ for Py3K Comat)""" item1 = models.Node(id=1, locationId=2, nodeTypeId=3) item2 = models.Node(id=1, locationId=2, nodeTypeId=3) self.assertEqual(item1, item2) #Order on HouseId item2.id = 0 self.assertGreater(item1, item2) item2.id = 2 self.assertLess(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 setUp(self): #Clean up the database before each run session = self.Session() session.execute("DELETE FROM Node") session.execute("DELETE FROM NodeState") session.execute("DELETE FROM Reading") session.flush() thenode = models.Node(id=4242) session.add(thenode) session.commit()
def saveData(self, nodeId, values): """Save a reading in the Database :var nodeId: String Containing the Current Cost Node Id :var values: Tuple containing sensor values as returned by ploggParseValue """ log = self.log log.debug("Saving data for {0} {1}".format(nodeId, values)) session = meta.Session() mappedId = NODEMAP[nodeId] theNode = session.query(models.Node).filter_by(id=mappedId).first() #Fetch Sensor Types wattSensor = session.query( models.SensorType).filter_by(name="Plogg Watts").first() log.debug("Watt Sensor {0}".format(wattSensor)) kWhSensor = session.query( models.SensorType).filter_by(name="Plogg kWh").first() log.debug("kW Sensor {0}".format(kWhSensor)) currentSensor = session.query( models.SensorType).filter_by(name="Plogg Current").first() log.debug("A Sensor {0}".format(currentSensor)) #Create if it doesnt Exist if not theNode: log.info("Node {0} / {1} does not exist, Creating".format( nodeId, mappedId)) theNode = models.Node(id=mappedId, locationId=None) session.add(theNode) session.flush() log.debug("Node is {0}".format(theNode)) #And we need to add a set of sensors for item in [wattSensor, kWhSensor, currentSensor]: theSensor = models.Sensor(sensorTypeId=item.id, nodeId=theNode.id, calibrationSlope=1.0, calibrationOffset=0.0) session.add(theSensor) session.flush() sampleTime, sampleWatts, samplekWh, sampleCurrent = values #Then Add the Readings theReading = models.Reading(time=sampleTime, nodeId=theNode.id, location=theNode.locationId, typeId=wattSensor.id, value=sampleWatts) session.add(theReading) theReading = models.Reading(time=sampleTime, nodeId=theNode.id, location=theNode.locationId, typeId=kWhSensor.id, value=samplekWh) session.add(theReading) theReading = models.Reading(time=sampleTime, nodeId=theNode.id, location=theNode.locationId, typeId=currentSensor.id, value=sampleCurrent) session.add(theReading) #And add a nodeState theNodeState = models.NodeState(time=sampleTime, nodeId=theNode.id, parent=theNode.id, localtime=sampleTime) session.add(theNodeState) session.flush() session.commit() session.close()
def __init__(self): log.debug("Initialising Push Script") engine = sqlalchemy.create_engine(LOCAL_URL) #Intialise the databse models.initialise_sql(engine) models.populate_data() #Get relevant models etc session = meta.Session() #Check if our data exists theDeployment = session.query( models.Deployment).filter_by(name="PushTest").first() if theDeployment is None: log.debug("--> Create New Deployment") #Create a new Deployment theDeployment = models.Deployment(name="PushTest") session.add(theDeployment) session.flush() log.debug("Deployment is {0}".format(theDeployment)) theHouse = session.query( models.House).filter_by(address="Push Address").first() if theHouse is None: log.debug("--> Create new House") theHouse = models.House(address="Push Address", deploymentId=theDeployment.id) session.add(theHouse) session.flush() log.debug("House is {0}".format(theHouse)) #Two Rooms roomType = session.query( models.RoomType).filter_by(name="Bedroom").first() masterBed = session.query( models.Room).filter_by(name="Master Bedroom").first() if masterBed is None: masterBed = models.Room(name="Master Bedroom", roomTypeId=roomType.id) session.add(masterBed) secondBed = session.query( models.Room).filter_by(name="Second Bedroom").first() if secondBed is None: secondBed = models.Room(name="Second Bedroom", roomTypeId=roomType.id) session.add(secondBed) session.flush() #Locations masterLoc = session.query(models.Location).filter_by( houseId=theHouse.id, roomId=masterBed.id).first() if masterLoc is None: log.debug("Create New Master Location") masterLoc = models.Location(houseId=theHouse.id, roomId=masterBed.id) session.add(masterLoc) secondLoc = session.query(models.Location).filter_by( houseId=theHouse.id, roomId=secondBed.id).first() if secondLoc is None: log.debug("Create New Second Location") secondLoc = models.Location(houseId=theHouse.id, roomId=secondBed.id) session.add(secondLoc) session.flush() log.debug("Master Location {0}".format(masterLoc)) log.debug("Second Location {0}".format(secondLoc)) #Add Nodes to each Location node37 = session.query(models.Node).filter_by(id=37).first() if not node37: node37 = models.Node(id=37) session.add(node37) node38 = session.query(models.Node).filter_by(id=38).first() if node38 is None: node38 = models.Node(id=38) session.add(node38) node37.location = masterLoc node38.location = secondLoc session.flush() #All Our Data adding script needs to worry about is the Nodes self.node37 = node37 self.node38 = node38 #Finally add an Upload URL if required # theUrl = session.query(models.UploadURL).filter_by(url="[email protected]").first() # if not theUrl: # theUrl = models.UploadURL(url="[email protected]", # dburl="mysql://*****:*****@localhost:3307/pushTest") # session.add(theUrl) # theUrl = session.query(models.UploadURL).filter_by(url="*****@*****.**").first() # if not theUrl: # theUrl = models.UploadURL(url="*****@*****.**", # dburl="mysql://*****:*****@localhost:3307/chtest") # session.add(theUrl) session.commit()
def populatedata(session = None): """Populate our testing database with an example deployment etc""" #The Deployment if not session: print "Creating a new Session" session = meta.Session() #Remove Existing nodes as they just confuse things qry = session.query(models.Node) qry.delete() session.flush() session.commit() transaction.commit() #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 want a server theserver = models.Server(id=1, hostname = "Testing Server") session.merge(theserver) session.flush() #We also add a testing house thehouse = models.House(id=1, address="testing house", deploymentId = thedeployment.id, startDate = now, serverid = 1) 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() session.commit() #update the nodes so they have the correct locations thenode = session.query(models.Node).filter_by(id=837).first() if thenode is None: print "Create Node 837" thenode = models.Node(id=837, locationId=1) session.add(thenode) thenode = session.query(models.Node).filter_by(id=838).first() if thenode is None: thenode = models.Node(id=838, locationId=2) print "Create Node 838" session.add(thenode) session.flush() transaction.commit() session.commit() #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, serverid=1) 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) session.flush() session.commit() transaction.commit() thenode = session.query(models.Node).filter_by(id=1061).first() if thenode is None: thenode = models.Node(id=1061, locationId=3) print "Create Node 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, locationId=4) print "Create Node 1063" session.add(thenode) session.flush() session.commit()
def _serialobj(self): """Helper Method to provide an object to serialise""" theItem = models.Node(id=1, locationId=2, nodeTypeId=3) return theItem
def run(self): """Single iteration of the mainloop""" #Wait for data = self.con.readline() log = self.log if data: session = meta.Session() log.debug("> {0}".format(data.strip())) if "PKT:" in data: now = datetime.datetime.now() pktdata = data.strip().split(":") #Get the main packet data pktitems = [int(x) for x in pktdata[1].split(",")] log.debug(">>PKT. {0}".format(pktitems)) (nodeid, time, ctp_seq, hops, tx_pwr, msg_seq, parent, n_count, temp, hum) = pktitems #Temperature / Humidity conversion temp = float(temp) temp = -39.6 + 0.01 * temp hum = float(hum) hum = -4 + 0.0405 * hum - 0.0000028 * (hum * hum) qry = session.query(models.Node).filter_by(id=nodeid) thenode = qry.first() if thenode is None: log.info("No such node {0}".format(nodeid)) thenode = models.Node(id=nodeid) session.add(thenode) session.flush() #Then we can create a nodestate ns = models.NodeState(time=now, nodeId=nodeid, localtime=time, seq_num=msg_seq, parent=parent) session.add(ns) #And Readings rdg = models.Reading(time=now, nodeId=nodeid, typeId=HOPS, locationId=thenode.locationId, value=hops) session.add(rdg) rdg = models.Reading(time=now, nodeId=nodeid, typeId=TX_PWR, locationId=thenode.locationId, value=tx_pwr) session.add(rdg) rdg = models.Reading(time=now, nodeId=nodeid, typeId=N_COUNT, locationId=thenode.locationId, value=n_count) session.add(rdg) rdg = models.Reading(time=now, nodeId=nodeid, typeId=CTP_SEQ, locationId=thenode.locationId, value=ctp_seq) session.add(rdg) #Temperature rdg = models.Reading(time=now, nodeId=nodeid, typeId=0, locationId=thenode.locationId, value=temp) session.add(rdg) rdg = models.Reading(time=now, nodeId=nodeid, typeId=2, locationId=thenode.locationId, value=hum) session.add(rdg) session.commit() #Now neighbor table info print pktdata if len(pktdata) > 2: neighinfo = pktdata[2:] log.info("Neighbor Table is {0}".format(neighinfo)) for idx, item in enumerate(neighinfo): print item, idx vals = item.split(",") rdg = models.Reading(time=now, nodeId=nodeid, typeId=2000 + idx, locationId=thenode.locationId, value=vals[0]) session.add(rdg) session.commit()
def download_house(self, hstr): screen = self.screen subwin = self.subwin session = self.mergesession() subwin.erase() subwin.box() subwin.addstr(1, 1, "Mirror House to server".format(hstr)) subwin.hline(2, 1, curses.ACS_HLINE, 77) #Fetch this particular house theurl = "{0}house/{1}".format(self.resturl, hstr) therequest = requests.get(theurl) jsonhouse = therequest.json() if len(jsonhouse) == 0: subwin.addstr(5, 1, "NO SUCH HOUSE") subwin.addstr(20, 1, "Any key to exit") subwin.refresh() event = screen.getch() return else: thehouse = models.clsFromJSON(jsonhouse).next() originalid = thehouse.id qry = session.query( models.House).filter_by(address=thehouse.address).first() if qry is None: matchstr = "No Match" thehouse.id = None session.add(thehouse) session.flush() else: #matchstr = "({0}) {1}".format(qry.id, qry.address) thehouse = qry #Fetch Relevant deployment (Which should have been synched allready) if thehouse.deploymentId is None: subwin.addstr(5, 1, "Deployment: None") else: theurl = "{0}deployment/{1}".format(self.resturl, thehouse.deploymentId) therequest = requests.get(theurl) jsondep = therequest.json() thedeployment = models.clsFromJSON(jsondep).next() #Check mappings for this qry = session.query( models.Deployment).filter_by(name=thedeployment.name).first() if qry is None: #matchstr = "No Match" thedeployment.id = None session.add(thedeployment) session.flush() thehouse.deploymentId = thedeployment.id else: #matchstr = "({0}) {1}".format(qry.id, qry.name) thedeployment = qry thehouse.deploymentId = qry.id #And Update our House / Deployment Strings session.flush() subwin.addstr( 4, 1, "House: {0} (Local) {1} {2} {3}".format(str(thehouse.address), thehouse.id, thehouse.address, thehouse.deploymentId)) if thehouse.deploymentId is not None: subwin.addstr( 5, 1, "Deployment: {0} (Local) {1} {2}".format( thedeployment.name, thedeployment.id, thedeployment.name)) #Fetch Locations params = urllib.urlencode({"houseId": originalid}) houseurl = "{0}location/?{1}".format(self.resturl, params) restqry = requests.get(houseurl) jsonstr = restqry.json() locations = models.clsFromJSON(jsonstr) roomurl = "{0}room/".format(self.resturl) restqry = requests.get(roomurl) jsonstr = restqry.json() ritr = models.clsFromJSON(jsonstr) rooms = {} for item in ritr: rooms[item.id] = item locidx = 7 mappedlocs = {} #And a place for Nodes #nodelist = [] for item in locations: roomqry = session.query( models.Room).filter_by(name=rooms[item.roomId].name).first() if roomqry is None: print "NEW ROOM {0}".format(item) #Add a Room (But these should have been synched) locqry = session.query(models.Location).filter_by( houseId=thehouse.id, roomId=roomqry.id).first() #outstr = "{0} : {1} : {2}".format(item, roomqry, locqry) if locqry is None: mappedloc = models.Location(houseId=thehouse.id, roomId=roomqry.id) session.add(mappedloc) session.flush() locstr = "NEW location: {0}".format(mappedloc) else: locstr = "Existing location: {0}".format(locqry) mappedloc = locqry #Work out nodes params = urllib.urlencode({"locationId": item.id}) nodeurl = "{0}node/?{1}".format(self.resturl, params) nodeqry = requests.get(nodeurl) nodestr = nodeqry.json() nodes = list(models.clsFromJSON(nodestr)) for node in nodes: #Just Create this node nodeqry = session.query( models.Node).filter_by(id=node.id).first() if nodeqry is None: thenode = models.Node(id=node.id, locationId=mappedloc.id) session.add(thenode) session.flush() outstr = "Create node {0} at {1} ({2})".format( node.id, locstr, rooms[item.roomId].name) else: outstr = "Update Node {0} to {1} ({2})".format( node.id, locstr, rooms[item.roomId].name) #outstr = "Node {0} : {1}".format(node.id, outstr) nodeqry.locationId = mappedloc.id subwin.addstr(locidx, 1, outstr) locidx += 1 session.flush() subwin.addstr(20, 1, "Confirm (Y)es (N)o") subwin.refresh() screen.refresh() while True: event = screen.getch() if event == ord("y"): print "SAVING" session.flush() session.commit() return elif event == ord("n"): print "EXITING" return