You need to import some stuff from the files in:
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;
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:
sudo -u openhab ssh-keygen -t rsa -f openhab.id_rsa
Then you need to copy the public key from:
(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:
sudo cp /home/openhabian/karaf_keys/openhab.id_rsa /var/lib/openhab2/.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:
logInfo("Running master scheduler (every 10 minutes)");
// Do 10-minute tasks
logInfo("ERROR running master scheduler (every 10 minutes)! Message: " + error);
when("Time cron 0 0/10 * * * ?")(OnTimer_RunMasterScheduler_EveryTenMinutes);
"On timer: run master scheduler (every 10 minutes)",
"Runs stuff every 10 minutes"
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:
var headers = ;
headers['Content-Type'] = '*/*';
headers['accept'] = '*/*';
headers['Authorization'] = 'Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; // Cloud user and pw encoded
headers['WWW-Authenticate'] = 'Basic';
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);
logInfo("Exception caught in SendCloudHeartbeat. Message: " + exception);
// 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)
var Exec = Java.type("org.openhab.core.model.script.actions.Exec");
var Duration = Java.type("java.time.Duration");
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.