var webSocket = undefined
import Common from "../common/Common.js";
import Notify from "pc-component-v3").Notify;
export default {

    //打开websocket
    open: function (readerEnvent) {
        if (this.webSocket == null) {
            //webSocket的URL            
			var url = Common.getMidURL("/ws/ReaderEventWebSocket");
            url = websocketUrl.replace("https","wss").replace("http", "ws");
            
            //创建新的webSocket
            this.webSocket = new WebSocket(url);
        }else{
            Notify.info("提示", "webSocket已经连接", false);
        }
        this.webSocket.onmessage = function (evt) {
            var datas = JSON.parse(evt.data);
            if (datas != null) {
                readerEnvent(datas);
            }
        };
        this.webSocket.onopen = function () {
            Notify.success("提示", "webSocket已连接",1500);
        }
        this.webSocket.onerror = function (evt) {
            Notify.error("提示", "webSocket出错", false);
        };
        this.webSocket.onclose = function (evt) {
            //Notify.info("提示", "webSocket已断开", false);
        };
    },
    //关闭websocket
    close: function () {
        if (this.webSocket) {
            this.webSocket.close()
            this.webSocket = null;
        };
    },
    //订阅读写器
    subscribe: function (readerId) {
        this.webSocket.send("Subscribe " + readerId);
    },
    //取消订阅
    unSubscribe: function (readerId) {
        this.webSocket.send("UnSubscribe " + readerId);

    },
    //启动盘点
    start: function (readerId) {
        $.ajax({
            type: "GET",
            url: Common.getMidURL("/api/ReaderResource/startInventory/" + readerId),
            dataType: "json",
            contentType: "application/json",
            success: function (result) {
                Notify.info("提示", "开启盘点成功", false);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                Common.processException(XMLHttpRequest, textStatus, errorThrown);
            }
        });
    },
    //关闭盘点
    stop: function (readerId) {
        $.ajax({
            type: "GET",
            url: Common.getMidURL("/api/ReaderResource/stopInventory/" + readerId),
            dataType: "json",
            contentType: "application/json",
            success: function (statusConfigs) {
                Notify.info("提示", "关闭盘点成功", false);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                Common.processException(XMLHttpRequest, textStatus, errorThrown);
            }
        });
    }

}