Calculating time for use in cron entry, and installing via SSH

In this example, the Astro binding is used with the suggested linked items that are used in this rule. The Exec binding is also used to SSH to a remote machine. This example assume you have already exchanged and installed SSH keys.

rule "Update Chicken Schedule" 
// Chicken egg production decreases in Winter, so to keep production up, lighting is 
// installed in the pen.
// The pen door needs to be closed at night for protection from predators.
// Since the chicken house data link is unreliable, actions are sent to the remote cron
// job handler.  In the event of connection loss, the schedule continues in place.
when 
    Time is noon
then 
    var doorOpenCron =  new DateTime(LocalSun_Daylight_StartTime.state.toString).getMinuteOfHour.toString + "@@" +
                        new DateTime(LocalSun_Daylight_StartTime.state.toString).getHourOfDay.toString + "@@" +
                        "\'*\'@@\'*\'@@\'*\'@@/usr/local/bin/doorOpen\n"
    var doorCloseCron = new DateTime(LocalSun_AstroDusk_EndTime.state.toString).getMinuteOfHour.toString + "@@" +
                        new DateTime(LocalSun_AstroDusk_EndTime.state.toString).getHourOfDay.toString + "@@" +
                        "\'*\'@@\'*\'@@\'*\'@@/usr/local/bin/doorClose\n"
    var lightOnCron =   new DateTime(LocalSun_Daylight_EndTime.state.toString).getMinuteOfHour.toString + "@@" +
                        new DateTime(LocalSun_Daylight_EndTime.state.toString).getHourOfDay.toString + "@@" +
                        "\'*\'@@\'*\'@@\'*\'@@/usr/local/bin/lightOn\n"
    val DateTime offTime = new DateTime((LocalSun_Daylight_StartTime.state as DateTimeType).zonedDateTime.toInstant().toEpochMilli).plusHours(20)
    var lightOffCron =  new DateTime(offTime.toString).getMinuteOfHour.toString + "@@" +
                        new DateTime(offTime.toString).getHourOfDay.toString + "@@" +
                        "\'*\'@@\'*\'@@\'*\'@@/usr/local/bin/lightOff\n"
    var allCron = doorOpenCron
                + doorCloseCron
                + lightOnCron
                + lightOffCron
    var cmd = "ssh@@pi@henhousepi.local@@echo@@\'" + allCron + "\'@@|@@crontab@@-"
    logInfo("Logger","Chicken cron is " + cmd )
    val results = executeCommandLine(cmd, 5000)
    logInfo("Logger", results)
end
2 Likes