Last active
December 14, 2015 22:19
-
-
Save sjungling/5157598 to your computer and use it in GitHub Desktop.
DDS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Desk Defense System | |
| * Don't steal my laptop, bro! | |
| * | |
| * Author: Team Kickass | |
| */ | |
| def preferences() { | |
| return [ | |
| sections: [ | |
| [ | |
| title: "Strobe Siren and Alarm", | |
| input: [ | |
| [ | |
| name: "alarm1", | |
| title: "Strobe Siren Alarm", | |
| type: "capability.alarm", | |
| description: "VERY loud.", | |
| multiple: false | |
| ] | |
| ] | |
| ], | |
| [ | |
| title: "When the laptop has left the launch pad...", | |
| input: [ | |
| [ | |
| name: "contact1", | |
| title: "Where?", | |
| type: "capability.contactSensor", | |
| description: "Tap to set", | |
| multiple: false | |
| ] | |
| ] | |
| ], | |
| [ | |
| title: "Do *awesome* things...", | |
| input: [ | |
| [ | |
| name: "switch1", | |
| title: "Which?", | |
| type: "capability.switch", | |
| description: "Tap to set", | |
| multiple: false | |
| ] | |
| ] | |
| ], | |
| [ | |
| title: "When a presence sensor arrives or departs this location..", | |
| input: [ | |
| [ | |
| name: "presence", | |
| title: "Which sensor?", | |
| type: "capability.presenceSensor", | |
| description: "Tap to set", | |
| multiple: false | |
| ] | |
| ] | |
| ] | |
| ] | |
| ] | |
| } | |
| def installed() { | |
| subscribe(contact1.contact); | |
| subscribe(presence.presence); | |
| state.contact = true; | |
| state.presence = true; | |
| state.counter = 0; | |
| state.alarmState = 0; | |
| state.alarm = "off" | |
| unschedule(); | |
| } | |
| def updated() { | |
| unsubscribe(); | |
| subscribe(contact1.contact); | |
| subscribe(presence.presence); | |
| unschedule(); | |
| state.contact = true; | |
| state.presence = true; | |
| state.counter = 0; | |
| state.alarmState = 0; | |
| state.alarm="off" | |
| } | |
| def contact(evt) { | |
| if (evt.value == "open") { | |
| state.contact = false; | |
| logger('Contact', 'Open', 'Disconnected from laptop'); | |
| soundAlarm(); | |
| } else if (evt.value == "closed") { | |
| state.contact = true; | |
| logger('Contact', 'Closed', 'Connected to laptop'); | |
| stopAlarm(); | |
| } | |
| } | |
| def presence(evt) { | |
| if (evt.value == "present") { | |
| state.presence = true; | |
| switch1.on(); | |
| logger('Presence', '@Work', "You're back!") | |
| } else if (evt.value == "not present") { | |
| state.presence = false; | |
| switch1.off(); | |
| logger('Presence', '@Away', "You've Left! :-(") | |
| } | |
| } | |
| def stopAlarm() { | |
| logger('Alarm', 'Off', 'Stop alarm called.') | |
| alarm1.off(); | |
| state.alarm = "off" | |
| state.alarmState = 0 | |
| unschedule('soundAlarm'); | |
| } | |
| def soundAlarm() { | |
| // Always log things and stuff. | |
| log.debug "SoundAlarm Called with a current alarmState of ${state.alarmState}" | |
| unschedule('soundAlarm'); | |
| state.counter = state.counter +1; | |
| if (state.presence & !state.contact) { | |
| // Increment alarm state | |
| state.alarmState = state.alarmState +1; | |
| // Determine which level of alarm to give. | |
| switch(state.alarmState) { | |
| // Grace Period | |
| case 1: | |
| logger('Alarm', 'Off', 'Grace Period.') | |
| break | |
| // Start Strobe | |
| case 2: | |
| logger('Alarm', 'Strobe', "He's Heating Up!") | |
| alarm1.strobe(); | |
| break | |
| // Fuck This... BRING THE NOISE! | |
| case 3: | |
| logger('Alarm', 'Siren', "SOUND THE ALARM!") | |
| //alarm1.test(); | |
| break | |
| } | |
| runIn(5, 'soundAlarm') | |
| } else { | |
| log.debug("ALL IS WELL IN DENMARK"); | |
| state.alarmState = 0 | |
| unschedule('soundAlarm'); | |
| } | |
| } | |
| def logger(evtDevice, evtType, evtMsg) { | |
| log.debug "[${evtDevice}](${evtType}) ${evtMsg}" | |
| def successClosure = { response -> | |
| log.debug "Request was successful, ${response}" | |
| } | |
| def uri = "https://docs.google.com/a/improvementdirect.com/forms/d/1tVP5E0_RcV2KkfNuwBCWIN_wSTnS_ZKIKQDTXnFYnJg/formResponse" | |
| def payload = "entry.545122041=${evtDevice}&entry.285088443=${evtType}&entry.1947490938=${evtMsg}&submit=Submit" | |
| httpPost(uri, payload, successClosure); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment