mirror of
https://github.com/mtan93/SmartThingsPublic.git
synced 2026-03-08 05:31:56 +00:00
Modifying 'Publish a SmartApp for OpenT2T - HMAC support'
This commit is contained in:
@@ -1,3 +1,7 @@
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.security.InvalidKeyException;
|
||||
|
||||
/**
|
||||
* OpenT2T SmartApp Test
|
||||
*
|
||||
@@ -116,14 +120,21 @@ def installed() {
|
||||
|
||||
def updated() {
|
||||
log.debug "Updating with settings: ${settings}"
|
||||
if(state.deviceSubscriptionMap == null){
|
||||
|
||||
//Initialize state variables if didn't exist.
|
||||
if( state.deviceSubscriptionMap == null ){
|
||||
state.deviceSubscriptionMap = [:]
|
||||
log.debug "deviceSubscriptionMap created."
|
||||
}
|
||||
if( state.locationSubscriptionMap == null){
|
||||
state.locationSubscriptionMap = [:]
|
||||
if( state.locationSubscriptionMap == null ){
|
||||
state.locationSubscriptionMap = [:]
|
||||
log.debug "locationSubscriptionMap created."
|
||||
}
|
||||
if(state.verificationKeyMap == null){
|
||||
state.verificationKeyMap = [:]
|
||||
log.debug "verificationKeyMap created."
|
||||
}
|
||||
|
||||
unsubscribe()
|
||||
registerAllDeviceSubscriptions()
|
||||
}
|
||||
@@ -132,9 +143,11 @@ def initialize() {
|
||||
log.debug "Initializing with settings: ${settings}"
|
||||
state.deviceSubscriptionMap = [:]
|
||||
log.debug "deviceSubscriptionMap created."
|
||||
registerAllDeviceSubscriptions()
|
||||
state.locationSubscriptionMap = [:]
|
||||
log.debug "locationSubscriptionMap created."
|
||||
state.verificationKeyMap = [:]
|
||||
log.debug "verificationKeyMap created."
|
||||
registerAllDeviceSubscriptions()
|
||||
}
|
||||
|
||||
/*** Subscription Functions ***/
|
||||
@@ -144,22 +157,12 @@ def registerAllDeviceSubscriptions() {
|
||||
registerChangeHandler(inputs)
|
||||
}
|
||||
|
||||
//Subscribe to events from a list of devices
|
||||
def registerChangeHandler(myList) {
|
||||
myList.each { myDevice ->
|
||||
def theAtts = myDevice.supportedAttributes
|
||||
theAtts.each {att ->
|
||||
subscribe(myDevice, att.name, deviceEventHandler)
|
||||
log.info "Registering for ${myDevice.displayName}.${att.name}"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Endpoints function: Subscribe to events from a specific device
|
||||
def registerDeviceChange() {
|
||||
def subscriptionEndpt = params.subscriptionURL
|
||||
def deviceId = params.deviceId
|
||||
def myDevice = findDevice(deviceId)
|
||||
def myDevice = findDevice(deviceId)
|
||||
|
||||
if( myDevice == null ){
|
||||
httpError(404, "Cannot find device with device ID ${deviceId}.")
|
||||
}
|
||||
@@ -179,12 +182,18 @@ def registerDeviceChange() {
|
||||
state.deviceSubscriptionMap[deviceId] << subscriptionEndpt
|
||||
log.info "Added subscription URL: ${subscriptionEndpt} for ${myDevice.displayName}"
|
||||
}
|
||||
|
||||
if(params.key != null){
|
||||
state.verificationKeyMap[subscriptionEndpt] = params.key
|
||||
log.info "Added verification key: ${params.key} for ${subscriptionEndpt}"
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
httpError(500, "something went wrong: $e")
|
||||
}
|
||||
|
||||
log.info "Current subscription map is ${state.deviceSubscriptionMap}"
|
||||
log.info "Current verification key map is ${state.verificationKeyMap}"
|
||||
return ["succeed"]
|
||||
}
|
||||
|
||||
@@ -206,6 +215,7 @@ def unregisterDeviceChange() {
|
||||
} else {
|
||||
state.deviceSubscriptionMap[deviceId].remove(subscriptionEndpt)
|
||||
}
|
||||
state.verificationKeyMap.remove(subscriptionEndpt)
|
||||
log.info "Removed subscription URL: ${subscriptionEndpt} for ${myDevice.displayName}"
|
||||
}
|
||||
} else {
|
||||
@@ -216,13 +226,14 @@ def unregisterDeviceChange() {
|
||||
httpError(500, "something went wrong: $e")
|
||||
}
|
||||
|
||||
log.info "Current subscription map is ${state.deviceSubscriptionMap}"
|
||||
log.info "Current subscription map is ${state.deviceSubscriptionMap}"
|
||||
log.info "Current verification key map is ${state.verificationKeyMap}"
|
||||
}
|
||||
|
||||
//Endpoints function: Subscribe to device additiona/removal updated in a location
|
||||
def registerDeviceGraph() {
|
||||
def subscriptionEndpt = params.subscriptionURL
|
||||
|
||||
|
||||
if (subscriptionEndpt != null && subscriptionEndpt != "undefined"){
|
||||
subscribe(location, "DeviceCreated", locationEventHandler, [filterEvents: false])
|
||||
subscribe(location, "DeviceUpdated", locationEventHandler, [filterEvents: false])
|
||||
@@ -230,12 +241,19 @@ def registerDeviceGraph() {
|
||||
|
||||
if(state.locationSubscriptionMap[location.id] == null){
|
||||
state.locationSubscriptionMap.put(location.id, [subscriptionEndpt])
|
||||
log.info "Added subscription URL: ${subscriptionEndpt} for Location ${location.name}"
|
||||
} else if (!state.locationSubscriptionMap[location.id].contains(subscriptionEndpt)){
|
||||
log.info "Added subscription URL: ${subscriptionEndpt} for Location ${location.name}"
|
||||
}else if (!state.locationSubscriptionMap[location.id].contains(subscriptionEndpt)){
|
||||
state.locationSubscriptionMap[location.id] << subscriptionEndpt
|
||||
log.info "Added subscription URL: ${subscriptionEndpt} for Location ${location.name}"
|
||||
log.info "Added subscription URL: ${subscriptionEndpt} for Location ${location.name}"
|
||||
}
|
||||
|
||||
if(params.key != null){
|
||||
state.verificationKeyMap[subscriptionEndpt] = params.key
|
||||
log.info "Added verification key: ${params.key} for ${subscriptionEndpt}"
|
||||
}
|
||||
|
||||
log.info "Current location subscription map is ${state.locationSubscriptionMap}"
|
||||
log.info "Current verification key map is ${state.verificationKeyMap}"
|
||||
return ["succeed"]
|
||||
} else {
|
||||
httpError(400, "missing input parameter: subscriptionURL")
|
||||
@@ -254,6 +272,7 @@ def unregisterDeviceGraph() {
|
||||
} else {
|
||||
state.locationSubscriptionMap[location.id].remove(subscriptionEndpt)
|
||||
}
|
||||
state.verificationKeyMap.remove(subscriptionEndpt)
|
||||
log.info "Removed subscription URL: ${subscriptionEndpt} for Location ${location.name}"
|
||||
}
|
||||
}else{
|
||||
@@ -263,27 +282,39 @@ def unregisterDeviceGraph() {
|
||||
httpError(500, "something went wrong: $e")
|
||||
}
|
||||
|
||||
log.info "Current location subscription map is ${state.locationSubscriptionMap}"
|
||||
log.info "Current location subscription map is ${state.locationSubscriptionMap}"
|
||||
log.info "Current verification key map is ${state.verificationKeyMap}"
|
||||
}
|
||||
|
||||
//When events are triggered, send HTTP post to web socket servers
|
||||
def deviceEventHandler(evt) {
|
||||
def evt_device = evt.device
|
||||
def evt_deviceType = getDeviceType(evt_device)
|
||||
def deviceInfo
|
||||
|
||||
def params = [ body: [deviceName: evt_device.displayName, deviceId: evt_device.id, locationId: location.id] ]
|
||||
|
||||
if(evt.data != null){
|
||||
def evtDevice = evt.device
|
||||
def evtDeviceType = getDeviceType(evtDevice)
|
||||
def deviceData = [];
|
||||
|
||||
if(evt.data != null){
|
||||
def evtData = parseJson(evt.data)
|
||||
log.info "Received event for ${evt_device.displayName}, data: ${evtData}, description: ${evt.descriptionText}"
|
||||
log.info "Received event for ${evtDevice.displayName}, data: ${evtData}, description: ${evt.descriptionText}"
|
||||
}
|
||||
|
||||
if(evtDeviceType == "thermostat") {
|
||||
deviceData = [name: evtDevice.displayName, id: evtDevice.id, status:evtDevice.status, deviceType:evtDeviceType, manufacturer:evtDevice.manufacturerName, model:evtDevice.modelName, attributes: deviceAttributeList(evtDevice, evtDeviceType), locationMode: getLocationModeInfo(), locationId: location.id ]
|
||||
} else {
|
||||
deviceData = [name: evtDevice.displayName, id: evtDevice.id, status:evtDevice.status, deviceType:evtDeviceType, manufacturer:evtDevice.manufacturerName, model:evtDevice.modelName, attributes: deviceAttributeList(evtDevice, evtDeviceType), locationId: location.id ]
|
||||
}
|
||||
|
||||
def params = [ body: deviceData ]
|
||||
|
||||
//send event to all subscriptions urls
|
||||
log.debug "Current subscription urls for ${evt_device.displayName} is ${state.deviceSubscriptionMap[evt_device.id]}"
|
||||
state.deviceSubscriptionMap[evt_device.id].each {
|
||||
log.debug "Current subscription urls for ${evtDevice.displayName} is ${state.deviceSubscriptionMap[evtDevice.id]}"
|
||||
state.deviceSubscriptionMap[evtDevice.id].each {
|
||||
params.uri = "${it}"
|
||||
if(state.verificationKeyMap[it] != null ){
|
||||
def key = state.verificationKeyMap[it]
|
||||
params.header = [Signature: ComputHMACValue(key, groovy.json.JsonOutput.toJson(params.body))]
|
||||
}
|
||||
log.trace "POST URI: ${params.uri}"
|
||||
log.trace "Header: ${params.header}"
|
||||
log.trace "Payload: ${params.body}"
|
||||
try{
|
||||
httpPostJson(params) { resp ->
|
||||
@@ -301,15 +332,22 @@ def locationEventHandler(evt) {
|
||||
switch(evt.name){
|
||||
case "DeviceCreated":
|
||||
case "DeviceDeleted":
|
||||
def evt_device = evt.device
|
||||
def evt_deviceType = getDeviceType(evt_device)
|
||||
log.info "DeviceName: ${evt_device.displayName}, DeviceID: ${evt_device.id}, deviceType: ${evt_deviceType}"
|
||||
|
||||
def params = [ body: [ eventType:evt.name, deviceId: evt_device.id, locationId: location.id ] ]
|
||||
def evtDevice = evt.device
|
||||
def evtDeviceType = getDeviceType(evtDevice)
|
||||
def params = [ body: [ eventType:evt.name, deviceId: evtDevice.id, locationId: location.id ] ]
|
||||
|
||||
if (evt.name == "DeviceDeleted" && state.deviceSubscriptionMap[deviceId] != null){
|
||||
state.deviceSubscriptionMap.remove(evtDevice.id)
|
||||
}
|
||||
|
||||
state.locationSubscriptionMap[location.id].each {
|
||||
params.uri = "${it}"
|
||||
if(state.verificationKeyMap[it] != null ){
|
||||
def key = state.verificationKeyMap[it]
|
||||
params.header = [Signature: ComputHMACValue(key, groovy.json.JsonOutput.toJson(params.body))]
|
||||
}
|
||||
log.trace "POST URI: ${params.uri}"
|
||||
log.trace "Header: ${params.header}"
|
||||
log.trace "Payload: ${params.body}"
|
||||
try{
|
||||
httpPostJson(params) { resp ->
|
||||
@@ -326,6 +364,23 @@ def locationEventHandler(evt) {
|
||||
}
|
||||
}
|
||||
|
||||
private ComputHMACValue(key, data){
|
||||
try {
|
||||
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA1")
|
||||
Mac mac = Mac.getInstance("HmacSHA1")
|
||||
mac.init(secretKeySpec)
|
||||
byte[] digest = mac.doFinal(data.getBytes("UTF-8"))
|
||||
return byteArrayToString(digest)
|
||||
} catch (InvalidKeyException e) {
|
||||
log.error "Invalid key exception while converting to HMac SHA1"
|
||||
}
|
||||
}
|
||||
|
||||
private def byteArrayToString(byte[] data) {
|
||||
BigInteger bigInteger = new BigInteger(1, data)
|
||||
String hash = bigInteger.toString(16)
|
||||
return hash
|
||||
}
|
||||
|
||||
/*** Device Query/Update Functions ***/
|
||||
|
||||
|
||||
Reference in New Issue
Block a user