def _process_message(data): """ Process a data message and save it """ reading_date = parser.parse(data['time']) try: # Check if the reading for this time already exists (ignore if so) Reading.objects.get(time = reading_date) return False except: pass # Save the reading into the DB reading = Reading() reading.time = reading_date reading.temperature = Decimal(str(data['temperature'])) reading.meter_id = data['sensor_id'] reading.meter_type = int(data['meter_type']) reading.ch1_wattage = Decimal(str(data['ch1'])) if data.has_key('ch2'): reading.ch2_wattage = Decimal(str(data['ch2'])) if data.has_key('ch3'): reading.ch3_wattage = Decimal(str(data['ch3'])) reading.save()
def submit( cls, uid, readingType, readingValue ): assert isinstance( uid, str ) assert isinstance( readingType, str ) assert isinstance( readingValue, float ) source = cls.getSource( uid ) reading = Reading( readingSource=source, value=readingValue, readingType=readingType ) reading.put() return reading.addtime
def seed_site(sensors = 1, days = 7, interval_seconds = 3600): site = Site.create(name = 'seeded_site') node = Node.create(name = 'seeded_node', site = site, latitude = 35.146623 + random() / 100.0, longitude = 139.9835682 + random() / 100.0) sensortype = SensorType.create(name = 'HC SR-04', unit = 'cm') for i in range(sensors): sensor = Sensor.create(sensortype = sensortype, node = node, name = 'water distance %s'%i) timestamp = datetime.utcnow() - timedelta(days = 7) data = loggeobrowngen() n_readings = 0 while timestamp < datetime.utcnow(): Reading.create(sensor = sensor, value = data.next(), timestamp = timestamp) timestamp += timedelta(seconds = interval_seconds) n_readings += 1 data.close() return {'site': site, 'node': node}
def add_reading(): response = {"errors": None} reading = json.loads(request.data) try: mac_addr = reading["mac_address"] except KeyError: response["errors"] = "Please provide a valid mac address." print(reading) temperature = float(reading["temperature"]) humidity = float(reading["humidity"]) light_intensity = float(reading["light_intensity"]) soil_moisture = float(reading["soil_moisture"]) if mac_addr is not None: plant = Plant.query.filter_by(mac_address=mac_addr).first() reading = Reading(time=time.time(), plant_id=plant.id, temperature=temperature, humidity=humidity, light_intensity=light_intensity, soil_moisture=soil_moisture, mac_address=mac_addr, moisture_index=None) db.session.add(reading) db.session.commit() return response
def test_sanitizing_ecg_data_nullifies_readings_with_240_alphabets(self): now = tm.time() reading = Reading(time = now, values = ['A'] * 240) self.ecg_data.inject(reading = reading) data = sanitize(self.ecg_data) last_values = data.iloc[-1]["values"] self.assertTrue(np.nan in last_values, "Oops! null not returned for invalid chunk")
def update_database(): last_data = {'readings': {'temp': 0, 'humidity': 0}, 'parameters': None} while 1: update_time = timezone.now() data = (yield) if (round(last_data["readings"]["temp"]) != round( data["readings"]["temp"])) or (round( last_data["readings"]["humidity"]) != round( data["readings"]["humidity"])): try: reading_record = Reading.objects.get(pk=1) except Reading.DoesNotExist: reading_record = Reading(time=update_time, temperature=data["readings"]["temp"], humidity=data["readings"]["humidity"]) else: reading_record.time = update_time reading_record.temperature = data["readings"]["temp"] reading_record.humidity = data["readings"]["humidity"] reading_record.save() if last_data["parameters"] != data["parameters"]: setting_record = Setting(time=update_time, mode=data["parameters"]["mode"], temperature=data["parameters"]["temp"], humidity=data["parameters"]["humidity"]) setting_record.save() last_data = data
def update_database(): last_data = {'readings': {'temp': 0, 'humidity': 0}, 'parameters': None} while 1: update_time = timezone.now() data = (yield) # Round current temperature and humidity readings temperature_rounded = round(data["readings"]["temp"]) humidity_rounded = round(data["readings"]["humidity"]) # Compare rounded current readings against the rounded previous readings if (round(last_data["readings"]["temp"]) != temperature_rounded) or ( round(last_data["readings"]["humidity"]) != humidity_rounded): try: reading_record = Reading.objects.get(pk=1) except Reading.DoesNotExist: reading_record = Reading(time=update_time, temperature=temperature_rounded, humidity=humidity_rounded) else: reading_record.time = update_time reading_record.temperature = temperature_rounded reading_record.humidity = humidity_rounded reading_record.save() if last_data["parameters"] != data["parameters"]: setting_record = Setting(time=update_time, source=0, mode=data["parameters"]["mode"], temperature=data["parameters"]["temp"], humidity=data["parameters"]["humidity"]) setting_record.save() last_data = data
def _getReadings(cls, uid, readingType ): """ Get all the readings of a particular type """ source = cls.getSource( uid ) r = Reading.all() r.filter( "readingSource =", source ) r.filter( "readingType =", readingType ) r.order("-adddate") return [ re for re in r ]
def do_reading(): """ Process reading """ name = request.form.get('name') spread_id = request.form.get('spread_id') new_reading = Reading(name=name, spread_id=spread_id) db.session.add(new_reading) db.session.commit() return redirect('/readings')
async def api_create_reading(request, *, name, author, content): #check_admin(request) if not name or not name.strip(): raise APIValueError('name', 'name cannot be empty.') if not author or not author.strip(): raise APIValueError('author', 'author cannot be empty.') if not content or not content.strip(): raise APIValueError('content', 'content cannot be empty.') reading = Reading(name=name.strip(), author=author.strip(), content=content.strip()) await reading.save() return reading
def post(self): parser = reqparse.RequestParser(bundle_errors = True) parser.add_argument('sensor_id', type=int, location='form', required=True, help='<int> sensor_id required') parser.add_argument('value', type=float, location='form', required=True, help='<float> value required') parser.add_argument('timestamp', type=float, location='form', required=True, help='<float> epoch timestamp required') args = parser.parse_args() sensor = Sensor.query.filter_by(id = args['sensor_id']).first() if not sensor: return jsonify(ApiError('sensor {} not found'.format(args['sensor_id']))) reading = Reading.create(sensor = sensor, value = args['value'], timestamp = args['timestamp']) return jsonify(ApiObjects(reading.json()))
def handle_mqtt_message(client, userdata, message): if message.topic == "SmartPlant_pairing": msg = message.payload.decode() print(msg) macform = MacAdressValidationForm() macform.addr.data = msg if macform.validate(): with application.app_context(): found = Device.query.filter_by(mac_address=msg).first() if not found: device = Device(msg, time.time()) db.session.add(device) else: found.last_seen = time.time() db.session.commit() elif message.topic == "SmartPlant": msg = message.payload.decode() msg = msg.replace('nan', '"nan"') print(msg) reading_data = json.loads(msg) # If no data for a field, do not save the reading. for i in reading_data.keys(): if reading_data[i] == "nan": print("Failed to get complete reading data.") return None with application.app_context(): plant = Plant.query.filter_by( mac_address=reading_data["mac_address"]).one_or_none() if plant and Reading.query.filter_by( time=reading_data["time"], mac_address=reading_data["mac_address"]).count() == 0: reading = Reading( time=reading_data["time"], plant_id=plant.id, temperature=reading_data["temp"], humidity=reading_data["humidity"], light_intensity=reading_data["light_intensity"], soil_moisture=reading_data['soil_moisture'] * 100, # CONVERT TO PERCENTAGE for database. moisture_index=reading_data["moisture_index"], mac_address=reading_data["mac_address"]) plant_health_alert(plant, plant.get_problems(reading)) db.session.add(reading) db.session.commit() else: print("Similar recording.")
def post(self): parser = reqparse.RequestParser(bundle_errors = True) parser.add_argument('format', type=str, required = True, choices = ['json', 'compact'], help='<str> data format [json|compact]') parser.add_argument('readings', type=str, required = True, help='<str> multiple readings') parser.add_argument('node_id', type=int, help='<int> node_id required') parser.add_argument('timestamp', type=str, location = 'form', required = False, help='<str> timestamp required. Format: %Y-%m-%d %H:%M:%S') args = parser.parse_args() stored_readings = list() if args['format'] == 'compact': try: timestamp = '20' + args['timestamp'] timestamp = datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S') except (ValueError, TypeError): timestamp = datetime.utcnow() #return jsonify(ApiError('could not parse timestamp: {}'.format(args['timestamp']))) try: readings = args['readings'].split(';') unpacked = map(lambda r: tuple(r.split(',')), readings) for r in unpacked: assert len(r) == 2 except Exception: return jsonify(ApiError('Could not store data. Please submit data in the format "sensor_id,value;sensor_id,value;..."')) # node = Node.query.filter_by(id = args['node_id']).first() # if not node: # return jsonify(ApiError('no such node: {}'.format(args['node_id']))) for sensor_id, value in unpacked: try: sensor_id = int(sensor_id) except ValueError: return jsonify(ApiError('could not convert sensor_id into integer: {}'.format(sensor_id))) sensor = Sensor.query.filter_by(id = sensor_id).first() if not sensor: return jsonify(ApiError('sensor {} not found'.format(sensor_id))) try: value = float(value) except ValueError: return jsonify(ApiError('value could not be converted to a number: {}'.format(value))) reading = Reading.create(sensor = sensor, value = value, timestamp = timestamp) stored_readings.append(reading.json()) return jsonify(ApiObjects(stored_readings))
def add_currently_reading(isbn, user_id): try: book = db.session.query(Book).filter_by(ISBN=isbn).first() book_id = book.book_id db.session.add( Reading(user_id=user_id, book_id=book_id, start_date=datetime.date.today())) db.session.add( Activity(user_id=user_id, action_id=IS_READING_ACTION_ID, object_id=BOOK_OBJECT_ID, date_created=datetime.datetime.now(), target_id=book_id)) db.session.commit() return make_response(jsonify({"success": "Added to bookshelf"}), 201) except AttributeError: traceback.print_exc() return make_response(jsonify({"error": "Failed to add to shelf"}), 404)
def post(self): parser = reqparse.RequestParser() print request.headers #parser.add_argument('X-Temperature', required=True, location='headers', help="Temperature can't be blank!") parser.add_argument('Content-Type', required=True, location='headers', help="Temperature can't be blank!") #parser.add_argument('X-Sensor-Id', required=True, location='headers', help="Sensor-Id can't be blank!") args = parser.parse_args() print args idx = args['Content-Type'].find('Temperature: ') temperature = args['Content-Type'][idx + 13:idx + 15] idx = args['Content-Type'].find('Sensor-Id: ') sensor_id = args['Content-Type'][idx + 11] print "TEMP:", temperature print "ID:", sensor_id reading = Reading(temperature, sensor_id) db.session.add(reading) db.session.commit() return ReadingSerializer(reading).data, 201
def add_record(): content = request.json # check for duplicate timestamp same_times = Reading.query.filter_by(time=content["time"]).first() if same_times is None: reading = Reading(baro_temp=content["baro_temp"], baro_pressure=content["baro_pressure"], cpu_temp=content["cpu_temp"], humid_temp=content["humid_temp"], humid_humid=content["humid_humid"], light=content["light"], time=content["time"], soil_moisture=content["soil_moisture"], water_level=content["water_level"], pump_status=content["pump_status"]) db.session.add(reading) db.session.commit() return "Record Added {}".format(reading.id) else: return ('duplicate timestamp')
def update_database(): last_data = {'readings':{'temp':0,'humidity':0}, 'parameters':None} while 1: update_time = timezone.now() data = (yield) if (round(last_data["readings"]["temp"]) != round(data["readings"]["temp"])) or (round(last_data["readings"]["humidity"]) != round(data["readings"]["humidity"])): try: reading_record = Reading.objects.get(pk=1) except Reading.DoesNotExist: reading_record = Reading(time=update_time, temperature=data["readings"]["temp"], humidity=data["readings"]["humidity"]) else: reading_record.time = update_time reading_record.temperature = data["readings"]["temp"] reading_record.humidity = data["readings"]["humidity"] reading_record.save() if last_data["parameters"] != data["parameters"]: setting_record = Setting(time=update_time, mode=data["parameters"]["mode"], temperature=data["parameters"]["temp"], humidity=data["parameters"]["humidity"]) setting_record.save() last_data = data
def update_database(): last_data = {'readings':{'temp':0,'humidity':0}, 'parameters':None} while 1: update_time = timezone.now() data = (yield) # Round current temperature and humidity readings temperature_rounded = round(data["readings"]["temp"]) humidity_rounded = round(data["readings"]["humidity"]) # Compare rounded current readings against the rounded previous readings if (round(last_data["readings"]["temp"]) != temperature_rounded) or (round(last_data["readings"]["humidity"]) != humidity_rounded): try: reading_record = Reading.objects.get(pk=1) except Reading.DoesNotExist: reading_record = Reading(time=update_time, temperature=temperature_rounded, humidity=humidity_rounded) else: reading_record.time = update_time reading_record.temperature = temperature_rounded reading_record.humidity = humidity_rounded reading_record.save() if last_data["parameters"] != data["parameters"]: setting_record = Setting(time=update_time, source=0, mode=data["parameters"]["mode"], temperature=data["parameters"]["temp"], humidity=data["parameters"]["humidity"]) setting_record.save() last_data = data
def run(self): for value in loggeobrowngen(): reading = Reading.create(sensor = self.sensor, value = value, timestamp = datetime.now()) print reading time.sleep(self.wait_interval)
def upload_file(): if request.method == 'POST': f_image = request.files.getlist('image')[0] f_audio = request.files.getlist('audio')[0] pi_id = request.form['pi_i'] r_ids = json.loads(request.form['r_ids']) pi_serial = request.form['pi_s'] r = True if request.form['r'] == 'T' else False d = request.form['d'] k = request.form['k'] i = request.form['i'] w = request.form['w'] s = request.form['s'] spect = list(map(lambda x: array(x), json.loads(request.form['spe']))) laser = list(map(lambda x: string_t(x), json.loads(request.form['la']))) led = list(map(lambda x: string_t(x), json.loads(request.form['le']))) uv = list(map(lambda x: string_t(x), json.loads(request.form['uv']))) read_image = f_image.read() read_audio = f_audio.read() s3_image_return = s3.Bucket(Config.S3_BUCKET).put_object( Key=k, Body=read_image) s3_audio_return = s3.Bucket(Config.S3_BUCKET).put_object( Key=i, Body=read_audio) if (len(spect[0]) and len(spect)): result = Result(pi_id=pi_id, pi_serial=pi_serial, s3_key=k, etag=s3_image_return.e_tag, ripe=r, timestamp=d, weather=w, slap_type=s, s3_audio_key=i, etag_audio=s3_audio_return.e_tag) db.session.add(result) for i, s in enumerate(spect): reading = Reading(id=r_ids[i], timestamp=d, pi_id=pi_id, pi_serial=pi_serial, reading=db.cast(s, ARRAY(db.Integer)), laser=laser[i], led=led[i], uv=uv[i]) result.readings.append(reading) db.session.add(reading) else: result = Result(pi_id=pi_id, pi_serial=pi_serial, s3_key=k, etag=s3_image_return.e_tag, ripe=r, timestamp=d, weather=w, slap_type=s, s3_audio_key=i, etag_audio=s3_audio_return.e_tag) db.session.add(result) db.session.commit() #eval_result = predictor.eval_result(read_file) #print("result:",str(result)) #print("eval:",str(eval_result)) return (str(result)) else: return ("POST API Endpoint only")
sensors = { "Termometer (mock)": ("Celsius", "Temperature"), "Hygrometer (mock)": ("%", "Humidity"), "Manometer (mock)": ("bar", "Pressure") } users = ["John", "Alex", "Rob", "Matt"] for sensor_name, (unit, quantity) in sensors.items(): Sensor.create(sensor_name, quantity) # For the sake of tests for sensor_name, (unit, quantity) in sensors.items(): for i in range(120, 180, 1): t = datetime(2020, 11, 26, 17, i % 60, 0) Reading.create(str(random.randint(20, 27)), unit, Sensor.find(sensor_name).id, timestamp=t) # Let's have 4 readings per hour start_date = datetime.utcnow() - timedelta(days=4) now = datetime.utcnow() sampling_interval = timedelta(minutes=15) for sensor_name, (unit, quantity) in sensors.items(): sample_date = start_date while sample_date < now: Reading.create(str(random.randint(20, 27)), unit, Sensor.find(sensor_name).id, timestamp=sample_date) sample_date = sample_date + sampling_interval
db.session.add(Card(number=1, arcana="Major", name="The Magician")) db.session.add(Card(number=2, arcana="Major", name="The High Priestess")) db.session.add(Card(number=3, arcana="Major", name="The Empress")) db.session.add(Card(number=4, arcana="Major", name="The Emperor")) db.session.add(Card(number=5, arcana="Major", name="The Heirophant")) db.session.add(Card(number=6, arcana="Major", name="The Lovers")) db.session.add(Card(number=7, arcana="Major", name="The Chariot")) db.session.add( Spread(name="Three Card Spread", num_of_cards=3, image_url="/", description="...")) db.session.add( Spread(name="Celtic Cross", num_of_cards=10, image_url="/", description="...")) db.session.add(Placement(spread_id=1, num=1, details="past")) db.session.add(Placement(spread_id=1, num=2, details="present")) db.session.add(Placement(spread_id=1, num=3, details="future")) db.session.add(Reading(spread_id=1, thoughts="meow")) db.session.add(ReadingCardPlacement(reading_id=1, card_num=3, placement_id=1)) db.session.add(ReadingCardPlacement(reading_id=1, card_num=7, placement_id=2)) db.session.add(ReadingCardPlacement(reading_id=1, card_num=0, placement_id=3)) db.session.commit()