-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
146 lines (111 loc) · 3.56 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
'use strict';
var os = require("os");
const exec = require('child_process').exec;
const inherits = require('util').inherits;
const version = require('./package.json').version;
let deviceInfo = {};
let Service;
let Characteristic;
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory('homebridge-raspberrypi-switch', 'pi_switch', pi_switch);
}
function initCustomService() {
/**
* Service "pi_switch" Based on Service.Switch
*/
let raspberryPiUUID = '00000049-0000-1000-8000-0026BB765291';
Service.pi_switch = function (displayName, subType) {
Service.call(this, displayName, raspberryPiUUID, subType);
// Required Characteristics
this.addCharacteristic(Characteristic.On);
// Optional Characteristics
this.addOptionalCharacteristic(Characteristic.Name);
}
inherits(Service.pi_switch, Service);
Service.pi_switch.UUID = raspberryPiUUID;
}
function getModel() {
const { execSync } = require('child_process');
// stderr is sent to stderr of parent process
// you can set options.stdio if you want it to go elsewhere
let stdout = execSync('cat /sys/firmware/devicetree/base/model');
const data = stdout.toString();
return data.substring(0, data.length - 1);
};
function pi_switch(log, config) {
this.log = log;
this.services = [];
this.name = config.name || 'Respberry Pi';
this.os = config.os || 'linux';
this.serial = config.serial || os.hostname();
this.operatingState = true;
initCustomService();
this.service = new Service.pi_switch(this.name, 'Shutdown');
this.serviceInfo = new Service.AccessoryInformation();
this.service
.getCharacteristic(Characteristic.On)
.on('get', this.getPowerState.bind(this))
.on('set', this.setPowerState.bind(this));
this.serviceInfo
.setCharacteristic(Characteristic.Manufacturer, 'Raspberry Pi Foundation')
.setCharacteristic(Characteristic.Model, getModel())
.setCharacteristic(Characteristic.SerialNumber, this.serial)
.setCharacteristic(Characteristic.FirmwareRevision, version);
this.services.push(this.service);
this.services.push(this.serviceInfo);
this.rebootService = new Service.Switch(this.name + ' Reboot', 'Reboot');
this.rebootService
.getCharacteristic(Characteristic.On)
.on('get', this.getRebootState.bind(this))
.on('set', this.setRebootState.bind(this));
this.services.push(this.rebootService);
}
pi_switch.prototype = {
getPowerState: function (callback) {
callback(null, this.operatingState);
},
setPowerState: function (state, callback) {
if (!this.operatingState) {
return;
}
const that = this;
exec('sudo shutdown -h now', function (error, stdout, stderr) {
if (error) {
that.log(error);
} else {
that.operatingState = false;
that.log.debug('operating state: %s', that.operatingState);
callback(null, that.operatingState);
}
});
},
getRebootState: function (callback) {
if (!this.operatingState) {
return;
}
callback(null, !this.operatingState);
},
setRebootState: function (state, callback) {
if (!this.operatingState) {
return;
}
const that = this;
exec('sudo reboot', function (error, stdout, stderr) {
if (error) {
that.log(error);
} else {
that.operatingState = false;
that.log.debug('operating state: %s', that.operatingState);
callback(null, that.operatingState);
}
});
},
identify: function (callback) {
callback();
},
getServices: function () {
return this.services;
}
};