-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.js
143 lines (115 loc) · 4.37 KB
/
handlers.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
135
136
137
138
139
140
141
142
143
//@ts-check
const { logger } = require('./logger.js');
const MODE = {
MODE_OFF: 'off',
MODE_PV: 'pv',
MODE_MINPV: 'minpv',
MODE_NOW: 'now'
}
const CHARGE_STATE = {
CHARGING: 'charging',
IDLE: 'idle',
};
const BATTERY_STATE = {
STANDBY: 'standby',
BACKUP: 'backup'
}
class Handler {
constructor(teslaApi) {
this.currentChargeMode = 'UNKNOWN';
this.currentChargeState = false;
this.currentBatteryState = 'UNKNOWN';
this.plannerActive = false;
this.teslaApi = teslaApi;
}
toString() {
return `currentChargeMode: ${this.currentChargeMode}, currentChargeState: ${this.currentChargeState}, currentBatteryState: ${this.currentBatteryState}, plannerActive: ${this.plannerActive}`;
}
setChargeMode(newMode) {
if (this.currentChargeMode == newMode) {
logger.trace("skipping chargemode update, mode stays at %s", this.currentChargeMode);
return; //NoOp
}
logger.debug("updating chargemode from %s to %s", this.currentChargeMode, newMode);
this.currentChargeMode = newMode;
this.update();
}
setCharging(newState) {
if (this.currentChargeState == newState) {
logger.trace("skipping chargestate update, state stays at %s", this.currentChargeState);
return; //NoOp
}
logger.debug("updating chargeState from %s to %s", this.currentChargeState, newState);
this.currentChargeState = newState;
this.update();
}
isCharging() {
return this.currentChargeState;
}
setPlannerActive(newValue) {
if (this.plannerActive == newValue) {
logger.trace("skipping plannerActive update, state stays at %s", this.plannerActive);
return; //NoOp
}
logger.debug("updating plannerActive from %s to %s", this.plannerActive, newValue);
this.plannerActive = newValue;
this.update();
}
isPlannerActive() {
return this.plannerActive
}
setBatteryState(newState) {
if (this.currentBatteryState == newState) {
logger.trace("skipping Batterystate update, state stays at %s", this.currentBatteryState);
return; //NoOp
}
logger.debug("updating BatteryState from %s to %s", this.currentBatteryState, newState);
this.currentBatteryState = newState;
this.updateTeslaAPI();
}
update() {
if (this.isCharging() == false) {
logger.debug("charge state is %s, setting battery to standby", this.isCharging());
this.setBatteryState(BATTERY_STATE.STANDBY);
return;
}
switch (this.currentChargeMode) {
case MODE.MODE_OFF:
case MODE.MODE_MINPV:
logger.debug("charge mode is %s, setting battery to standby", this.currentChargeMode);
this.setBatteryState(BATTERY_STATE.STANDBY);
break;
case MODE.MODE_PV:
if (this.isPlannerActive() == false) {
logger.debug("charge mode is %s, planner is not active, setting battery to standby", this.currentChargeMode);
this.setBatteryState(BATTERY_STATE.STANDBY);
} else {
logger.debug("charge mode is %s, planner is active, setting battery to backup only", this.currentChargeMode);
this.setBatteryState(BATTERY_STATE.BACKUP);
}
break;
case MODE.MODE_NOW:
logger.debug("charge mode is %s, setting battery to backup only", this.currentChargeMode);
this.setBatteryState(BATTERY_STATE.BACKUP);
break;
default:
logger.warn("ignoring unknown charge mode '%s'", this.currentChargeMode);
break;
}
}
updateTeslaAPI() {
logger.info("Updating TESLA API for Battery state: %s", this.currentBatteryState);
switch (this.currentBatteryState) {
case BATTERY_STATE.STANDBY:
this.teslaApi.setStandbyMode();
break;
case BATTERY_STATE.BACKUP:
this.teslaApi.setBackupOnlyMode();
break;
default:
logger.error("Unknown Battery state encountered while updating Tesla API");
break;
}
}
}
exports.Handler = Handler;