function check(v, re, msg) { if (!re.test(v)) { alert(msg) } } function padPkcs7(data) { var d = data.length % 16; d = 16 - d; var arr = Array.from(data) for (var i = 0; i < d; i++) { arr.push(d) } return arr; } function stripPcks7(data) { var arr = Array.from(data) var padder = arr[arr.length - 1]; return arr.slice(0, arr.length - padder); } function ecb(bytes, cb) { var ret = [] for (var i = 0; i < bytes.length; i += 16) { var d = bytes.slice(i, i + 16); var en = cb(d) ret = ret.concat(Array.from(en)) } return ret; } function stringToHex(str){ var val = ""; for (var i = 0; i < str.length; i++) { var v = str.charCodeAt(i).toString(16); if ( v.length < 2 ) { v = "0" + v; } val += v; } return val } function hexToString(hex){ var arr = hex.split("") var out = "" for (var i = 0; i < arr.length / 2; i++) { var tmp = "0x" + arr[i * 2] + arr[i * 2 + 1] var charValue = String.fromCharCode(tmp); out += charValue } return out } class ActiveIC { constructor() { this.server = "ws://127.0.0.1:22222/active"; this.waitMap = {} } __debug(...str) { if (window.active_debug) { console.log(...str) } } connect() { this.__debug("connect") var _this = this; try { if (this.sock) { this.sock.onclose = function () { } this.sock.close() } this.sock = new WebSocket(this.server); this.__debug("sock") this.sock.onopen = function () { _this.__debug("open"); _this.sock.isopened = true; _this.sock.onmessage = function (e) { _this.__debug(e.data) var ret = JSON.parse(e.data); var cb = _this.waitMap[ret.id] cb && cb(ret) } }; this.sock.onclose = function () { _this.sock.isopened = false; _this.__debug("close"); _this.reconnect(); }; // this.sock.onerror = function () { // _this.sock.isopened = false; // this.__debug("onerror"); // _this.reconnect(); // }; } catch (e) { _this.__debug("onerror"); _this.reconnect(); } } reconnect() { this.__debug("reconnect") var _this = this; if (_this.sock && _this.sock.isopened) { this.__debug("miss") return } setTimeout(function () { _this.connect(); }, 3000); } __send(cmd, param, cb) { var id = (Math.random() + "").substr(2); this.sock.send( JSON.stringify({ id: id, cmd: cmd, param: param }) ); if (cb) { this.waitMap[id] = cb; } } __check(v, re, msg, reject) { if (!re.test(v)) { reject && reject({ msg, code: "C0" }) } } readText(addr, len) { var _this = this; return new Promise(function (resolve, reject) { if (!_this.sock || !_this.sock.isopened) { reject({ code: "CB", msg: "Active 通信失败" }) } _this.__check(addr, /\d{1,2}/g, "用户编号填写错误!", reject); _this.__check(len, /\d{1,2}/g, "用户类型填写错误!", reject); _this.__send("read", { addr, len }, function (ret) { if (ret.code === "00") { resolve(ret) } else { reject(ret) } }) }) } writeText(addr, data) { return this.writeHex(addr, stringToHex( data )) } writeHex(addr, data) { var _this = this; return new Promise(function (resolve, reject) { if (!_this.sock || !_this.sock.isopened) { reject({ code: "CB", msg: "Active 通信失败" }) } _this.__check(addr, /\d{1,2}/g, "地址填写错误!", reject); _this.__check(data, /^([A-Fa-f0-9][A-Fa-f0-9])+$/g, "数据填写错误!", reject); _this.__send("write", { addr, data }, function (ret) { if (ret.code === "00") { resolve(ret) } else { reject(ret) } }) }) } } /** * 检测通过返回 true * 检测不通过返回 fase */ ActiveIC.checkSupport = function () { try { if (!!window.WebSocket && window.WebSocket.prototype.send) { return true; } else { console.error("您的浏览器不支持!") return false; } } catch (e) { console.error("当前环境不支持!", e) return false; } }