1

Using webtcp i build a bridge between socket and websocket and can get data from a machine and display it on browser. But this data is raw and needs to be parsed. I was able to parse it up until array object but there was no further success. Whatever i do it just returns as an object. Below is the initial raw data from socket:

received: 2018-08-13T16:43:34.0689|power|ON|mode|MANUAL|execution|READY|Xact|0.00|Yact|0.00|Zact|0.00|Xcom|0.00|Ycom|0.00|Zcom|0.00|path_feedrate|0.00|line|0|Block|0|program|O-Logo.ord

received: 2018-08-13T16:43:34.0689|comms|NORMAL||||
2018-08-13T16:43:34.0689|Sspeed|0.00

then i removed timestamp and turned it to an array by doing:

socket.on('data', function (data) {
    var machineData = data.split("|");
    var arrData = machineData.slice(1);
    console.log("received: " + arrData);
    document.getElementById("data").innerHTML = ("Received: " + arrData);
});

and i get this:

received: power,ON,mode,MANUAL,execution,READY,Xact,0.00,Yact,0.00,Zact,0.00,Xcom,0.00,Ycom,0.00,Zcom,0.00,path_feedrate,0.00,line,0,Block,0,program,O-Logo.ord

received: comms,NORMAL,,,,
2018-08-13T16:47:40.0978,Sspeed,0.00

Unfortunately i can not parse it further, i tried so many methods and all i get was Object:object. I want this data to be key:value pairs, like

power:ON
mode: MANUAL
execution: READY 

and so on and without any timestamps. This data is dynamic and it will change as machine is working. I tried most of the methods posted here and there but always it would just return Object with no data. Please help me!

2 Answers 2

1

Try this:

var str = "power|ON|mode|MANUAL|execution|READY|Xact|0.00|Yact|0.00|Zact|0.00|Xcom|0.00|Ycom|0.00|Zcom|0.00|path_feedrate|0.00|line|0|Block|0|program|O-Logo.ord";

var arr = str.split("|");

var obj = {}

arr.map((o, i) => {
  if ((i + 1) % 2 != 0) obj[o] = arr[i + 1];
})

console.log(obj)

In your code you should do something like:

socket.on("data", function(data) {
  var arr = data.split("|").slice(1);

  var obj = {};

  arr.map((o, i) => {
    if ((i + 1) % 2 != 0) obj[o] = arr[i + 1];
  });

  console.log(obj);
})

If you want to put inside html, here a full example: https://jsfiddle.net/p5d7f3Lx/

Sign up to request clarification or add additional context in comments.

10 Comments

unfortunately it did not work, this is what i got back: [object Object] socket_client.html:59 Uncaught ReferenceError: result is not defined at Socket.ondataCustom (socket_client.html:59) at Socket.WebTCPIO.onEvent (webtcp-0.0.1.min.js:1356) at e.WebTCP.sock.onmessage (webtcp-0.0.1.min.js:1339) at e.p.dispatchEvent (webtcp-0.0.1.min.js:172) at e._dispatchMessage (webtcp-0.0.1.min.js:694) at e._didMessage (webtcp-0.0.1.min.js:722) at WebSocket.f.ws.onmessage (webtcp-0.0.1.min.js:780)
this is what i get each time. I tried so many methods and all i get will be this: received: [object Object]
yes i tried both of them. console.log(data) i get just string data: 2018-08-13T18:09:07.0760|power|ON|mode|MANUAL|execution|READY|Xact|0.00|Yact|0.00|Zact|0.00|Xcom|0.00|Ycom|0.00|Zcom|0.00|path_feedrate|0.00|line|0|Block|0|program|O-Logo.ord
as you see i added JSON.stringify but the result got parsed again to received: [object Object]. It is so weird. I can stringify and i can .split and .slice but i can't make it key value pairs...
@Asyl If you are able to split and slice you are parsing the result, maybe the error is in other place nothing to do with this place, where is line 59?
|
1

This is a classic scenario for Array.reduce, converting the string into a flat array, and with the reduce you building the obj, just skipping every 2 items.

const str = "power|ON|mode|MANUAL|execution|READY|Xact|0.00|Yact|0.00|Zact|0.00|Xcom|0.00|Ycom|0.00|Zcom|0.00|path_feedrate|0.00|line|0|Block|0|program|O-Logo.ord";

const arr = str.split("|");

const obj = arr.reduce((acc, current, i, original) => {
  if (i % 2 === 1) {
    acc[original[i - 1]] = current;
  }
  return acc;
}, {})

console.log(obj)

5 Comments

i am just getting this: [object Object]. why it is not parsing string object to anything what i try...
thank you. i fixed my error. it was in outputing with string
how can i output this to html?
You can just use JSON.stringify on the object, that will return to u a string, that you will be able to put inside html.
document.getElementById('element-id').innerText = JSON.stringify(obj);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.