Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.openhab.core.library.types.*
- import org.openhab.core.persistence.*
- import org.openhab.model.script.actions.*
- import org.java.math.*
- import org.joda.time.*
- import org.openhab.action.squeezebox.*
- import java.util.concurrent.locks.ReentrantLock
- var Timer timer1 = null
- var java.util.concurrent.locks.ReentrantLock lock1 = new java.util.concurrent.locks.ReentrantLock()
- //****************************************************
- rule "Initialization"
- when
- System started
- then
- postUpdate(alarmClockHour, 6)
- postUpdate(alarmClockMinute, 40)
- postUpdate(alarmMonday, ON)
- postUpdate(alarmTuesday, ON)
- postUpdate(alarmWednesday, ON)
- postUpdate(alarmThursday, ON)
- postUpdate(alarmFriday, ON)
- postUpdate(alarmSaturday, OFF)
- postUpdate(alarmSunday, OFF)
- end
- //****************************************************
- rule "Bedroom Alarm"
- when
- Item alarmClockHour changed or
- Item alarmClockMinute changed
- then
- // If the UI to change the Alarm time is clicked several times the code below
- // is subject to race conditions. Therefore we make sure that all events
- // are processed one after the other.
- lock1.lock()
- try {
- var String msg = ""
- //Copy the Alarm-Time from the UI to local variables
- var hour = alarmClockHour.state as DecimalType
- var minute = alarmClockMinute.state as DecimalType
- //Combine the hour and minutes to one string to be displayed in the
- //user interface
- if (hour < 10) ( msg = "0" )
- msg = msg + alarmClockHour.state.format("%d") + ":"
- if (minute < 10) {msg = msg + "0" }
- msg = msg + alarmClockMinute.state.format("%d")
- postUpdate(alarmClockMessage,msg)
- // calculate the alarm time [min]
- var int alarm1
- alarm1 = (alarmClockHour.state as DecimalType).intValue * 60 +
- (alarmClockMinute.state as DecimalType).intValue
- alarm1 = alarm1.intValue
- // calculate current time [min]
- var int timeNow
- timeNow = now.getMinuteOfDay
- timeNow = timeNow.intValue
- // calculate the difference between the requested alarm time and
- // current time (again in minutes)
- var int delta1
- delta1 = (alarm1 - timeNow)
- delta1 = delta1.intValue
- // add one day (1440 minutes) if alarm time for today already passed
- if (timeNow > alarm1) { delta1 = delta1 + 1440 }
- // check if there is already an alarm timer; cancel it if present
- if (timer1 != null) {
- timer1.cancel
- timer1 = null
- }
- // create a new timer using the calculated delta [min]
- timer1 = createTimer(now.plusMinutes(delta1)) [|
- // This code will be executed if the timer triggers
- // ************************************************
- // check if alarm clock is armed for this weekday
- var Number day = now.getDayOfWeek
- if (((day == 1) && (alarmMonday.state == ON)) ||
- ((day == 2) && (alarmTuesday.state == ON)) ||
- ((day == 3) && (alarmWednesday.state == ON)) ||
- ((day == 4) && (alarmThursday.state == ON)) ||
- ((day == 5) && (alarmFriday.state == ON)) ||
- ((day == 6) && (alarmSaturday.state == ON)) ||
- ((day == 7) && (alarmSunday.state == ON))) {
- // The things to do if the alarm clock is enabled for this day of week:
- lamp_br.sendCommand(ON)
- }
- // Re-Arm the timer to trigger again tomorrow (same time)
- timer1.reschedule(now.plusHours(24))
- // ************************************************
- // Here the code ends that executes once the timer triggers
- ]
- } finally {
- // release the lock - we are ready to process the next event
- lock1.unlock()
- }
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement