68 lines
2.1 KiB
JavaScript
68 lines
2.1 KiB
JavaScript
define([
|
|
"dojo/_base/config",
|
|
'dojo/_base/declare',
|
|
'dstore/Rest',
|
|
'dstore/RequestMemory',
|
|
'dstore/SimpleQuery',
|
|
'dstore/Trackable',
|
|
"hos/Socket",
|
|
"dojo/request/xhr",
|
|
"dojox/uuid/generateRandomUuid"
|
|
], function(config, declare, Rest, RequestMemory, SimpleQuery, Trackable, Socket, xhr, generateRandomUuid){
|
|
|
|
var RuleEngineStore = declare([Rest, RequestMemory, SimpleQuery, Trackable]);
|
|
|
|
var _store = new RuleEngineStore({
|
|
target: config.hos.deviceRepoUrl
|
|
});
|
|
|
|
var _updateSocketOpened = false;
|
|
var _updateSocketClientId = generateRandomUuid();
|
|
var _updateSocket = new Socket({
|
|
url: config.hos.deviceRepoUrl + '?onevent=changes&clientid=' + _updateSocketClientId,
|
|
headers: {
|
|
"On-Event": "changes",
|
|
"Connection-Timeout": 60000,
|
|
"Client-Id": 1234
|
|
},
|
|
transport: xhr.post
|
|
});
|
|
_updateSocket.on("open", function(event){
|
|
_socketOpened = true;
|
|
console.log("Socket Opened ", event.message);
|
|
});
|
|
_updateSocket.on("close", function(event){
|
|
_socketOpened = false;
|
|
console.log("Socket Closed ", event.message);
|
|
});
|
|
_updateSocket.on("error", function(event){
|
|
_socketOpened = false;
|
|
console.log("Socket Error ", event.message);
|
|
});
|
|
_updateSocket.on("message", function(event) {
|
|
var data = JSON.parse(event.data);
|
|
switch (data['eventtype']) {
|
|
case "statechanged":
|
|
_store.evict(data['deviceid']);
|
|
_store.emit('update', {id: data['deviceid'], target: data['device']});
|
|
break;
|
|
}
|
|
console.log("Message: " + data);
|
|
});
|
|
_store.sendCommand = function(id, cmd) {
|
|
console.log("ToDo: send command", cmd ,"to " + id);
|
|
//var devurl = config.hos.deviceRepoUrl + "/" + id + "?method=command&value=" + encodeURIComponent(JSON.stringify(cmd));
|
|
var devurl = config.hos.deviceRepoUrl + "/" + id;
|
|
xhr.post(devurl, {handleAs: 'json', data: {method: 'command', value: JSON.stringify(cmd)}}).then(function(data){
|
|
// Do something with the handled data
|
|
}, function(err){
|
|
// Handle the error condition
|
|
}, function(evt){
|
|
// Handle a progress event from the request if the
|
|
// browser supports XHR2
|
|
});
|
|
};
|
|
|
|
return _store;
|
|
});
|