DrTom
August 2, 2019, 10:01pm
1
I am trying to register a Javascript crontab callback as in the following:
ffi('void mgos_crontab_register_handler(char*, void (*)(char*, char*, userdata), userdata)')(
"vlx_cron", function(foo,bar,baz){
print("vlx_cron cb", JSON.stringify([foo,bar,baz]));
}, null);
When the crontab fires the log says no handler is found:
... Cron job 1 is firing: "vlx_cron" ...
... No actual handler for the cron action "vlx_cron"
What is wrong and how looks a working ffi call to mgos_crontab_register_handler
?
nliviu
August 14, 2019, 9:31am
2
You have to write some wrappers in C to be able to do that.
The signatures of mgos_crontab_register_handler
and mgos_crontab_cb
use struct mg_str
.
mgos_crontab_cb
mgos_crontab_register_handler
This was something of a PIA for me as well, but thankfully someone in the Gitter chat helped me with the code, so i’m happy to share it with you:
crontab.c
#include "mgos.h"
#include "mgos_cron.h"
#include "mgos_crontab.h"
struct mgos_cronmanager_mjs_data
{
void (*cb)(const char *, void *);
void *ud;
};
This file has been truncated. show original
crontab.js
let CronManager = {
_reg: ffi('void mgos_cronmanager_register_handler_mjs(char *, int, void(*)(char *,userdata), userdata)'),
reg: function(action, cb, ud){
this._reg(action, action.length, cb, ud);
}
};
I also added this in the Awesome Mongoose OS list as well under Code Snippets section:
Hi @tripflex ,
I was stubling about this today and was wondering if you could give me some more hints were you put the crontab.c, crontab.js and how you register your callbacks in the end?
That would be very helpful.
Thanks a lot!
Please read the docs and ask specific questions