hoffmation-base 3.0.0-alpha.43 → 3.0.0-alpha.44

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.
package/lib/index.js CHANGED
@@ -84,9 +84,9 @@ class HoffmationBase {
84
84
  server_1.SonosService.addOwnDevices(server_1.OwnSonosDevices.ownDevices);
85
85
  server_1.SonosService.initialize();
86
86
  }
87
- if (server_1.SettingsService.settings.goveeDevicesPresent) {
87
+ if (server_1.SettingsService.settings.goveeSettings) {
88
88
  server_1.GooveeService.addOwnDevices(server_1.OwnGoveeDevices.ownDevices);
89
- server_1.GooveeService.initialize();
89
+ server_1.GooveeService.initialize(server_1.SettingsService.settings.goveeSettings);
90
90
  }
91
91
  if ((_b = server_1.SettingsService.settings.daikin) === null || _b === void 0 ? void 0 : _b.active) {
92
92
  server_1.DaikinService.addOwnDevices(server_1.OwnAcDevices.ownDevices);
@@ -23,6 +23,7 @@ import { iEnergyManagerSettings } from './iEnergyManagerSettings';
23
23
  import { iUnifiSettings } from './iUnifiSettings';
24
24
  import { iBlockAutomaticHandlerDefaults } from './iBlockAutomaticHandlerDefaults';
25
25
  import { iRestSettings } from './iRestSettings';
26
+ import { iGoveeSettings } from './iGoveeSettings';
26
27
  /**
27
28
  * The main configuration for Hoffmation normally stored in the `main-config.json` file.
28
29
  */
@@ -56,9 +57,9 @@ export interface iConfig {
56
57
  */
57
58
  espresense?: iEspresenseSettings;
58
59
  /**
59
- * Whether any Govee devices are present in the house and should be included in Hoffmation.
60
+ * @see iGoveeSettings
60
61
  */
61
- goveeDevicesPresent?: boolean;
62
+ goveeSettings?: iGoveeSettings;
62
63
  /**
63
64
  * @see iHeaterSettings
64
65
  */
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Settings to connect to an external GoveeApi
3
+ */
4
+ export interface iGoveeSettings {
5
+ /**
6
+ * The url of the {@link https://github.com/theimo1221/govee-express-api GoveeApi}
7
+ */
8
+ url: string;
9
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -26,3 +26,4 @@ export * from './iUnifiSettings';
26
26
  export * from './iUnifiConnectionOptions';
27
27
  export * from './iVictronSettings';
28
28
  export { iBlockAutomaticHandlerDefaults } from './iBlockAutomaticHandlerDefaults';
29
+ export { iGoveeSettings } from './iGoveeSettings';
@@ -54,6 +54,7 @@ class AcDevice {
54
54
  this._room = room;
55
55
  if (room !== undefined && !this._movementCallbackAdded) {
56
56
  this._movementCallbackAdded = true;
57
+ // TODO: Maybe change to any Movement
57
58
  (_a = room === null || room === void 0 ? void 0 : room.PraesenzGroup) === null || _a === void 0 ? void 0 : _a.addFirstEnterCallback(this.onRoomFirstEnter.bind(this));
58
59
  (_b = room === null || room === void 0 ? void 0 : room.PraesenzGroup) === null || _b === void 0 ? void 0 : _b.addLastLeftCallback(this.onRoomLastLeave.bind(this));
59
60
  }
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Interface for any device data updates
3
+ */
4
+ export interface GoveeDeviceData {
5
+ /**
6
+ * The device id form Govee
7
+ */
8
+ id: string;
9
+ /**
10
+ * The current state
11
+ */
12
+ actuatorOn: boolean;
13
+ /**
14
+ * The active brightness
15
+ */
16
+ brightness: number;
17
+ /**
18
+ * The hex coded active color starting with #
19
+ */
20
+ hexColor: string;
21
+ /**
22
+ * The colortemp
23
+ */
24
+ colortemp: number;
25
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,12 +1,14 @@
1
1
  import { OwnGoveeDevice } from './own-govee-device';
2
+ import { iGoveeSettings } from '../../config';
2
3
  export declare class GooveeService {
3
- private static devicesDict;
4
- private static goveeApi;
4
+ private static _serverUrl;
5
5
  private static ownDevices;
6
+ private static _refreshInterval;
6
7
  static addOwnDevices(gvDevice: {
7
8
  [name: string]: OwnGoveeDevice;
8
9
  }): void;
9
- static initialize(): void;
10
- private static initializeDevice;
11
- private static updateDevice;
10
+ static initialize(config: iGoveeSettings): void;
11
+ static cleanup(): void;
12
+ static sendCommand(device: OwnGoveeDevice, command: string): Promise<boolean>;
13
+ private static loadDevices;
12
14
  }
@@ -1,63 +1,90 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  Object.defineProperty(exports, "__esModule", { value: true });
3
26
  exports.GooveeService = void 0;
4
27
  const log_service_1 = require("../log-service");
5
28
  const models_1 = require("../../../models");
6
- const govee_lan_controller_1 = require("@j3lte/govee-lan-controller");
29
+ const http = __importStar(require("http"));
30
+ const utils_1 = require("../utils");
7
31
  class GooveeService {
8
32
  static addOwnDevices(gvDevice) {
9
33
  this.ownDevices = gvDevice;
10
34
  }
11
- static initialize() {
35
+ static initialize(config) {
12
36
  log_service_1.ServerLogService.writeLog(models_1.LogLevel.Debug, 'Initializing Goovee-Service');
13
- this.goveeApi = new govee_lan_controller_1.Govee({
14
- discover: true,
15
- discoverInterval: 300000,
16
- });
17
- this.goveeApi.on(govee_lan_controller_1.GoveeEventTypes.Scan, (data) => {
18
- log_service_1.ServerLogService.writeLog(models_1.LogLevel.Info, `GoveeDevice ${data.ip} scanned`);
19
- });
20
- this.goveeApi.on(govee_lan_controller_1.GoveeEventTypes.Ready, () => {
21
- log_service_1.ServerLogService.writeLog(models_1.LogLevel.Info, 'Govee ready');
22
- });
23
- this.goveeApi.on(govee_lan_controller_1.GoveeEventTypes.Error, (err) => {
24
- log_service_1.ServerLogService.writeLog(models_1.LogLevel.Error, `Govee-Error: ${err}`);
25
- });
26
- this.goveeApi.on(govee_lan_controller_1.GoveeEventTypes.NewDevice, (device) => {
27
- log_service_1.ServerLogService.writeLog(models_1.LogLevel.Trace, `GoveeDevice ${device.id} joined`);
28
- GooveeService.initializeDevice(device);
29
- });
30
- this.goveeApi.on(govee_lan_controller_1.GoveeEventTypes.UnknownDevice, (_data) => {
31
- log_service_1.ServerLogService.writeLog(models_1.LogLevel.Warn, 'GoveeDevice unknown');
32
- });
33
- this.goveeApi.on(govee_lan_controller_1.GoveeEventTypes.UnknownMessage, (data) => {
34
- log_service_1.ServerLogService.writeLog(models_1.LogLevel.Warn, `GoveeDevice unknown message: ${data}`);
35
- });
36
- this.goveeApi.discover();
37
+ this._serverUrl = config.url;
38
+ this._refreshInterval = utils_1.Utils.guardedInterval(this.loadDevices, 5000, this, true);
37
39
  }
38
- static initializeDevice(d) {
39
- this.devicesDict[d.id] = d;
40
- if (this.ownDevices[d.id] === undefined) {
41
- log_service_1.ServerLogService.writeLog(models_1.LogLevel.Alert, `Unknown Govee Device "${d.id}"`);
42
- return;
40
+ static cleanup() {
41
+ if (this._refreshInterval !== null) {
42
+ clearInterval(this._refreshInterval);
43
+ this._refreshInterval = null;
43
44
  }
44
- const ownDevice = this.ownDevices[d.id];
45
- ownDevice.device = d;
46
- ownDevice.update(d.getState());
47
- d.on(govee_lan_controller_1.GoveeDeviceEventTypes.StateChange, (data) => {
48
- log_service_1.ServerLogService.writeLog(models_1.LogLevel.Debug, `Govee ${d.id} state changed`);
49
- this.updateDevice(d, data);
45
+ }
46
+ static async sendCommand(device, command) {
47
+ return new Promise((resolve, _reject) => {
48
+ const requestLink = `${this._serverUrl}/device/${device.deviceId}/${command}`;
49
+ const req = http.get(requestLink, (res) => {
50
+ if (res.statusCode !== 200) {
51
+ log_service_1.ServerLogService.writeLog(models_1.LogLevel.Alert, `Failed to send Govee Command(${requestLink}): ${res.statusCode} - ${res.statusMessage}`);
52
+ resolve(false);
53
+ return;
54
+ }
55
+ resolve(true);
56
+ return;
57
+ });
58
+ req.on('error', (e) => {
59
+ log_service_1.ServerLogService.writeLog(models_1.LogLevel.Info, `Govee Error: ${e.message}`);
60
+ });
50
61
  });
51
- log_service_1.ServerLogService.writeLog(models_1.LogLevel.Debug, `Govee ${d.id} found at address ${d.ipAddr}`);
52
62
  }
53
- static updateDevice(device, data) {
54
- if (this.ownDevices[device.id] === undefined) {
55
- log_service_1.ServerLogService.writeLog(models_1.LogLevel.Alert, `Unknown Govee Device "${device.id}"`);
56
- return;
57
- }
58
- this.ownDevices[device.id].update(data);
63
+ static loadDevices() {
64
+ const req = http.get(`${this._serverUrl}/devices`, (res) => {
65
+ if (res.statusCode !== 200) {
66
+ log_service_1.ServerLogService.writeLog(models_1.LogLevel.Alert, `Failed to load Govee Devices: ${res.statusCode}`);
67
+ return;
68
+ }
69
+ res.on('data', (d) => {
70
+ const data = JSON.parse(d.toString());
71
+ Object.keys(data).forEach((key) => {
72
+ const deviceData = data[key];
73
+ const ownDevice = this.ownDevices[key];
74
+ if (ownDevice === undefined) {
75
+ log_service_1.ServerLogService.writeLog(models_1.LogLevel.DeepTrace, `Unknown Govee Device "${key}"`);
76
+ return;
77
+ }
78
+ ownDevice.update(deviceData);
79
+ });
80
+ });
81
+ });
82
+ req.on('error', (e) => {
83
+ log_service_1.ServerLogService.writeLog(models_1.LogLevel.Info, `Govee Error: ${e.message}`);
84
+ });
59
85
  }
60
86
  }
61
87
  exports.GooveeService = GooveeService;
62
- GooveeService.devicesDict = {};
88
+ GooveeService._serverUrl = 'http://127.0.0.1:3000';
63
89
  GooveeService.ownDevices = {};
90
+ GooveeService._refreshInterval = null;
@@ -3,11 +3,9 @@ import { ActuatorSetStateCommand, ActuatorToggleCommand, ActuatorWriteStateToDev
3
3
  import { LogDebugType } from '../log-service';
4
4
  import { DeviceCapability } from '../../devices/DeviceCapability';
5
5
  import { iLedRgbCct } from '../../devices/baseDeviceInterfaces/iLedRgbCct';
6
- import { Device as GoveeDevice, DeviceStateInfo as GoveeDeviceStateInfo } from '@j3lte/govee-lan-controller';
7
6
  import { BlockAutomaticHandler } from '../blockAutomaticHandler';
8
- import { DeviceState as GoveeDeviceState } from '@j3lte/govee-lan-controller/build/types/device';
7
+ import { GoveeDeviceData } from './govee-device-data';
9
8
  export declare class OwnGoveeDevice implements iLedRgbCct, iTemporaryDisableAutomatic {
10
- device: GoveeDevice | undefined;
11
9
  /** @inheritDoc */
12
10
  settings: LedSettings;
13
11
  /** @inheritDoc */
@@ -31,7 +29,7 @@ export declare class OwnGoveeDevice implements iLedRgbCct, iTemporaryDisableAuto
31
29
  private _color;
32
30
  private _colortemp;
33
31
  private _room;
34
- constructor(deviceId: string, ownDeviceName: string, roomName: string, device: GoveeDevice | undefined);
32
+ constructor(deviceId: string, ownDeviceName: string, roomName: string);
35
33
  get color(): string;
36
34
  get colortemp(): number;
37
35
  get room(): RoomBase;
@@ -54,7 +52,7 @@ export declare class OwnGoveeDevice implements iLedRgbCct, iTemporaryDisableAuto
54
52
  toggleActuator(c: ActuatorToggleCommand): boolean;
55
53
  toggleLight(c: LampToggleLightCommand): boolean;
56
54
  writeActuatorStateToDevice(c: ActuatorWriteStateToDeviceCommand): void;
57
- update(data: GoveeDeviceState & GoveeDeviceStateInfo): void;
55
+ update(data: GoveeDeviceData): void;
58
56
  private setBrightness;
59
57
  private setColor;
60
58
  private turnOn;
@@ -12,9 +12,9 @@ const lodash_1 = __importDefault(require("lodash"));
12
12
  const DeviceCapability_1 = require("../../devices/DeviceCapability");
13
13
  const api_1 = require("../api");
14
14
  const blockAutomaticHandler_1 = require("../blockAutomaticHandler");
15
+ const govee_service_1 = require("./govee-service");
15
16
  class OwnGoveeDevice {
16
- constructor(deviceId, ownDeviceName, roomName, device) {
17
- this.device = device;
17
+ constructor(deviceId, ownDeviceName, roomName) {
18
18
  /** @inheritDoc */
19
19
  this.settings = new models_1.LedSettings();
20
20
  /** @inheritDoc */
@@ -165,54 +165,55 @@ class OwnGoveeDevice {
165
165
  }
166
166
  update(data) {
167
167
  this.queuedValue = null;
168
- this._actuatorOn = data.onOff === 1;
168
+ this._actuatorOn = data.actuatorOn;
169
169
  this.brightness = data.brightness;
170
- this._color = `#${data.color.r.toString(16)}${data.color.g.toString(16)}${data.color.b.toString(16)}`;
171
- this._colortemp = data.colorTemInKelvin;
170
+ this._color = data.hexColor;
171
+ this._colortemp = data.colortemp;
172
172
  }
173
173
  setBrightness(brightness, cb) {
174
- var _a;
175
- (_a = this.device) === null || _a === void 0 ? void 0 : _a.setBrightness(brightness).then(() => {
174
+ govee_service_1.GooveeService.sendCommand(this, `brightness/${brightness}`).then((result) => {
175
+ if (!result) {
176
+ this.log(models_1.LogLevel.Error, 'Govee set brightness resulted in error');
177
+ }
176
178
  cb();
177
- }).catch((error) => {
178
- this.log(models_1.LogLevel.Error, `Govee set brightness resulted in error: ${error}`);
179
179
  });
180
180
  }
181
181
  setColor(color) {
182
- var _a;
183
- if (color === this._color) {
184
- return;
185
- }
186
- const colors = utils_1.Utils.hexToRgb(color);
187
- if (colors === null) {
188
- this.log(models_1.LogLevel.Error, `Govee set color resulted in error: ${color} is not a valid color`);
189
- return;
190
- }
191
- (_a = this.device) === null || _a === void 0 ? void 0 : _a.setColorRGB(colors).then(() => {
192
- this.log(models_1.LogLevel.Debug, `Govee set color to ${color}`, log_service_1.LogDebugType.SetActuator);
193
- }).catch((error) => {
194
- this.log(models_1.LogLevel.Error, `Govee set color resulted in error: ${error}`);
182
+ govee_service_1.GooveeService.sendCommand(this, `color/${color}`).then((result) => {
183
+ if (!result) {
184
+ this.log(models_1.LogLevel.Error, 'Govee set color resulted in error');
185
+ }
186
+ else {
187
+ this.log(models_1.LogLevel.Debug, `Govee set color to ${color}`, log_service_1.LogDebugType.SetActuator);
188
+ }
195
189
  });
196
190
  }
197
191
  turnOn() {
198
- var _a;
199
192
  if (this._actuatorOn) {
200
193
  return;
201
194
  }
202
195
  this.queuedValue = true;
203
- (_a = this.device) === null || _a === void 0 ? void 0 : _a.turnOn().then(() => {
204
- this.log(models_1.LogLevel.Debug, 'Govee turned on', log_service_1.LogDebugType.SetActuator);
205
- }).catch((error) => {
206
- this.log(models_1.LogLevel.Error, `Govee turn on resulted in error: ${error}`);
196
+ govee_service_1.GooveeService.sendCommand(this, `on/true`).then((result) => {
197
+ if (!result) {
198
+ this.log(models_1.LogLevel.Error, 'Govee turn on resulted in error');
199
+ }
200
+ else {
201
+ this.log(models_1.LogLevel.Debug, `Govee turned on`, log_service_1.LogDebugType.SetActuator);
202
+ }
207
203
  });
208
204
  }
209
205
  turnOff() {
210
- var _a;
211
- this.queuedValue = false;
212
- (_a = this.device) === null || _a === void 0 ? void 0 : _a.turnOff().then(() => {
213
- this.log(models_1.LogLevel.Debug, 'Govee turned off', log_service_1.LogDebugType.SetActuator);
214
- }).catch((error) => {
215
- this.log(models_1.LogLevel.Error, `Govee turn off resulted in error: ${error}`);
206
+ if (this._actuatorOn) {
207
+ return;
208
+ }
209
+ this.queuedValue = true;
210
+ govee_service_1.GooveeService.sendCommand(this, `on/false`).then((result) => {
211
+ if (!result) {
212
+ this.log(models_1.LogLevel.Error, 'Govee turn off resulted in error');
213
+ }
214
+ else {
215
+ this.log(models_1.LogLevel.Debug, `Govee turned off`, log_service_1.LogDebugType.SetActuator);
216
+ }
216
217
  });
217
218
  }
218
219
  }