Beispiel #1
0
def post_message(mkey):
    if not request.json:
        abort(400)
    c = concs.find_by_mkey(mkey)
    if c is None:
        abort(404)
    app.logger.debug(request.json)
    devices = Devices(c)
    senml_ser = opentrv.data.senml.Serializer()
    records = senml_ser.from_json_object(request.json)
    for r in records:
        d = devices.find_by_topic(r.topic)
        if d is None:
            d = devices.add_topic(r.topic)
            app.logger.debug("Adding device {0}/{1}".format(d["mkey"], d["bn"]))
        else:
            app.logger.debug("Retrieving device {0}/{1}".format(d["mkey"], d["bn"]))
        sensors = Sensors(d)
        s = sensors.find_by_record(r)
        if s is None:
            s = sensors.add_record(r)
            app.logger.debug("Adding sensor {0}/{1}/{2}".format(s["mkey"], s["bn"], s["n"]))
        else:
            app.logger.debug("Retrieving sensor {0}/{1}/{2}".format(s["mkey"], s["bn"], s["n"]))
        sensors.save()
        ts = Series(s)
        ts.add_record(r)
        ts.save()
    devices.save()
    return jsonify({'ok': True}), 201
Beispiel #2
0
 def test_find_by_record(self):
     mkey = "test_mkey"
     bn = "mytopic"
     d = {"mkey": mkey, "bn": bn}
     r = opentrv.data.Record("t", datetime.datetime.utcnow(), 10)
     sensors = Sensors(d)
     sensors.add_record(r)
     s = sensors.find_by_record(r)
     self.assertIsNotNone(s)
     self.assertEqual(mkey, s["mkey"])
     self.assertEqual(bn, s["bn"])
     self.assertEqual("t", s["n"])
Beispiel #3
0
 def test_find_by_n(self):
     mkey = "test_mkey"
     bn = "mytopic"
     n = "t"
     d = {"mkey": mkey, "bn": bn}
     sensors = Sensors(d)
     sensors.add({"mkey": mkey, "bn": bn, "n": n})
     s = sensors.find_by_n("t")
     self.assertIsNotNone(s)
     self.assertEqual(mkey, s["mkey"])
     self.assertEqual(bn, s["bn"])
     self.assertEqual(n, s["n"])
Beispiel #4
0
def get_sensor(mkey, bn, n):
    c = concs.find_by_mkey(mkey)
    if c is None:
        abort(404)
    devices = Devices(c)
    d = devices.find_by_bn(bn)
    if d is None:
        abort(404)
    sensors = Sensors(d)
    s = sensors.find_by_n(n)
    if s is None:
        abort(404)
    ts = Series(s)
    return opentrv.data.senml.Serializer().to_json(ts.find_all_records())
Beispiel #5
0
def get_sensor(mkey, bn, n):
    c = concs.find_by_mkey(mkey)
    if c is None:
        abort(404)
    devices = Devices(c)
    d = devices.find_by_bn(bn)
    if d is None:
        abort(404)
    sensors = Sensors(d)
    s = sensors.find_by_n(n)
    if s is None:
        abort(404)
    ts = Series(s)
    return opentrv.data.senml.Serializer().to_json(ts.find_all_records())
Beispiel #6
0
def get_device(mkey, bn):
    c = concs.find_by_mkey(mkey)
    if c is None:
        abort(404)
    devices = Devices(c)
    d = devices.find_by_bn(bn)
    if d is None:
        abort(404)
    sensors = Sensors(d)
    cat = opentrv.data.hypercat.Catalogue([
        opentrv.data.hypercat.CatalogueItem(
            url_for('get_sensor', mkey=s['mkey'], bn=s['bn'], n=s['n']),
            "Sensor {0}/{1}/{2}".format(s['mkey'], s['bn'], s['n']),
            content_type=opentrv.data.senml.MIME_TYPE,
            payload=s) for s in sensors.find_all()
    ])
    return opentrv.data.hypercat.Serializer().to_json(cat)
Beispiel #7
0
def get_device(mkey, bn):
    c = concs.find_by_mkey(mkey)
    if c is None:
        abort(404)
    devices = Devices(c)
    d = devices.find_by_bn(bn)
    if d is None:
        abort(404)
    sensors = Sensors(d)
    cat = opentrv.data.hypercat.Catalogue(
        [opentrv.data.hypercat.CatalogueItem(
            url_for('get_sensor', mkey=s['mkey'], bn=s['bn'], n=s['n']),
            "Sensor {0}/{1}/{2}".format(s['mkey'], s['bn'], s['n']),
            content_type = opentrv.data.senml.MIME_TYPE,
            payload = s
            ) for s in sensors.find_all()]
        )
    return opentrv.data.hypercat.Serializer().to_json(cat)
Beispiel #8
0
def post_message(mkey):
    if not request.json:
        abort(400)
    c = concs.find_by_mkey(mkey)
    if c is None:
        abort(404)
    app.logger.debug(request.json)
    devices = Devices(c)
    senml_ser = opentrv.data.senml.Serializer()
    records = senml_ser.from_json_object(request.json)
    for r in records:
        d = devices.find_by_topic(r.topic)
        if d is None:
            d = devices.add_topic(r.topic)
            app.logger.debug("Adding device {0}/{1}".format(
                d["mkey"], d["bn"]))
        else:
            app.logger.debug("Retrieving device {0}/{1}".format(
                d["mkey"], d["bn"]))
        sensors = Sensors(d)
        s = sensors.find_by_record(r)
        if s is None:
            s = sensors.add_record(r)
            app.logger.debug("Adding sensor {0}/{1}/{2}".format(
                s["mkey"], s["bn"], s["n"]))
        else:
            app.logger.debug("Retrieving sensor {0}/{1}/{2}".format(
                s["mkey"], s["bn"], s["n"]))
        sensors.save()
        ts = Series(s)
        ts.add_record(r)
        ts.save()
    devices.save()
    return jsonify({'ok': True}), 201