def recent_temperatures_reset_in_range(): logging.debug("Received request /recent/temperatures/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)) RecentTemperatureModel.delete_by_range(start, end) elapsedTime = time.monotonic() - startTime logging.debug("temperature 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 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 recent_temperatures_get_average(): logging.debug("Received request /recent/temperatures/average") startTime = time.monotonic() try: returnValue = RecentTemperatureModel.get_average() data = RecentTemperatureModel.average_json(returnValue) elapsedTime = time.monotonic() - startTime logging.debug("temperature 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_all(): # Init returnValue = True # Execution RecentPressureModel.delete_all() RecentHumidityModel.delete_all() RecentCO2Model.delete_all() RecentTemperatureModel.delete_all() # Clean and return return returnValue
def comp_T(adc_temp): global flo v1 = (adc_temp / 16384.0 - dT[0] / 1024.0) * dT[1] v2 = (adc_temp / 131072.0 - dT[0] / 8192.0) * (adc_temp / 131072.0 - dT[0] / 8192.0) * dT[2] flo = v1 + v2 temperature = flo / 5120.0 #print "temp : %-6.2f " % (temperature) # Save to db newReading = RecentTemperatureModel(None, None, temperature) logging.info("Reading temperature sensor with value: " + str(temperature)) newReading.post()
def mockRun(): while True: try: # Create Mock data value = random.uniform(-30, 40) newReading = RecentTemperatureModel(None, None, value) # Save to db logging.info( "Generating mock data for temperature sensor with value: " + str(value)) newReading.post() time.sleep(5) except BaseException as e: logging.exception(e)
def recent_temperatures_get_by_search(): logging.debug("Received request /recent/temperatures/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 = [] temperatureArray = RecentTemperatureModel.get_by_search(start, end) for tempModel in temperatureArray: dataArray.append(tempModel.to_json()) elapsedTime = time.monotonic() - startTime logging.debug("temperature get by search 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 recent_temperatures_reset(): logging.debug("Received request /recent/temperatures/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) RecentTemperatureModel.delete_all() elapsedTime = time.monotonic() - startTime logging.debug("temperature 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_temperatures_get_average_in_range(): logging.debug("Received request /recent/temperatures/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 = RecentTemperatureModel.get_average_by_range(start, end) data = RecentTemperatureModel.average_json(returnValue) elapsedTime = time.monotonic() - startTime logging.debug("temperature get average in range 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_temperatures_get_newest(): logging.debug("Received request /recent/temperatures/newest") startTime = time.monotonic() try: returnValue = RecentTemperatureModel.get_newest() if returnValue is None: abort(404) data = returnValue.to_json() elapsedTime = time.monotonic() - startTime logging.debug("temperature get newest 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 temperatures_get_all(): logging.debug("Received request /recent/temperatures") startTime = time.monotonic() try: dataArray = [] temperatureArray = RecentTemperatureModel.get_all() for tempModel in temperatureArray: 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: 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