Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Sending JSON object to device using file transfer

ANSWERED

I have a JSON object full of weather data that I am trying to send from the companion to the device using file transfer.  The outbox.enqueue appears to be successful on the companion, and is the inbox.nextFile also appears to work.  But when I then try to read the file using the fs (file system) the file (which is there) is either empty or not formatted as expected.

 

Again the objective is to take a large JSON object with weather data at the companion and get it to a JSON object on the device with the same weather data.  Any hints on how to do that properly?

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

You can encode the data as CBOR, the open the file on the device in CBOR format. Like this:

 

 

// companion
import { encode } from "cbor"; import { outbox } from "file-transfer";

outbox
.enqueue(YOUR_FILENAME, encode(YOUR_JSON_OBJECT))
.catch((err) => {
throw new Error(`Failed to queue ${filename}. Error: ${err}`);
});

//device
import { readFileSync } from "fs";

let YOUR_JSON_OBJECT = readFileSync(YOUR_FILENAME, "cbor");

 

 

View best answer in original post

Best Answer
0 Votes
2 REPLIES 2

You can encode the data as CBOR, the open the file on the device in CBOR format. Like this:

 

 

// companion
import { encode } from "cbor"; import { outbox } from "file-transfer";

outbox
.enqueue(YOUR_FILENAME, encode(YOUR_JSON_OBJECT))
.catch((err) => {
throw new Error(`Failed to queue ${filename}. Error: ${err}`);
});

//device
import { readFileSync } from "fs";

let YOUR_JSON_OBJECT = readFileSync(YOUR_FILENAME, "cbor");

 

 

Best Answer
0 Votes

Thanks!  This is working for me.

Best Answer
0 Votes