def comp_P(adc_pres): global flo pressure = 0.0 v1 = (flo / 2.0) - 64000.0 v2 = (((v1 / 4.0) * (v1 / 4.0)) / 2048) * dP[5] v2 = v2 + ((v1 * dP[4]) * 2.0) v2 = (v2 / 4.0) + (dP[3] * 65536.0) v1 = (((dP[2] * (((v1 / 4.0) * (v1 / 4.0)) / 8192)) / 8) + ((dP[1] * v1) / 2.0)) / 262144 v1 = ((32768 + v1) * dP[0]) / 32768 if v1 == 0: return 0 pressure = ((1048576 - adc_pres) - (v2 / 4096)) * 3125 if pressure < 0x80000000: pressure = (pressure * 2.0) / v1 else: pressure = (pressure / v1) * 2 v1 = (dP[8] * (((pressure / 8.0) * (pressure / 8.0)) / 8192.0)) / 4096 v2 = ((pressure / 4.0) * dP[7]) / 8192.0 pressure = pressure + ((v1 + v2 + dP[6]) / 16.0) pressure = pressure / 100 #converts to hPa # Save to db newReading = RecentPressureModel(None, None, pressure) logging.info("Reading pressure sensor with value: " + str(pressure)) newReading.post()
def recent_pressures_reset_in_range(): logging.debug("Received request /recent/pressures/reset/range") startTime = time.monotonic() try: # Requires a simple pw pw = req.args.get("pw") logging.debug("pw arg is: "+ str(pw)) if pw != "A7G2V9": abort(403) start = req.args.get('start') if start is None: start = '2020-01-01T00:00:00' logging.debug("Start arg is: "+ str(start)) end = req.args.get('end') if end is None: end = datetime.utcnow() logging.debug("End arg is: "+ str(end)) RecentPressureModel.delete_by_range(start,end) elapsedTime = time.monotonic() - startTime logging.debug("pressures reset in range request time: " + str(round(elapsedTime,5))+ " seconds") return res(204, timeUTC=datetime.utcnow()) except mariadb.Error as e: abort(500, str(e))
def recent_pressures_get_average(): logging.debug("Received request /recent/pressures/average") startTime = time.monotonic() try: returnValue = RecentPressureModel.get_average() data = RecentPressureModel.average_json(returnValue) elapsedTime = time.monotonic() - startTime logging.debug("pressure get average request time: " + str(round(elapsedTime,5))+ " seconds") return res(200, data=data, timeUTC=datetime.utcnow()) except mariadb.Error as e: abort(500, str(e))
def delete_by_range(start, end): # Init returnValue = True # Execution RecentPressureModel.deltee_by_range(start, end) RecentHumidityModel.delete_by_range(start, end) RecentCO2Model.delete_by_range(start, end) RecentTemperatureModel.delete_by_range(start, end) # Clean and return return returnValue
def delete_all(): # Init returnValue = True # Execution RecentPressureModel.delete_all() RecentHumidityModel.delete_all() RecentCO2Model.delete_all() RecentTemperatureModel.delete_all() # Clean and return return returnValue
def mockRun(): while True: try: # Mock data creation value = random.uniform(0,100) newReading = RecentPressureModel(None,None,value) # Insert logging.info("Generating mock data for pressure sensor with value: "+str(value)) newReading.post() time.sleep(5) except BaseException as e: logging.exception(e)
def recent_pressures_reset(): logging.debug("Received request /recent/pressures/reset") startTime = time.monotonic() try: # Requires a simple pw pw = req.args.get("pw") logging.debug("pw arg is: "+ str(pw)) if pw != "A7G2V9": abort(403) RecentPressureModel.delete_all() elapsedTime = time.monotonic() - startTime logging.debug("pressures reset request time: " + str(round(elapsedTime,5))+ " seconds") return res(204, timeUTC=datetime.utcnow()) except mariadb.Error as e: abort(500, str(e))
def recent_pressure_get_by_search(): logging.debug("Received request /recent/pressures/search") startTime = time.monotonic() try: start = req.args.get('start') if start is None: start = '2020-01-01T00:00:00' logging.debug("Start arg is: " + str(start)) end = req.args.get('end') if end is None: end = datetime.utcnow() logging.debug("End arg is: " + str(end)) dataArray = [] pressArray = RecentPressureModel.get_by_search(start, end) for tempModel in pressArray: dataArray.append(tempModel.to_json()) elapsedTime = time.monotonic() - startTime logging.debug("temperature get all request time: " + str(round(elapsedTime, 5)) + " seconds") return res(200, data=dataArray, timeUTC=datetime.utcnow()) except mariadb.Error as e: logging.exception(e) abort(500, str(e))
def pressure_get_average_in_range(): logging.debug("Received request /recent/pressures/average/range") startTime = time.monotonic() try: start = req.args.get('start') if start is None: start = '2020-01-01T00:00:00' logging.debug("Start arg is: "+ str(start)) end = req.args.get('end') if end is None: end = datetime.utcnow() logging.debug("End arg is: "+ str(end)) returnValue = RecentPressureModel.get_average_by_range(start,end) data = RecentPressureModel.average_json(returnValue) elapsedTime = time.monotonic() - startTime logging.debug("pressure get average by range all request time: " + str(round(elapsedTime,5))+ " seconds") return res(200, data=data, timeUTC=datetime.utcnow()) except mariadb.Error as e: abort(500, str(e))
def recent_pressure_get_newest(): logging.debug("Received request /recent/pressures/newest") startTime = time.monotonic() try: returnValue = RecentPressureModel.get_newest() if returnValue is None: abort(404) data = returnValue.to_json() elapsedTime = time.monotonic() - startTime logging.debug("pressure get newest all request time: " + str(round(elapsedTime,5))+ " seconds") return res(200, data=data, timeUTC=datetime.utcnow()) except mariadb.Error as e: abort(500, str(e))
def recent_pressure_get_all(): logging.debug("Received request /recent/pressures") startTime = time.monotonic() try: dataArray = [] pressArray = RecentPressureModel.get_all() for tempModel in pressArray: dataArray.append(tempModel.to_json()) elapsedTime = time.monotonic() - startTime logging.debug("pressure get all request time: " + str(round(elapsedTime,5))+ " seconds") return res(200, data=dataArray, timeUTC=datetime.utcnow()) except mariadb.Error as e: abort(500, str(e))
def to_average_json(self): logging.debug("Formatting SensorModel to average json") json = { 'type': 'All recent sensor average', 'attributes': { 'pressure': RecentPressureModel.average_json(self.pressures), 'humidity': RecentHumidityModel.average_json(self.humidities), 'co2': RecentCO2Model.average_json(self.co2), 'temperature': RecentTemperatureModel.average_json(self.temperatures) } } return json
def get_average(): # init foundPressure = None foundHumidities = None foundCo2 = None foundTemperatures = None # Execution (4 connection, could be more efficient but query time is hardly an issue) foundPressure = RecentPressureModel.get_average() foundHumidities = RecentHumidityModel.get_average() foundCo2 = RecentCO2Model.get_average() foundTemperatures = RecentTemperatureModel.get_average() # Build object returnObj = RecentSensorModel(foundPressure, foundHumidities, foundCo2, foundTemperatures) # Clean and return return returnObj
def get_by_search(start, end): # init foundPressure = [] foundHumidities = [] foundCo2 = [] foundTemperatures = [] # Execution (4 connection, could be more efficient but query time is hardly an issue) foundPressure = RecentPressureModel.get_by_search(start, end) foundHumidities = RecentHumidityModel.get_by_search(start, end) foundCo2 = RecentCO2Model.get_by_search(start, end) foundTemperatures = RecentTemperatureModel.get_by_search(start, end) # Build object returnObj = RecentSensorModel(foundPressure, foundHumidities, foundCo2, foundTemperatures) # Clean and return return returnObj