def main(): #Checks to see if openpyxl is installed try: import openpyxl except ImportError as iE: print("Python Excel module 'openpyxl' not found") sys.exit(1) # PPrint is used for interal testing output formatting pp = pprint.PrettyPrinter(indent=4) # wb = openpyxl.Workbook() # fileName = input("Name File: \n") # ws = wb.active # numOfHouses = inputLogic.intValidation(input("Enter Number of Houses on Farm: \n")) #Connects to a mongo DB client = pymongo.MongoClient() db = client.chicken_db_test farmName = str(input("Enter Name of Farm:\n")) collection = db[farmName] houseObjs = [] #List for the House objects in OOP form try: house = models.House() print("Post mongo format: \n") cursor = db.collection.find({"farmName": farmName}) for key in cursor: house.convertToHouse(key) houseObjs.append(house) #Adds house obj to list pp.pprint(house.__dict__) print("NumBirds: {0} \n".format(house.numBirds)) F_ROI = algorithms.calc_F_ROI(house.numBirds) print("Last Fill of Hop1: {} ".format(house.hop1[-1])) # print(key["hop2"][-1]) h1_F_Rem = algorithms.calc_TotalF_Rem((house.hop1), F_ROI) # h2_F_Rem=algorithms.calc_F_Rem((key ["hop2"]), F_ROI) print("Feed Rate of Ingestion: {0:.2f} Lb's/hr \n".format(F_ROI)) print( "Estimated Feed Remaining in Hop 1 after last fill: {0:.2f} Lb's \n" .format(h1_F_Rem)) # print("Feed Remaining in Hop 2: {0:.2f} Lb's \n".format(h2_F_Rem, F_ROI)) TTE_h1 = algorithms.calc_TTE(h1_F_Rem, F_ROI) print("Time Till Hop1 is empty: {0:.2f} Hr's".format(TTE_h1)) E_EDT_h1 = algorithms.calc_E_EDT(TTE_h1) print("Estimated date Hop1 will be empty: {0}".format(E_EDT_h1)) hr_OD = algorithms.calc_hr_OD(house.dateTimeDict, E_EDT_h1) print("Number of hours over, or under extraction date/time: {}". format(hr_OD)) E_FRet = algorithms.calc_E_FRet(hr_OD, F_ROI) print( "Amount of feed left by flock extraction: {0:.3f} Lb's".format( E_FRet)) except Exception as e: print("Unexpected error: \n") print(e) print(traceback.format_exc()) raise
def main(): #Checks to see if openpyxl is installed try: import openpyxl except ImportError as iE: print("Python Excel module 'openpyxl' not found") sys.exit(1) pp = pprint.PrettyPrinter(indent=4) wb = openpyxl.Workbook() fileName = input("Name File: \n") ws = wb.active ws.title = input("Enter Name of First Farm:\n") numOfHouses = inputLogic.intValidation(input("Enter Number of Houses on Farm: \n")) #Creates mongo DB client = pymongo.MongoClient() db = client.chicken_db_test collection = db[str(ws.title)] #Lists for storing house OBJ and Dict houses = [] houseObjs = [] #List for the House objects in OOP form try: if numOfHouses is None: sys.exit(0)#Testing for i in range(int(numOfHouses)): # houses.append(inputLogic.houseInput(house)) # print(houses[i].dateTimeRet(), sep="\n") # houses.append(inputLogic.testData(house)) house = models.House() house.farmName = str(ws.title) house = inputLogic.testData(house) houseObjs.append(house) #Adds house obj to list print("Pre-mongo format: \n") houseDict = house.connectTime() pp.pprint(houseDict) # houses.append(houseDict)#Adds houseDict object to list for DB insertion # pp.pprint(db.collection.find_one({'farmName': house.farmName})) # Converts House model to Mongo Model print("Mongo House Model: \n") m_House = models.MongoHouse() m_House.convertToMongo(house) houses.append(m_House.__dict__) pp.pprint(m_House.__dict__) # print("Post mongo format: \n") db.collection.ensure_index('farmName') db.collection.create_index([('dateTimeDict.TimeIn', 1), ('dateTimeDict.TimeOut', 1)]) db.collection.insert_many(houses) except Exception as e: print("Unexpected error: \n") print(e) print(traceback.format_exc()) raise
def test_house_insert(db): test_house = { "name": "TestHouse", "region": "Norf", "words": "DA KING OF DA NORF", "current_lord": "TestLord", "title": "TestTitle", "overlord": "TestOverLord", "imageLink": "-" } h = models.House(**test_house) db.session.add(h) result = db.session.query( models.House).filter(models.House.name == "TestHouse").first() TESTOR.assertEqual(result.region, "Norf") TESTOR.assertEqual(result.words, "DA KING OF DA NORF") TESTOR.assertEqual(result.current_lord, "TestLord") TESTOR.assertEqual(result.title, "TestTitle") TESTOR.assertEqual(result.overlord, "TestOverLord")
def createDatabase(self): """Create the Required Database Objects""" session = models.meta.Session() arSession = self.Session() #log.setLevel(logging.WARNING) deploymentName = "archRock" houseName = self.houseAddress theDep = session.query(models.Deployment).filter_by(name=deploymentName).first() log.debug("Checking for existing deployment {0}".format(theDep)) if theDep is None: #Create a new deployment theDep = models.Deployment(name=deploymentName) session.add(theDep) session.flush() log.debug("Adding Deployment to database {0}".format(theDep)) #And check for Houses theHouse = session.query(models.House).filter_by(address=houseName).first() log.debug("Checking for house {0}".format(theHouse)) if theHouse is None: theHouse = models.House(address=houseName, deploymentId = theDep.id) session.add(theHouse) session.flush() log.debug("Adding New House {0}".format(theHouse)) self.theHouse = theHouse emptyRoom = session.query(models.RoomType).filter_by(name="Unocupied").first() nodeMap = {} #Temp storage for <ADDDR> <Node> addrMap = {} #We want to setup nodes / Rooms / Locations based on the node_dimension table log.info("Setting Up Nodes") nodeQry = arSession.query(Ar_Node) for item in nodeQry: #Dont bother with the router if item.name == "archrock-router": continue #Check / Create a node if required nodeId = int(item.addr[8:],16) log.debug("{0} {1} {2}".format(item,item.addr,nodeId)) #nodeId = BASE_NODE_ID + item.short_addr theNode = session.query(models.Node).filter_by(id = nodeId).first() if theNode is None: theNode = models.Node(id=nodeId) session.add(theNode) session.flush() log.debug("Creating New Node {0}".format(theNode)) #Next we create a room / Location roomName = item.name if not roomName == "": log.debug("Room Name is {0}".format(roomName)) theRoom = session.query(models.Room).filter_by(name=roomName).first() if theRoom is None: theRoom = models.Room(name=roomName, roomTypeId=emptyRoom.id) log.debug("Creating Room {0}".format(theRoom)) session.add(theRoom) session.flush() #And now we can turn this room into a Location theLocation = session.query(models.Location).filter_by(houseId=theHouse.id, roomId = theRoom.id).first() if theLocation is None: theLocation = models.Location(houseId = theHouse.id, roomId = theRoom.id) session.add(theLocation) log.debug("Creating Location {0}".format(theLocation)) session.flush() #Last thing we create a mapping between the node and the Location nodeMap[item.node_key] = [theNode,theLocation] addrMap[item.addr] = theNode #log.debug(nodeMap) self.nodeMap = nodeMap #We also need to do mapping for sensor types etc #theQry = arSession.query(Source) #Map the Types we are expecting to types from the database sensorTypeMap = {} log.info("Mapping Sensor Types") for sType in ALLOWED_TYPES: theType = session.query(models.SensorType).filter_by(name=sType[1]).first() log.debug("AR: {0} Local {1}".format(sType,theType)) sensorTypeMap[sType[0]] = theType #log.debug(sensorTypeMap) sensorMap = {} sQry = arSession.query(Source) for item in sQry: thisItem = sensorTypeMap.get(item.source,None) if thisItem: sensorMap[item.datasource_key] = sensorTypeMap[item.source] self.sensorMap = sensorMap log.setLevel(logging.DEBUG) log.info("Commtitting Changes") session.flush() session.commit()
def createDeployment(self): """ Create a new deployment and house etc on the Transfer Database """ deploymentName = "archRock" houseName = self.houseName session = models.meta.Session() #Check for deployment theDep = session.query(models.Deployment).filter_by(name=deploymentName).first() log.debug("Checking for existing deployment {0}".format(theDep)) if theDep is None: #Create a new deployment theDep = models.Deployment(name=deploymentName) session.add(theDep) session.flush() log.debug("Adding Deployment to database {0}".format(theDep)) #And check for Houses theHouse = session.query(models.House).filter_by(address=houseName).first() log.debug("Checking for house {0}".format(theHouse)) if theHouse is None: theHouse = models.House(address=houseName, deploymentId = theDep.id) session.add(theHouse) session.flush() log.debug("Adding New House {0}".format(theHouse)) self.theHouse = theHouse #Create a location for this particular node theRoom = session.query(models.Room).filter_by(name="External").first() if theRoom is None: theRoom = models.Room(name="External",roomTypeId=1) session.add(theRoom) session.flush() log.debug("External Room is {0}".format(theRoom)) #theLocation = models.Location(houseId = theHouse.id,roomId = theRoom.id) theLocation = session.query(models.Location).filter_by(houseId=theHouse.id, roomId = theRoom.id).first() log.debug("Checking for existing location {0}".format(theLocation)) if theLocation is None: theLocation = models.Location(houseId = theHouse.id, roomId = theRoom.id) session.add(theLocation) session.flush() self.theLocation = theLocation #Node / Node Type theNode = session.query(models.Node).filter_by(id=118118).first() if theNode is None: theNode = models.Node(id=118118) session.add(theNode) session.flush() log.debug("Node is {0}".format(theNode)) self.theNode = theNode sensorType = session.query(models.SensorType).filter_by(name="Power").first() self.avgType = sensorType log.debug("Sensor is {0}".format(sensorType)) sensorType = session.query(models.SensorType).filter_by(name="Power Min").first() self.minType = sensorType sensorType = session.query(models.SensorType).filter_by(name="Power Max").first() self.maxType = sensorType session.commit()