@svrooij/sunspec
Version:
Reading sunspec
83 lines • 4.77 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SunspecReader = void 0;
const promise_modbus_1 = require("./promise-modbus");
const sunspec_converter_1 = require("./sunspec-converter");
class SunspecReader {
constructor(host, port) {
this.host = host;
this.port = port;
this.data = {};
this.client = new promise_modbus_1.PromiseModbus(host, port);
}
readInverterInfo() {
return __awaiter(this, void 0, void 0, function* () {
yield this.client.waitOpenAsync();
// Read the registers starting at 0 (= 40001) and read 69 items
const result = yield this.client.readHoldingRegisters(0, 69);
// console.log('Converter info: ', result.data)
return this.updateInfo(result.data);
});
}
readData() {
return __awaiter(this, void 0, void 0, function* () {
yield this.client.waitOpenAsync();
// Read the registers starting at 69 (= 40070) and read 40 items
const result = yield this.client.readHoldingRegisters(69, 40);
return this.updateData(result.data);
});
}
updateData(reading) {
// Replace override some value of this.data
this.data = Object.assign(Object.assign({}, this.data), {
did: reading[0],
lifetimeProduction: (reading[24] * Math.pow(2, 16)) + reading[25],
temperature: sunspec_converter_1.SunspecConverter.computeWithFactor(reading[34], reading[37]),
status: reading[38],
acTotalCurrent: sunspec_converter_1.SunspecConverter.computeWithFactor(reading[2], reading[6]),
acPower: sunspec_converter_1.SunspecConverter.computeWithFactor(reading[14], reading[15]),
acFrequency: sunspec_converter_1.SunspecConverter.computeWithFactor(reading[16], reading[17]),
apparentPower: sunspec_converter_1.SunspecConverter.computeWithFactor(reading[18], reading[19]),
reactivePower: sunspec_converter_1.SunspecConverter.computeWithFactor(reading[20], reading[21]),
powerFactor: sunspec_converter_1.SunspecConverter.computeWithFactor(reading[22], reading[23]),
dcCurrent: sunspec_converter_1.SunspecConverter.computeWithFactor(reading[27], reading[28]),
dcVoltage: sunspec_converter_1.SunspecConverter.computeWithFactor(sunspec_converter_1.SunspecConverter.int16(reading[29]), reading[30]),
dcPower: sunspec_converter_1.SunspecConverter.computeWithFactor(reading[31], reading[32]),
});
if (this.data === 103) { // add three phase data
this.data = Object.assign(Object.assign({}, this.data), {
acCurrentPhaseA: sunspec_converter_1.SunspecConverter.computeWithFactor(reading[3], reading[6]),
acCurrentPhaseB: sunspec_converter_1.SunspecConverter.computeWithFactor(reading[4], reading[6]),
acCurrentPhaseC: sunspec_converter_1.SunspecConverter.computeWithFactor(reading[5], reading[6])
});
}
// console.log(this.data);
// reading.forEach((val, i) => {
// console.log('Index %d (addr. %d) => %d', i, i + 70, val)
// })
return this.data;
}
updateInfo(uints) {
// The info are strings saved as uints, so 2 letters in one number.
// Slice takes a porting ot the string from start index until end index (excluding the item in end index)
this.data = Object.assign(Object.assign({}, this.data), {
// manufacturer: SunspecConverter.chars(uints[4])
manufacturer: sunspec_converter_1.SunspecConverter.string(uints.slice(4, 20)),
model: sunspec_converter_1.SunspecConverter.string(uints.slice(20, 36)),
version: sunspec_converter_1.SunspecConverter.string(uints.slice(44, 52)),
serial: sunspec_converter_1.SunspecConverter.string(uints.slice(52, 68))
});
return this.data;
}
}
exports.SunspecReader = SunspecReader;
//# sourceMappingURL=sunspec-reader.js.map