forked from StewartSnow/homebridge-mac-screensaver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
187 lines (153 loc) · 4.91 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
const package = require('./package.json');
const exec = require('child_process').exec;
const http = require('http'); // or 'https' for HTTPS requests
let Service, Characteristic;
// Set up homebridge
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-url-screensaver", "ScreenSwitch", urlScreenSwitch); // register
};
function urlScreenSwitch(log, config) {
this.log = log;
this.config = config;
this.log('config is ? ' + this.config);
}
urlScreenSwitch.prototype.getServices = function() {
let informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, "Kirsch")
.setCharacteristic(Characteristic.Model, "ScreenSwitch")
.setCharacteristic(Characteristic.SerialNumber, package.version);
let switchService = new Service.Switch("ScreenSwitch");
switchService
.getCharacteristic(Characteristic.On)
.on('set', this.setSwitchOnCharacteristic.bind(this))
.on('get', this.getSwitchOnCharacteristic.bind(this));
this.informationService = informationService;
this.switchService = switchService;
return [informationService, switchService];
}
urlScreenSwitch.prototype.isRunning = function () {
return new Promise(function (resolve, reject) {
// Define the HTTP options based on the platform
const options = {
hostname: 'nick.home.kirsch.org',
port: 19820,
path: '/',
method: 'GET'
};
// Make an HTTP GET request
const req = http.request(options, (res) => {
let responseData = '';
res.on('data', (chunk) => {
responseData += chunk;
});
res.on('end', () => {
const parsedData = JSON.parse(responseData);
// Check if the screensaver is running based on the parsed data
const isRunning = parsedData.hasOwnProperty('isRunning') ? parsedData.isRunning : false;
resolve(isRunning);
});
});
// Handle potential errors
req.on('error', (error) => {
// Handle the error here
console.error('HTTP GET request error:', error);
resolve(false); // Return false in case of an error
});
// Send the HTTP GET request
req.end();
});
};
// Returns proper state of display
urlScreenSwitch.prototype.getSwitchOnCharacteristic = function(next) {
this.isRunning().then((v) =>
{
this.log('screen saver running? ' + v);
next(null, v)
}
);
//next(null, false);
}
// Sets the display on or off
urlScreenSwitch.prototype.setSwitchOnCharacteristic = function (on, next) {
this.log('Setting url screensaver: ' + (on ? 'on' : 'off'));
if (on) {
// Define the JSON data to send in the POST request
const jsonData = {
password: 'nightnight',
display: true,
on: true,
};
// Convert the JSON data to a string
const jsonPayload = JSON.stringify(jsonData);
// Set the options for the POST request
const options = {
hostname: 'nick.home.kirsch.org',
port: '19820', // Replace with your target port (e.g., 443 for HTTPS)
path: '/', // Replace with your API endpoint path
method: 'POST', // Change to 'POST' for a POST request
headers: {
'Content-Type': 'application/json',
'Content-Length': jsonPayload.length,
},
};
// Create the HTTP request
const req = http.request(options, (res) => {
let responseData = '';
res.on('data', (chunk) => {
responseData += chunk;
});
res.on('end', () => {
// Send the JSON payload in the POST request
req.write(jsonPayload);
req.end();
next();
});
});
// Handle potential errors
req.on('error', (error) => {
this.log('HTTP request error:', error);
next();
});
} else {
// Define the JSON data to send in the POST request
const jsonData = {
password: 'nightnight',
display: false,
on: false,
};
// Convert the JSON data to a string
const jsonPayload = JSON.stringify(jsonData);
// Set the options for the POST request
const options = {
hostname: 'nick.home.kirsch.org',
port: 19820, // Replace with your target port (e.g., 443 for HTTPS)
path: '/', // Replace with your API endpoint path
method: 'POST', // Change to 'POST' for a POST request
headers: {
'Content-Type': 'application/json',
'Content-Length': jsonPayload.length,
},
};
// Create the HTTP request
const req = http.request(options, (res) => {
let responseData = '';
res.on('data', (chunk) => {
responseData += chunk;
});
res.on('end', () => {
// Send the JSON payload in the POST request
req.write(jsonPayload);
req.end();
next();
});
});
// Handle potential errors
req.on('error', (error) => {
this.log('HTTP request error:', error);
next();
});
}
};