Skip to content

Commit

Permalink
implement wol
Browse files Browse the repository at this point in the history
  • Loading branch information
so298 committed Dec 3, 2022
1 parent 9c7d9fe commit d762822
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"@types/ping": "^0.4.1",
"cors": "^2.8.5",
"express": "^4.18.2",
"ping": "^0.4.2"
"ping": "^0.4.2",
"wol": "^1.0.7"
},
"devDependencies": {
"@types/express": "^4.17.14"
Expand Down
29 changes: 15 additions & 14 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const { exec } = require("child_process");
const wol = require("wol");
const ping = require("ping");
const { loadDeviceData } = require("./util");

Expand All @@ -20,13 +20,17 @@ const updateDeviceStatus = () => {
timeout: 0.1,
};
deviceData.forEach((elem, idx) => {
ping.sys.probe(elem.localip, (isAlive) => {
if (isAlive) {
deviceData[idx].status = "acitive";
} else {
deviceData[idx].status = "down";
}
}, pingConfig);
ping.sys.probe(
elem.localip,
(isAlive) => {
if (isAlive) {
deviceData[idx].status = "acitive";
} else {
deviceData[idx].status = "down";
}
},
pingConfig
);
});
};
setInterval(() => {
Expand Down Expand Up @@ -65,15 +69,12 @@ app.post("/devices/wol/", (req, res, next) => {

for (device of deviceData) {
if (device.mac == reqDevice.mac) {
// TODO: change command to wol
exec(`echo ${reqDevice.mac}`, (err, stdout, stderr) => {
wol.wake(reqDevice.mac, (err) => {
if (err) {
res
.status(404)
.json({ status: "Exec command failed", stdout, stderr, err });
res.status(404).json({ status: "Exec command failed" });
return;
}
res.status(200).json({ status: "Success wol", stdout, stderr, err });
res.status(200).json({ status: "Success wol" });
});
return;
}
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -501,3 +501,8 @@ vary@^1, vary@~1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc"
integrity sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg==

wol@^1.0.7:
version "1.0.7"
resolved "https://registry.yarnpkg.com/wol/-/wol-1.0.7.tgz#a2e70efca2a28324a744a5d12331d359da470ff7"
integrity sha512-kg7ETY8g3V5+3GVhUfWCVjeXuCmfrX6xfw4cw4c88+MtoxkbFmcs9Y5yhT1wwOL8inogFUQZ8JMzH9OekaaawQ==

0 comments on commit d762822

Please sign in to comment.