/
ditto-scope.js
145 lines (131 loc) · 6.39 KB
/
ditto-scope.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* Defines the Ditto Protocol message which is understood by Eclipse Ditto.
* @typedef {Object} DittoProtocolMessage
* @property {string} topic - The topic of the Ditto Protocol message
* @property {string} path - The path containing the info what to change / what changed
* @property {Object.<string, string>} headers - The Ditto headers
* @property {*} [value] - The value to change to / changed value
* @property {number} [status] - The status code that indicates the result of the command.
* @property {Object} [extra] - The enriched extra fields when selected via "extraFields" option.
*/
/**
* Defines an external (not yet mapped) message.
* @typedef {Object} ExternalMessage
* @property {Object.<string, string>} headers - The external headers
* @property {string} [textPayload] - The String to be mapped
* @property {ArrayBuffer} [bytePayload] - The bytes to be mapped as ArrayBuffer
* @property {string} contentType - The external Content-Type, e.g. "application/json"
*/
/**
* Defines the Ditto scope containing helper methods.
*/
let Ditto = (function () {
/**
* Builds a Ditto Protocol message from the passed parameters.
* @param {string} namespace - The namespace of the entity in java package notation, e.g.: "org.eclipse.ditto". Or "_"
* (underscore) for connection announcements.
* @param {string} name - The name of the entity, e.g.: "device".
* @param {string} channel - The channel for the signal: "twin"|"live"|"none"
* @param {string} group - The affected group/entity: "things"|"policies"|"connections".
* @param {string} criterion - The criterion to apply: "commands"|"events"|"search"|"messages"|"announcements"|"errors".
* @param {string} action - The action to perform: "create"|"retrieve"|"modify"|"delete". Or the announcement name:
* "opened"|"closed"|"subjectDeletion". Or the subject of the message.
* @param {string} path - The path which is affected by the message (e.g.: "/attributes"), or the destination
* of a message (e.g.: "inbox"|"outbox").
* @param {Object.<string, string>} dittoHeaders - The headers Object containing all Ditto Protocol header values.
* @param {*} [value] - The value to apply / which was applied (e.g. in a "modify" action).
* @param {number} [status] - The status code that indicates the result of the command. If setting a status code,
* the Ditto Protocol Message will be interpreted as a response (e.g. content will be ignored when using 204).
* @param {Object} [extra] - The enriched extra fields when selected via "extraFields" option.
* @returns {DittoProtocolMessage} dittoProtocolMessage(s) -
* The mapped Ditto Protocol message or
* <code>null</code> if the message could/should not be mapped
*/
function buildDittoProtocolMsg(namespace, name, group, channel, criterion, action, path, dittoHeaders, value, status, extra) {
const topic = buildTopic(namespace, name, group, channel, criterion, action);
return {
topic,
path: path,
headers: dittoHeaders,
value: value,
status: status,
extra: extra,
};
}
/**
* Builds a Ditto Protocol topic from the passed parameters.
* @param {string} namespace - The namespace of the entity in java package notation, e.g.: "org.eclipse.ditto". Or "_"
* (underscore) for connection announcements.
* @param {string} name - The name of the entity, e.g.: "device".
* @param {string} channel - The channel for the signal: "twin"|"live"|"none"
* @param {string} group - The affected group/entity: "things"|"policies"|"connections".
* @param {string} criterion - The criterion to apply: "commands"|"events"|"search"|"messages"|"announcements"|"errors".
* @param {string} action - The action to perform: "create"|"retrieve"|"modify"|"delete". Or the announcement name:
* "opened"|"closed"|"subjectDeletion". Or the subject of the message.
* @returns {string} topic - the topic.
*/
function buildTopic(namespace, name, group, channel, criterion, action) {
const topicChannel = 'none' === channel ? '' : '/' + channel;
return namespace + "/" + name + "/" + group + topicChannel + "/" + criterion + "/" + action;
}
/**
* Builds an external message from the passed parameters.
* @param {Object.<string, string>} headers - The external headers Object containing header values
* @param {string} [textPayload] - The external mapped String
* @param {ArrayBuffer} [bytePayload] - The external mapped bytes as ArrayBuffer
* @param {string} [contentType] - The returned Content-Type
* @returns {ExternalMessage} externalMessage -
* The mapped external message or
* <code>null</code> if the message could/should not be mapped
*/
function buildExternalMsg(headers, textPayload, bytePayload, contentType) {
return {
headers: headers,
textPayload: textPayload,
bytePayload: bytePayload,
contentType: contentType,
};
}
/**
* Transforms the passed ArrayBuffer to a String interpreting the content of the passed arrayBuffer as unsigned 8
* bit integers.
*
* @param {ArrayBuffer} arrayBuffer the ArrayBuffer to transform to a String
* @returns {String} the transformed String
*/
function arrayBufferToString(arrayBuffer) {
return String.fromCharCode.apply(null, new Uint8Array(arrayBuffer));
}
/**
* Transforms the passed String to an ArrayBuffer using unsigned 8 bit integers.
*
* @param {String} string the String to transform to an ArrayBuffer
* @returns {ArrayBuffer} the transformed ArrayBuffer
*/
function stringToArrayBuffer(string) {
let buf = new ArrayBuffer(string.length);
let bufView = new Uint8Array(buf);
for (let i = 0, strLen = string.length; i < strLen; i++) {
bufView[i] = string.charCodeAt(i);
}
return buf;
}
/**
* Transforms the passed ArrayBuffer to a {ByteBuffer} (from bytebuffer.js library which needs to be loaded).
*
* @param {ArrayBuffer} arrayBuffer the ArrayBuffer to transform
* @returns {ByteBuffer} the transformed ByteBuffer
*/
function asByteBuffer(arrayBuffer) {
let byteBuffer = new ArrayBuffer(arrayBuffer.byteLength);
new Uint8Array(byteBuffer).set(new Uint8Array(arrayBuffer));
return dcodeIO.ByteBuffer.wrap(byteBuffer);
}
return {
buildDittoProtocolMsg: buildDittoProtocolMsg,
buildExternalMsg: buildExternalMsg,
arrayBufferToString: arrayBufferToString,
stringToArrayBuffer: stringToArrayBuffer,
asByteBuffer: asByteBuffer
}
})();