-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
162 lines (132 loc) · 4.73 KB
/
index.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
'use strict'
let Service, Characteristic
var SmartStartLib = require('smartstart');
module.exports = (homebridge) => {
Service = homebridge.hap.Service
Characteristic = homebridge.hap.Characteristic
homebridge.registerAccessory('homebridge-smartstart', 'SmartStart', SmartStart)
homebridge.registerAccessory('homebridge-smartstart', 'SmartStartLock', SmartStartLock)
}
class SmartStart {
constructor (log, config) {
// get config values
this.log = log;
this.name = config['name'];
this.startService = new Service.Switch(this.name);
this.startDelay = config['delay'];
this.deviceIndex = config['deviceIndex'];
this.startTimer;
// define SmartStart vehicle
this.vehicle = new SmartStartLib({
username: config['username'],
password: config['password'],
deviceIndex: config['deviceIndex']
});
this.vehicle.getDeviceId(this.deviceIndex, this.infoCallback.bind(this));
}
getServices () {
var informationService = new Service.AccessoryInformation()
.setCharacteristic(Characteristic.Manufacturer, 'SmartStart')
.setCharacteristic(Characteristic.Model, 'SmartStart')
.setCharacteristic(Characteristic.SerialNumber, '')
this.startService.getCharacteristic(Characteristic.On)
.on('get', this.getOnCharacteristicHandler.bind(this))
.on('set', this.setOnCharacteristicHandler.bind(this))
return [informationService, this.startService]
}
infoCallback(err, result) {
//this.log(this.vehicle);
}
actionCallback(err, result) {
if(err) return console.error(err);
console.log(result);
}
setOnCharacteristicHandler (value, callback) {
if (this.isOn == value) {
this.log(`vehicle aready in the called state via homebridge :`+this.name, value)
} else {
this.log(`starting/stopping `+this.name, value)
this.vehicle.remote(this.actionCallback);
this.isOn = value
}
if (!value) {
clearTimeout(this.smartTimer);
} else {
this.smartTimer = setTimeout(function() {
this.log('Time is Up!');
this.startService.getCharacteristic(Characteristic.On).updateValue(false);
this.isOn = false;
}.bind(this), this.startDelay);
}
callback(null)
}
getOnCharacteristicHandler (callback) {
this.log(`updating vehcile status`, this.isOn)
/*
if the SmartStart API can be figured out, add timer based updates to poll the vehicles, then return those cached values here
*/
callback(null, this.isOn)
}
}
class SmartStartLock {
constructor (log, config) {
// get config values
this.log = log;
this.name = config['name'];
this.lockService = new Service.LockMechanism(this.name);
this.lockState = Characteristic.LockCurrentState.SECURED;
// define SmartStart vehicle
this.vehicle = new SmartStartLib({
username: config['username'],
password: config['password'],
deviceIndex: config['deviceIndex']
});
}
getServices () {
const informationService = new Service.AccessoryInformation()
.setCharacteristic(Characteristic.Manufacturer, 'SmartStart')
.setCharacteristic(Characteristic.Model, 'SmartStart')
.setCharacteristic(Characteristic.SerialNumber, '');
this.lockService.getCharacteristic(Characteristic.LockCurrentState)
.on('get', this.getLockCharacteristicHandler.bind(this));
this.lockService.getCharacteristic(Characteristic.LockTargetState)
.on('get', this.getLockCharacteristicHandler.bind(this))
.on('set', this.setLockCharacteristicHandler.bind(this));
return [informationService, this.lockService]
}
actionCallback(err, result) {
if(err) {
this.updateCurrentState(Characteristic.LockCurrentState.JAMMED);
return console.error(err);
}
this.updateCurrentState(this.lockState);
//this.log(this);
this.log(this.lockState+" "+this.name);
//this.log(result);
}
// Lock Handler
setLockCharacteristicHandler (targetState, callback) {
var lockh = this;
if (targetState == Characteristic.LockCurrentState.SECURED) {
this.log(`locking/arming `+this.name, targetState)
this.lockState = targetState
this.vehicle.arm(this.actionCallback.bind(this));
} else {
this.log(`unlocking/disarming `+this.name, targetState)
this.lockState = targetState
this.vehicle.disarm(this.actionCallback.bind(this));
}
callback();
}
updateCurrentState(toState) {
this.lockService
.getCharacteristic(Characteristic.LockCurrentState)
.updateValue(toState);
}
getLockCharacteristicHandler (callback) {
/*
if the SmartStart API can be figured out, add timer based updates to poll the vehicles, then return those cached values here
*/
callback(null, this.lockState)
}
}