Register a Javascript crontab callback

#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?

#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

#3

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:

I also added this in the Awesome Mongoose OS list as well under Code Snippets section: