0
I’m working on a project with websokets and I’m not sure how to solve the following problem. I receive via WS JSON data from various devices. Each device sends a value once per second. In the client I receive data from various devices so.
{ab1: {date: "2019-06-12 12:45:50", value: "31"}}
{cd2: {date: "2019-06-12 12:45:50", value: "34"}}
{ab1: {date: "2019-06-12 12:45:51", value: "32"}}
{cd2: {date: "2019-06-12 12:45:51", value: "36"}}
...
So far I’ve managed to organize the data by id:
ws.onmessage = function (evt) {
var data = JSON.parse(evt.data);
var id = data.id;
var date = data.date;
var value = data.value;
datas[id] = {'date':date,'value':value};
};
the result is like this:
{ab1:{date:"2019-06-12 12:45:50",value:"31"}, cd2:{date:"2019-06-12 12:45:50",value:"34"}}
{ab1:{date:"2019-06-12 12:45:51",value:"32"}, cd2:{date:"2019-06-12 12:45:51",value:"36"}}
...
But how do I need to group the data by date to later create the average of the received values every second and display in a graph that updates once per second. This format does not facilitate.
Ideally the above data would be in the following format:
{2019-06-12 12:45:50: {id: "ab1", value: "31"}, {id: "cd2", value: "34"}}
{2019-06-12 12:45:51: {id: "ab1", value: "32"}, {id: "cd2", value: "36"}}
I needed an orientation to convert the received data in this format.
interesting, but the problem is that the json messages arrive one at a time in real fear. I should be able to process the data that comes once a second.
– helderk