Created
January 9, 2022 13:33
-
-
Save celso/8cff867b8bd53be103717f5db8110809 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const url = require("url"); | |
const path = require("path"); | |
const fs = require("fs"); | |
const { readdir } = require("fs").promises; | |
const https = require("https"); | |
const baseUrl = "https://api.wall-box.com/"; | |
const httpOptions = { | |
hostname: "api.wall-box.com", | |
port: 443, | |
path: "/", | |
method: "GET", | |
headers: { | |
Accept: "application/json", | |
"Content-Type": "application/json;charset=UTF-8", | |
}, | |
}; | |
let jwt = false; | |
const hash = (c) => { | |
return crypto.createHmac("sha256", config.secret).update(JSON.stringify(c)).digest("hex"); | |
}; | |
const get = (path, headers = {}) => { | |
return new Promise((resolve, reject) => { | |
let options = { ...httpOptions }; | |
options.path = path; | |
if (jwt) { | |
headers["Authorization"] = `Bearer ${jwt}`; | |
} | |
options.headers = { ...httpOptions.headers, ...headers }; | |
console.log(options); | |
const req = https.request(options, (res) => { | |
console.log(`statusCode: ${res.statusCode}`); | |
const body = []; | |
res.on("data", (d) => body.push(d)); | |
res.on("end", () => resolve(body.join(""))); | |
}); | |
req.on("error", (error) => reject(error)); | |
req.end(); | |
}); | |
}; | |
const auth = (username, password) => { | |
return new Promise((resolve, reject) => { | |
if (jwt) resolve(jwt); | |
else { | |
get("/auth/token/user", { | |
Authorization: "Basic " + new Buffer.from(username + ":" + password).toString("base64"), | |
}) | |
.then((r) => { | |
jwt = JSON.parse(r).jwt; | |
resolve(jwt); | |
}) | |
.catch((err) => { | |
reject(err); | |
}); | |
} | |
}); | |
}; | |
const getChargersList = () => { | |
return new Promise((resolve, reject) => { | |
get("/v3/chargers/groups") | |
.then((r) => { | |
r = JSON.parse(r); | |
resolve(r); | |
}) | |
.catch((err) => { | |
reject(err); | |
}); | |
}); | |
}; | |
const getSessionList = (id, start, end) => { | |
return new Promise((resolve, reject) => { | |
get(`/v4/sessions/stats?charger=${id}&start_date=${parseInt(start) / 1000}&end_date=${parseInt(end) / 1000}`) | |
.then((r) => { | |
r = JSON.parse(r); | |
resolve(r); | |
}) | |
.catch((err) => { | |
console.log(err); | |
reject(err); | |
}); | |
}); | |
}; | |
const getChargerStatus = (id) => { | |
return new Promise((resolve, reject) => { | |
get(`/chargers/status/${id}`) | |
.then((r) => { | |
r = JSON.parse(r); | |
resolve(r); | |
}) | |
.catch((err) => { | |
console.log(err); | |
reject(err); | |
}); | |
}); | |
}; | |
module.exports = { | |
auth, | |
getChargersList, | |
getSessionList, | |
getChargerStatus, | |
}; |
Author
celso
commented
Jan 9, 2022
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment