Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* Raspberry Pi - IP Camera
*
* Copyright 2015 st.john.johnson@gmail.com
*
* 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: "RPi IP Camera", namespace: "stj", author: "stjohnjohnson") {
capability "Image Capture"
capability "Motion Sensor"
capability "Polling"
capability "Refresh"
}
preferences {
input("rpiip", "string",
title: "Raspberry Pi IP Address",
description: "Raspberry Pi IP Address",
required: true,
displayDuringSetup: true
)
input("rpiport", "string",
title: "Raspberry Pi Port",
description: "Raspberry Pi Port",
required: true,
displayDuringSetup: true
)
}
simulator {
}
tiles {
carouselTile("cameraDetails", "device.image", width: 3, height: 2) {
}
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", "command.refresh", inactiveLabel: false) {
state "default", label:'refresh', action:"refresh.refresh", icon:"st.secondary.refresh-icon"
}
main "camera"
details(["cameraDetails", "take", "refresh"])
}
}
def installed() {
log.debug "Installed with settings: ${settings}"
refresh()
}
def updated() {
log.debug "Updated with settings: ${settings}"
refresh()
}
// Take picture
def take() {
log.debug("Taking Photo")
}
// Poll for status
def poll() {
log.debug("Get Status Notice")
}
// Returns the address of the hub itself
private String getCallBackAddress() {
return device.hub.getDataValue("localIP") + ":" + device.hub.getDataValue("localSrvPortTCP")
}
def refresh() {
log.debug "Refreshing"
// Setting Network Device Id
def iphex = convertIPtoHex(rpiip)
def porthex = convertPortToHex(rpiport)
if (device.deviceNetworkId != "$iphex:$porthex") {
device.deviceNetworkId = "$iphex:$porthex"
}
log.debug "Device Network Id set to ${device.deviceNetworkId}"
def headers = [:]
headers.put("HOST", "$rpiip:$rpiport")
def hubAction = new physicalgraph.device.HubAction(
method: "POST",
path: "/${id}",
headers: headers,
query: [
callback: getCallBackAddress()
]
)
hubAction
}
// Parse events into attributes
def parse(String description) {
log.debug "Parsing '${description}'"
def msg = parseLanMessage(description)
log.debug "data ${msg.data}"
log.debug "headers ${msg.headers}"
}
private String getRPiAddress() {
return "${rpiip}:${rpiport}"
}
private String convertIPtoHex(ipAddress) {
String hex = ipAddress.tokenize( '.' ).collect { String.format( '%02x', it.toInteger() ) }.join()
return hex
}
private String convertPortToHex(port) {
String hexport = port.toString().format( '%04x', port.toInteger() )
return hexport
}
{
"name": "RPiSmartCam-smartthings",
"version": "0.1.0",
"description": "Abstract out access to webcamera",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"repository": {
"type": "git",
"url": "https://github.com/stjohnjohnson/RPiSmartCam.git"
},
"bugs": {
"url": "https://github.com/stjohnjohnson/RPiSmartCam/issues"
},
"homepage": "https://github.com/stjohnjohnson/RPiSmartCam",
"author": "St. John Johnson <st.john.johnson@gmail.com>",
"license": "MIT",
"dependencies": {
"body-parser": "^1.14.1",
"express": "^4.13.3",
"request": "^2.65.0"
}
}
var express = require('express'),
bodyparser = require('body-parser'),
request = require('request');
var callback,
app = express();
// Accept JSON
app.use(bodyparser.json());
// Refresh from SmartThings
app.post('/:id', function (req, res) {
console.log(req.query, req.params.id, req.headers);
callback = 'http://' + req.query.callback;
res.send({
'location': 'express'
});
});
// Send notifications every 5 seconds to test
setInterval(function() {
if (!callback) {
return;
}
console.log('Sending push event to', callback);
request.post({
url: callback,
json: {
'location': 'request'
}
}, function (error, resp) {
console.log('response', error, resp.statusCode);
});
}, 5000);
// Listen only on localhost
server = app.listen(8000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.