The rule files for Javascript go in:
/etc/openhab/automation/jsr223/javascript/personal
You need to import some stuff from the files in:
/etc/openhab/automation/lib/javascript/core
where you need to have the helper libraries installed (there should be 9 files I think).
var sOPENHAB_CONF_DIR = (sOPENHAB_CONF_DIR === undefined) ? Java.type("java.lang.System").getenv("OPENHAB_CONF") : sOPENHAB_CONF_DIR;
load(sOPENHAB_CONF_DIR+'/automation/lib/javascript/core/rules.js');
load(sOPENHAB_CONF_DIR+'/automation/lib/javascript/core/actions.js');
Then, to make the binding restart work requires extra steps. You have to pass a key file in the command line, which needs to be set up.
You need to generate public and private RSA keys:
cd /home/openhabian/karaf_keys
sudo -u openhab ssh-keygen -t rsa -f openhab.id_rsa
Then you need to copy the public key from:
/home/openhabian/karaf_keys/openhab.id_rsa.pub
(the long character string with no spaces) and put it in this file:
sudo nano /var/lib/openhab/etc/keys.properties
So that you end up with:
openhab=<LONG KEY STRING>,_g_:admingroup
Then you have to copy the private key to the openhab ssh directory (or at least I did), a directory which may need to be created. I couldn’t work out whether it needed to go in openhab2 or openhab, so I did both:
mkdir /var/lib/openhab2/.ssh
sudo cp /home/openhabian/karaf_keys/openhab.id_rsa /var/lib/openhab2/.ssh
mkdir /var/lib/openhab/.ssh
sudo cp /home/openhabian/karaf_keys/openhab.id_rsa /var/lib/openhab/.ssh
Then you have to give openhab ownership of .ssh. Again, I did both folders.
sudo chown openhab:openhab -R /var/lib/openhab/.ssh
sudo chown openhab:openhab -R /var/lib/openhab2/.ssh
Not sure if that’s every step. Then you should be able to pass the public key in the RestartBundle function from above. If you add the cloud heartbeat update and the call to RestartBundle from above when it’s out of date, you have almost everything. Perhaps the last missing piece is the timer function to update the item periodically and do the check:
function OnTimer_RunMasterScheduler_EveryTenMinutes(event)
{
try
{
logInfo("Running master scheduler (every 10 minutes)");
// Do 10-minute tasks
SendCloudHeartbeat() ;
CheckIfAutomateAndCloudOffline();
}
catch(error)
{
logInfo("ERROR running master scheduler (every 10 minutes)! Message: " + error);
}
}
// @ts-ignore
when("Time cron 0 0/10 * * * ?")(OnTimer_RunMasterScheduler_EveryTenMinutes);
// @ts-ignore
rule
(
"On timer: run master scheduler (every 10 minutes)",
"Runs stuff every 10 minutes"
)(OnTimer_RunMasterScheduler_EveryTenMinutes);
I have most of my functions organised in classes, but to simplify I’ll remove the class stuff and also the export directive, which you need to stop Visual Code from complaining, but has to be stripped off before copying to openHab:
function GetOpenHabCloudCallAuthorisationHeaders()
{
var headers = [];
headers['Content-Type'] = '*/*';
headers['accept'] = '*/*';
headers['Authorization'] = 'Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; // Cloud user and pw encoded
headers['WWW-Authenticate'] = 'Basic';
return headers;
}
function SendCloudHeartbeat()
{
// logInfo("SendCloudHeartbeat");
try
{
var headers = GetOpenHabCloudCallAuthorisationHeaders();
// logInfo("Cloud heartbeat headers: " + headers);
var dateNow = new Date();
DoHttpPutRequest("https://myopenhab.org:443/rest/items/Cloud_Heartbeat_TimeStamp/state", "Set cloud heartbeat", "*/*", dateNow.toISOString(), headers);
}
catch(exception)
{
logInfo("Exception caught in SendCloudHeartbeat. Message: " + exception);
}
}
function CheckIfAutomateAndCloudOffline()
{
// Try restarting cloud client if down for a short amount of time
if (nCloudHeartbeatAge > this.CLOUD_MAX_HEARTBEAT_AGE && (nCloudHeartbeatAge < this.CLOUD_MIN_AGE_ALERT_THRESHOLD)
{
RestartBundle("org.openhab.io.openhabcloud");
}
}
function RestartBundle(sBundleID)
{
var Exec = Java.type("org.openhab.core.model.script.actions.Exec");
var Duration = Java.type("java.time.Duration");
// @ts-ignore
Exec.executeCommandLine(Duration.ofSeconds(6), "sudo", "/usr/bin/ssh", "-p", "8101", "-i", "/var/lib/openhab/.ssh/openhab.id_rsa", "openhab@localhost", "bundle:restart", sBindingID);
}
I also took out the minutes since reboot and push notification to simplify further.