mycontroller-org / mycontroller-v1-legacy

The Open Source Controller
http://www.MyController.org
Apache License 2.0
148 stars 89 forks source link

[Improvement] Add info on timespan and number ob objects to stats-api #355

Closed cimba007 closed 7 years ago

cimba007 commented 7 years ago

Hi,

I am just trying to connect an esp8266 to mycontroller to get the latest data of the current day:

http://raspi:8443/mc/rest/metrics/stats?sensorId=234&timestampFrom=1486141273860

This is working so far .. I even found a nice json streaming library: https://github.com/squix78/json-streaming-parser

For an microcontroller with constrained memory etc. it would be really nice to know how many items are in the response and what timespan is covered. The current response is like this:

[ {
  "id" : 263,
  "internalId" : 234,
  "variableType" : "Humidity",
  "unit" : "%",
  "dataType" : "Double",
  "timeFormat" : "HH:mm:ss",
  "resourceName" : "[G]:mygateway >> [N]:31:MultiSensor >> [S]:0:Humidity >> [SV]:Humidity",
  "chartType" : "lineChart",
  "chartInterpolate" : "linear",
  "chartData" : [ {
    "key" : "Average",
    "values" : [ [ 1486144083769, 45.52727272727273 ], [ 1486144143769

I suggest adding from-timestamp, to-timestamp and number of items

[ { "id" : 263, "internalId" : 234, "variableType" : "Humidity", "unit" : "%", "dataType" : "Double", "timeFormat" : "HH:mm:ss", "resourceName" : "[G]:mygateway >> [N]:31:MultiSensor >> [S]:0:Humidity >> [SV]:Humidity", "chartType" : "lineChart", "chartInterpolate" : "linear", "chartFromTS" : "1486144083769", "chartToTS" : "1486230303769", "chartDataItems" : 12345, "chartData" : [ { "key" : "Average", "values" : [ [ 1486144083769, 45.52727272727273 ], [ 1486144143769

jkandasa commented 7 years ago

@cimba007 I guess you are using an old version. In the SNAPSHOT version, we have added a friendly metric query for any application. Currently you are using MyController specific graphical API, which is moved to /stats >> /nvd3data

you can use as follows,

https://192.168.1.100:8443/mc/rest/metrics/stats?resourceId=152&resourceType=Sensor variable&timestampFrom=1486141273860&bucketDuration=2h

Result:

[
    {
        "start": 1486217987274,
        "end": 1486225187274,
        "empty": false,
        "min": 28.5,
        "max": 29.1,
        "avg": 28.99666666666666,
        "samples": 240
    },
    {
        "start": 1486225187274,
        "end": 1486232387274,
        "empty": false,
        "min": 27.8,
        "max": 28.9,
        "avg": 28.700836820083694,
        "samples": 239
    }
]

supported inputs: https://github.com/mycontroller-org/mycontroller/blob/development/modules/core/src/main/java/org/mycontroller/standalone/api/jaxrs/MetricsHandler.java#L142~L147

related queries: http://forum.mycontroller.org/topic/85/monitoring-electricity-consumption http://forum.mycontroller.org/topic/79/temperature-min-max-avg/2

cimba007 commented 7 years ago

Very nice, I will give it a try tomorrow .. today I spent too much time to figure out how to get chunked-http-responses to work:

https://github.com/esp8266/Arduino/issues/2872

Edit: I check your related two links to the forum. They both deal with other ways of displaying data in mycontroller (this is really nice too!) .. but I wan't to display the data (temperature over last 24 hours) as a graph on one of those fancy SPI-TFTs:

http://www.ebay.de/itm/Touchscreen-2-8-inch-Zoll-240x320-SD-SPI-TFT-LCD-MCUfriend-display-fur-Arduino-/172327778901?hash=item281f893a55:g:Zz8AAOSw9IpXyYi8

I will have a closer look at "https://192.168.1.100:8443/mc/rest/metrics/stats?"

jkandasa commented 7 years ago

@cimba007 ok, I use MyControllerDevice library(MQTT) with ESP8266 to display data on OLED 128x64 I2C communication. It works great!

I check your related two links to the forum. They both deal with other ways of displaying data in mycontroller

Yes, it is dealing with direct API. You can access same way from REST API for external device.

cimba007 commented 7 years ago

I tried the new "stats"-api and am very impressed so far. The only thing that is giving me a headache is the timestamp:

https://192.168.1.100:8443/mc/rest/metrics/stats?resourceId=152&resourceType=Sensor variable&timestampFrom=1486141273860&bucketDuration=2h

Would it be possible to introduce negative timestamps? e.g. -6000000 for the "last 100 minutes" ? This would make stuff much easier on embedded systems and would not break any current APIs.

cimba007 commented 7 years ago

@jkandasa: You got any input on the idea of "negative"-timestamp or extension of the api? Getting the current timestamp to calculate start-end is not "that" easy on mobile microcontrollers.

You mind adding an option to calculate startTimestamp as offset from current timestamp?

e.g. to show the data of the last 12hours? (no absolute timestamp)

Thanks in advance ;_)

jkandasa commented 7 years ago

@cimba007 the fix, I have added will address as follows,

https://192.168.1.100:8443/mc/rest/metrics/stats?resourceId=3&resourceType=Sensor variable&bucketDuration=1h&duration=12h

When you specify start or end values with duration, calculation will be done as follows,

When you specify start and end, duration will be ignored.

You mind adding an option to calculate startTimestamp as offset from current timestamp? e.g. to show the data of the last 12hours? (no absolute timestamp)

Do you want only values with comma separated? to draw a graph on an embedded device?

cimba007 commented 7 years ago

@jkandasa: I am pretty happy with your latest implementation of duration. There is an pretty nice json-library which I use to parse the json returned by your api.

Just a sneak-preview of what I want to use it for:

Notice the little graph at the bottom, data fresh pulled from mycontroller .. last 12hours in 30minute steps .. it is pretty rough for now .. no x-axis .. but this will be my work for next weekend.

PS: Did you already add documentation for the "new" api? I think it is awesome and might help other fellow hackers out there a lot ;-)

jkandasa commented 7 years ago

@cimba007 No, I do not have any document for REST API. I have to do. We have an issue very long time for this https://github.com/mycontroller-org/mycontroller/issues/24

I will try to close this when I do release 0.0.3.Final

Redferne commented 7 years ago

@cimba007

Nice looking hardware/software in the picture. Care to share more details?

cimba007 commented 7 years ago

@Redferne

I will just list the components I used and some libs, should give you a decent start if you want to look further into things:

Hardware:

Librarys:

Redferne commented 7 years ago

Oh yes! Thanks for the details. I'm planning a similar project using a MoteinoMega (https://lowpowerlab.com/shop/product/138) I'm hoping that 128kbyte + 512kbyte flash is enough to hold all graphics.