-
Notifications
You must be signed in to change notification settings - Fork 10
/
homebridge-carwings.js
142 lines (118 loc) · 4.36 KB
/
homebridge-carwings.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
var carwings = require('carwings');
var Service, Characteristic;
var carwingsSession = null;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-carwings", "Carwings", CarwingsAccessory);
}
var base64regex = /^(?:[A-Z0-9+\/]{4})*(?:[A-Z0-9+\/]{2}==|[A-Z0-9+\/]{3}=|[A-Z0-9+\/]{4})$/i;
var email, password, region;
function loginCarwings() {
carwings.loginSession(email, password, region).then(function(session){
carwingsSession = session;
carwings.batteryStatusCheckRequest(carwingsSession);
});
}
function CarwingsAccessory(log, config) {
this.log = log;
this.config = config;
this.name = config["name"];
email = config["email"];
password = config["password"];
region = config["region"];
if(base64regex.test(password)){
password = Buffer.from(password, 'base64');
}
loginCarwings();
this.battery = new Service.BatteryService(this.name);
this.battery
.getCharacteristic(Characteristic.BatteryLevel)
.on('get', this.getLevel.bind(this));
this.battery
.getCharacteristic(Characteristic.ChargingState)
.on('get', this.getCharging.bind(this));
this.hvac = new Service.Fanv2(this.name);
this.hvac.getCharacteristic(Characteristic.Active)
.on('get', this.getHVAC.bind(this))
.on('set', this.setHVAC.bind(this));
this.heater = new Service.HeaterCooler(this.name);
this.heater.getCharacteristic(Characteristic.Active)
.on('get', this.getHVAC.bind(this))
.on('set', this.setHVAC.bind(this));
this.switch = new Service.Switch(this.name);
this.switch.getCharacteristic(Characteristic.On)
.on('get', this.getHVAC.bind(this))
.on('set', this.setHVAC.bind(this));
var updateInterval = config["updateInterval"] ? config["updateInterval"] : 600000;
if(updateInterval != "never") {
setInterval(function(){
carwings.batteryStatusCheckRequest(carwingsSession).then(function(checkStatus){
console.log("Got LEAF request on interval", checkStatus);
if(checkStatus.status == 401) {
loginCarwings();
}
});
}, 600000);
}
}
CarwingsAccessory.prototype.getLevel = function(callback) {
//console.log(this.battery.getCharacteristic(Characteristic.BatteryLevel));
var _this = this;
carwings.batteryRecords(carwingsSession).then(function(status){
console.log(status);
if(status.status == 401) {
loginCarwings();
}
carwings.batteryStatusCheckRequest(carwingsSession);
/*_this.battery.getCharacteristic(Characteristic.BatteryLevel).setProp({
maxValue: status.BatteryStatusRecords.BatteryStatus.BatteryCapacity
});*/
//_this.battery.getCharacteristic(Characteristic.BatteryLevel).props.maxValue = parseInt(status.BatteryStatusRecords.BatteryStatus.BatteryCapacity);
var chargePercent = parseInt((status.BatteryStatusRecords.BatteryStatus.BatteryRemainingAmount / 12) * 100);
if(chargePercent > 100) chargePercent = 100;
console.log("LEAF charge percent = ", chargePercent);
callback(null, chargePercent);
});
}
CarwingsAccessory.prototype.getCharging = function(callback) {
carwings.batteryRecords(carwingsSession).then(function(status){
console.log(status);
if(status.status == 401) {
loginCarwings();
}
callback(null, status.BatteryStatusRecords.BatteryStatus.BatteryChargingStatus.indexOf("CHARGING") !== -1);
});
}
CarwingsAccessory.prototype.getHVAC = function(callback) {
console.log(this);
carwings.hvacStatus(carwingsSession).then(function(status){
console.log(status);
if(status.status == 401) {
loginCarwings();
}
callback(null, status.RemoteACRecords.RemoteACOperation !== 'STOP');
});
};
CarwingsAccessory.prototype.setHVAC = function(on, callback) {
if(on) {
carwings.hvacOn(carwingsSession).then(function(status){
console.log(status);
if(status.status == 401) {
loginCarwings();
}
callback(null, true);
});
} else {
carwings.hvacOff(carwingsSession).then(function(status){
console.log(status);
if(status.status == 401) {
loginCarwings();
}
callback(null, false);
});
}
};
CarwingsAccessory.prototype.getServices = function() {
return [this.battery, this.hvac, this.heater, this.switch];
}