예제 #1
0
def test_get_sensor_log_when_sensor_has_logs(client):
    sensor_id = "some-id"
    Sensor.create(id=sensor_id,
                  name="a name",
                  sensor_type="temperature",
                  next_update=datetime.max)

    log_row_1 = {
        "timestamp": 1547477541,
        "message": "some message to log here"
    }
    log_row_2 = {
        "timestamp": 1547478541,
        "message": "some other message to log here"
    }
    client.post(url_for("api.add_sensor_log", sensor_id=sensor_id),
                json=log_row_1)
    client.post(url_for("api.add_sensor_log", sensor_id=sensor_id),
                json=log_row_2)

    res = client.get(url_for("api.get_sensor_log", sensor_id=sensor_id))

    assert res.status_code == 200
    for row_in_res in res.json:
        assert "id" in row_in_res
        assert row_in_res["timestamp"] in [
            "2019-01-14T14:52:21", "2019-01-14T15:09:01"
        ]
        assert row_in_res["message"] in log_row_1["message"] or log_row_2[
            "message"]
예제 #2
0
def test_sensor_add_sensor_log_when_message_length_over_256_chars(client):
    sensor_id = "some-id"
    Sensor.create(
        id=sensor_id,
        name="the-name",
        sensor_type="temperature",
        next_update=datetime.max,
    )

    log = {"timestamp": 1567447541, "message": "*" * 257}
    res = client.post(url_for("api.add_sensor_log", sensor_id=sensor_id),
                      json=log)
    assert res.status_code == 400
예제 #3
0
def test_sensor_add_sensor_log_when_timestamp_missing(client):
    sensor_id = "some-id"
    Sensor.create(
        id=sensor_id,
        name="the-name",
        sensor_type="temperature",
        next_update=datetime.max,
    )

    log = {"message": "some message to log here"}
    res = client.post(url_for("api.add_sensor_log", sensor_id=sensor_id),
                      json=log)
    assert res.status_code == 400
예제 #4
0
def createsensor():
    device_name = request.json.get('device_name')
    hostname = request.json.get('hostname')
    ip_address = request.json.get('ip_address')
    location = request.json.get('location')
    protected_subnet = request.json.get('protected_subnet')
    external_subnet = request.json.get('external_subnet')
    oinkcode = request.json.get('oinkcode')
    company = g.user['company']
    sensor = Sensor(company=company, device_name=device_name,
                    hostname=hostname, ip_address=ip_address, location=location,
                    protected_subnet=protected_subnet)

    if external_subnet:
        sensor.set_oinkcode(oinkcode)
    if external_subnet:
        sensor.set_external_subnet(external_subnet)

    sensor.create_dev_id(device_name)
    sensor.create_topic_cmd()
    sensor.create_topic_resp()

    Sensor.create(company=sensor['company'],
                  device_id=sensor['device_id'],
                  device_name=sensor['device_name'],
                  hostname=sensor['hostname'],
                  ip_address=sensor['ip_address'],
                  location=sensor['location'],
                  protected_subnet=sensor['protected_subnet'],
                  external_subnet=sensor['external_subnet'],
                  oinkcode=sensor['oinkcode'],
                  topic_global=sensor['topic_global'],
                  topic_cmd=sensor['topic_cmd'],
                  topic_resp=sensor['topic_resp'],
                  sensor_key=sensor['sensor_key'],
                  time_created=sensor['time_created']
                  )

    app.session.execute(
        """
        INSERT INTO sensor_status (device_id, status, ts) 
        VALUES (%s, %s, %s)
        """,
        (sensor['device_id'], "STOPPED", datetime.now())
    )

    return jsonify({
        'device_id': sensor['device_id'],
        'device_name': sensor['device_name'],
        'sensor_key': sensor['sensor_key'],
    })
예제 #5
0
def test_update_sensor_set_name_when_sensor_exists(client):
    sensor = Sensor.create(
        id="the-id",
        name="the-name",
        sensor_type="temperature",
        next_update=datetime.max,
    )

    new_name = "some-name"
    res = client.patch(
        url_for("api.update_sensor", sensor_id=sensor.id),
        json=[{
            "op": "replace",
            "path": "/name",
            "value": new_name
        }],
    )
    expected = Sensor(
        id=sensor.id,
        name=new_name,
        sensor_type=sensor.sensor_type,
        next_update=datetime.max,
        firmware_version="",
    )
    assert res.status_code == 200
    assert res.json == expected.to_json()

    res = client.get(url_for("api.get_sensor", sensor_id=sensor.id))
    assert res.json == expected.to_json()
예제 #6
0
def test_get_sensors_with_sensors_returns_the_sensors(client):
    sensor = Sensor.create(id="the-id",
                           name="the-name",
                           sensor_type="temperature")

    res = client.get(url_for("api.get_sensors"))
    assert res.json == [sensor.to_json()]
예제 #7
0
def add_sensor_value(sensor_type):
    temp_reading = request.get_json()
    if "timestamp" not in temp_reading:
        return bad_request("timestamp attribute missing")
    if "sensor" not in temp_reading:
        return bad_request("sensor attribute missing")
    if "value" not in temp_reading:
        return bad_request("value attribute missing")
    if "next_update" not in temp_reading:
        return bad_request("next_update attribute missing")
    sensor_id = temp_reading["sensor"].replace(":", "")
    sensor = Sensor.get_by_id(sensor_id)
    if sensor is not None and sensor.sensor_type != sensor_type:
        return bad_request("Sensor is already registered as another type")
    if sensor is None:
        sensor = Sensor.create(id=sensor_id, name=sensor_id, sensor_type=sensor_type)
    sensor.next_update = datetime.utcfromtimestamp(
        temp_reading["timestamp"] + temp_reading["next_update"]
    )
    db.session.commit()
    reading = Reading(
        sensor=sensor_id,
        timestamp=datetime.utcfromtimestamp(temp_reading["timestamp"]),
        value=temp_reading["value"] * 100,
    )
    db.session.add(reading)
    db.session.commit()

    return jsonify({"message": "thanks"}), 201
예제 #8
0
def test_get_sensor_when_sensor_exists(client):
    sensor = Sensor.create(id="the-id",
                           name="the-name",
                           sensor_type="temperature")

    res = client.get(url_for("api.get_sensor", sensor_id=sensor.id))
    assert res.status_code == 200
    assert res.json == sensor.to_json()
예제 #9
0
def test_add_humidity_when_sensor_registered_as_other_type(client):
    sensor = Sensor.create(id="the-id",
                           name="the-name",
                           sensor_type="not-humidity")

    res = client.post(
        url_for("api.add_humidity"),
        json={
            "timestamp": 1567447540,
            "sensor": sensor.id,
            "value": 75.2
        },
    )
    assert res.status_code == 400
예제 #10
0
def test_update_sensor_when_sensor_exists_and_patch_changes_is_invalid(client):
    sensor = Sensor.create(id="the-id",
                           name="the-name",
                           sensor_type="temperature")

    res = client.patch(
        url_for("api.update_sensor", sensor_id=sensor.id),
        json=[{
            "op": "replace",
            "path": "/name",
            "valu": "some-name"
        }],
    )
    assert res.status_code == 400