homebridge-openwrt-control 0.0.2-beta.5 → 0.0.2-beta.6

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.
Files changed (3) hide show
  1. package/index.js +1 -1
  2. package/package.json +1 -1
  3. package/src/openwrt.js +41 -40
package/index.js CHANGED
@@ -27,7 +27,7 @@ class OpenWrtPlatform {
27
27
 
28
28
  api.on('didFinishLaunching', async () => {
29
29
  for (const deviceConfig of config.devices) {
30
- const { name, host, port, displayType } =deviceConfig;
30
+ const { name, host, displayType } = deviceConfig;
31
31
  if (!name || !host || !displayType) {
32
32
  log.warn(`Device: ${host || 'host missing'}, ${name || 'name missing'}, ${!displayType ? ', disply type disabled' : ''} in config, will not be published in the Home app`);
33
33
  continue;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "displayName": "OpenWrt Control",
3
3
  "name": "homebridge-openwrt-control",
4
- "version": "0.0.2-beta.5",
4
+ "version": "0.0.2-beta.6",
5
5
  "description": "Homebridge plugin to control OpenWrt flashed devices.",
6
6
  "license": "MIT",
7
7
  "author": "grzegorz914",
package/src/openwrt.js CHANGED
@@ -1,7 +1,7 @@
1
- import EventEmitter from "events";
2
- import axios from "axios";
1
+ import EventEmitter from 'events';
2
+ import axios from 'axios';
3
3
  import Functions from './functions.js';
4
- import ImpulseGenerator from "./impulsegenerator.js";
4
+ import ImpulseGenerator from './impulsegenerator.js';
5
5
 
6
6
  class OpenWrt extends EventEmitter {
7
7
  constructor(config) {
@@ -26,19 +26,19 @@ class OpenWrt extends EventEmitter {
26
26
 
27
27
  this.functions = new Functions();
28
28
  this.axiosInstance = axios.create({
29
- baseURL: `${config.host}/ubus`,
29
+ baseURL: `http://${config.host}/ubus`,
30
30
  timeout: 5000,
31
31
  headers: {
32
- "Content-Type": "application/json"
32
+ 'Content-Type': 'application/json'
33
33
  }
34
34
  });
35
35
 
36
36
  this.impulseGenerator = new ImpulseGenerator()
37
- .on("connect", () => this.handleWithLock(async () => {
37
+ .on('connect', () => this.handleWithLock(async () => {
38
38
  await this.connect();
39
39
  }))
40
- .on("state", (state) => {
41
- this.emit(state ? "success" : "warn", `Impulse generator ${state ? "started" : "stopped"}`);
40
+ .on('state', (state) => {
41
+ this.emit(state ? 'success' : 'warn', `Impulse generator ${state ? 'started' : 'stopped'}`);
42
42
  });
43
43
  }
44
44
 
@@ -49,7 +49,7 @@ class OpenWrt extends EventEmitter {
49
49
  try {
50
50
  await fn();
51
51
  } catch (error) {
52
- this.emit("error", `Impulse generator error: ${error.message}`
52
+ this.emit('error', `Impulse generator error: ${error.message}`
53
53
  );
54
54
  } finally {
55
55
  this.lock = false;
@@ -62,50 +62,51 @@ class OpenWrt extends EventEmitter {
62
62
  return this.sessionId;
63
63
  }
64
64
 
65
- const response = await this.axiosInstance.post("", {
66
- jsonrpc: "2.0",
65
+ const response = await this.axiosInstance.post('', {
66
+ jsonrpc: '2.0',
67
67
  id: 1,
68
- method: "call",
69
- params: ["00000000000000000000000000000000", "session", "login", { username: this.user, password: this.passwd }]
68
+ method: 'call',
69
+ params: ['00000000000000000000000000000000', 'session', 'login', { username: this.user, password: this.passwd }]
70
70
  });
71
71
 
72
72
  const result = response.data?.result?.[1];
73
73
  if (!result?.ubus_rpc_session) {
74
- throw new Error("ubus login failed");
74
+ throw new Error('ubus login failed');
75
75
  }
76
76
 
77
77
  this.sessionId = result.ubus_rpc_session;
78
78
  this.sessionExpiresAt = now + 240_000;
79
79
 
80
- if (this.logDebug) this.emit("debug", `Ubus login OK`);
80
+ if (this.logDebug) this.emit('debug', `Ubus login OK`);
81
81
  return this.sessionId;
82
82
  }
83
83
 
84
84
  async ubusCall(service, method, params = {}) {
85
85
  const session = await this.login();
86
86
 
87
- const response = await this.axiosInstance.post("", {
88
- jsonrpc: "2.0",
87
+ const response = await this.axiosInstance.post('', {
88
+ jsonrpc: '2.0',
89
89
  id: 2,
90
- method: "call",
90
+ method: 'call',
91
91
  params: [session, service, method, params]
92
92
  });
93
93
 
94
94
  if (response.data?.error) {
95
- throw new Error(response.data.error.message || "ubus call error");
95
+ throw new Error(response.data.error.message || 'ubus call error');
96
96
  }
97
97
 
98
98
  return response.data.result[1];
99
99
  }
100
100
 
101
101
  async connect() {
102
+ const openWrtInfo = { state: false, systemInfo: {}, wirelessStatus: {}, wirelessRadios: [], ssids: [] }
103
+
102
104
  try {
103
- const openWrtInfo = { state: false, systemInfo: {}, wirelessStatus: {}, wirelessRadios: [], ssids: [] }
104
- const systemInfo = await this.ubusCall("system", "board");
105
- if (this.logDebug) this.emit("debug", `System info data: ${JSON.stringify(systemInfo, null, 2)}`);
105
+ const systemInfo = await this.ubusCall('system', 'board');
106
+ if (this.logDebug) this.emit('debug', `System info data: ${JSON.stringify(systemInfo, null, 2)}`);
106
107
 
107
- const wirelessStatus = await this.ubusCall("network.wireless", "status");
108
- if (this.logDebug) this.emit("debug", `Wireless status data: ${JSON.stringify(wirelessStatus, null, 2)}`);
108
+ const wirelessStatus = await this.ubusCall('network.wireless', 'status');
109
+ if (this.logDebug) this.emit('debug', `Wireless status data: ${JSON.stringify(wirelessStatus, null, 2)}`);
109
110
 
110
111
  const wirelessRadios = Object.values(status.radios).map(radio => ({
111
112
  name: radio.name,
@@ -118,13 +119,13 @@ class OpenWrt extends EventEmitter {
118
119
  }));
119
120
 
120
121
  const ssids = wirelessRadios.flatMap(radio => radio.interfaces);
121
- this.emit("systemInfo", systemInfo);
122
- this.emit("wirelessStatus", wirelessStatus);
123
- this.emit("wirelessRadios", wirelessRadios);
124
- this.emit("ssids", ssids);
122
+ this.emit('systemInfo', systemInfo);
123
+ this.emit('wirelessStatus', wirelessStatus);
124
+ this.emit('wirelessRadios', wirelessRadios);
125
+ this.emit('ssids', ssids);
125
126
 
126
127
  if (this.firstRun) {
127
- this.emit("success", `Connect success`);
128
+ this.emit('success', `Connect success`);
128
129
  this.firstRun = false;
129
130
  }
130
131
 
@@ -134,11 +135,11 @@ class OpenWrt extends EventEmitter {
134
135
  openWrtInfo.wirelessRadios = wirelessRadios;
135
136
  openWrtInfo.ssids = ssids;
136
137
 
137
- if (this.logDebug) this.emit("debug", `OpenWrt Data: ${JSON.stringify(openWrtInfo, null, 2)}`);
138
+ if (this.logDebug) this.emit('debug', `OpenWrt Data: ${JSON.stringify(openWrtInfo, null, 2)}`);
138
139
  return openWrtInfo;
139
140
  } catch (error) {
140
- if (this.logError) this.emit("error", `Connect error: ${error.message}`);
141
- return null;
141
+ if (this.logError) this.emit('error', `Connect error: ${error.message}`);
142
+ return openWrtInfo;
142
143
  }
143
144
  }
144
145
 
@@ -146,29 +147,29 @@ class OpenWrt extends EventEmitter {
146
147
  switch (type) {
147
148
  case 'ssid':
148
149
  await this.handleWithLock(async () => {
149
- if (this.logDebug) this.emit("debug", `${state ? "Enabling" : "Disabling"} SSID ${ssidName}`);
150
+ if (this.logDebug) this.emit('debug', `${state ? 'Enabling' : 'Disabling'} SSID ${ssidName}`);
150
151
 
151
- const status = await this.ubusCall("network.wireless", "status");
152
+ const status = await this.ubusCall('network.wireless', 'status');
152
153
  const iface = await this.functions.findIfaceBySsid(status, ssidName);
153
154
  if (!iface) throw new Error(`SSID ${ssidName} not found`);
154
155
 
155
156
  const section = iface.section;
156
157
  if (!section) throw new Error(`No UCI section for SSID ${ssidName}`);
157
158
 
158
- await this.ubusCall("uci", "set",
159
+ await this.ubusCall('uci', 'set',
159
160
  {
160
- config: "wireless",
161
+ config: 'wireless',
161
162
  section: section,
162
163
  values: {
163
- disabled: state ? "0" : "1"
164
+ disabled: state ? '0' : '1'
164
165
  }
165
166
  }
166
167
  );
167
168
 
168
- await this.ubusCall("uci", "commit", { config: "wireless" });
169
- await this.ubusCall("network.wireless", "reload", {});
169
+ await this.ubusCall('uci', 'commit', { config: 'wireless' });
170
+ await this.ubusCall('network.wireless', 'reload', {});
170
171
 
171
- if (this.logDebug) this.emit("debug", `Send SSID ${ssidName} ${state ? "enabled" : "disabled"}`);
172
+ if (this.logDebug) this.emit('debug', `Send SSID ${ssidName} ${state ? 'enabled' : 'disabled'}`);
172
173
  });
173
174
  break;
174
175
  case 'switch':