package com.steerinfo.ems.Utils;

import com.client.MswebserviceStub;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.rmi.RemoteException;

/**
 * @author Shadow
 * @create 2021-10-27 22:23
 * @project xt-ems-api-new
 */
public class MosaicUtils {
    private static final Logger logger = LoggerFactory.getLogger(MosaicUtils.class);

    static MswebserviceStub.UserInfo userInfo =new MswebserviceStub.UserInfo();
    static MswebserviceStub.WSQueryExecute query = new MswebserviceStub.WSQueryExecute();
    static MswebserviceStub stub;

    /**
     * 打开mosaic连接
     */
    public static void SetOpenConnSample() {
        try {
            stub = new MswebserviceStub("http://172.16.90.229:9900");
            userInfo = new MswebserviceStub.UserInfo();
            userInfo.setMdbName("scada");
            userInfo.setMdbInstance("master");
            userInfo.setComText("");
            userInfo.setMIP("");
            userInfo.setMUserID("");
            userInfo.setMUserName("mosaic");
            userInfo.setMUserPass("mosaic");
            MswebserviceStub.WSConnectOpen openReq = new MswebserviceStub.WSConnectOpen();
            MswebserviceStub.WSConnectOpenResponse openResp = null;
            // Open
            openReq.setUserInfo(userInfo);

            openResp = stub.wSConnectOpen(openReq);
            logger.info("打开mosaic链接成功");
            // Save UserID
            userInfo.setMUserID(openResp.getId());

        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            logger.info("打开mosaic链接失败");
        }

    }

    /**
     *    更新点的值
     * @param sql sql语句
     */
    public static void SetUpdateValue(String sql) {
        // 0x0002:5365:2e
        MswebserviceStub.DataResult queryData = new MswebserviceStub.DataResult();
        try {
            //String sql = "update Ana set(Eng,User)=(100,0x0002:5365:2e) where AltId='PUMP1_SPEED' ";
            // Dig表为 update Dig set(Cs,User)=(1,0x0002:5365:2e) update Ana set Eng='1812482' where Equip='0x06f4:0bfe:1e' AND Quantity='0x006a:489c:22'
            userInfo.setComText(sql);
            query.setUserInfo(userInfo);
            queryData = stub.wSQueryExecute(query);
            //logger.info(queryData.getData());

        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     *    关闭mosaic链接
     */
    public static void SetCloseConnSample() {
        MswebserviceStub.WSConnectClose closeReq = new MswebserviceStub.WSConnectClose();
        closeReq.setUserInfo(userInfo);
        try {
            stub.wSConnectClose(closeReq);
            userInfo = null;
            logger.info("关闭mosaic链接成功");
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
