-
-
Save joseananio/61719c4da1693d211f33ba64d73389ae to your computer and use it in GitHub Desktop.
This file contains 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
var iotivity = require("../../iotivity-node/lowlevel"); | |
module.exports = { | |
init: function () { | |
var template = { | |
"handler": "iotivity", | |
"sid": "org.iotivity", | |
"capability": [ | |
{ | |
"cid": "org.iotivity.findresource", | |
"isauthrequired": false, | |
"endpoint": "/wsi/iotivity/getresource", | |
"endpointtype": "IOTIVITY", | |
"operation": "GET", | |
"params": { | |
"sampleUri": "/a/light" | |
}, | |
"tags": [ | |
"get reosurce properties" | |
] | |
}, | |
{ | |
"cid": "org.iotivity.getresource", | |
"isauthrequired": false, | |
"endpoint": "/wsi/iotivity/getresource", | |
"endpointtype": "IOTIVITY", | |
"operation": "GET", | |
"params": { | |
"sampleUri": "/a/light", | |
"address" : "TBD", | |
"property" : "power" | |
}, | |
"tags": [ | |
"get reosurce properties" | |
] | |
}, | |
{ | |
"cid": "org.iotivity.putresource", | |
"isauthrequired": false, | |
"endpoint": "/wsi/iotivity/getresource", | |
"endpointtype": "IOTIVITY", | |
"operation": "PUT", | |
"params": { | |
"sampleUri": "/a/light", | |
"address" : "TBD", | |
"nvp" : { | |
"property" : "power", | |
"value" : 10 | |
} | |
}, | |
"tags": [ | |
"get reosurce properties" | |
] | |
} | |
] | |
}; | |
// Start iotivity and set up the processing loop | |
iotivity.OCInit(null, 0, iotivity.OCMode.OC_CLIENT); | |
return template; | |
}, | |
findResource: function( response, uuid ) { | |
var index, | |
resources, | |
returnValue = false; | |
if ( response && | |
response.payload && | |
response.payload.resources && | |
response.payload.resources.length ) { | |
resources = response.payload.resources; | |
for ( index in resources ) { | |
if ( resources[ index ].uri === "/a/" + uuid ) { | |
return response.addr; | |
} | |
} | |
} | |
return returnValue; | |
}, | |
request: function (cap, auth, res) { | |
// Start iotivity and set up the processing loop | |
iotivity.OCInit( null, 0, iotivity.OCMode.OC_CLIENT ); | |
var intervalId = setInterval( function() { | |
iotivity.OCProcess(); | |
}, 1000 ); | |
var sampleUri = cap.params.sampleUri; | |
console.log("IoTivity Request Handling " + sampleUri); | |
var handleReceptacle = {}; | |
if(cap.cid == "org.iotivity.findresource") | |
{ | |
iotivity.OCDoResource( | |
handleReceptacle, | |
iotivity.OCMethod.OC_REST_DISCOVER, | |
iotivity.OC_MULTICAST_DISCOVERY_URI, | |
null, | |
null, | |
iotivity.OCConnectivityType.CT_DEFAULT, | |
iotivity.OCQualityOfService.OC_HIGH_QOS, | |
function (handle, response) { | |
for ( index = 0; index < resourceCount; index++ ) { | |
console.log(resources[ index ].uri + " vs " + sampleUri); | |
if ( resources[ index ].uri === sampleUri ) { | |
console.log("Received response to FindResource request:"); | |
console.log(JSON.stringify(response, null, 4)); | |
var index, | |
destination = response.addr, | |
getHandleReceptacle = {}, | |
resources = response && response.payload && response.payload.resources, | |
resourceCount = resources && resources.length ? resources.length : 0, | |
getResponseHandler = function (handle, response) { | |
console.log(JSON.stringify(response.addr, null, 4)); | |
res.send(200, response.addr); | |
return iotivity.OCStackApplicationResult.OC_STACK_DELETE_TRANSACTION; | |
}; | |
} | |
} | |
return iotivity.OCStackApplicationResult.OC_STACK_KEEP_TRANSACTION; | |
}, | |
null); | |
} | |
if(cap.cid == "org.iotivity.getresource") | |
{ | |
var getResult, getHandleReceptacle = {}; | |
console.log("Received response to PUT request:" + cap.params.addr + "---" + cap.params.property); | |
getResult = iotivity.OCDoResource( | |
getHandleReceptacle, | |
iotivity.OCMethod.OC_REST_GET, | |
sampleUri, | |
cap.params.addr, | |
{ | |
type: iotivity.OCPayloadType.PAYLOAD_TYPE_REPRESENTATION, | |
values: { | |
question: cap.params.property | |
} | |
}, | |
iotivity.OCConnectivityType.CT_DEFAULT, | |
iotivity.OCQualityOfService.OC_HIGH_QOS, | |
function( handle, response ) { | |
var returnValue = iotivity.OCStackApplicationResult.OC_STACK_KEEP_TRANSACTION; | |
if ( response && | |
response.payload && | |
response.payload.type === iotivity.OCPayloadType.PAYLOAD_TYPE_REPRESENTATION && | |
response.payload.values && | |
response.payload.values.answer ) { | |
returnValue = iotivity.OCStackApplicationResult.OC_STACK_DELETE_TRANSACTION; | |
console.log(JSON.stringify(response.payload.values.answer)); | |
res.send(200, response.addr); | |
} else { | |
console.log( JSON.stringify( response, null, 4 ) + "\n***" ); | |
} | |
return returnValue; | |
}, | |
null ); | |
} | |
if(cap.cid == "org.iotivity.putresource") | |
{ | |
var getResult, getHandleReceptacle = {}; | |
console.log("Received response to PUT request:" + cap.params.addr + "---" + cap.params.nvp); | |
getResult = iotivity.OCDoResource( | |
getHandleReceptacle, | |
iotivity.OCMethod.OC_REST_PUT, | |
sampleUri, | |
cap.params.addr, | |
{ | |
type: iotivity.OCPayloadType.PAYLOAD_TYPE_REPRESENTATION, | |
values: cap.params.nvp | |
}, | |
iotivity.OCConnectivityType.CT_DEFAULT, | |
iotivity.OCQualityOfService.OC_HIGH_QOS, | |
function( handle, response ) { | |
var returnValue = iotivity.OCStackApplicationResult.OC_STACK_KEEP_TRANSACTION; | |
if ( response && | |
response.payload && | |
response.payload.type === iotivity.OCPayloadType.PAYLOAD_TYPE_REPRESENTATION && | |
response.payload.values && | |
response.payload.values.answer ) { | |
returnValue = iotivity.OCStackApplicationResult.OC_STACK_DELETE_TRANSACTION; | |
console.log(JSON.stringify(response.payload.values.answer)); | |
res.send(200, response.addr); | |
} else { | |
console.log( JSON.stringify( response, null, 4 ) + "\n***" ); | |
} | |
return returnValue; | |
}, | |
null); | |
} | |
} | |
} | |
console.log("IoTivity initialized"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment