forked from gadget-monk/homebridge-poolcontroller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pumpAccessory.js
executable file
·134 lines (99 loc) · 4.29 KB
/
pumpAccessory.js
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
var Accessory, Service, Characteristic, UUIDGen;
//var debug = false;
var utils = require('./utils.js')
// var moment = require('moment');
var PoolPumpAccessory = function (log, accessory, pumpData, homebridge, platform) {
Accessory = homebridge.platformAccessory;
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
UUIDGen = homebridge.hap.uuid;
Homebridge = homebridge;
this.accessory = accessory;
this.log = log;
this.accessory.log = log
var customtypes = require('./customTypes.js')
var CustomTypes = new customtypes(Homebridge)
var FakeGatoHistoryService = require('fakegato-history')(homebridge);
this.loggingService = new FakeGatoHistoryService("energy", this.accessory, { size: 11520, disableTimer: true, storage: 'fs' });
this.pumpData = pumpData
this.platform = platform
this.debug = platform.debug;
this.service = accessory.getService(Service.Fan);
if (this.service) {
this.service
.getCharacteristic(Characteristic.On)
.on('get', this.getPumpState.bind(this))
.on('set', this.setPumpState.bind(this));
this.service
.getCharacteristic(Characteristic.RotationSpeed)
.on('get', this.getPumpSpeed.bind(this))
.on('set', this.setPumpState.bind(this));
this.service
.getCharacteristic(CustomTypes.CurrentPowerConsumption)
.on('get', this.getPumpWatts.bind(this))
this.service
.getCharacteristic(CustomTypes.PumpGPM)
.on('get', this.getPumpGPM.bind(this))
this.service
.getCharacteristic(CustomTypes.PumpRPM)
.on('get', this.getPumpRPM.bind(this))
}
this.updateState(pumpData)
// not needed/used with latest HomeKit API's
// accessory.updateReachability(true);
}
PoolPumpAccessory.prototype.getPumpState = function (callback) {
callback(null, this.pumpData.rpm > 0 ? true : false);
};
PoolPumpAccessory.prototype.setPumpState = function (newValue, callback) {
if (this.platform.LogLevel >= 4) this.log('Set pump state called, ignoring')
callback();
};
PoolPumpAccessory.prototype.getPumpSpeed = function (callback) {
callback(null, this.pumpData.rpm / this.pumpData.type.maxSpeed * 100);
};
PoolPumpAccessory.prototype.setPumpSpeed = function (newValue, callback) {
// do nothing
if (this.platform.LogLevel >= 4) this.log('Set pump speed called, ignoring')
callback();
};
PoolPumpAccessory.prototype.getPumpRPM = function (callback) {
callback(null, this.pumpData.rpm);
};
PoolPumpAccessory.prototype.getPumpGPM = function (callback) {
callback(null, this.pumpData.flow);
};
PoolPumpAccessory.prototype.getPumpWatts = function (callback) {
callback(null, this.pumpData.watts);
};
// For when state is changed elsewhere.
PoolPumpAccessory.prototype.updateState = function (newpumpData) {
var customtypes = require('./customTypes.js')
var CustomTypes = new customtypes(Homebridge)
this.pumpData = newpumpData;
if (this.platform.LogLevel >= 3)
this.log('Updating pump to ', this.pumpData.rpm > 0 ? true : false)
if (this.platform.LogLevel >= 4) {
this.log('watts ', this.pumpData.watts)
this.log('RPM % ', this.pumpData.rpm / this.pumpData.type.maxSpeed)
this.log('Flow ', this.pumpData.flow)
}
this.accessory.getService(Service.Fan).getCharacteristic(Characteristic.On)
.updateValue(this.pumpData.rpm > 0 ? true : false);
this.accessory.getService(Service.Fan).getCharacteristic(Characteristic.RotationSpeed)
.updateValue(this.pumpData.rpm / this.pumpData.type.maxSpeed * 100);
this.accessory.getService(Service.Fan).getCharacteristic(CustomTypes.CurrentPowerConsumption)
.updateValue(this.pumpData.watts);
this.accessory.getService(Service.Fan).getCharacteristic(CustomTypes.PumpGPM)
.updateValue(this.pumpData.flow);
this.accessory.getService(Service.Fan).getCharacteristic(CustomTypes.PumpRPM)
.updateValue(this.pumpData.rpm);
this.loggingService.addEntry({ time: Math.round(new Date().valueOf() / 1000), power: this.pumpData.watts });
var interval = 5 * 60 * 1000
clearTimeout(this.pumpUpdateTimer)
this.pumpUpdateTimer = setInterval(function (platform, loggingService, pumpData) {
loggingService.addEntry({ time: Math.round(new Date().valueOf() / 1000), power: pumpData.watts })
}, interval, this.platform, this.loggingService, this.pumpData)
return
}
module.exports = PoolPumpAccessory;