forked from dkilgore90/google-sdm-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdm-api-camera.groovy
190 lines (166 loc) · 6.15 KB
/
sdm-api-camera.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
*
* Copyright 2020 David Kilgore. All Rights Reserved
*
* This software is free for Private Use. You may use and modify the software without distributing it.
* If you make a fork, and add new code, then you should create a pull request to add value, there is no
* guarantee that your pull request will be merged.
*
* You may not grant a sublicense to modify and distribute this software to third parties without permission
* from the copyright holder
* Software is provided without warranty and your use of it is at your own risk.
*
* version: 0.4.0
*/
metadata {
definition(name: 'Google Nest Camera', namespace: 'dkilgore90', author: 'David Kilgore', importUrl: 'https://raw.githubusercontent.com/dkilgore90/google-sdm-api/master/sdm-api-camera.groovy') {
//capability 'VideoCamera'
capability 'ImageCapture'
capability 'Refresh'
capability 'MotionSensor'
capability 'PresenceSensor'
capability 'SoundSensor'
capability 'Initialize'
attribute 'room', 'string'
attribute 'imgWidth', 'number'
attribute 'imgHeight', 'number'
attribute 'rawImg', 'string'
attribute 'lastEventTime', 'string'
attribute 'lastEventType', 'string'
attribute 'streamUrl', 'string'
}
preferences {
input 'minimumMotionTime', 'number', title: 'Motion timeout (s)', required: true, defaultValue: 15
input 'minimumPresenceTime', 'number', title: 'Presence timeout (s)', required: true, defaultValue: 15
input 'minimumSoundTime', 'number', title: 'Sound timeout (s)', required: true, defaultValue: 15
input 'personImageCapture', 'bool', title: 'Person - Capture image?', required: true, defaultValue: true
input 'motionImageCapture', 'bool', title: 'Motion - Capture image?', required: true, defaultValue: true
input 'soundImageCapture', 'bool', title: 'Sound - Capture image?', required: true, defaultValue: true
input 'enableVideoStream', 'bool', title: 'Enable Video Stream?', required: true, defaultValue: false
input name: "debugOutput", type: "bool", title: "Enable Debug Logging?", defaultValue: false
}
}
private logDebug(msg) {
if (settings?.debugOutput) {
log.debug "${device.label}: $msg"
}
}
def installed() {
initialize()
}
def updated() {
if (!personImageCapture && !motionImageCapture && !soundImageCapture) {
device.sendEvent(name: 'image', value: '<img src="" />')
device.sendEvent(name: 'rawImg', value: ' ')
}
initialize()
}
def uninstalled() {
unschedule()
}
def initialize() {
if (enableVideoStream) {
Random rnd = new Random()
parent.deviceGenerateStream(device)
schedule("${rnd.nextInt(60)} ${rnd.nextInt(4)}/4 * ? * * *", extendStream)
} else {
unschedule(extendStream)
if (state.streamUrl) {
parent.deviceStopStream(device, state.streamExtensionToken)
}
state.remove('streamUrl')
state.remove('streamToken')
state.remove('streamExtensionToken')
device.sendEvent(name: 'streamUrl', value: ' ')
}
device.sendEvent(name: 'presence', value: device.currentValue('presence') ?: 'not present')
device.sendEvent(name: 'motion', value: device.currentValue('motion') ?: 'inactive')
device.sendEvent(name: 'sound', value: device.currentValue('sound') ?: 'not detected')
device.sendEvent(name: 'rawImg', value: device.currentValue('rawImg') ?: ' ')
device.sendEvent(name: 'image', value: device.currentValue('image') ?: '<img src="" />')
device.sendEvent(name: 'streamUrl', value: device.currentValue('streamUrl') ?: ' ')
}
def refresh() {
parent.getDeviceData(device)
}
def processPerson() {
logDebug('Person -- present')
device.sendEvent(name: 'presence', value: 'present')
if (minimumPresenceTime == null) {
device.updateSetting('minimumPresenceTime', 15)
}
runIn(minimumPresenceTime, presenceInactive, [overwrite: true])
}
def presenceInactive() {
logDebug('Person -- not present')
device.sendEvent(name: 'presence', value: 'not present')
}
def processMotion() {
logDebug('Motion -- active')
device.sendEvent(name: 'motion', value: 'active')
if (minimumMotionTime == null) {
device.updateSetting('minimumMotionTime', 15)
}
runIn(minimumMotionTime, motionInactive, [overwrite: true])
}
def motionInactive() {
logDebug('Motion -- inactive')
device.sendEvent(name: 'motion', value: 'inactive')
}
def processSound() {
logDebug('Sound -- detected')
device.sendEvent(name: 'sound', value: 'detected')
if (minimumSoundTime == null) {
device.updateSetting('minimumSoundTime', 15)
}
runIn(minimumSoundTime, soundInactive, [overwrite: true])
}
def soundInactive() {
logDebug('Sound -- not detected')
device.sendEvent(name: 'sound', value: 'not detected')
}
def shouldGetImage(String event) {
switch (event) {
case 'Person':
return personImageCapture != null ? personImageCapture : true
break
case 'Motion':
return motionImageCapture != null ? motionImageCapture : true
break
case 'Sound':
return soundImageCapture != null ? soundImageCapture : true
break
default:
return true
break
}
}
def extendStream() {
parent.deviceExtendStream(device, state.streamExtensionToken)
}
def updateStreamData(Map data) {
String url = state.streamUrl
if (data.results.streamUrls) {
int queryIndex = data.results.streamUrls.rtspUrl.indexOf('?')
url = data.results.streamUrls.rtspUrl.substring(0, queryIndex)
state.streamUrl = url
}
device.sendEvent(name: 'streamUrl', value: "${url}?auth=${data.results.streamToken}")
state.streamToken = data.results.streamToken
state.streamExtensionToken = data.results.streamExtensionToken
}
def take() {
log.warn('on-demand image capture is not supported')
}
def getFolderId() {
if (state.folderId) {
return state.folderId
} else {
device.sendEvent(name: 'rawImg', value: ' ')
parent.createFolder(device)
return false
}
}
def setFolderId(String id) {
state.folderId = id
}