-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstrophe.control.js
executable file
·129 lines (119 loc) · 4.23 KB
/
strophe.control.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
* This program is distributed under the terms of the MIT license.
* Please see the LICENSE file for details.
*
* Copyright 2014, Sustainable Innovation AB http://sust.se, and Swedish Institute computer Science http://sisc.se.
* This was developed as part of the http://iea.sust.se project.
* Authors: Olov Stahl, Anders Wallberg
*/
/** File: strophe.control.js
* Strophe IOT control (XEP 325) plugin
*
* Implements basic functionality of the xmpp-iot functinality
* ToDo add forms mgmt
*/
var CONTROL_SET_EVENT = 'control_set_event';
var CONTROL_PARAM_EVENT = 'control_param_event';
var CONTROL_SET_ERROR_EVENT = 'control_set_error_event';
var CONTROL_GET_ERROR_EVENT = 'control_get_error_event';
var XEP_325_TYPE_BOOLEAN = 'boolean';
var XEP_325_TYPE_INT = 'int';
var XEP_325_TYPE_LONG = 'long';
var XEP_325_TYPE_DOUBLE = 'double';
var XEP_325_TYPE_STRING = 'string';
var XEP_325_TYPE_DATE = 'date';
var XEP_325_TYPE_TIME = 'time';
var XEP_325_RESULT = 'result';
var XEP_325_ERROR = 'error';
var XEP_325_SET_RESPONSE = 'setResponse';
var XEP_325_GET_FORM_RESPONSE = 'getFormResponse';
var XEP_325_RESPONSE_CODE = 'responseCode';
var XEP_325_RESPONSE_CODE_OK = 'OK';
var XEP_325_NODE = 'node';
var XEP_325_FIELD = 'field';
var XEP_325_VAR = 'var';
var XEP_325_TYPE = 'type';
/** Function: Strophe.addConnectionPlugin
* Extend Strophe.Connection to have member 'control'
*
* Parameters:
* (String) ns - name
* (Object) dictionary with plugin functions
*
* Returns:
*
*/
Strophe.addConnectionPlugin('control', {
init: function (connection) {
this.connection = connection;
connection.addHandler(control_on_iq_error, null, 'iq', XEP_325_ERROR);
connection.addHandler(control_on_iq_result, null, 'iq', XEP_325_RESULT);
},
set: function (to, nodeId, param, type, val) {
log('control.set to=' + to + ' nodeId=' + nodeId + ' param=' + param + ' type=' + type + ' value=' + val);
var iq = $iq({type: 'set', to: to}).c('set', {xmlns: control_getXmlNameSpace(), 'xml:lang': 'en'}).c('node', {'nodeId': nodeId}).up().c(type, {name: param, value: val}); // XXX validate type
// var iq = $iq({type: 'set', to: to}).c('set', {xmlns: control_getXmlNameSpace(), 'xml:lang': 'en'}).c(type, {name: param, value: val}); // XXX validate type
connection.sendIQ(iq);
},
getForm: function(to) {
// log('control.getForm to=' + to);
var iq = $iq({type: 'get', to: to}).c('getForm', {xmlns: control_getXmlNameSpace()});
connection.sendIQ(iq);
}
});
/** Function: control_getXmlNameSpace
*
*/
function control_getXmlNameSpace() {
return 'urn:xmpp:iot:control'; // XXX
}
/** Function: control_on_iq_error
* called when we receive a iq message of type error
*/
function control_on_iq_error(stanza) {
var type = $(stanza).attr('type');
var from = $(stanza).attr('from');
var id = $(stanza).attr('id');
// log('control_on_iq_error type=' + type + ' from=' + from + ' id=' + id);
var tag = $(stanza).find(XEP_325_SET_RESPONSE);
if (tag.length > 0) {
$(document).trigger(CONTROL_SET_ERROR_EVENT, [from, id]);
} else {
tag = $(stanza).find(XEP_325_GET_FORM_RESPONSE);
if (tag.length > 0) {
$(document).trigger(CONTROL_GET_ERROR_EVENT, [from]);
} else {
// log('control_on_iq: ignoring message');
}
}
return true;
}
/** Function: control_on_iq_error
* called when we receive a iq message of type result
*/
function control_on_iq_result(stanza) {
var type = $(stanza).attr('type');
var from = $(stanza).attr('from');
// log('control_on_iq_result type=' + type + ' from=' + from);
var tag = $(stanza).find(XEP_325_SET_RESPONSE);
if (tag.length > 0) {
var code = $(tag).attr(XEP_325_RESPONSE_CODE);
log('control_on_iq: response code is ' + code);
var nodeId = null;
var fieldName = null;
var node = $(tag).find(XEP_325_NODE);
$(document).trigger(CONTROL_SET_EVENT, [from, nodeId, fieldName, code]);
} else {
tag = $(stanza).find(XEP_325_GET_FORM_RESPONSE);
if (tag.length > 0) {
$(tag).find(XEP_325_FIELD).each(function() {
var paramName = $(this).attr(XEP_325_VAR);
var paramType = $(this).attr(XEP_325_TYPE);
$(document).trigger(CONTROL_PARAM_EVENT, [from, paramName, paramType]);
});
} else {
// log('control_on_iq: ignoring message');
}
}
return true;
}