// pushcat自定义设备,可接收消息推送并通知 window.PushCatDevice = /** @class */ (function () { function PushCatDevice(param) { var _this = this; this.accessKey = param.accessKey; this.host = param.host || "https://sct.icodef.com/"; this.lasttime = param.lasttime || Math.floor(new Date().getTime() / 1000); // 先注册设备 fetch(this.host + "openapi/v1/device/register?access_key=" + this.accessKey, { method: "POST", body: JSON.stringify({ name: param.name, describe: param.describe, tags: param.tags, auto_create: param.auto_create, }), headers: { "Content-Type": "application/json", }, }) .then(function (resp) { return resp.json(); }) .then(function (resp) { var _a; if (resp.code !== 0) { (_a = _this.onerror) === null || _a === void 0 ? void 0 : _a.call(_this, resp); } else { // 第一个版本使用每分钟轮询的形式,以减少服务器压,后续缓存websocket _this.timer = setInterval(function () { fetch(_this.host + "openapi/v1/message/pull?access_key=" + _this.accessKey + "&last_time=" + _this.lasttime + "&device_id=" + resp.data.id, { method: "GET", }) .then(function (resp) { return resp.json(); }) .then(function (resp) { if (resp.code === 0) { _this.lasttime = Math.floor(new Date().getTime() / 1000); _this.onmessage && _this.onmessage(resp.data.list); } else { _this.onerror && _this.onerror(resp); } }); }, (param.check_interval || 60) * 1000); } }); } PushCatDevice.prototype.onMessage = function (callback) { this.onmessage = callback; }; PushCatDevice.prototype.onError = function (callback) { }; // TODO PushCatDevice.prototype.listen = function (callback, onerror) { // const ws = new WebSocket(this.host + "ws/" + this.accessKey); // ws.onmessage = (e) => { // callback(); // }; // ws.onerror = (e) => { // onerror(); // }; }; PushCatDevice.prototype.stop = function () { clearInterval(this.timer); }; return PushCatDevice; }()); window.PushCat = /** @class */ (function () { function PushCat(param) { this.accessKey = param.accessKey; this.host = param.host || "https://sct.icodef.com/"; } PushCat.prototype.Send = function (title, content, target, options) { if ((options === null || options === void 0 ? void 0 : options.method) === "GET") { var searchParams_1 = new URLSearchParams(); searchParams_1.append("access_key", this.accessKey); searchParams_1.append("title", title); searchParams_1.append("content", content); if (options === null || options === void 0 ? void 0 : options.parameters) { searchParams_1.append("parameters", JSON.stringify(options === null || options === void 0 ? void 0 : options.parameters)); } if (target.tags) { target.tags.forEach(function (val) { searchParams_1.append("tags", val); }); } else if (target.devices) { target.devices.forEach(function (val) { searchParams_1.append("devices", val); }); } return fetch(this.host + "openapi/v1/message/send?" + searchParams_1, { method: "GET", }); } return fetch(this.host + "openapi/v1/message/send", { method: "POST", body: JSON.stringify({ access_key: this.accessKey, title: title, content: content, target: target, parameters: options === null || options === void 0 ? void 0 : options.parameters, }), headers: { "Content-Type": "application/json", }, }); }; return PushCat; }());