-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paththing-textual-definition.js
61 lines (52 loc) · 1.83 KB
/
thing-textual-definition.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
export default function (thing, thingType) {
if (!thing.UID || !thingType.UID) return ''
let definition = ''
if (thing.bridgeUID) {
definition +=
'# Attention: This Thing is provided by a Bridge (' +
thing.bridgeUID +
').' +
'\n# You can also include it within the Bridge block and remove' +
'\n# the reference between parentheses to the bridge below.\n\n'
}
definition += '# Thing definition (put in a .things file):\n\n'
definition += thingType.bridge ? 'Bridge' : 'Thing'
definition += ' ' + thing.UID
definition += ' ' + JSON.stringify(thing.label)
if (thing.location) { definition += ' @ ' + JSON.stringify(thing.location) }
if (thing.bridgeUID) definition += ' (' + thing.bridgeUID + ')'
definition += ' [ '
let parameters = []
for (let parameter in thing.configuration) {
if (!Array.isArray(thing.configuration[parameter])) {
parameters.push(
parameter +
'=' +
JSON.stringify(thing.configuration[parameter])
)
}
}
definition += parameters.join(', ') + ' ]'
// TODO: for bridges, handle things related to that bridge
let itemDefinitions = []
for (let channel of thing.channels) {
if (!channel.itemType) continue
let itemDefinition = ''
itemDefinition += channel.itemType
itemDefinition +=
' ' +
thing.label.replace(/[^0-9a-z]/gi, '') +
'_' +
channel.id.replace(/[^0-9a-z]/gi, '')
itemDefinition += ' "' + channel.label + '"'
itemDefinition += ' {channel=' + JSON.stringify(channel.uid) + '}'
itemDefinitions.push(itemDefinition)
}
if (itemDefinitions.length) {
definition += '\n\n\n# Item definitions (put in a .items file):\n\n'
definition += itemDefinitions.join('\n')
}
definition +=
'\n\n# END ' + thing.UID + ' - ' + thing.label + '\n\n'
return definition
}