-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
130 lines (114 loc) · 3.95 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
var fs = require('fs');
const miio = require('miio');
var Accessory, Service, Characteristic, UUIDGen;
module.exports = function(homebridge) {
if(!isConfig(homebridge.user.configPath(), "accessories", "MiGatewayFM")) {
return;
}
Accessory = homebridge.platformAccessory;
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
UUIDGen = homebridge.hap.uuid;
homebridge.registerAccessory('homebridge-mi-gateway-fm', 'MiGatewayFM', MiGatewayFM);
}
function isConfig(configFile, type, name) {
var config = JSON.parse(fs.readFileSync(configFile));
if("accessories" === type) {
var accessories = config.accessories;
for(var i in accessories) {
if(accessories[i]['accessory'] === name) {
return true;
}
}
} else if("platforms" === type) {
var platforms = config.platforms;
for(var i in platforms) {
if(platforms[i]['platform'] === name) {
return true;
}
}
} else {
}
return false;
}
function MiGatewayFM(log, config) {
if(null == config) {
return;
}
this.log = log;
this.config = config;
var that = this;
this.device = new miio.Device({
address: that.config.ip,
token: that.config.token
});
}
MiGatewayFM.prototype = {
identify: function(callback) {
callback();
},
getServices: function() {
var services = [];
var infoService = new Service.AccessoryInformation();
infoService
.setCharacteristic(Characteristic.Manufacturer, "XiaoMi")
.setCharacteristic(Characteristic.Model, "Gateway")
.setCharacteristic(Characteristic.SerialNumber, "Undefined");
services.push(infoService);
var fmService = new Service.Switch(this.config['name']);
fmService
.getCharacteristic(Characteristic.On)
.on('get', this.getFMStatus.bind(this))
.on('set', this.setFMStatus.bind(this));
services.push(fmService);
return services;
},
getFMStatus: function(callback) {
var that = this;
this.device.call("get_prop_fm", [null]).then(result => {
callback(null, result['current_status'] === 'pause' ? false : true);
}).catch(function(err) {
that.log.error("[MiGatewayFM][ERROR]getFMStatus Error: " + err);
callback(err);
});
},
setFMStatus: function(value, callback) {
var that = this;
if(value) {
if(this.config['url']) {
this.device.call("play_specify_fm", {"type": 0, "id": 0, "url": this.config['url']}).then(result => {
if(result[0] === "ok") {
callback(null);
} else {
callback(new Error(result[0]));
}
}).catch(function(err) {
that.log.error("[MiGatewayFM][ERROR]setFMStatus Error: " + err);
callback(err);
});
} else {
this.device.call("play_fm", ['on']).then(result => {
if(result[0] === "ok") {
callback(null);
} else {
callback(new Error(result[0]));
}
}).catch(function(err) {
that.log.error("[MiGatewayFM][ERROR]setFMStatus Error: " + err);
callback(err);
});
}
} else {
this.device.call("play_fm", ['off']).then(result => {
if(result[0] === "ok") {
callback(null);
} else {
callback(new Error(result[0]));
}
}).catch(function(err) {
that.log.error("[MiGatewayFM][ERROR]setFMStatus Error: " + err);
callback(err);
});
}
}
}