def recent_co2_reset_in_range(): logging.debug("Received request /recent/co2/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)) RecentCO2Model.delete_by_range(start,end) elapsedTime = time.monotonic() - startTime logging.debug("co2 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_co2_get_average(): logging.debug("Received request /recent/co2/average") startTime = time.monotonic() try: returnValue = RecentCO2Model.get_average() data = RecentCO2Model.average_json(returnValue) elapsedTime = time.monotonic() - startTime logging.debug("co 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 recent_co2_reset(): logging.debug("Received request /recent/co2/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) RecentCO2Model.delete_all() elapsedTime = time.monotonic() - startTime logging.debug("co2 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_co2_get_by_search(): logging.debug("Received request /recent/co2/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 = [] co2Array = RecentCO2Model.get_by_search(start, end) for tempModel in co2Array: dataArray.append(tempModel.to_json()) elapsedTime = time.monotonic() - startTime logging.debug("co2 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_co2_get_average_in_range(): logging.debug("Received request /recent/co2/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 = RecentCO2Model.get_average_by_range(start,end) data = RecentCO2Model.average_json(returnValue) elapsedTime = time.monotonic() - startTime logging.debug("co2 get average by 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_co2_get_newest(): logging.debug("Received request /recent/co2/newest") startTime = time.monotonic() try: returnValue = RecentCO2Model.get_newest() if returnValue is None: abort(404) data = returnValue.to_json() elapsedTime = time.monotonic() - startTime logging.debug("co2 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 recent_co2_get_all(): logging.debug("Received request /recent/co2") startTime = time.monotonic() try: dataArray = [] co2Array = RecentCO2Model.get_all() for tempModel in co2Array: dataArray.append(tempModel.to_json()) elapsedTime = time.monotonic() - startTime logging.debug("co2 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 collectData(): #!/usr/bin/python import math, struct, array, time, io, fcntl class i2c(object): def __init__(self, device, bus): # define bus self.fr = io.open("/dev/i2c-1", "rb", buffering=0) self.fw = io.open("/dev/i2c-1", "wb", buffering=0) # set slave address fcntl.ioctl(self.fr, 0x0703, device) fcntl.ioctl(self.fw, 0x0703, device) def write(self, bytes): self.fw.write(bytes) def read(self, bytes): return self.fr.read(bytes) def close(self): self.fw.close() self.fr.close() class T6713(object): def __init__(self): self.dev = i2c(0x15, 1) def gasPPM(self): buffer = array.array('B', [0x04, 0x13, 0x8b, 0x00, 0x01]) self.dev.write(buffer) time.sleep(0.1) data = self.dev.read(4) buffer = array.array('B', data) return buffer[2] * 256 + buffer[3] while True: try: # Collect sensor data obj = T6713() value = obj.gasPPM() #print "PPM: ", value newReading = RecentCO2Model(None, None, value) # Insert into db logging.info("Collected co2 sensor data with value: " + str(value)) newReading.post() time.sleep(5) except BaseException as e: logging.exception(e)
def mockRun(): while True: try: # Create Mock data value = random.uniform(0, 69) newReading = RecentCO2Model(None, None, value) # Insert into db logging.info( "Generating mock data for co2 sensor with value: "+str(value)) newReading.post() time.sleep(5) except BaseException as e: logging.exception(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