I did something similar using pstuarts original Generic Camera Device code. Combining it with this smart app. I’m repurposing an old Conbrov camera that doesn’t natively support streaming outside of their app or a complicated setup with a third party server. It does however have a snapshot.cgi so I can retrieve an image using the “Take” function.
The combined code is below:
/**
* Z-Wave Garage Door Opener w/ Camera (Customized)
*
* Copyright 2014 SmartThings
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
* for the specific language governing permissions and limitations under the License.
*
*/
metadata {
definition (name: "Simulated Garage Door Opener w/ Camera (Custom)", namespace: "Smartthings", author: "fieldsjm", vid: "generic-contact-2", ocfdevicetype: "oic.d.garagedoor", mnmn: "SmartThings") {
capability "Actuator"
capability "Door Control"
capability "Garage Door Control"
capability "Contact Sensor"
capability "Refresh"
capability "Sensor"
capability "Health Check"
capability "Image Capture"
attribute "hubactionMode", "string"
}
preferences {
input("CameraIP", "string", title:"Camera IP Address", description: "Please enter your camera's IP Address", required: true, displayDuringSetup: true)
input("CameraPort", "string", title:"Camera Port", description: "Please enter your camera's Port", defaultValue: 80 , required: true, displayDuringSetup: true)
input("CameraPath", "string", title:"Camera Path to Image", description: "Please enter the path to the image", defaultValue: "/SnapshotJPEG?Resolution=640x480&Quality=Clarity", required: true, displayDuringSetup: true)
input("CameraAuth", "bool", title:"Does Camera require User Auth?", description: "Please choose if the camera requires authentication (only basic is supported)", defaultValue: true, displayDuringSetup: true)
input("CameraPostGet", "string", title:"Does Camera use a Post or Get, normally Get?", description: "Please choose if the camera uses a POST or a GET command to retreive the image", defaultValue: "GET", displayDuringSetup: true)
input("CameraUser", "string", title:"Camera User", description: "Please enter your camera's username", required: false, displayDuringSetup: true)
input("CameraPassword", "string", title:"Camera Password", description: "Please enter your camera's password", required: false, displayDuringSetup: true)
}
simulator {
}
tiles {
standardTile("toggle", "device.door", width: 2, height: 2) {
state("closed", label:'${name}', action:"door control.open", icon:"st.doors.garage.garage-closed", backgroundColor:"#00A0DC", nextState:"opening")
state("open", label:'${name}', action:"door control.close", icon:"st.doors.garage.garage-open", backgroundColor:"#e86d13", nextState:"closing")
state("opening", label:'${name}', icon:"st.doors.garage.garage-closed", backgroundColor:"#e86d13")
state("closing", label:'${name}', icon:"st.doors.garage.garage-open", backgroundColor:"#00A0DC")
}
standardTile("open", "device.door", inactiveLabel: false, decoration: "flat") {
state "default", label:'open', action:"door control.open", icon:"st.doors.garage.garage-opening"
}
standardTile("close", "device.door", inactiveLabel: false, decoration: "flat") {
state "default", label:'close', action:"door control.close", icon:"st.doors.garage.garage-closing"
}
standardTile("take", "device.image", width: 1, height: 1, canChangeIcon: false, inactiveLabel: true, canChangeBackground: false) {
state "take", label: "Take", action: "Image Capture.take", icon: "st.camera.camera", backgroundColor: "#FFFFFF", nextState:"taking"
state "taking", label:'Taking', action: "", icon: "st.camera.take-photo", backgroundColor: "#53a7c0"
state "image", label: "Take", action: "Image Capture.take", icon: "st.camera.camera", backgroundColor: "#FFFFFF", nextState:"taking"
}
standardTile("refresh", "device.alarmStatus", inactiveLabel: false, decoration: "flat") {
state "refresh", action:"polling.poll", icon:"st.secondary.refresh"
}
standardTile("blank", "device.image", width: 1, height: 1, canChangeIcon: false, canChangeBackground: false, decoration: "flat") {
state "blank", label: "", action: "", icon: "", backgroundColor: "#FFFFFF"
}
carouselTile("cameraDetails", "device.image", width: 3, height: 2) { }
main "toggle"
details(["toggle", "open", "close", "take", "blank", "refresh", "cameraDetails"])
}
}
def parse(String description) {
log.trace "parse($description)"
def map = [:]
def retResult = []
def descMap = parseDescriptionAsMap(description)
//Image
def imageKey = descMap["tempImageKey"] ? descMap["tempImageKey"] : descMap["key"]
if (imageKey) {
storeTemporaryImage(imageKey, getPictureName())
}
}
//one second delay (default is 6)
def open() {
sendEvent(name: "door", value: "opening")
runIn(1, finishOpening)
}
//one second delay (default is 6)
def close() {
sendEvent(name: "door", value: "closing")
runIn(1, finishClosing)
}
def finishOpening() {
sendEvent(name: "door", value: "open")
sendEvent(name: "contact", value: "open")
}
def finishClosing() {
sendEvent(name: "door", value: "closed")
sendEvent(name: "contact", value: "closed")
}
def installed() {
log.trace "Executing 'installed'"
initialize()
}
def updated() {
log.trace "Executing 'updated'"
initialize()
}
def take() {
def userpassascii = "${CameraUser}:${CameraPassword}"
def userpass = "Basic " + userpassascii.encodeAsBase64().toString()
def host = CameraIP
def hosthex = convertIPtoHex(host).toUpperCase() //thanks to @foxxyben for catching this
def porthex = convertPortToHex(CameraPort).toUpperCase()
device.deviceNetworkId = "$hosthex:$porthex"
log.debug "The device id configured is: $device.deviceNetworkId"
def path = CameraPath
log.debug "path is: $path"
log.debug "Requires Auth: $CameraAuth"
log.debug "Uses which method: $CameraPostGet"
def headers = [:]
headers.put("HOST", "$host:$CameraPort")
if (CameraAuth) {
headers.put("Authorization", userpass)
}
log.debug "The Header is $headers"
def method = "GET"
try {
if (CameraPostGet.toUpperCase() == "POST") {
method = "POST"
}
}
catch (Exception e) { // HACK to get around default values not setting in devices
settings.CameraPostGet = "GET"
log.debug e
log.debug "You must not of set the perference for the CameraPOSTGET option"
}
log.debug "The method is $method"
try {
def hubAction = new physicalgraph.device.HubAction(
method: method,
path: path,
headers: headers
)
hubAction.options = [outputMsgToS3:true]
log.debug hubAction
hubAction
}
catch (Exception e) {
log.debug "Hit Exception $e on $hubAction"
}
}
def parseDescriptionAsMap(description) {
description.split(",").inject([:]) { map, param ->
def nameAndValue = param.split(":")
map += [(nameAndValue[0].trim()):nameAndValue[1].trim()]
}
}
private initialize() {
log.trace "Executing 'initialize'"
sendEvent(name: "DeviceWatch-DeviceStatus", value: "online")
sendEvent(name: "healthStatus", value: "online")
sendEvent(name: "DeviceWatch-Enroll", value: [protocol: "cloud", scheme:"untracked"].encodeAsJson(), displayed: false)
}
private getPictureName() {
def pictureUuid = java.util.UUID.randomUUID().toString().replaceAll('-', '')
log.debug pictureUuid
def picName = device.deviceNetworkId.replaceAll(':', '') + "_$pictureUuid" + ".jpg"
return picName
}
private String convertIPtoHex(ipAddress) {
String hex = ipAddress.tokenize( '.' ).collect { String.format( '%02x', it.toInteger() ) }.join()
log.debug "IP address entered is $ipAddress and the converted hex code is $hex"
return hex
}
private String convertPortToHex(port) {
String hexport = port.toString().format( '%04x', port.toInteger() )
log.debug hexport
return hexport
}
private Integer convertHexToInt(hex) {
Integer.parseInt(hex,16)
}
private String convertHexToIP(hex) {
log.debug("Convert hex to ip: $hex")
[convertHexToInt(hex[0..1]),convertHexToInt(hex[2..3]),convertHexToInt(hex[4..5]),convertHexToInt(hex[6..7])].join(".")
}
private getHostAddress() {
def parts = device.deviceNetworkId.split(":")
log.debug device.deviceNetworkId
def ip = convertHexToIP(parts[0])
def port = convertHexToInt(parts[1])
return ip + ":" + port
}
To Install, follow instructions above to setup the garage components first.
Camera settings will be customized using the settings button (gear icon) in the device itself. Refer to the original camera post for community help/tips/hints on how to setup different camera types.