示例#1
0
文件: app.py 项目: DamonHD/OpenTRV
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
示例#2
0
 def test_add_record(self):
     mkey = "test_mkey"
     bn = "mytopic"
     n = "t"
     s = {"mkey": mkey, "bn": bn, "n": n}
     tnow = datetime.datetime.utcnow()
     tsnow = int((tnow - datetime.datetime.utcfromtimestamp(0)).total_seconds())
     ts = Series(s)
     r = opentrv.data.Record("t", tnow, 10)
     ts.add_record(r)
     rlist = ts.find_all()
     self.assertListEqual([{"t":tsnow, "v":10}], rlist)
示例#3
0
文件: app.py 项目: DamonHD/OpenTRV
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())
示例#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())
示例#5
0
 def test_init(self):
     mkey = "test_mkey"
     bn = "mytopic"
     n = "t"
     s = {"mkey": mkey, "bn": bn, "n": n}
     ts = Series(s)
     self.assertIsNotNone(ts)
     self.assertEqual("series_{0}".format(n), ts.name)
示例#6
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
示例#7
0
 def test_find_all_records(self):
     mkey = "test_mkey"
     bn = "mytopic"
     n = "t"
     s = {"mkey": mkey, "bn": bn, "n": n}
     tnow = datetime.datetime.utcnow()
     tsnow = (tnow - datetime.datetime.utcfromtimestamp(0)).total_seconds()
     ts = Series(s)
     ts.add({'t':tsnow, 'v': 10})
     ts.add({'t':tsnow+30, 'v': 10.5})
     ts.add({'t':tsnow+60, 'v': 11})
     rlist = ts.find_all_records()
     self.assertEqual(3, len(rlist))
     self.assertEqual("test_mkey/mytopic", rlist[0].topic.path())
     self.assertEqual("t", rlist[0].name)
     self.assertEqual(tnow, rlist[0].timestamp)
     self.assertEqual(10, rlist[0].value)
     self.assertEqual(10.5, rlist[1].value)
     self.assertEqual(11, rlist[2].value)