Update keep-me-cozy-ii.groovy
[smartapps.git] / official / medicine-reminder.groovy
1 /**
2  *  Copyright 2015 SmartThings
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  *  in compliance with the License. You may obtain a copy of the License at:
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
10  *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
11  *  for the specific language governing permissions and limitations under the License.
12  *
13  *  Medicine Reminder
14  *
15  *  Author: SmartThings
16  */
17
18 definition(
19     name: "Medicine Reminder",
20     namespace: "smartthings",
21     author: "SmartThings",
22     description: "Set up a reminder so that if you forget to take your medicine (determined by whether a cabinet or drawer has been opened) by specified time you get a notification or text message.",
23     category: "Health & Wellness",
24     iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/text_contact.png",
25     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/text_contact@2x.png"
26 )
27
28 preferences {
29         section("Choose your medicine cabinet..."){
30                 input "cabinet1", "capability.contactSensor", title: "Where?"
31         }
32         section("Take my medicine at..."){
33                 input "time1", "time", title: "Time 1"
34                 input "time2", "time", title: "Time 2", required: false
35                 input "time3", "time", title: "Time 3", required: false
36                 input "time4", "time", title: "Time 4", required: false
37         }
38         section("I forget send me a notification and/or text message..."){
39         input("recipients", "contact", title: "Send notifications to") {
40             input "sendPush", "enum", title: "Push Notification", required: false, options: ["Yes", "No"]
41             input "phone1", "phone", title: "Phone Number", required: false
42         }
43         }
44         section("Time window (optional, defaults to plus or minus 15 minutes") {
45                 input "timeWindow", "decimal", title: "Minutes", required: false
46         }
47 }
48
49 def installed()
50 {
51         initialize()
52 }
53
54 def updated()
55 {
56         unschedule()
57         initialize()
58 }
59
60 def initialize() {
61         def window = timeWindowMsec
62         [time1, time2, time3, time4].eachWithIndex {time, index ->
63                 if (time != null) {
64                         def endTime = new Date(timeToday(time, location?.timeZone).time + window)
65                         log.debug "Scheduling check at $endTime"
66                         //runDaily(endTime, "scheduleCheck${index}")
67                         switch (index) {
68                                 case 0:
69                                         schedule(endTime, scheduleCheck0)
70                                         break
71                                 case 1:
72                                         schedule(endTime, scheduleCheck1)
73                                         break
74                                 case 2:
75                                         schedule(endTime, scheduleCheck2)
76                                         break
77                                 case 3:
78                                         schedule(endTime, scheduleCheck3)
79                                         break
80                         }
81                 }
82         }
83 }
84
85 def scheduleCheck0() { scheduleCheck() }
86 def scheduleCheck1() { scheduleCheck() }
87 def scheduleCheck2() { scheduleCheck() }
88 def scheduleCheck3() { scheduleCheck() }
89
90 def scheduleCheck()
91 {
92         log.debug "scheduleCheck"
93         def t0 = new Date(now() - (2 * timeWindowMsec))
94         def t1 = new Date()
95         def cabinetOpened = cabinet1.eventsBetween(t0, t1).find{it.name == "contact" && it.value == "open"}
96         log.trace "Looking for events between $t0 and $t1: $cabinetOpened"
97
98         if (cabinetOpened) {
99                 log.trace "Medicine cabinet was opened since $midnight, no notification required"
100         } else {
101                 log.trace "Medicine cabinet was not opened since $midnight, sending notification"
102                 sendMessage()
103         }
104 }
105
106 private sendMessage() {
107         def msg = "Please remember to take your medicine"
108         log.info msg
109     if (location.contactBookEnabled) {
110         sendNotificationToContacts(msg, recipients)
111     }
112     else {
113         if (phone1) {
114             sendSms(phone1, msg)
115         }
116         if (sendPush == "Yes") {
117             sendPush(msg)
118         }
119     }
120 }
121
122 def getTimeWindowMsec() {
123         (timeWindow ?: 15) * 60000 as Long
124 }