{"size": 1109, "ext": "js", "max_stars_repo_path": "nodeservice/index.js", "max_stars_repo_name": "mamacker/pi_to_potter", "max_stars_repo_stars_event_min_datetime": "2018-02-28T02:52:16.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-26T14:31:14.000Z", "max_issues_repo_path": "nodeservice/index.js", "max_issues_repo_name": "mamacker/pi_to_potter", "max_issues_repo_issues_event_min_datetime": "2018-11-28T00:55:35.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-29T22:26:53.000Z", "max_forks_repo_path": "nodeservice/index.js", "max_forks_repo_name": "mamacker/pi_to_potter", "max_forks_repo_forks_event_min_datetime": "2018-11-02T23:59:35.000Z", "max_forks_repo_forks_event_max_datetime": "2021-01-13T03:09:55.000Z"}
{"max_stars_count": 15, "max_issues_count": 4, "max_forks_count": 7, "avg_line_length": 22.6326530612, "max_line_length": 78, "alphanum_fraction": 0.5897204689}
const { Client } = require('hs100-api'); 
const client = new Client();

let d = null;
let newState = 1;
function findPlug() {
  let plug = client.getDevice({host: '192.168.1.31'}).then((device)=>{
    device.getSysInfo().then(console.log);
    d = device;
    setInterval(() => {
      d.setPowerState(newState);
    }, 3000);

    d.startPolling(1000);
  }).catch((ex) => {
    console.log("Exception setting up outlet: ", ex);
    setTimeout(findPlug, 1000);
  });
}

var express = require("express");
var app = express();
app.listen(3000, () => {
  console.log("Server running on port 3000");
});

app.get("/device/:state", (req, res, next) => {
  if (req.params.state == "t") {
    newState = (newState == 0 ? newState = 1 : newState = 0);
  } else {
    newState = req.params.state - 0;
  }

  console.log("State: ", newState);
  try {
    if (newState) {
      d.setPowerState(1);
    } else {
      d.setPowerState(0);
    }
  } catch(ex) {
    setTimeout(findPlug, 1000);
  }

  res.json({"done": newState == true});
});

app.use('/static', express.static('/home/pi/pi_to_potter/nodeservice/static'))
