forked from anujsrc/devicehive-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevicehive.device.jquery.js
More file actions
82 lines (67 loc) · 2.65 KB
/
devicehive.device.jquery.js
File metadata and controls
82 lines (67 loc) · 2.65 KB
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
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof exports === 'object') {
module.exports = factory();
} else {
root.JqDHDevice = factory();
}
}(this, function() {
var jqUtils = (function ($) {
'use strict';
var toDeferred = function (func, ind, resWrapper) {
return function () {
var def = $.Deferred(),
args = Array.prototype.slice.call(arguments);
ind = (typeof ind !== 'undefined') ? ind : args.length;
args.splice(ind, 0, function (err, res) {
if (err) {
def.reject(err);
} else {
def.resolve(resWrapper ? resWrapper(res) : res);
}
});
return $.extend(true, func.apply(this, args), def.promise());
}
};
return {
toDeferredLast: function (func, resWrapper) {
return toDeferred(func, void 0, resWrapper);
},
toDeferredFirst: function (func, resWrapper) {
return toDeferred(func, 0, resWrapper);
}
};
}(jQuery));
var JqDHDevice = (function ($) {
'use strict';
var JqDHDevice = function (serviceUrl, deviceId, deviceKeyOrAccessKey) {
DHDevice.call(this, serviceUrl, deviceId, deviceKeyOrAccessKey);
};
$.extend(JqDHDevice.prototype, DHDevice.prototype, {
getDevice: jqUtils.toDeferredLast(DHDevice.prototype.getDevice),
registerDevice: jqUtils.toDeferredLast(DHDevice.prototype.registerDevice),
updateDevice: jqUtils.toDeferredLast(DHDevice.prototype.updateDevice),
subscribe: function(){
var self = this;
return jqUtils.toDeferredFirst(DHDevice.prototype.subscribe, function (sub) {
sub._handleMessage = function(deviceId, cmd){
self._populateCmd(cmd);
var oldUpdate = cmd.update;
cmd.update = jqUtils.toDeferredLast(oldUpdate);
sub._handleMessageOld(cmd)
};
return sub;
}).apply(this, arguments);
},
unsubscribe: jqUtils.toDeferredLast(DHDevice.prototype.unsubscribe),
sendNotification: jqUtils.toDeferredLast(DHDevice.prototype.sendNotification),
openChannel: jqUtils.toDeferredFirst(DHDevice.prototype.openChannel),
closeChannel: jqUtils.toDeferredLast(DHDevice.prototype.closeChannel)
});
return $.dhDevice = function (serviceUrl, deviceId, deviceKeyOrAccessKey) {
return new JqDHDevice(serviceUrl, deviceId, deviceKeyOrAccessKey);
};
}(jQuery));
return JqDHDevice;
}));