Last active
February 25, 2022 08:43
-
-
Save Robdel12/60011235a0e1e3d8a99367bf9fcb388b to your computer and use it in GitHub Desktop.
Toggle Meross Plug from Node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
// TODO: I know, I know, don't use request... | |
const request = require("request"); | |
// Wrap request with a promise to make it awaitable | |
function doRequest(options) { | |
return new Promise(function(resolve, reject) { | |
request(options, function(error, res, body) { | |
if (!error && res.statusCode == 200) { | |
resolve(body); | |
} else { | |
reject(error); | |
} | |
}); | |
}); | |
} | |
async function toggleMerossPlug(url, onoff) { | |
if (onoff !== "off" && onoff !== "on") { | |
console.log('You must send "on" or "off" as a arg for "onoff"'); | |
return; | |
} | |
let booleanOnOff = onoff === "on" ? 1 : 0; | |
let response; | |
try { | |
response = await doRequest({ | |
json: true, | |
method: "POST", | |
strictSSL: false, | |
url: `${url}/config`, | |
headers: { | |
"Content-Type": "application/json" | |
}, | |
body: { | |
payload: { | |
togglex: { | |
onoff: booleanOnOff | |
} | |
}, | |
header: { | |
messageId: "c3222c7d2b9163fe2968f06c45338a9f", | |
method: "SET", | |
from: `${url}/config`, | |
namespace: "Appliance.Control.ToggleX", | |
timestamp: 1543987687, | |
// TODO probably can recycle the 'sign' from the response of this request | |
// in case this gets stale and no longer works. No idea what it does. | |
sign: "9cb8004faf1ea39e94256227c9fb0b19", | |
payloadVersion: 1 | |
} | |
} | |
}); | |
} catch (e) { | |
console.log("Failed to POST to the Meross Plug:", e); | |
} | |
if (response) { | |
console.log("Set succeeded", response); | |
} else { | |
console.log("Set failed", response); | |
} | |
return response; | |
} | |
// Example usage: | |
// Comment these lines below out and add your plugs IP: | |
// (async () => { | |
// await toggleMerossPlug("http://192.168.86.250", "off"); | |
// })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
Great idea, but I've got an error:
Failed to POST to the Meross Plug: { Error: socket hang up at createHangUpError (_http_client.js:332:15) at Socket.socketOnEnd (_http_client.js:435:23) at Socket.emit (events.js:203:15) at endReadableNT (_stream_readable.js:1145:12) at process._tickCallback (internal/process/next_tick.js:63:19) code: 'ECONNRESET' } Set failed undefined
What could be the reason?