I’m working on queuing MQTT messages in the absence of internet and I ran into a problem writing a file to disk, my input is
{“three”:2,“two”:2,“one”:2}
when I use function json_vfprintf
the data stored on the disk looks like this
{"“three”":1,"“two”":1,"“one”":1}
which is incompatible with JSON
Is it possible to call the function so that it doesn’t add another " to the JSON or in somehow delete extra " from file ?
Or it’s possible to convert mqtt_message in mjs to {three:2,two:2,one:2} not to {“three”:2,“two”:2,“one”:2} ??
I’m making an IIoT device and I want MQTT packets to be cached for a few days, I’ve seen caching in the MQTT library but it only caches a few packets.
I modified the library
I added support for mJS but I have a problem with converting to JSON, the library sends the packages correctly but queue file is written in the form {“”three”“:1,”“two”“:1,”“one”“:1} and no i know how to solve this problem.
This function
int result = json_vfprintf((const char*) new_file, json_fmt, ap);
convert {“three”:2,”two”:2,”one”:2} to {“”three”“:1,”“two”“:1,”“one”“:1}
Ok. I see what happens. You try to ffi a variadic function.
I don’t know if there is a trick to do that. You should create a function bool mgos_mqtt_queue_gcp_send_event_sub_json(const char *subfolder, const char *json) which takes a json string and ffi it, or simply call the existing function from C code.
But it’s work I only have problem with this function:
int result = json_vfprintf((const char*) new_file, json_fmt, ap);
It’s convert {“three”:2,”two”:2,”one”:2} to {“”three”“:1,”“two”“:1,”“one”“:1}
Is there any other way to save the file to disk so as not to add more quotes.
Or convert mqtt_message object to string with I can send using MQTT_queue.pub(topic, message) but without quotas (not using JSON.stringify(mqtt_message) ?
let mqtt_message = {};
mqtt_message.one = i;
mqtt_message.two = i;
mqtt_message.three = i;
let s = mkstr(mqtt_message, 20);
print(‘STRING ’ + s);
let message = JSON.stringify(mqtt_message);
json_vfprintf works as it is intended to. If you provide it with a json string, it will add another pair of qoutes to the json key.
The error comes from your code.
Try to do what I suggested in my previous message.
I edited library and now it’s working and saving unsend message to file.
Library is here, I must check sending queue massage from file when device recive Internet, it save massage to file but I must restart device to send queue.