generated from homebridge/homebridge-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
platform.ts
191 lines (165 loc) · 7.53 KB
/
platform.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { API, DynamicPlatformPlugin, Logger, PlatformAccessory, PlatformConfig, Service, Characteristic } from 'homebridge';
import { PLATFORM_NAME, PLUGIN_NAME } from './settings';
import util from 'util';
import { PowerStationAccessory } from './powerStationAccessory';
import { ElectricityWattAccessory } from './electricityWattAccessory';
import { TemperatureAccessory } from './temperatureAccessory';
import child_process from 'node:child_process';
const execPromise = util.promisify(child_process.exec);
export class HomebridgeGoodWeInverter implements DynamicPlatformPlugin {
public readonly Service: typeof Service = this.api.hap.Service;
public readonly Characteristic: typeof Characteristic = this.api.hap.Characteristic;
// this is used to track restored cached accessories
public readonly accessories: PlatformAccessory[] = [];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private retry: any = {};
constructor(
public readonly log: Logger,
public readonly config: PlatformConfig,
public readonly api: API,
) {
this.log.debug('Finished initializing platform:', this.config.name);
this.api.on('didFinishLaunching', () => {
log.debug('Executed didFinishLaunching callback');
this.discoverDevices();
});
}
configureAccessory(accessory: PlatformAccessory) {
this.log.info('Loading accessory from cache:', accessory.displayName);
this.accessories.push(accessory);
}
loadElectricityAccessory(localInstanceConfig, instanceData, dataDigPath, name): ElectricityWattAccessory {
const uuid = this.api.hap.uuid.generate(`local_instance_${localInstanceConfig.localIp}_${dataDigPath.join()}`);
const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid);
let ret;
if(existingAccessory) {
this.log.info('found cached accessory', existingAccessory.displayName);
ret = new ElectricityWattAccessory(this, existingAccessory);
} else {
this.log.info('found new accessory', name);
const accessory = new this.api.platformAccessory(name, uuid);
accessory.context.device = { data: instanceData, dataDigPath: dataDigPath, id: localInstanceConfig.localIp, name: name };
ret = new ElectricityWattAccessory(this, accessory);
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
}
return ret;
}
loadTemperatureAccessory(localInstanceConfig, instanceData, dataDigPath, name): ElectricityWattAccessory {
const uuid = this.api.hap.uuid.generate(`local_instance_${localInstanceConfig.localIp}_${dataDigPath.join()}`);
const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid);
let ret;
if(existingAccessory) {
this.log.info('found cached accessory', existingAccessory.displayName);
ret = new TemperatureAccessory(this, existingAccessory);
} else {
this.log.info('found new accessory', name);
const accessory = new this.api.platformAccessory(name, uuid);
accessory.context.device = { data: instanceData, dataDigPath: dataDigPath, id: localInstanceConfig.localIp, name: name };
ret = new TemperatureAccessory(this, accessory);
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
}
return ret;
}
async lookupLocalInstance(localInstanceConfig) {
const cmd = `${this.config.SFKLocation} cudp ${localInstanceConfig.localIp} ${localInstanceConfig.port} -listen -noerror ` +
`-timeout=${localInstanceConfig.timeout} ${localInstanceConfig.options} -pure +xed "/[eol]//"`;
this.log.debug('running cmd: ', cmd);
let output = '';
try {
const { stdout, stderr } = await execPromise(cmd);
this.log.debug('cmd:response:stdout', stdout);
this.log.debug('cmd:response:stderr', stderr);
output = stdout;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch(e: any) {
// NOTE: not sure why it raises an exception and exitcode == 1
this.log.debug('cmd:error', e.stdout);
output = e.stdout;
}
// TODO: add error output here!
return this.parseInverter(output);
}
parseInverter(output) {
this.log.debug('parseInverter:output', output);
const ret = { internalTemperature: -1, mode: 'offline', generationToday: -1, power: -1, generationTotal: -1 };
const temperatureIndex = 174;
const temperature = Number('0x' + output.substring(temperatureIndex, temperatureIndex + 4)) / 10;
if(temperature && temperature < 80 && temperature > -20) {
ret.internalTemperature = temperature;
}
const modeIndex = 126;
const mode = Number('0x' + output.substring(modeIndex, modeIndex + 4));
if(mode && mode > 0) {
if(mode === 0) {
ret.mode = 'working';
} else if (mode === 1) {
ret.mode = 'normal';
} else if (mode === 2) {
ret.mode = 'error';
} else if (mode === 3) {
ret.mode = 'check';
}
}
// in kWh
const generationTodayIndex = 186;
const generationToday = Number('0x' + output.substring(generationTodayIndex, generationTodayIndex + 4)) * 100;
if (generationToday >= 0) {
ret.generationToday = generationToday;
}
// power in watt
const powerIndex = 122;
const power = Number('0x' + output.substring(powerIndex, powerIndex + 4));
if(power > 0) {
ret.power = power;
}
const generationTotalIndex = 190;
const generationTotal = Number('0x' + output.substring(generationTotalIndex, generationTotalIndex + 8)) / 10000;
if (generationTotal > 0) {
ret.generationTotal = generationTotal;
}
return ret;
}
async discoverDevices() {
for(const localInstanceConfig of this.config.localIps) {
const accessories: PowerStationAccessory[] = [];
const inverterData = await this.lookupLocalInstance(localInstanceConfig);
// TODO: add some timeout values, and wait upon timeout with requesting new lookup.
if(this.config.showCurrentPowerLevel) {
accessories.push(
this.loadElectricityAccessory(localInstanceConfig, inverterData, ['power'], 'Generation Watt'));
}
if(this.config.showDayTotal) {
accessories.push(
this.loadElectricityAccessory(localInstanceConfig, inverterData, ['generationToday'], 'Day Generation Wh'));
}
if(this.config.showInternalTemperature) {
accessories.push(
this.loadTemperatureAccessory(localInstanceConfig, inverterData, ['internalTemperature'], 'Internal Temperature'));
}
if(this.config.showTotal) {
accessories.push(
this.loadElectricityAccessory(localInstanceConfig, inverterData, ['generationTotal'], 'Total Generation MWh'));
}
// TODO: check if this will still work with a timeout catch around the block..
this.fetchLocalInstanceUpdate(localInstanceConfig.timeout + 500, localInstanceConfig, accessories);
}
}
async fetchLocalInstanceUpdate(timeout: number, localInstanceConfig, accessories) {
setInterval(async () => {
const inverterData = await this.lookupLocalInstance(localInstanceConfig);
this.log.debug(`fetchPowerStationUpdate local-instance: ${localInstanceConfig.localIp}:${localInstanceConfig.port} ` +
`with # ${accessories.length} accessories`);
this.log.debug('inverterData', inverterData);
for(const accessory of accessories) {
accessory.update(inverterData);
}
}, timeout);
}
timeout(ms: number) {
return new Promise( resolve => setTimeout(resolve, ms));
}
async sleep(timeout, fn, ...args) {
await this.timeout(timeout);
return fn(...args);
}
}