I am an embedded programmer and I do not know JS or python. I would like to prepare the universal data generator for the ingestion service.
I do have data.bin in a separate file and I need a JS script to join in according to the Binary payloads (e.g. for audio) requirements in https://docs.edgeimpulse.com/reference#data-acquisition-format
I struggle to join the header and binary data file.
let header = {
protected: {
ver: “v1”,
alg: “HS256”,
iat: Math.floor(Date.now() / 1000) // epoch time, seconds since 1970
},
signature: emptySignature,
payload: {
device_name: “00:00:00:00:00:01”,
device_type: “Board name”,
interval_ms: 0.001,
sensors: [
{ name: “raw”, units: “-” },
],
values: [
[ “Ref-Binary-i16” ],
[ 0xFF ]
]
}
};
console.log('Header: ', Buffer.from(JSON.stringify(header)));
// read binary file
let binary = fs.readFileSync(Path.join(__dirname, ‘data.bin’));
console.log('Binary: ', binary.slice(0));
// concat header and binary file
let data = Buffer.concat([Buffer.from(JSON.stringify(header)), binary.slice(0)]);
console.log('Data: ', data);
// now calculate the HMAC and fill in the signature
let hmac = crypto.createHmac(‘sha256’, HMAC_KEY);
hmac.update(data);
let signature = hmac.digest().toString(‘hex’);
// update the signature in the message and re-encode
header.signature = signature;
data = Buffer.concat([Buffer.from(JSON.stringify(header)), binary.slice(0)]);
// now upload the buffer to Edge Impulse
request.post(‘https://ingestion.edgeimpulse.com/api/training/data’, {
headers: {
‘x-api-key’: API_KEY,
‘x-file-name’: ‘raw_data’,
‘Content-Type’: ‘application/json’
},
body: data,
encoding: ‘binary’
}, function (err, response, body) {
if (err) return console.error(‘Request failed’, err);
console.log(‘Uploaded file to Edge Impulse’, response.statusCode, body);
Can you please help me?