wiegand-control
Version:
Communicate with wiegand door access controller.
178 lines (177 loc) • 6.54 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var dgram_1 = require("dgram");
var util_1 = require("util");
var utils_1 = require("./utils");
var funcNames_1 = __importDefault(require("./funcNames"));
var _a = process.env, WGC_HIDE_LOG = _a.WGC_HIDE_LOG, WGC_ECHO_1 = _a.WGC_ECHO_1, WGC_ECHO_2 = _a.WGC_ECHO_2, WGC_ECHO_3 = _a.WGC_ECHO_3;
var config = {
hideLog: !!WGC_HIDE_LOG,
echo1: WGC_ECHO_1 ? +WGC_ECHO_1 : 0,
echo2: WGC_ECHO_2 ? +WGC_ECHO_2 : 0,
echo3: WGC_ECHO_3 ? +WGC_ECHO_3 : 0
};
var WgCtl = /** @class */ (function () {
function WgCtl(socket, serial, serverIp, serverPort, ip, port) {
if (ip === void 0) { ip = ""; }
if (port === void 0) { port = 60000; }
this.ip = ip;
this.port = port;
this.serial = serial;
if (socket instanceof dgram_1.Socket) {
this.localSocket = socket;
// server ip and port only available for local mode
this.serverIp = serverIp;
this.serverPort = serverPort;
}
else {
this.remoteSocket = socket;
if (this.serverIp || this.serverPort) {
throw new Error("Server ip and port only available for local mode.");
}
}
}
WgCtl.prototype.packData = function (funcCode, payload) {
var data = Buffer.alloc(64);
data.writeUInt8(0x17, 0);
data.writeUInt8(funcCode, 1);
if (this.serial) {
data.writeUInt32LE(this.serial, 4);
}
if (payload) {
if (util_1.isBuffer(payload)) {
data.fill(payload, 8, 8 + payload.byteLength);
}
else if (util_1.isNumber(payload)) {
data.writeUInt8(payload, 8);
}
else {
data.write(payload.replace(/\s/g, ""), 8, "hex");
}
}
else {
payload = Buffer.alloc(0);
}
var funcCodeStr = "0x" + funcCode.toString(16).toUpperCase();
if (!config.hideLog) {
console.log("[WGC] Func " + funcNames_1.default[funcCodeStr] + ", controller " + (this.serial ||
"all") + ", payload to send:", payload);
}
return data;
};
WgCtl.prototype.sendData = function (funcCode, payload) {
var data = this.packData(funcCode, payload);
if (this.remoteSocket) {
return this.remoteSendData(data);
}
else {
return this.localSendData(data);
}
};
WgCtl.prototype.remoteSendData = function (data) {
if (!this.remoteSocket)
return;
this.remoteSocket.write(data, function (err) {
if (err) {
console.error(err);
}
});
};
WgCtl.prototype.localSendData = function (data, isEcho) {
var _this = this;
if (isEcho === void 0) { isEcho = false; }
if (!this.localSocket)
return;
if (!this.ip) {
this.localSocket.setBroadcast(true);
}
if (!WGC_HIDE_LOG) {
console.log("[WGC] Sending local data to " + (this.ip || "255.255.255.255") + ".");
}
this.localSocket.send(data, 0, data.byteLength, this.port, this.ip || "255.255.255.255", function (err, result) {
if (err) {
console.error(err);
if (!_this.ip && _this.localSocket) {
_this.localSocket.setBroadcast(false);
}
}
});
if (!isEcho && config.echo1) {
setTimeout(function () {
_this.localSendData(data, true);
if (config.echo2) {
setTimeout(function () {
_this.localSendData(data, true);
if (config.echo3) {
setTimeout(function () {
_this.localSendData(data, true);
}, +config.echo3);
}
}, +config.echo2);
}
}, +config.echo1);
}
};
WgCtl.prototype.search = function () {
this.sendData(0x94);
};
WgCtl.prototype.openDoor = function (door) {
this.sendData(0x40, door);
};
WgCtl.prototype.getDate = function () {
this.sendData(0x32);
};
WgCtl.prototype.setDate = function (date) {
this.sendData(0x30, utils_1.buildBcdDate(date || new Date()));
};
WgCtl.prototype.setAuth = function (cardNo, door) {
var payload = Buffer.alloc(16);
payload.writeUInt32LE(cardNo, 0);
payload.write("20190101", 4, "hex");
payload.write("20291231", 8, "hex");
payload.writeUInt8(door && door !== 1 ? 0 : 1, 12);
payload.writeUInt8(door && door !== 2 ? 0 : 1, 13);
payload.writeUInt8(door && door !== 3 ? 0 : 1, 14);
payload.writeUInt8(door && door !== 4 ? 0 : 1, 15);
this.sendData(0x50, payload);
};
WgCtl.prototype.getAuth = function (cardNo) {
var payload = Buffer.alloc(4);
payload.writeUInt32LE(cardNo, 0);
this.sendData(0x5a, payload);
};
WgCtl.prototype.removeAuth = function (cardNo) {
var payload = Buffer.alloc(4);
payload.writeUInt32LE(cardNo, 0);
this.sendData(0x52, payload);
};
WgCtl.prototype.clearAuth = function () {
var payload = Buffer.from("55aaaa55", "hex");
this.sendData(0x54, payload);
};
WgCtl.prototype.setServerAddress = function (ip, port, interval) {
if (interval === void 0) { interval = 0; }
var payload = Buffer.alloc(7);
payload.write(utils_1.ipToHex(ip), "hex");
payload.writeUInt16LE(port, 4);
payload.writeUInt8(interval, 6);
this.sendData(0x90, payload);
};
WgCtl.prototype.setAddress = function (ip, subnet, gateway) {
var payload = Buffer.alloc(16);
payload.write(utils_1.ipToHex(ip), "hex");
payload.write(utils_1.ipToHex(subnet), 4, "hex");
payload.write(utils_1.ipToHex(gateway), 8, "hex");
payload.write("55aaaa55", 12, "hex");
this.sendData(0x96, payload);
this.ip = "";
};
WgCtl.prototype.getServerAddress = function () {
this.sendData(0x92);
};
return WgCtl;
}());
exports.default = WgCtl;