homebridge-sensus 1.0.0
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/config.schema.json +69 -0
- package/dist/accessory.d.ts +25 -0
- package/dist/accessory.js +102 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +7 -0
- package/dist/platform.d.ts +14 -0
- package/dist/platform.js +51 -0
- package/dist/sensusApi.d.ts +42 -0
- package/dist/sensusApi.js +144 -0
- package/dist/settings.d.ts +3 -0
- package/dist/settings.js +6 -0
- package/package.json +38 -0
- package/src/accessory.ts +164 -0
- package/src/index.ts +7 -0
- package/src/platform.ts +79 -0
- package/src/sensusApi.ts +197 -0
- package/src/settings.ts +2 -0
- package/tsconfig.json +18 -0
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
{
|
|
2
|
+
"pluginAlias": "SensusAnalytics",
|
|
3
|
+
"pluginType": "platform",
|
|
4
|
+
"singular": false,
|
|
5
|
+
"schema": {
|
|
6
|
+
"type": "object",
|
|
7
|
+
"properties": {
|
|
8
|
+
"name": {
|
|
9
|
+
"title": "Name",
|
|
10
|
+
"type": "string",
|
|
11
|
+
"default": "Sensus Water Meter",
|
|
12
|
+
"required": true
|
|
13
|
+
},
|
|
14
|
+
"baseUrl": {
|
|
15
|
+
"title": "Base URL",
|
|
16
|
+
"type": "string",
|
|
17
|
+
"placeholder": "https://yourcity.sensus-analytics.com",
|
|
18
|
+
"description": "The base URL of your Sensus Analytics portal (no trailing slash).",
|
|
19
|
+
"required": true
|
|
20
|
+
},
|
|
21
|
+
"username": {
|
|
22
|
+
"title": "Username",
|
|
23
|
+
"type": "string",
|
|
24
|
+
"required": true
|
|
25
|
+
},
|
|
26
|
+
"password": {
|
|
27
|
+
"title": "Password",
|
|
28
|
+
"type": "string",
|
|
29
|
+
"required": true
|
|
30
|
+
},
|
|
31
|
+
"accountNumber": {
|
|
32
|
+
"title": "Account Number",
|
|
33
|
+
"type": "string",
|
|
34
|
+
"required": true
|
|
35
|
+
},
|
|
36
|
+
"meterNumber": {
|
|
37
|
+
"title": "Meter Number",
|
|
38
|
+
"type": "string",
|
|
39
|
+
"required": true
|
|
40
|
+
},
|
|
41
|
+
"leakThreshold": {
|
|
42
|
+
"title": "Leak Alert Threshold (gallons)",
|
|
43
|
+
"type": "number",
|
|
44
|
+
"default": 150,
|
|
45
|
+
"description": "Daily usage (in gallons) above which a leak alert is triggered in HomeKit. Default: 150."
|
|
46
|
+
},
|
|
47
|
+
"pollInterval": {
|
|
48
|
+
"title": "Poll Interval (minutes)",
|
|
49
|
+
"type": "number",
|
|
50
|
+
"default": 30,
|
|
51
|
+
"description": "How often to fetch new data from Sensus Analytics, in minutes. Default: 30."
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
"layout": [
|
|
56
|
+
"name",
|
|
57
|
+
"baseUrl",
|
|
58
|
+
"username",
|
|
59
|
+
"password",
|
|
60
|
+
"accountNumber",
|
|
61
|
+
"meterNumber",
|
|
62
|
+
{
|
|
63
|
+
"type": "fieldset",
|
|
64
|
+
"title": "Advanced Options",
|
|
65
|
+
"expandable": true,
|
|
66
|
+
"items": ["leakThreshold", "pollInterval"]
|
|
67
|
+
}
|
|
68
|
+
]
|
|
69
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { PlatformAccessory } from 'homebridge';
|
|
2
|
+
import { SensusAnalyticsPlatform } from './platform';
|
|
3
|
+
import { SensusAnalyticsApi } from './sensusApi';
|
|
4
|
+
export declare class SensusWaterMeterAccessory {
|
|
5
|
+
private readonly platform;
|
|
6
|
+
private readonly accessory;
|
|
7
|
+
private readonly apiClient;
|
|
8
|
+
private readonly leakService;
|
|
9
|
+
private readonly leakThreshold;
|
|
10
|
+
private readonly pollIntervalMs;
|
|
11
|
+
private readonly eveConsumptionChar;
|
|
12
|
+
private readonly eveTotalChar;
|
|
13
|
+
private lastData;
|
|
14
|
+
constructor(platform: SensusAnalyticsPlatform, accessory: PlatformAccessory, apiClient: SensusAnalyticsApi);
|
|
15
|
+
/**
|
|
16
|
+
* Creates and registers a custom Characteristic on the LeakSensor service.
|
|
17
|
+
* If the characteristic already exists (restored from cache), it is returned as-is.
|
|
18
|
+
*/
|
|
19
|
+
private addEveCharacteristic;
|
|
20
|
+
/** Called by HomeKit to read the current leak state. */
|
|
21
|
+
private handleLeakDetectedGet;
|
|
22
|
+
/** Fetch latest data from Sensus Analytics and push updates to HomeKit. */
|
|
23
|
+
private poll;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=accessory.d.ts.map
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SensusWaterMeterAccessory = void 0;
|
|
4
|
+
// Eve for HomeKit custom characteristic UUIDs for water meters
|
|
5
|
+
const EVE_UUID_WATER_CONSUMPTION = 'E863F10D-079E-48FF-8F27-9C2605A29F52'; // daily usage
|
|
6
|
+
const EVE_UUID_TOTAL_WATER = 'E863F10C-079E-48FF-8F27-9C2605A29F52'; // odometer / total
|
|
7
|
+
const DEFAULT_POLL_INTERVAL_MINUTES = 30;
|
|
8
|
+
class SensusWaterMeterAccessory {
|
|
9
|
+
constructor(platform, accessory, apiClient) {
|
|
10
|
+
this.platform = platform;
|
|
11
|
+
this.accessory = accessory;
|
|
12
|
+
this.apiClient = apiClient;
|
|
13
|
+
this.lastData = null;
|
|
14
|
+
const { hap } = platform.api;
|
|
15
|
+
const { Service: Svc, Characteristic: Char } = platform;
|
|
16
|
+
this.leakThreshold = platform.config.leakThreshold ?? 150;
|
|
17
|
+
this.pollIntervalMs =
|
|
18
|
+
(platform.config.pollInterval ?? DEFAULT_POLL_INTERVAL_MINUTES) *
|
|
19
|
+
60 *
|
|
20
|
+
1000;
|
|
21
|
+
// ── Accessory Information ─────────────────────────────────────────────
|
|
22
|
+
this.accessory
|
|
23
|
+
.getService(Svc.AccessoryInformation)
|
|
24
|
+
.setCharacteristic(Char.Manufacturer, 'Sensus')
|
|
25
|
+
.setCharacteristic(Char.Model, 'Smart Water Meter')
|
|
26
|
+
.setCharacteristic(Char.SerialNumber, accessory.context.meterNumber ?? 'Unknown');
|
|
27
|
+
// ── Leak Sensor (primary HomeKit service) ─────────────────────────────
|
|
28
|
+
// Visible in Apple Home app as a leak sensor tile with alert notifications
|
|
29
|
+
this.leakService =
|
|
30
|
+
this.accessory.getService(Svc.LeakSensor) ?? this.accessory.addService(Svc.LeakSensor);
|
|
31
|
+
this.leakService.setCharacteristic(Char.Name, accessory.displayName);
|
|
32
|
+
this.leakService
|
|
33
|
+
.getCharacteristic(Char.LeakDetected)
|
|
34
|
+
.onGet(this.handleLeakDetectedGet.bind(this));
|
|
35
|
+
this.leakService
|
|
36
|
+
.getCharacteristic(Char.StatusActive)
|
|
37
|
+
.onGet(() => true);
|
|
38
|
+
// ── Eve Custom Characteristics ────────────────────────────────────────
|
|
39
|
+
// These are shown in the Eve for HomeKit app as consumption graphs.
|
|
40
|
+
// They are attached to the LeakSensor service so they share one accessory tile.
|
|
41
|
+
this.eveConsumptionChar = this.addEveCharacteristic('Water Consumption', EVE_UUID_WATER_CONSUMPTION, "float" /* hap.Formats.FLOAT */, ["ev" /* hap.Perms.NOTIFY */, "pr" /* hap.Perms.PAIRED_READ */], 0, 1000000, 0.001);
|
|
42
|
+
this.eveTotalChar = this.addEveCharacteristic('Total Water Consumption', EVE_UUID_TOTAL_WATER, "float" /* hap.Formats.FLOAT */, ["ev" /* hap.Perms.NOTIFY */, "pr" /* hap.Perms.PAIRED_READ */], 0, 1000000000, 0.001);
|
|
43
|
+
// ── Start polling ─────────────────────────────────────────────────────
|
|
44
|
+
this.poll();
|
|
45
|
+
setInterval(() => this.poll(), this.pollIntervalMs);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Creates and registers a custom Characteristic on the LeakSensor service.
|
|
49
|
+
* If the characteristic already exists (restored from cache), it is returned as-is.
|
|
50
|
+
*/
|
|
51
|
+
addEveCharacteristic(displayName, uuid, format, perms, minValue, maxValue, minStep) {
|
|
52
|
+
const { hap } = this.platform.api;
|
|
53
|
+
const existing = this.leakService.characteristics.find((c) => c.UUID === uuid);
|
|
54
|
+
if (existing) {
|
|
55
|
+
return existing;
|
|
56
|
+
}
|
|
57
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
58
|
+
const props = { format, unit: 'gal', minValue, maxValue, minStep, perms };
|
|
59
|
+
const char = new hap.Characteristic(displayName, uuid, props);
|
|
60
|
+
char.setValue(0);
|
|
61
|
+
this.leakService.addCharacteristic(char);
|
|
62
|
+
return char;
|
|
63
|
+
}
|
|
64
|
+
/** Called by HomeKit to read the current leak state. */
|
|
65
|
+
handleLeakDetectedGet() {
|
|
66
|
+
const { LEAK_DETECTED, LEAK_NOT_DETECTED } = this.platform.Characteristic.LeakDetected;
|
|
67
|
+
if (!this.lastData) {
|
|
68
|
+
return LEAK_NOT_DETECTED;
|
|
69
|
+
}
|
|
70
|
+
return this.lastData.daily.dailyUsage > this.leakThreshold
|
|
71
|
+
? LEAK_DETECTED
|
|
72
|
+
: LEAK_NOT_DETECTED;
|
|
73
|
+
}
|
|
74
|
+
/** Fetch latest data from Sensus Analytics and push updates to HomeKit. */
|
|
75
|
+
async poll() {
|
|
76
|
+
this.platform.log.debug('Sensus Analytics: polling for new data...');
|
|
77
|
+
const data = await this.apiClient.fetchData();
|
|
78
|
+
if (!data) {
|
|
79
|
+
this.platform.log.warn('Sensus Analytics: no data returned, will retry next interval');
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
this.lastData = data;
|
|
83
|
+
const { daily, hourly } = data;
|
|
84
|
+
const { LEAK_DETECTED, LEAK_NOT_DETECTED } = this.platform.Characteristic.LeakDetected;
|
|
85
|
+
const isLeaking = daily.dailyUsage > this.leakThreshold;
|
|
86
|
+
// Push updates to HomeKit
|
|
87
|
+
this.leakService.updateCharacteristic(this.platform.Characteristic.LeakDetected, isLeaking ? LEAK_DETECTED : LEAK_NOT_DETECTED);
|
|
88
|
+
// Eve consumption characteristics
|
|
89
|
+
this.eveConsumptionChar.updateValue(daily.dailyUsage);
|
|
90
|
+
this.eveTotalChar.updateValue(daily.odometer);
|
|
91
|
+
// Log a summary
|
|
92
|
+
const lastHour = hourly.at(-1);
|
|
93
|
+
this.platform.log.info(`[${this.accessory.displayName}] ` +
|
|
94
|
+
`daily=${daily.dailyUsage} ${daily.usageUnit} | ` +
|
|
95
|
+
`odometer=${daily.odometer} ${daily.usageUnit} | ` +
|
|
96
|
+
`billing=${daily.billingUsage} ${daily.usageUnit} | ` +
|
|
97
|
+
`leak=${isLeaking}` +
|
|
98
|
+
(lastHour ? ` | lastHourUsage=${lastHour.usage} | temp=${lastHour.temp}°F` : ''));
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
exports.SensusWaterMeterAccessory = SensusWaterMeterAccessory;
|
|
102
|
+
//# sourceMappingURL=accessory.js.map
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { API, Characteristic, DynamicPlatformPlugin, Logger, PlatformAccessory, PlatformConfig, Service } from 'homebridge';
|
|
2
|
+
export declare class SensusAnalyticsPlatform implements DynamicPlatformPlugin {
|
|
3
|
+
readonly log: Logger;
|
|
4
|
+
readonly config: PlatformConfig;
|
|
5
|
+
readonly api: API;
|
|
6
|
+
readonly Service: typeof Service;
|
|
7
|
+
readonly Characteristic: typeof Characteristic;
|
|
8
|
+
readonly cachedAccessories: PlatformAccessory[];
|
|
9
|
+
constructor(log: Logger, config: PlatformConfig, api: API);
|
|
10
|
+
/** Called by Homebridge to restore cached accessories on startup. */
|
|
11
|
+
configureAccessory(accessory: PlatformAccessory): void;
|
|
12
|
+
private discoverDevices;
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=platform.d.ts.map
|
package/dist/platform.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SensusAnalyticsPlatform = void 0;
|
|
4
|
+
const settings_1 = require("./settings");
|
|
5
|
+
const accessory_1 = require("./accessory");
|
|
6
|
+
const sensusApi_1 = require("./sensusApi");
|
|
7
|
+
class SensusAnalyticsPlatform {
|
|
8
|
+
constructor(log, config, api) {
|
|
9
|
+
this.log = log;
|
|
10
|
+
this.config = config;
|
|
11
|
+
this.api = api;
|
|
12
|
+
// Restored cached accessories from disk
|
|
13
|
+
this.cachedAccessories = [];
|
|
14
|
+
this.Service = this.api.hap.Service;
|
|
15
|
+
this.Characteristic = this.api.hap.Characteristic;
|
|
16
|
+
this.log.debug('Sensus Analytics platform initialising');
|
|
17
|
+
this.api.on('didFinishLaunching', () => {
|
|
18
|
+
this.discoverDevices();
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
/** Called by Homebridge to restore cached accessories on startup. */
|
|
22
|
+
configureAccessory(accessory) {
|
|
23
|
+
this.log.info('Restoring cached accessory:', accessory.displayName);
|
|
24
|
+
this.cachedAccessories.push(accessory);
|
|
25
|
+
}
|
|
26
|
+
discoverDevices() {
|
|
27
|
+
const { baseUrl, username, password, accountNumber, meterNumber } = this.config;
|
|
28
|
+
if (!baseUrl || !username || !password || !accountNumber || !meterNumber) {
|
|
29
|
+
this.log.error('Sensus Analytics: missing required config fields ' +
|
|
30
|
+
'(baseUrl, username, password, accountNumber, meterNumber)');
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
const apiClient = new sensusApi_1.SensusAnalyticsApi(baseUrl, username, password, accountNumber, meterNumber, this.log);
|
|
34
|
+
const displayName = this.config.name || 'Sensus Water Meter';
|
|
35
|
+
const uuid = this.api.hap.uuid.generate(`sensus-${meterNumber}`);
|
|
36
|
+
const existing = this.cachedAccessories.find((a) => a.UUID === uuid);
|
|
37
|
+
if (existing) {
|
|
38
|
+
this.log.info('Restoring water meter accessory:', existing.displayName);
|
|
39
|
+
new accessory_1.SensusWaterMeterAccessory(this, existing, apiClient);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
this.log.info('Adding new water meter accessory:', displayName);
|
|
43
|
+
const accessory = new this.api.platformAccessory(displayName, uuid);
|
|
44
|
+
accessory.context.meterNumber = meterNumber;
|
|
45
|
+
new accessory_1.SensusWaterMeterAccessory(this, accessory, apiClient);
|
|
46
|
+
this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
exports.SensusAnalyticsPlatform = SensusAnalyticsPlatform;
|
|
51
|
+
//# sourceMappingURL=platform.js.map
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Logger } from 'homebridge';
|
|
2
|
+
export interface DailyData {
|
|
3
|
+
dailyUsage: number;
|
|
4
|
+
usageUnit: string;
|
|
5
|
+
meterAddress: string;
|
|
6
|
+
lastRead: string;
|
|
7
|
+
meterId: string;
|
|
8
|
+
meterLat: number;
|
|
9
|
+
meterLong: number;
|
|
10
|
+
odometer: number;
|
|
11
|
+
billingUsage: number;
|
|
12
|
+
}
|
|
13
|
+
export interface HourlyEntry {
|
|
14
|
+
timestamp: number;
|
|
15
|
+
usage: number;
|
|
16
|
+
rain: number;
|
|
17
|
+
temp: number;
|
|
18
|
+
usageUnit: string;
|
|
19
|
+
rainUnit: string;
|
|
20
|
+
tempUnit: string;
|
|
21
|
+
}
|
|
22
|
+
export interface SensusData {
|
|
23
|
+
daily: DailyData;
|
|
24
|
+
hourly: HourlyEntry[];
|
|
25
|
+
}
|
|
26
|
+
export declare class SensusAnalyticsApi {
|
|
27
|
+
private readonly baseUrl;
|
|
28
|
+
private readonly username;
|
|
29
|
+
private readonly password;
|
|
30
|
+
private readonly accountNumber;
|
|
31
|
+
private readonly meterNumber;
|
|
32
|
+
private readonly log;
|
|
33
|
+
private readonly client;
|
|
34
|
+
private readonly jar;
|
|
35
|
+
private loggedIn;
|
|
36
|
+
constructor(baseUrl: string, username: string, password: string, accountNumber: string, meterNumber: string, log: Logger);
|
|
37
|
+
login(): Promise<boolean>;
|
|
38
|
+
fetchData(): Promise<SensusData | null>;
|
|
39
|
+
private fetchDailyData;
|
|
40
|
+
private fetchHourlyData;
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=sensusApi.d.ts.map
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SensusAnalyticsApi = void 0;
|
|
4
|
+
const axios_1 = require("axios");
|
|
5
|
+
const axios_cookiejar_support_1 = require("axios-cookiejar-support");
|
|
6
|
+
const tough_cookie_1 = require("tough-cookie");
|
|
7
|
+
class SensusAnalyticsApi {
|
|
8
|
+
constructor(baseUrl, username, password, accountNumber, meterNumber, log) {
|
|
9
|
+
this.baseUrl = baseUrl;
|
|
10
|
+
this.username = username;
|
|
11
|
+
this.password = password;
|
|
12
|
+
this.accountNumber = accountNumber;
|
|
13
|
+
this.meterNumber = meterNumber;
|
|
14
|
+
this.log = log;
|
|
15
|
+
this.loggedIn = false;
|
|
16
|
+
this.jar = new tough_cookie_1.CookieJar();
|
|
17
|
+
this.client = (0, axios_cookiejar_support_1.wrapper)(axios_1.default.create({
|
|
18
|
+
baseURL: baseUrl.replace(/\/$/, ''),
|
|
19
|
+
jar: this.jar,
|
|
20
|
+
withCredentials: true,
|
|
21
|
+
// Follow redirects but treat 302 as success for login
|
|
22
|
+
maxRedirects: 5,
|
|
23
|
+
timeout: 15000,
|
|
24
|
+
}));
|
|
25
|
+
}
|
|
26
|
+
async login() {
|
|
27
|
+
try {
|
|
28
|
+
this.log.debug('Sensus Analytics: attempting login...');
|
|
29
|
+
await this.client.post('/j_spring_security_check', new URLSearchParams({
|
|
30
|
+
j_username: this.username,
|
|
31
|
+
j_password: this.password,
|
|
32
|
+
}).toString(), {
|
|
33
|
+
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
34
|
+
// A 302 redirect means login was processed; axios follows it automatically
|
|
35
|
+
validateStatus: (s) => s < 400,
|
|
36
|
+
});
|
|
37
|
+
this.loggedIn = true;
|
|
38
|
+
this.log.debug('Sensus Analytics: login successful');
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
catch (err) {
|
|
42
|
+
this.loggedIn = false;
|
|
43
|
+
this.log.error('Sensus Analytics: login failed:', err.message);
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
async fetchData() {
|
|
48
|
+
if (!this.loggedIn) {
|
|
49
|
+
const ok = await this.login();
|
|
50
|
+
if (!ok) {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
try {
|
|
55
|
+
const [daily, hourly] = await Promise.all([
|
|
56
|
+
this.fetchDailyData(),
|
|
57
|
+
this.fetchHourlyData(),
|
|
58
|
+
]);
|
|
59
|
+
if (!daily) {
|
|
60
|
+
// Session may have expired — clear and retry once
|
|
61
|
+
this.loggedIn = false;
|
|
62
|
+
const ok = await this.login();
|
|
63
|
+
if (!ok) {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
const retried = await this.fetchDailyData();
|
|
67
|
+
if (!retried) {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
return { daily: retried, hourly: hourly ?? [] };
|
|
71
|
+
}
|
|
72
|
+
return { daily, hourly: hourly ?? [] };
|
|
73
|
+
}
|
|
74
|
+
catch (err) {
|
|
75
|
+
this.loggedIn = false;
|
|
76
|
+
this.log.error('Sensus Analytics: error fetching data:', err.message);
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
async fetchDailyData() {
|
|
81
|
+
const response = await this.client.post('/water/widget/byPage', {
|
|
82
|
+
group: 'meters',
|
|
83
|
+
accountNumber: this.accountNumber,
|
|
84
|
+
deviceId: this.meterNumber,
|
|
85
|
+
});
|
|
86
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
87
|
+
const device = response.data?.widgetList?.[0]?.data?.devices?.[0];
|
|
88
|
+
if (!device) {
|
|
89
|
+
this.log.warn('Sensus Analytics: unexpected daily data structure');
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
return {
|
|
93
|
+
dailyUsage: parseFloat(device.dailyUsage) || 0,
|
|
94
|
+
usageUnit: device.usageUnit || 'CCF',
|
|
95
|
+
meterAddress: device.meterAddress1 || '',
|
|
96
|
+
lastRead: device.lastRead || '',
|
|
97
|
+
meterId: String(device.meterId || ''),
|
|
98
|
+
meterLat: parseFloat(device.meterLat) || 0,
|
|
99
|
+
meterLong: parseFloat(device.meterLong) || 0,
|
|
100
|
+
odometer: parseFloat(device.latestReadUsage) || 0,
|
|
101
|
+
billingUsage: parseFloat(device.billingUsage) || 0,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
async fetchHourlyData() {
|
|
105
|
+
// Fetch the previous day's data (Sensus updates at midnight)
|
|
106
|
+
const yesterday = new Date();
|
|
107
|
+
yesterday.setDate(yesterday.getDate() - 1);
|
|
108
|
+
const startOfDay = new Date(yesterday.getFullYear(), yesterday.getMonth(), yesterday.getDate());
|
|
109
|
+
const endOfDay = new Date(startOfDay.getTime() + 86400000 - 1);
|
|
110
|
+
const response = await this.client.get(`/water/usage/${this.accountNumber}/${this.meterNumber}`, {
|
|
111
|
+
params: {
|
|
112
|
+
start: startOfDay.getTime(),
|
|
113
|
+
end: endOfDay.getTime(),
|
|
114
|
+
zoom: 'day',
|
|
115
|
+
page: 'null',
|
|
116
|
+
weather: '1',
|
|
117
|
+
},
|
|
118
|
+
});
|
|
119
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
120
|
+
const data = response.data;
|
|
121
|
+
if (!data?.operationSuccess) {
|
|
122
|
+
this.log.warn('Sensus Analytics: hourly data fetch unsuccessful');
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
const usageArray = data?.data?.usage;
|
|
126
|
+
if (!Array.isArray(usageArray) || usageArray.length < 2) {
|
|
127
|
+
return [];
|
|
128
|
+
}
|
|
129
|
+
// First element is units row: [usageUnit, rainUnit, tempUnit, altUnit]
|
|
130
|
+
const [usageUnit, rainUnit, tempUnit] = usageArray[0];
|
|
131
|
+
const rows = usageArray.slice(1);
|
|
132
|
+
return rows.map((row) => ({
|
|
133
|
+
timestamp: row[0],
|
|
134
|
+
usage: row[1] ?? 0,
|
|
135
|
+
rain: row[2] ?? 0,
|
|
136
|
+
temp: row[3] ?? 0,
|
|
137
|
+
usageUnit: usageUnit ?? 'CCF',
|
|
138
|
+
rainUnit: rainUnit ?? 'INCHES',
|
|
139
|
+
tempUnit: tempUnit ?? 'FAHRENHEIT',
|
|
140
|
+
}));
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
exports.SensusAnalyticsApi = SensusAnalyticsApi;
|
|
144
|
+
//# sourceMappingURL=sensusApi.js.map
|
package/dist/settings.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "homebridge-sensus",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Homebridge plugin for Sensus Analytics water meters",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"watch": "tsc --watch",
|
|
10
|
+
"prepare": "npm run build",
|
|
11
|
+
"prepublishOnly": "npm run build"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"homebridge-plugin",
|
|
15
|
+
"sensus",
|
|
16
|
+
"water",
|
|
17
|
+
"meter",
|
|
18
|
+
"homekit"
|
|
19
|
+
],
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=18.0.0",
|
|
22
|
+
"homebridge": ">=1.6.0"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"axios": "^1.6.0",
|
|
26
|
+
"axios-cookiejar-support": "^4.0.7",
|
|
27
|
+
"tough-cookie": "^4.1.3"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@types/node": "^20.0.0",
|
|
31
|
+
"@types/tough-cookie": "^4.0.5",
|
|
32
|
+
"homebridge": "^1.8.0",
|
|
33
|
+
"typescript": "^5.3.0"
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"homebridge": ">=1.6.0"
|
|
37
|
+
}
|
|
38
|
+
}
|
package/src/accessory.ts
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { PlatformAccessory, CharacteristicValue, Service, Characteristic } from 'homebridge';
|
|
2
|
+
import { SensusAnalyticsPlatform } from './platform';
|
|
3
|
+
import { SensusAnalyticsApi, SensusData } from './sensusApi';
|
|
4
|
+
|
|
5
|
+
// Eve for HomeKit custom characteristic UUIDs for water meters
|
|
6
|
+
const EVE_UUID_WATER_CONSUMPTION = 'E863F10D-079E-48FF-8F27-9C2605A29F52'; // daily usage
|
|
7
|
+
const EVE_UUID_TOTAL_WATER = 'E863F10C-079E-48FF-8F27-9C2605A29F52'; // odometer / total
|
|
8
|
+
|
|
9
|
+
const DEFAULT_POLL_INTERVAL_MINUTES = 30;
|
|
10
|
+
|
|
11
|
+
export class SensusWaterMeterAccessory {
|
|
12
|
+
private readonly leakService: Service;
|
|
13
|
+
private readonly leakThreshold: number;
|
|
14
|
+
private readonly pollIntervalMs: number;
|
|
15
|
+
|
|
16
|
+
// Direct references to Eve custom characteristics to avoid UUID lookups on update
|
|
17
|
+
private readonly eveConsumptionChar: Characteristic;
|
|
18
|
+
private readonly eveTotalChar: Characteristic;
|
|
19
|
+
|
|
20
|
+
private lastData: SensusData | null = null;
|
|
21
|
+
|
|
22
|
+
constructor(
|
|
23
|
+
private readonly platform: SensusAnalyticsPlatform,
|
|
24
|
+
private readonly accessory: PlatformAccessory,
|
|
25
|
+
private readonly apiClient: SensusAnalyticsApi,
|
|
26
|
+
) {
|
|
27
|
+
const { hap } = platform.api;
|
|
28
|
+
const { Service: Svc, Characteristic: Char } = platform;
|
|
29
|
+
|
|
30
|
+
this.leakThreshold = (platform.config.leakThreshold as number | undefined) ?? 150;
|
|
31
|
+
this.pollIntervalMs =
|
|
32
|
+
((platform.config.pollInterval as number | undefined) ?? DEFAULT_POLL_INTERVAL_MINUTES) *
|
|
33
|
+
60 *
|
|
34
|
+
1000;
|
|
35
|
+
|
|
36
|
+
// ── Accessory Information ─────────────────────────────────────────────
|
|
37
|
+
this.accessory
|
|
38
|
+
.getService(Svc.AccessoryInformation)!
|
|
39
|
+
.setCharacteristic(Char.Manufacturer, 'Sensus')
|
|
40
|
+
.setCharacteristic(Char.Model, 'Smart Water Meter')
|
|
41
|
+
.setCharacteristic(
|
|
42
|
+
Char.SerialNumber,
|
|
43
|
+
(accessory.context.meterNumber as string | undefined) ?? 'Unknown',
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
// ── Leak Sensor (primary HomeKit service) ─────────────────────────────
|
|
47
|
+
// Visible in Apple Home app as a leak sensor tile with alert notifications
|
|
48
|
+
this.leakService =
|
|
49
|
+
this.accessory.getService(Svc.LeakSensor) ?? this.accessory.addService(Svc.LeakSensor);
|
|
50
|
+
|
|
51
|
+
this.leakService.setCharacteristic(Char.Name, accessory.displayName);
|
|
52
|
+
|
|
53
|
+
this.leakService
|
|
54
|
+
.getCharacteristic(Char.LeakDetected)
|
|
55
|
+
.onGet(this.handleLeakDetectedGet.bind(this));
|
|
56
|
+
|
|
57
|
+
this.leakService
|
|
58
|
+
.getCharacteristic(Char.StatusActive)
|
|
59
|
+
.onGet(() => true);
|
|
60
|
+
|
|
61
|
+
// ── Eve Custom Characteristics ────────────────────────────────────────
|
|
62
|
+
// These are shown in the Eve for HomeKit app as consumption graphs.
|
|
63
|
+
// They are attached to the LeakSensor service so they share one accessory tile.
|
|
64
|
+
|
|
65
|
+
this.eveConsumptionChar = this.addEveCharacteristic(
|
|
66
|
+
'Water Consumption',
|
|
67
|
+
EVE_UUID_WATER_CONSUMPTION,
|
|
68
|
+
hap.Formats.FLOAT,
|
|
69
|
+
[hap.Perms.NOTIFY, hap.Perms.PAIRED_READ],
|
|
70
|
+
0, 1_000_000, 0.001,
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
this.eveTotalChar = this.addEveCharacteristic(
|
|
74
|
+
'Total Water Consumption',
|
|
75
|
+
EVE_UUID_TOTAL_WATER,
|
|
76
|
+
hap.Formats.FLOAT,
|
|
77
|
+
[hap.Perms.NOTIFY, hap.Perms.PAIRED_READ],
|
|
78
|
+
0, 1_000_000_000, 0.001,
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
// ── Start polling ─────────────────────────────────────────────────────
|
|
82
|
+
this.poll();
|
|
83
|
+
setInterval(() => this.poll(), this.pollIntervalMs);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Creates and registers a custom Characteristic on the LeakSensor service.
|
|
88
|
+
* If the characteristic already exists (restored from cache), it is returned as-is.
|
|
89
|
+
*/
|
|
90
|
+
private addEveCharacteristic(
|
|
91
|
+
displayName: string,
|
|
92
|
+
uuid: string,
|
|
93
|
+
format: string,
|
|
94
|
+
perms: string[],
|
|
95
|
+
minValue: number,
|
|
96
|
+
maxValue: number,
|
|
97
|
+
minStep: number,
|
|
98
|
+
): Characteristic {
|
|
99
|
+
const { hap } = this.platform.api;
|
|
100
|
+
|
|
101
|
+
const existing = this.leakService.characteristics.find((c) => c.UUID === uuid);
|
|
102
|
+
if (existing) {
|
|
103
|
+
return existing;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
107
|
+
const props: any = { format, unit: 'gal', minValue, maxValue, minStep, perms };
|
|
108
|
+
const char = new hap.Characteristic(displayName, uuid, props);
|
|
109
|
+
char.setValue(0);
|
|
110
|
+
this.leakService.addCharacteristic(char);
|
|
111
|
+
return char;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Called by HomeKit to read the current leak state. */
|
|
115
|
+
private handleLeakDetectedGet(): CharacteristicValue {
|
|
116
|
+
const { LEAK_DETECTED, LEAK_NOT_DETECTED } = this.platform.Characteristic.LeakDetected;
|
|
117
|
+
|
|
118
|
+
if (!this.lastData) {
|
|
119
|
+
return LEAK_NOT_DETECTED;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return this.lastData.daily.dailyUsage > this.leakThreshold
|
|
123
|
+
? LEAK_DETECTED
|
|
124
|
+
: LEAK_NOT_DETECTED;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** Fetch latest data from Sensus Analytics and push updates to HomeKit. */
|
|
128
|
+
private async poll(): Promise<void> {
|
|
129
|
+
this.platform.log.debug('Sensus Analytics: polling for new data...');
|
|
130
|
+
|
|
131
|
+
const data = await this.apiClient.fetchData();
|
|
132
|
+
if (!data) {
|
|
133
|
+
this.platform.log.warn('Sensus Analytics: no data returned, will retry next interval');
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
this.lastData = data;
|
|
138
|
+
const { daily, hourly } = data;
|
|
139
|
+
const { LEAK_DETECTED, LEAK_NOT_DETECTED } = this.platform.Characteristic.LeakDetected;
|
|
140
|
+
|
|
141
|
+
const isLeaking = daily.dailyUsage > this.leakThreshold;
|
|
142
|
+
|
|
143
|
+
// Push updates to HomeKit
|
|
144
|
+
this.leakService.updateCharacteristic(
|
|
145
|
+
this.platform.Characteristic.LeakDetected,
|
|
146
|
+
isLeaking ? LEAK_DETECTED : LEAK_NOT_DETECTED,
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
// Eve consumption characteristics
|
|
150
|
+
this.eveConsumptionChar.updateValue(daily.dailyUsage);
|
|
151
|
+
this.eveTotalChar.updateValue(daily.odometer);
|
|
152
|
+
|
|
153
|
+
// Log a summary
|
|
154
|
+
const lastHour = hourly.at(-1);
|
|
155
|
+
this.platform.log.info(
|
|
156
|
+
`[${this.accessory.displayName}] ` +
|
|
157
|
+
`daily=${daily.dailyUsage} ${daily.usageUnit} | ` +
|
|
158
|
+
`odometer=${daily.odometer} ${daily.usageUnit} | ` +
|
|
159
|
+
`billing=${daily.billingUsage} ${daily.usageUnit} | ` +
|
|
160
|
+
`leak=${isLeaking}` +
|
|
161
|
+
(lastHour ? ` | lastHourUsage=${lastHour.usage} | temp=${lastHour.temp}°F` : ''),
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
}
|
package/src/index.ts
ADDED
package/src/platform.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import {
|
|
2
|
+
API,
|
|
3
|
+
Characteristic,
|
|
4
|
+
DynamicPlatformPlugin,
|
|
5
|
+
Logger,
|
|
6
|
+
PlatformAccessory,
|
|
7
|
+
PlatformConfig,
|
|
8
|
+
Service,
|
|
9
|
+
} from 'homebridge';
|
|
10
|
+
|
|
11
|
+
import { PLATFORM_NAME, PLUGIN_NAME } from './settings';
|
|
12
|
+
import { SensusWaterMeterAccessory } from './accessory';
|
|
13
|
+
import { SensusAnalyticsApi } from './sensusApi';
|
|
14
|
+
|
|
15
|
+
export class SensusAnalyticsPlatform implements DynamicPlatformPlugin {
|
|
16
|
+
public readonly Service: typeof Service;
|
|
17
|
+
public readonly Characteristic: typeof Characteristic;
|
|
18
|
+
|
|
19
|
+
// Restored cached accessories from disk
|
|
20
|
+
public readonly cachedAccessories: PlatformAccessory[] = [];
|
|
21
|
+
|
|
22
|
+
constructor(
|
|
23
|
+
public readonly log: Logger,
|
|
24
|
+
public readonly config: PlatformConfig,
|
|
25
|
+
public readonly api: API,
|
|
26
|
+
) {
|
|
27
|
+
this.Service = this.api.hap.Service;
|
|
28
|
+
this.Characteristic = this.api.hap.Characteristic;
|
|
29
|
+
|
|
30
|
+
this.log.debug('Sensus Analytics platform initialising');
|
|
31
|
+
|
|
32
|
+
this.api.on('didFinishLaunching', () => {
|
|
33
|
+
this.discoverDevices();
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Called by Homebridge to restore cached accessories on startup. */
|
|
38
|
+
configureAccessory(accessory: PlatformAccessory): void {
|
|
39
|
+
this.log.info('Restoring cached accessory:', accessory.displayName);
|
|
40
|
+
this.cachedAccessories.push(accessory);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
private discoverDevices(): void {
|
|
44
|
+
const { baseUrl, username, password, accountNumber, meterNumber } = this.config;
|
|
45
|
+
|
|
46
|
+
if (!baseUrl || !username || !password || !accountNumber || !meterNumber) {
|
|
47
|
+
this.log.error(
|
|
48
|
+
'Sensus Analytics: missing required config fields ' +
|
|
49
|
+
'(baseUrl, username, password, accountNumber, meterNumber)',
|
|
50
|
+
);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const apiClient = new SensusAnalyticsApi(
|
|
55
|
+
baseUrl,
|
|
56
|
+
username,
|
|
57
|
+
password,
|
|
58
|
+
accountNumber,
|
|
59
|
+
meterNumber,
|
|
60
|
+
this.log,
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
const displayName = (this.config.name as string | undefined) || 'Sensus Water Meter';
|
|
64
|
+
const uuid = this.api.hap.uuid.generate(`sensus-${meterNumber}`);
|
|
65
|
+
|
|
66
|
+
const existing = this.cachedAccessories.find((a) => a.UUID === uuid);
|
|
67
|
+
|
|
68
|
+
if (existing) {
|
|
69
|
+
this.log.info('Restoring water meter accessory:', existing.displayName);
|
|
70
|
+
new SensusWaterMeterAccessory(this, existing, apiClient);
|
|
71
|
+
} else {
|
|
72
|
+
this.log.info('Adding new water meter accessory:', displayName);
|
|
73
|
+
const accessory = new this.api.platformAccessory(displayName, uuid);
|
|
74
|
+
accessory.context.meterNumber = meterNumber;
|
|
75
|
+
new SensusWaterMeterAccessory(this, accessory, apiClient);
|
|
76
|
+
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
package/src/sensusApi.ts
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import axios, { AxiosInstance } from 'axios';
|
|
2
|
+
import { wrapper } from 'axios-cookiejar-support';
|
|
3
|
+
import { CookieJar } from 'tough-cookie';
|
|
4
|
+
import { Logger } from 'homebridge';
|
|
5
|
+
|
|
6
|
+
export interface DailyData {
|
|
7
|
+
dailyUsage: number;
|
|
8
|
+
usageUnit: string;
|
|
9
|
+
meterAddress: string;
|
|
10
|
+
lastRead: string;
|
|
11
|
+
meterId: string;
|
|
12
|
+
meterLat: number;
|
|
13
|
+
meterLong: number;
|
|
14
|
+
odometer: number;
|
|
15
|
+
billingUsage: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface HourlyEntry {
|
|
19
|
+
timestamp: number;
|
|
20
|
+
usage: number;
|
|
21
|
+
rain: number;
|
|
22
|
+
temp: number;
|
|
23
|
+
usageUnit: string;
|
|
24
|
+
rainUnit: string;
|
|
25
|
+
tempUnit: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface SensusData {
|
|
29
|
+
daily: DailyData;
|
|
30
|
+
hourly: HourlyEntry[];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export class SensusAnalyticsApi {
|
|
34
|
+
private readonly client: AxiosInstance;
|
|
35
|
+
private readonly jar: CookieJar;
|
|
36
|
+
private loggedIn = false;
|
|
37
|
+
|
|
38
|
+
constructor(
|
|
39
|
+
private readonly baseUrl: string,
|
|
40
|
+
private readonly username: string,
|
|
41
|
+
private readonly password: string,
|
|
42
|
+
private readonly accountNumber: string,
|
|
43
|
+
private readonly meterNumber: string,
|
|
44
|
+
private readonly log: Logger,
|
|
45
|
+
) {
|
|
46
|
+
this.jar = new CookieJar();
|
|
47
|
+
this.client = wrapper(
|
|
48
|
+
axios.create({
|
|
49
|
+
baseURL: baseUrl.replace(/\/$/, ''),
|
|
50
|
+
jar: this.jar,
|
|
51
|
+
withCredentials: true,
|
|
52
|
+
// Follow redirects but treat 302 as success for login
|
|
53
|
+
maxRedirects: 5,
|
|
54
|
+
timeout: 15000,
|
|
55
|
+
}),
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async login(): Promise<boolean> {
|
|
60
|
+
try {
|
|
61
|
+
this.log.debug('Sensus Analytics: attempting login...');
|
|
62
|
+
await this.client.post(
|
|
63
|
+
'/j_spring_security_check',
|
|
64
|
+
new URLSearchParams({
|
|
65
|
+
j_username: this.username,
|
|
66
|
+
j_password: this.password,
|
|
67
|
+
}).toString(),
|
|
68
|
+
{
|
|
69
|
+
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
70
|
+
// A 302 redirect means login was processed; axios follows it automatically
|
|
71
|
+
validateStatus: (s) => s < 400,
|
|
72
|
+
},
|
|
73
|
+
);
|
|
74
|
+
this.loggedIn = true;
|
|
75
|
+
this.log.debug('Sensus Analytics: login successful');
|
|
76
|
+
return true;
|
|
77
|
+
} catch (err) {
|
|
78
|
+
this.loggedIn = false;
|
|
79
|
+
this.log.error('Sensus Analytics: login failed:', (err as Error).message);
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async fetchData(): Promise<SensusData | null> {
|
|
85
|
+
if (!this.loggedIn) {
|
|
86
|
+
const ok = await this.login();
|
|
87
|
+
if (!ok) {
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
try {
|
|
93
|
+
const [daily, hourly] = await Promise.all([
|
|
94
|
+
this.fetchDailyData(),
|
|
95
|
+
this.fetchHourlyData(),
|
|
96
|
+
]);
|
|
97
|
+
|
|
98
|
+
if (!daily) {
|
|
99
|
+
// Session may have expired — clear and retry once
|
|
100
|
+
this.loggedIn = false;
|
|
101
|
+
const ok = await this.login();
|
|
102
|
+
if (!ok) {
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
const retried = await this.fetchDailyData();
|
|
106
|
+
if (!retried) {
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
return { daily: retried, hourly: hourly ?? [] };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return { daily, hourly: hourly ?? [] };
|
|
113
|
+
} catch (err) {
|
|
114
|
+
this.loggedIn = false;
|
|
115
|
+
this.log.error('Sensus Analytics: error fetching data:', (err as Error).message);
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
private async fetchDailyData(): Promise<DailyData | null> {
|
|
121
|
+
const response = await this.client.post('/water/widget/byPage', {
|
|
122
|
+
group: 'meters',
|
|
123
|
+
accountNumber: this.accountNumber,
|
|
124
|
+
deviceId: this.meterNumber,
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
128
|
+
const device = (response.data as any)?.widgetList?.[0]?.data?.devices?.[0];
|
|
129
|
+
if (!device) {
|
|
130
|
+
this.log.warn('Sensus Analytics: unexpected daily data structure');
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return {
|
|
135
|
+
dailyUsage: parseFloat(device.dailyUsage) || 0,
|
|
136
|
+
usageUnit: device.usageUnit || 'CCF',
|
|
137
|
+
meterAddress: device.meterAddress1 || '',
|
|
138
|
+
lastRead: device.lastRead || '',
|
|
139
|
+
meterId: String(device.meterId || ''),
|
|
140
|
+
meterLat: parseFloat(device.meterLat) || 0,
|
|
141
|
+
meterLong: parseFloat(device.meterLong) || 0,
|
|
142
|
+
odometer: parseFloat(device.latestReadUsage) || 0,
|
|
143
|
+
billingUsage: parseFloat(device.billingUsage) || 0,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
private async fetchHourlyData(): Promise<HourlyEntry[] | null> {
|
|
148
|
+
// Fetch the previous day's data (Sensus updates at midnight)
|
|
149
|
+
const yesterday = new Date();
|
|
150
|
+
yesterday.setDate(yesterday.getDate() - 1);
|
|
151
|
+
const startOfDay = new Date(
|
|
152
|
+
yesterday.getFullYear(),
|
|
153
|
+
yesterday.getMonth(),
|
|
154
|
+
yesterday.getDate(),
|
|
155
|
+
);
|
|
156
|
+
const endOfDay = new Date(startOfDay.getTime() + 86_400_000 - 1);
|
|
157
|
+
|
|
158
|
+
const response = await this.client.get(
|
|
159
|
+
`/water/usage/${this.accountNumber}/${this.meterNumber}`,
|
|
160
|
+
{
|
|
161
|
+
params: {
|
|
162
|
+
start: startOfDay.getTime(),
|
|
163
|
+
end: endOfDay.getTime(),
|
|
164
|
+
zoom: 'day',
|
|
165
|
+
page: 'null',
|
|
166
|
+
weather: '1',
|
|
167
|
+
},
|
|
168
|
+
},
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
172
|
+
const data = response.data as any;
|
|
173
|
+
if (!data?.operationSuccess) {
|
|
174
|
+
this.log.warn('Sensus Analytics: hourly data fetch unsuccessful');
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const usageArray: unknown[][] = data?.data?.usage;
|
|
179
|
+
if (!Array.isArray(usageArray) || usageArray.length < 2) {
|
|
180
|
+
return [];
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// First element is units row: [usageUnit, rainUnit, tempUnit, altUnit]
|
|
184
|
+
const [usageUnit, rainUnit, tempUnit] = usageArray[0] as string[];
|
|
185
|
+
const rows = usageArray.slice(1);
|
|
186
|
+
|
|
187
|
+
return rows.map((row) => ({
|
|
188
|
+
timestamp: row[0] as number,
|
|
189
|
+
usage: (row[1] as number) ?? 0,
|
|
190
|
+
rain: (row[2] as number) ?? 0,
|
|
191
|
+
temp: (row[3] as number) ?? 0,
|
|
192
|
+
usageUnit: usageUnit ?? 'CCF',
|
|
193
|
+
rainUnit: rainUnit ?? 'INCHES',
|
|
194
|
+
tempUnit: tempUnit ?? 'FAHRENHEIT',
|
|
195
|
+
}));
|
|
196
|
+
}
|
|
197
|
+
}
|
package/src/settings.ts
ADDED
package/tsconfig.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2020",
|
|
4
|
+
"module": "commonjs",
|
|
5
|
+
"lib": ["ES2020"],
|
|
6
|
+
"strict": true,
|
|
7
|
+
"noImplicitAny": true,
|
|
8
|
+
"strictNullChecks": true,
|
|
9
|
+
"moduleResolution": "node",
|
|
10
|
+
"experimentalDecorators": true,
|
|
11
|
+
"skipLibCheck": true,
|
|
12
|
+
"outDir": "dist",
|
|
13
|
+
"declaration": true,
|
|
14
|
+
"declarationMap": true,
|
|
15
|
+
"sourceMap": true
|
|
16
|
+
},
|
|
17
|
+
"include": ["src"]
|
|
18
|
+
}
|