1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | 'use strict';
|
7 |
|
8 | const defaults = require(__dirname + '/devices/default').defaults;
|
9 | const shelly1 = require(__dirname + '/devices/shelly1').shelly1;
|
10 | const shelly1pm = require(__dirname + '/devices/shelly1pm').shelly1pm;
|
11 | const shelly1l = require(__dirname + '/devices/shelly1l').shelly1l;
|
12 | const shellyswitch = require(__dirname + '/devices/shellyswitch').shellyswitch;
|
13 | const shellyswitch25 = require(__dirname + '/devices/shellyswitch25').shellyswitch25;
|
14 | const shellyht = require(__dirname + '/devices/shellyht').shellyht;
|
15 | const shellysmoke = require(__dirname + '/devices/shellysmoke').shellysmoke;
|
16 | const shellybulb = require(__dirname + '/devices/shellybulb').shellybulb;
|
17 | const shellyrgbww = require(__dirname + '/devices/shellyrgbww').shellyrgbww;
|
18 | const shellyrgbw2 = require(__dirname + '/devices/shellyrgbw2').shellyrgbw2;
|
19 | const shelly2led = require(__dirname + '/devices/shelly2led').shelly2led;
|
20 | const shellyplug = require(__dirname + '/devices/shellyplug').shellyplug;
|
21 | const shellyplugs = require(__dirname + '/devices/shellyplugs').shellyplugs;
|
22 | const shelly4pro = require(__dirname + '/devices/shelly4pro').shelly4pro;
|
23 | const shellysense = require(__dirname + '/devices/shellysense').shellysense;
|
24 | const shellyem = require(__dirname + '/devices/shellyem').shellyem;
|
25 | const shellyem3 = require(__dirname + '/devices/shellyem3').shellyem3;
|
26 | const shellyflood = require(__dirname + '/devices/shellyflood').shellyflood;
|
27 | const shellydimmer = require(__dirname + '/devices/shellydimmer').shellydimmer;
|
28 | const shellydimmer2 = require(__dirname + '/devices/shellydimmer2').shellydimmer2;
|
29 | const shellydw = require(__dirname + '/devices/shellydw').shellydw;
|
30 | const shellydw2 = require(__dirname + '/devices/shellydw2').shellydw2;
|
31 | const shellybulbduo = require(__dirname + '/devices/shellybulbduo').shellybulbduo;
|
32 | const shellyvintage = require(__dirname + '/devices/shellyvintage').shellyvintage;
|
33 | const shellyix3 = require(__dirname + '/devices/shellyix3').shellyix3;
|
34 | const shellybutton1 = require(__dirname + '/devices/shellybutton1').shellybutton1;
|
35 | const shellygas = require(__dirname + '/devices/shellygas').shellygas;
|
36 | const shellyuni = require(__dirname + '/devices/shellyuni').shellyuni;
|
37 | const shellycolorbulb = require(__dirname + '/devices/shellycolorbulb').shellycolorbulb;
|
38 | const shellymotion = require(__dirname + '/devices/shellymotion').shellymotion;
|
39 |
|
40 | let devices = {
|
41 | 'shellyswitch': shellyswitch,
|
42 | 'shellyswitch25': shellyswitch25,
|
43 | 'shelly1': shelly1,
|
44 | 'shelly1pm': shelly1pm,
|
45 | 'shelly1l': shelly1l,
|
46 | 'shellyht': shellyht,
|
47 | 'shellysmoke': shellysmoke,
|
48 | 'shellybulb': shellybulb,
|
49 | 'shellyrgbww': shellyrgbww,
|
50 | 'shellyrgbw2': shellyrgbw2,
|
51 | 'shelly2led': shelly2led,
|
52 | 'shellyplug': shellyplug,
|
53 | 'shelly4pro': shelly4pro,
|
54 | 'shellysense': shellysense,
|
55 | 'shellyplug-s': shellyplugs,
|
56 | 'shellyem': shellyem,
|
57 | 'shellyem3': shellyem3,
|
58 | 'shellyflood': shellyflood,
|
59 | 'shellydimmer': shellydimmer,
|
60 | 'shellydw': shellydw,
|
61 | 'shellydw2': shellydw2,
|
62 | 'ShellyBulbDuo': shellybulbduo,
|
63 | 'ShellyVintage': shellyvintage,
|
64 | 'shellyix3': shellyix3,
|
65 | 'shellybutton1': shellybutton1,
|
66 | 'shellygas': shellygas,
|
67 | 'shellydimmer2': shellydimmer2,
|
68 | 'shellyuni': shellyuni,
|
69 | 'shellycolorbulb': shellycolorbulb,
|
70 | 'shellymotion': shellymotion
|
71 | };
|
72 |
|
73 | let devicenames = {
|
74 | 'shellyswitch': ['SHSW-21'],
|
75 | 'shellyswitch25': ['SHSW-25'],
|
76 | 'shelly1': ['SHSW-1'],
|
77 | 'shelly1pm': ['SHSW-PM'],
|
78 | 'shelly1l': ['SHSW-L'],
|
79 | 'shellyht': ['SHHT-1'],
|
80 | 'shellysmoke': ['SHSM-01'],
|
81 | 'shellybulb': ['SHBLB-1'],
|
82 |
|
83 | 'shellyrgbw2': ['SHRGBW2'],
|
84 | 'shelly2led': ['SH2LED'],
|
85 | 'shellyplug': ['SHPLG-1'],
|
86 | 'shelly4pro': ['SHSW-44'],
|
87 | 'shellysense': ['SHSEN-1'],
|
88 | 'shellyplug-s': ['SHPLG-S', 'SHPLG2-1'],
|
89 | 'shellyem': ['SHEM'],
|
90 | 'shellyem3': ['SHEM-3'],
|
91 | 'shellyflood': ['SHWT-1'],
|
92 | 'shellydimmer': ['SHDM-1'],
|
93 | 'shellydw': ['SHDW-1'],
|
94 | 'shellydw2': ['SHDW-2'],
|
95 | 'ShellyBulbDuo': ['SHBDUO-1'],
|
96 | 'ShellyVintage': ['SHVIN-1'],
|
97 | 'shellyix3': ['SHIX3-1'],
|
98 | 'shellybutton1': ['SHBTN-1', 'SHBTN-2'],
|
99 | 'shellygas': ['SHGS-1'],
|
100 | 'shellydimmer2': ['SHDM-2'],
|
101 | 'shellyuni': ['SHUNI-1'],
|
102 | 'shellycolorbulb': ['SHCB-1'],
|
103 | 'shellymotion': ['SHMOS-01']
|
104 | };
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | function clone(obj) {
|
111 | if (obj === null || typeof (obj) !== 'object' || 'isActiveClone' in obj)
|
112 | return obj;
|
113 | let temp;
|
114 | if (obj instanceof Date)
|
115 | temp = new obj.constructor();
|
116 | else
|
117 | temp = obj.constructor();
|
118 | for (let key in obj) {
|
119 | if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
120 | obj['isActiveClone'] = null;
|
121 | temp[key] = clone(obj[key]);
|
122 | delete obj['isActiveClone'];
|
123 | }
|
124 | }
|
125 | return temp;
|
126 | }
|
127 |
|
128 | function deleteNoDisplay(device, protocol) {
|
129 | if (protocol === 'coap' || protocol === 'mqtt') {
|
130 | for (let key in device) {
|
131 | let dev = device[key];
|
132 | if (protocol === 'coap' && dev.coap && dev.coap.no_display) {
|
133 | delete device[key];
|
134 | continue;
|
135 | }
|
136 | if (protocol === 'mqtt' && dev.mqtt && dev.mqtt.no_display) {
|
137 | delete device[key];
|
138 | continue;
|
139 | }
|
140 | }
|
141 | }
|
142 | return device;
|
143 | }
|
144 |
|
145 | function getAll(protocol) {
|
146 | let cdevices = clone(devices);
|
147 | if (protocol === 'coap' || protocol === 'mqtt') {
|
148 | for (let key in cdevices) {
|
149 | cdevices[key] = deleteNoDisplay(cdevices[key], protocol);
|
150 | }
|
151 | }
|
152 | return cdevices;
|
153 | }
|
154 |
|
155 | function getDeviceByType(type, protocol) {
|
156 | if (!devices[type]) return;
|
157 | let device = clone(Object.assign(devices[type], defaults));
|
158 | device = deleteNoDisplay(device, protocol);
|
159 | return device;
|
160 | }
|
161 |
|
162 | function getDeviceNameForMQTT(name) {
|
163 | return devicenames[name] ? devicenames[name][0] : undefined;
|
164 |
|
165 | }
|
166 |
|
167 | function getDeviceNameForCoAP(name) {
|
168 | let value = Object.keys(devicenames).find((key) => devicenames[key].indexOf(name) !== -1);
|
169 |
|
170 | return value;
|
171 | }
|
172 |
|
173 | module.exports = {
|
174 | getAll: getAll,
|
175 | getDeviceByType: getDeviceByType,
|
176 | getDeviceNameForMQTT: getDeviceNameForMQTT,
|
177 | getDeviceNameForCoAP: getDeviceNameForCoAP
|
178 | };
|