def update(): data = request.get_json(silent=True) macs = [] slave_id = data['slave_id'] for entry in data['data']: mac = entry['mac'] macs.append(mac) power = int(entry['power']) last_seen = float(entry['last_seen']) last_seen = datetime.datetime.fromtimestamp(last_seen) detection = Detection.query.filter_by(mac=mac).first() if not detection: detection = Detection(mac) db.session.add(detection) measurement = None for m in Detection.query.filter_by(mac=mac).first().measurements.all(): if m.slave_id == slave_id: measurement = m break if measurement: measurement.power = power measurement.last_seen = last_seen else: measurement = Measurement(slave_id, power, last_seen, detection) db.session.add(measurement) db.session.commit() return "Updated measurement of " + str(len(data)) + " entries"
def sample_search_data(): search_data = [ {"id": 2, "sensor": "s1", "value": "12.34", "timestamp": "2019-06-23 12:38:01"}, {"id": 3, "sensor": "s2", "value": "12.35", "timestamp": "2019-06-23 12:38:02"}, {"id": 4, "sensor": "s1", "value": "12.36", "timestamp": "2019-06-23 12:38:03"}, ] for d in search_data: m = Measurement(**d) db.session.add(m)
def test_get_single_measurement(test_client): data = { "id": 2, "sensor": "test", "value": "12.34", "timestamp": "2019-06-23 12:38:01"} m = Measurement(**data) db.session.add(m) rv = test_client.get('/api/v1/measurements' + '/' + str(data['id'])) assert rv.status_code == 200 assert rv.json == data
def add_training(): if current_user.is_anonymous: return "Sorry but you must be logged in to add training data." mac = request.form['mac'] location = request.form['location'] current_detection = Detection.query.filter_by(type='detection', mac=mac).first() training_detection = TrainingDetection(location=Location.query.filter_by(value=location).first(), mac=mac) db.session.add(training_detection) for m in current_detection.measurements: power = m.power if measurement_too_old(m): power = -100 measurement = Measurement(m.slave_id, power, datetime.datetime.now(), training_detection) db.session.add(measurement) db.session.commit() user = User.query.filter_by(email=current_user.email).first() if not Device.query.filter_by(user=user, mac=mac).first(): db.session.add(Device(mac=mac, user=user)) db.session.commit() return redirect(url_for('training'))
def _get_measurement(): return Measurement(value=44.51, time=datetime.now())