homebridge-melcloud-control 2.5.0-beta.0 → 2.5.0-beta.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,10 +7,10 @@
7
7
  <form id="configForm">
8
8
  <div class="text-center">
9
9
  <label id="accountName" style="font-size: 23px">Account Name</label><br>
10
- <label id="info" style="text-align: center; font-size: 17px;">MELCloud Account</label><br><br>
10
+ <label id="info" style="text-align: center; font-size: 17px;">MELCloud</label><br><br>
11
11
  </div>
12
12
  <div class="form-group">
13
- <label for="name">Account Name</label>
13
+ <label for="name">Name</label>
14
14
  <input id="name" type="text" class="form-control"><br>
15
15
  <label for="user">MELCloud User</label>
16
16
  <input id="user" type="text" class="form-control"><br>
@@ -20,8 +20,7 @@
20
20
  <input id="language" type="text" class="form-control"><br>
21
21
  </div>
22
22
  <div class="text-center">
23
- <button id="logIn" type="button" class="btn btn-primary">Log In</button>
24
- <button id="getDevices" type="button" class="btn btn-primary">Get Devices</button>
23
+ <button id="logIn" type="button" class="btn btn-primary">Connect to MELCloud</button>
25
24
  <button id="configButton" type="button" class="btn btn-primary"><i class="fas fa-gear"></i></button>
26
25
  </div>
27
26
  </form>
@@ -57,11 +56,11 @@
57
56
  const setRemoveAtribute = j === i ? document.getElementById(`button${j}`).setAttribute("class", "btn btn-secondary") : document.getElementById(`button${j}`).setAttribute("class", "btn btn-primary");
58
57
  }
59
58
 
60
- document.getElementById('accountName').value = pluginConfig[0].accounts[i].name || '';
59
+ document.getElementById('accountName').innerHTML = pluginConfig[0].accounts[i].name || '';
61
60
  document.getElementById('name').value = pluginConfig[0].accounts[i].name || '';
62
61
  document.getElementById('user').value = pluginConfig[0].accounts[i].user || '';
63
62
  document.getElementById('passwd').value = pluginConfig[0].accounts[i].passwd || '';
64
- document.getElementById('lnguage').value = pluginConfig[0].accounts[i].language || '';
63
+ document.getElementById('language').value = pluginConfig[0].accounts[i].language || '';
65
64
 
66
65
  const accountConfigured = pluginConfig[0].accounts[i].name && pluginConfig[0].accounts[i].user && pluginConfig[0].accounts[i].passwd && pluginConfig[0].accounts[i].language;
67
66
  const setButtonName = accountConfigured ? document.getElementById('logIn').removeAttribute('disabled') : document.getElementById('logIn').setAttribute('disabled');
@@ -101,7 +100,7 @@
101
100
 
102
101
  try {
103
102
  await homebridge.updatePluginConfig(pluginConfig)
104
- document.getElementById('info').innerHTML = '';
103
+ document.getElementById('info').innerHTML = 'Connecting...';
105
104
 
106
105
  const accountName = pluginConfig[0].accounts[this.deviceIndex].name;
107
106
  const user = pluginConfig[0].accounts[this.deviceIndex].user;
@@ -111,7 +110,6 @@
111
110
  const refreshInterval = pluginConfig[0].accounts[this.deviceIndex].refreshInterval;
112
111
 
113
112
  const payload = {
114
- mode: 'connect',
115
113
  accountName: accountName,
116
114
  user: user,
117
115
  passwd: passwd,
@@ -122,75 +120,51 @@
122
120
  const response = await homebridge.request('/connect', payload);
123
121
  const info = response.info;
124
122
  const status = response.status;
125
- const data = response.data;
126
- const error = response.error;
123
+ const devices = response.data ?? [];
127
124
 
128
125
  switch (status) {
129
126
  case 0:
130
- document.getElementById('info').innerHTML = info;
131
- document.getElementById('getDevices').removeAttribute('disabled');
132
- break;
133
- case 2:
134
- document.getElementById('getDevices').setAttribute('disabled');
135
- document.getElementById('info').innerHTML = `MELCloud error: ${JSON.stringify(error, null, 2)}`;
136
- document.getElementById('info').style.color = 'red';
137
- break;
138
- };
139
-
140
- homebridge.hideSpinner();
141
- } catch (e) {
142
- homebridge.toast.error(`Data error: ${e.message}`);
143
- } finally {
144
- homebridge.hideSpinner();
145
- };
146
- });
147
-
148
- //get devices
149
- document.getElementById('getDevices').addEventListener('click', async () => {
150
- homebridge.showSpinner();
151
- try {
152
- const devicesId = [];
153
-
154
- const payload = {
155
- mode: 'getdevices'
156
- };
157
- const response = await homebridge.request('/getDevices', payload);
158
-
159
- const info = response.info;
160
- const status = response.status;
161
- const devices = response.data;
162
- const error = response.error;
163
-
164
- switch (status) {
165
- case 1:
166
127
  //get device info fom devices
167
128
  for (const deviceInfo of devices) {
168
129
  const deviceId = deviceInfo.DeviceID;
169
130
  const deviceName = deviceInfo.DeviceName;
170
-
171
- //prepare device if not in devices array
172
- if (!devicesId.includes(deviceId)) {
173
- devicesId.push(deviceId);
174
- }
175
131
  };
176
132
 
177
- document.getElementById('info').innerHTML = `${info} Found: ${devicesId.length} devices. Now You can start configure it.`;
133
+ document.getElementById('info').innerHTML = `${info} Found: ${devices.length} devices. Now You can start to configure it.`;
134
+ document.getElementById('info').style.color = 'green';
178
135
  await homebridge.updatePluginConfig(pluginConfig);
179
136
  await homebridge.savePluginConfig(pluginConfig);
180
137
  break;
138
+ case 1:
139
+ document.getElementById('info').innerHTML = info;
140
+ document.getElementById('info').style.color = 'green';
141
+ break;
181
142
  case 2:
182
- document.getElementById('info').innerHTML = `MELCloud error: ${JSON.stringify(error, null, 2)}`;
143
+ document.getElementById('info').innerHTML = info;
144
+ document.getElementById('info').style.color = 'white';
145
+ break;
146
+ case 3:
147
+ document.getElementById('info').innerHTML = info;
148
+ document.getElementById('info').style.color = 'gray';
149
+ break;
150
+ case 4:
151
+ document.getElementById('info').innerHTML = info;
152
+ document.getElementById('info').style.color = 'yellow';
153
+ break;
154
+ case 5:
155
+ document.getElementById('info').innerHTML = info;
183
156
  document.getElementById('info').style.color = 'red';
184
157
  break;
185
158
  };
186
159
 
187
160
  homebridge.hideSpinner();
161
+ document.getElementById('info').innerHTML = 'Please try again.';
188
162
  } catch (e) {
189
- homebridge.toast.error(`Get devices error: ${e.message}`);
163
+ document.getElementById('info').innerHTML = 'Please try again.';
164
+ homebridge.toast.error(`Data error: ${e.message}`);
190
165
  } finally {
191
166
  homebridge.hideSpinner();
192
- }
167
+ };
193
168
  });
194
-
195
169
  })();
196
170
  </script>
@@ -7,9 +7,6 @@ class PluginUiServer extends HomebridgePluginUiServer {
7
7
  constructor() {
8
8
  super();
9
9
 
10
- //get devices
11
- this.onRequest('/getdevices', this.start.bind(this));
12
-
13
10
  //connect
14
11
  this.onRequest('/connect', this.start.bind(this));
15
12
 
@@ -18,8 +15,6 @@ class PluginUiServer extends HomebridgePluginUiServer {
18
15
  };
19
16
 
20
17
  async start(payload) {
21
-
22
- const mode = payload.mode;
23
18
  const accountName = payload.accountName;
24
19
  const user = payload.user;
25
20
  const passwd = payload.passwd;
@@ -28,41 +23,56 @@ class PluginUiServer extends HomebridgePluginUiServer {
28
23
  const refreshInterval = payload.refreshInterval;
29
24
  const accountInfoFile = `${this.homebridgeStoragePath}/melcloud/${accountName}_Account`;
30
25
  const deviceInfoFile = `${this.homebridgeStoragePath}/melcloud/${accountName}_Device_`;
31
- const melCloud = new MelCloud(user, passwd, language, accountInfoFile, deviceInfoFile, enableDebugMode, refreshInterval);
32
-
26
+ const buildingsFile = `${this.homebridgeStoragePath}/melcloud/${accountName}_Buildings`;
33
27
  let data = {};
34
- switch (mode) {
35
- case 'connect':
36
- try {
37
- const response = await melCloud.connect();
38
- return data = {
39
- info: 'Connect to MELCloud success, now pres the button Get Devices.',
40
- status: 1,
41
- data: response
28
+
29
+ try {
30
+ const melCloud = new MelCloud(user, passwd, language, accountInfoFile, deviceInfoFile, buildingsFile, enableDebugMode, refreshInterval, true);
31
+ melCloud.on('devicesList', (devices) => {
32
+ data = {
33
+ info: 'Check devices list complete.',
34
+ status: 0,
35
+ data: devices
36
+ };
37
+ })
38
+ .on('success', (message) => {
39
+ data = {
40
+ info: message,
41
+ status: 1
42
42
  };
43
- } catch (error) {
44
- return data = {
45
- info: 'Connect error:',
46
- status: 2,
47
- data: error
43
+ })
44
+ .on('message', (message) => {
45
+ data = {
46
+ info: message,
47
+ status: 2
48
48
  };
49
- };
50
- case 'getdevices':
51
- try {
52
- const response = await melCloud.chackDevicesList();
53
- return data = {
54
- info: 'Get devices success.',
55
- status: 0,
56
- data: response
49
+ })
50
+ .on('debug', (debug) => {
51
+ data = {
52
+ info: debug,
53
+ status: 3
57
54
  };
58
- } catch (error) {
59
- return data = {
60
- info: 'Get devices error:',
61
- status: 2,
62
- data: error
55
+ })
56
+ .on('warn', (warn) => {
57
+ data = {
58
+ info: warn,
59
+ status: 4
63
60
  };
64
- };
61
+ })
62
+ .on('error', async (error) => {
63
+ data = {
64
+ info: error,
65
+ status: 5
66
+ };
67
+ });
68
+ } catch (error) {
69
+ data = {
70
+ info: error,
71
+ status: 5
72
+ };
65
73
  };
74
+
75
+ return data;
66
76
  };
67
77
  };
68
78
 
package/index.js CHANGED
@@ -78,7 +78,7 @@ class MelCloudPlatform {
78
78
  const deviceRefreshInterval = account.deviceRefreshInterval * 1000 || 5000;
79
79
 
80
80
  //melcloud account
81
- const melCloud = new MelCloud(user, passwd, language, accountInfoFile, buildingsFile, deviceInfoFile, enableDebugMode, refreshInterval);
81
+ const melCloud = new MelCloud(user, passwd, language, accountInfoFile, buildingsFile, deviceInfoFile, enableDebugMode, refreshInterval, false);
82
82
  melCloud.on('checkDevicesListComplete', (accountInfo, contextKey, deviceInfo) => {
83
83
  const deviceId = deviceInfo.DeviceID.toString();
84
84
  const deviceType = deviceInfo.Type;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "displayName": "MELCloud Control",
3
3
  "name": "homebridge-melcloud-control",
4
- "version": "2.5.0-beta.0",
4
+ "version": "2.5.0-beta.10",
5
5
  "description": "Homebridge plugin to control Mitsubishi Air Conditioner, Heat Pump and Energy Recovery Ventilation.",
6
6
  "license": "MIT",
7
7
  "author": "grzegorz914",
package/src/melcloud.js CHANGED
@@ -8,13 +8,14 @@ const ImpulseGenerator = require('./impulsegenerator.js');
8
8
  const CONSTANTS = require('./constants.json');
9
9
 
10
10
  class MelCloud extends EventEmitter {
11
- constructor(user, passwd, language, accountInfoFile, deviceInfoFile, buildingsFile, enableDebugMode, refreshInterval) {
11
+ constructor(user, passwd, language, accountInfoFile, deviceInfoFile, buildingsFile, enableDebugMode, refreshInterval, connectForConfig) {
12
12
  super();
13
13
  this.enableDebugMode = enableDebugMode;
14
14
  this.accountInfoFile = accountInfoFile;
15
15
  this.buildingsFile = buildingsFile;
16
16
  this.deviceInfoFile = deviceInfoFile;
17
17
  this.refreshInterval = refreshInterval;
18
+ this.connectForConfig = connectForConfig;
18
19
  this.devicesId = [];
19
20
 
20
21
  this.options = {
@@ -128,7 +129,7 @@ class MelCloud extends EventEmitter {
128
129
 
129
130
  //start impulse generator
130
131
  const timers = [{ name: 'checkDevicesList', sampling: this.refreshInterval }];
131
- this.impulseGenerator.start(timers)
132
+ const startImpulseGenerator = this.connectForConfig ? false : this.impulseGenerator.start(timers);
132
133
 
133
134
  return true;
134
135
  } catch (error) {
@@ -175,6 +176,12 @@ class MelCloud extends EventEmitter {
175
176
  }
176
177
  const debug3 = this.enableDebugMode ? this.emit('debug', `Found: ${devicesCount} devices.`) : false;
177
178
 
179
+ //emit devices list for config
180
+ this.emit('devicesList', devices);
181
+ if (this.connectForConfig) {
182
+ return;
183
+ }
184
+
178
185
  //get device info fom devices
179
186
  for (const deviceInfo of devices) {
180
187
  const deviceId = deviceInfo.DeviceID
@@ -192,7 +199,7 @@ class MelCloud extends EventEmitter {
192
199
  }
193
200
  };
194
201
 
195
- return devices;
202
+ return true;
196
203
  } catch (error) {
197
204
  this.impulseGenerator.stop();
198
205
  throw new Error(`Scanning for devices error: ${error.message ?? error}.`);