-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·106 lines (88 loc) · 2.57 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
var Service, Characteristic;
var RollingSpider = require("rolling-spider");
var temporal = require("temporal");
module.exports = function(homebridge){
console.log("homebridge API version: " + homebridge.version);
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-rolling-spider", "RollingSpider", RollingSpiderAccessory);
}
var isConnected = false;
function RollingSpiderAccessory(log, config) {
var self = this;
this.log = log;
// Recieve settings from config.json
this.uuid = config["uuid"] || "";
// Set default service as a Switch
this.service = config["service"] || "Switch";
// Initialize Rolling Spider
this.rs = (this.uuid!="") ?
new RollingSpider({uuid:this.uuid}) : new RollingSpider();
// Connect with Rolling Spider
this.rs.connect( function(err) {
if( err ) {
self.log("err="+err);
return;
}
self.log("Connected with rolling-spider:"+self.rs.name);
self.rs.setup( function() {
// Stabilize
self.rs.flatTrim();
self.rs.startPing();
self.rs.flatTrim();
// Set connected flag to true
temporal.queue([
{
delay: 1000,
task: function() {
self.log("Ready to takeoff:" + self.rs.name);
isConnected = true;
}
}
])
});
});
}
RollingSpiderAccessory.prototype = {
setFlyingState: function(takeoff, callback) {
this.log("setFlyingState // CONNECTED="+isConnected+", takeoff="+takeoff);
if( isConnected ) {
if (takeoff) {
this.log("Setting flying state to - takeoff");
this.rs.takeoff();
this.rs.flatTrim();
} else {
this.log("Setting flying state to - landing");
this.rs.land();
}
callback();
} else {
// callback();
callback("error");
}
},
identify: function(callback) {
this.log("Identify!!!");
callback(); // success
},
getServices: function() {
this.log("getServices // this.service="+this.service);
var services = [];
// Information service
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, "Parrot")
.setCharacteristic(Characteristic.Model, "RollingSpider Model")
.setCharacteristic(Characteristic.SerialNumber, "RollingSpider Serial Number");
services.push(informationService);
// Switch
if( this.service=='Switch' ) {
var switchService = new Service.Switch(this.name);
switchService
.getCharacteristic(Characteristic.On)
.on('set', this.setFlyingState.bind(this));
services.push(switchService);
}
return services;
}
};