UNPKG

6.19 kBJavaScriptView Raw
1/* jshint -W097 */
2/* jshint -W030 */
3/* jshint strict:true */
4/* jslint node: true */
5/* jslint esversion: 6 */
6'use strict';
7
8const defaults = require(__dirname + '/devices/default').defaults;
9const shelly1 = require(__dirname + '/devices/shelly1').shelly1;
10const shelly1pm = require(__dirname + '/devices/shelly1pm').shelly1pm;
11const shelly1l = require(__dirname + '/devices/shelly1l').shelly1l;
12const shellyswitch = require(__dirname + '/devices/shellyswitch').shellyswitch;
13const shellyswitch25 = require(__dirname + '/devices/shellyswitch25').shellyswitch25;
14const shellyht = require(__dirname + '/devices/shellyht').shellyht;
15const shellysmoke = require(__dirname + '/devices/shellysmoke').shellysmoke;
16const shellybulb = require(__dirname + '/devices/shellybulb').shellybulb;
17const shellyrgbww = require(__dirname + '/devices/shellyrgbww').shellyrgbww;
18const shellyrgbw2 = require(__dirname + '/devices/shellyrgbw2').shellyrgbw2;
19const shelly2led = require(__dirname + '/devices/shelly2led').shelly2led;
20const shellyplug = require(__dirname + '/devices/shellyplug').shellyplug;
21const shellyplugs = require(__dirname + '/devices/shellyplugs').shellyplugs;
22const shelly4pro = require(__dirname + '/devices/shelly4pro').shelly4pro;
23const shellysense = require(__dirname + '/devices/shellysense').shellysense;
24const shellyem = require(__dirname + '/devices/shellyem').shellyem;
25const shellyem3 = require(__dirname + '/devices/shellyem3').shellyem3;
26const shellyflood = require(__dirname + '/devices/shellyflood').shellyflood;
27const shellydimmer = require(__dirname + '/devices/shellydimmer').shellydimmer;
28const shellydimmer2 = require(__dirname + '/devices/shellydimmer2').shellydimmer2;
29const shellydw = require(__dirname + '/devices/shellydw').shellydw;
30const shellydw2 = require(__dirname + '/devices/shellydw2').shellydw2;
31const shellybulbduo = require(__dirname + '/devices/shellybulbduo').shellybulbduo;
32const shellyvintage = require(__dirname + '/devices/shellyvintage').shellyvintage;
33const shellyix3 = require(__dirname + '/devices/shellyix3').shellyix3;
34const shellybutton1 = require(__dirname + '/devices/shellybutton1').shellybutton1;
35const shellygas = require(__dirname + '/devices/shellygas').shellygas;
36const shellyuni = require(__dirname + '/devices/shellyuni').shellyuni;
37const shellycolorbulb = require(__dirname + '/devices/shellycolorbulb').shellycolorbulb;
38const shellymotion = require(__dirname + '/devices/shellymotion').shellymotion;
39
40let 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
73let 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 // 'shellyrgbww': ['SHRGBWW-01'],
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 * Copy an object
108 * @param {*} obj
109 */
110function 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(); //or new Date(obj);
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
128function 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
145function 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
155function 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
162function getDeviceNameForMQTT(name) {
163 return devicenames[name] ? devicenames[name][0] : undefined;
164 // return devicenames[name];
165}
166
167function getDeviceNameForCoAP(name) {
168 let value = Object.keys(devicenames).find((key) => devicenames[key].indexOf(name) !== -1);
169 // let value = Object.keys(devicenames).find((key) => devicenames[key] === name);
170 return value;
171}
172
173module.exports = {
174 getAll: getAll,
175 getDeviceByType: getDeviceByType,
176 getDeviceNameForMQTT: getDeviceNameForMQTT,
177 getDeviceNameForCoAP: getDeviceNameForCoAP
178};