Advertisement
Guest User

Untitled

a guest
Mar 17th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.77 KB | None | 0 0
  1. import org.openhab.core.library.types.*
  2. import org.openhab.core.persistence.*
  3. import org.openhab.model.script.actions.*
  4. import org.java.math.*
  5. import org.joda.time.*
  6. import org.openhab.action.squeezebox.*
  7.  
  8. import java.util.concurrent.locks.ReentrantLock
  9.  
  10. var Timer timer1 = null
  11. var java.util.concurrent.locks.ReentrantLock lock1 = new java.util.concurrent.locks.ReentrantLock()
  12.  
  13. //****************************************************
  14.  
  15. rule "Initialization"
  16. when
  17.     System started
  18. then
  19.     postUpdate(alarmClockHour,    6)
  20.     postUpdate(alarmClockMinute, 40)
  21.     postUpdate(alarmMonday,      ON)
  22.     postUpdate(alarmTuesday,     ON)
  23.     postUpdate(alarmWednesday,   ON)
  24.     postUpdate(alarmThursday,    ON)
  25.     postUpdate(alarmFriday,      ON)
  26.     postUpdate(alarmSaturday,   OFF)
  27.     postUpdate(alarmSunday,     OFF)
  28. end
  29.  
  30. //****************************************************
  31.  
  32. rule "Bedroom Alarm"
  33. when
  34.     Item alarmClockHour changed or
  35.     Item alarmClockMinute changed
  36. then
  37.   // If the UI to change the Alarm time is clicked several times the code below
  38.   // is subject to race conditions. Therefore we make sure that all events
  39.   // are processed one after the other.
  40.   lock1.lock()
  41.   try {
  42.     var String msg = ""
  43.    
  44.     //Copy the Alarm-Time from the UI to local variables
  45.     var hour = alarmClockHour.state as DecimalType
  46.     var minute = alarmClockMinute.state as DecimalType
  47.    
  48.     //Combine the hour and minutes to one string to be displayed in the
  49.     //user interface
  50.     if (hour < 10) ( msg = "0" )
  51.     msg = msg + alarmClockHour.state.format("%d") + ":"
  52.    
  53.     if (minute < 10) {msg = msg + "0" }
  54.     msg = msg + alarmClockMinute.state.format("%d")
  55.     postUpdate(alarmClockMessage,msg)
  56.    
  57.     // calculate the alarm time [min]
  58.     var int alarm1
  59.     alarm1 = (alarmClockHour.state as DecimalType).intValue * 60 +
  60.              (alarmClockMinute.state as DecimalType).intValue
  61.     alarm1 = alarm1.intValue
  62.    
  63.     // calculate current time [min]
  64.     var int timeNow
  65.     timeNow = now.getMinuteOfDay
  66.     timeNow = timeNow.intValue
  67.    
  68.     // calculate the difference between the requested alarm time and
  69.     // current time (again in minutes)  
  70.     var int delta1
  71.     delta1 = (alarm1 - timeNow)
  72.     delta1 = delta1.intValue
  73.    
  74.     // add one day (1440 minutes) if alarm time for today already passed
  75.     if (timeNow > alarm1) { delta1 = delta1 + 1440 }
  76.    
  77.     // check if there is already an alarm timer; cancel it if present
  78.     if (timer1 != null) {
  79.        timer1.cancel
  80.        timer1 = null
  81.     }
  82.    
  83.     // create a new timer using the calculated delta [min]
  84.     timer1 = createTimer(now.plusMinutes(delta1)) [|
  85.         // This code will be executed if the timer triggers
  86.         // ************************************************
  87.         // check if alarm clock is armed for this weekday
  88.         var Number day = now.getDayOfWeek
  89.         if (((day == 1) && (alarmMonday.state == ON))     ||
  90.             ((day == 2) && (alarmTuesday.state == ON))   ||
  91.             ((day == 3) && (alarmWednesday.state == ON))   ||
  92.             ((day == 4) && (alarmThursday.state == ON)) ||
  93.             ((day == 5) && (alarmFriday.state == ON))    ||
  94.             ((day == 6) && (alarmSaturday.state == ON))    ||
  95.             ((day == 7) && (alarmSunday.state == ON))) {
  96.                 // The things to do if the alarm clock is enabled for this day of week:
  97.                 lamp_br.sendCommand(ON)
  98.            }
  99.            // Re-Arm the timer to trigger again tomorrow (same time)
  100.            timer1.reschedule(now.plusHours(24))
  101.         // ************************************************
  102.         // Here the code ends that executes once the timer triggers
  103.         ]
  104.   } finally  {
  105.     // release the lock - we are ready to process the next event
  106.     lock1.unlock()
  107.   }
  108. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement