I am trying to process binary data retrieved from a Davis weather station via their web server.
I do this using a script attached to a Google Apps Spreadsheet.
var lResponse = UrlFetchApp.fetch(lWLDataUrl);
var lContentBytes = lResponse.getAs('application/octet-stream').getBytes();
The data returned by Weatherlink is composed of 52 byte long records, so I slice them in a loop using :
var lContentObject = {};
for (var li=1; li<=lNumberOfRecords; li++) {
var lBinaryData = lContentBytes.slice((li-1)*52, (li-1)*52+52);
lContentObject[li] = {"binary_data": lBinaryData};
}
But the result is not what I expect. It is difficult for me to say if the server returns faulty data (unlikely) or if I do not process it properly on my side (likely). I should be able to turn the response into an array of unsigned 8 bit integers and decode them into a record of weather data that looks like this :
2015/01/01;00:30;-1,9;-1,9;-2,0;92;-3,1;1,6;W;0,80;4,8;W;-1,9;-2,1;-2,1;1031,5;0,00;0,0;0,422;0,000;18,7;42;5,5;17,4;8,14;1,2209;M;M;M;M;M;M;M;M;M;-0,6;-1,1;M;M;4;0;-0,6;-1,1;698;1;100,0;30
Instead, the data retrieved into lContentBytes
shows negative numbers in the debugger, though it should not be the case.
Although the total size of the byte array returned corresponds to what I expect (the number of records * 52), the last records 'sliced' out of the byte array are filled with '-1', which should not be the case.
I did a lot of research but didn't find any solution...
Here are the headers of the response to the GET request :
- 'content-transfer-encoding':"binary",
- Connection:"keep-alive",
- 'Content-Encoding':"gzip",
- Vary:"Accept-Encoding",
- 'Content-Length':"161",
- Date:"Wed, 15 Apr 2020 20:29:13 GMT",
- 'Content-Type':"application/octet-stream"
what I expect
. In order to correctly understand about your question, can you provide a sample input and output values you expect?lResponse
. AndlResponse
retrieved from the URL is the binary data. You want to decode the data fromlResponse
. Is my understanding correct?