ez1cli
Version:
NodeJS command line API client and library for APSystems EZ1-(M|H) microinverters API
68 lines (67 loc) • 1.78 kB
JavaScript
import axios from "axios";
export class EZ1API {
ip;
port;
constructor(ip, port) {
this.ip = ip;
this.port = port || 8050;
}
async call(method) {
switch (method) {
case "data": {
return this.getData();
}
case "device-info": {
return this.getDeviceInfo();
}
default: {
throw new Error(`Unknown method: ${method}`);
}
}
}
async getData() {
try {
const response = await axios({
method: "GET",
url: `http://${this.ip}:${this.port}/getOutputData`,
headers: {
Accept: 'application/json',
},
timeout: 2000
});
const d = response.data.data;
return {
status: 200,
data: {
channel1: {
lifetimeGeneration: d.te1,
power: d.p1,
startupGeneration: d.e1,
}, channel2: {
lifetimeGeneration: d.te2,
power: d.p2,
startupGeneration: d.e2,
}
}
};
}
catch (err) {
return {
status: 500
};
}
}
async getDeviceInfo() {
const response = await axios({
url: `http://${this.ip}:${this.port}/getDeviceInfo`,
headers: {
Accept: 'application/json',
},
timeout: 2000
});
return {
status: 200,
data: response.data
};
}
}