"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"); // })();