package org.ts.ddcs.Worker;

import com.alibaba.fastjson.JSONObject;
import org.ts.ddcs.log.LogHelper;
import org.ts.ddcs.utils.GBConstants;

import java.util.concurrent.Callable;


@SuppressWarnings("all")
public class DataRelayToManageProcess implements Callable<Integer> {
    private String _rtuCode;
    private JSONObject _dataInfo;
    private String _host;
    private int _port;


    DataRelayToManageProcess(String rtuCode, JSONObject dataInfo, String host, int port) {
        this._rtuCode = rtuCode;
        this._dataInfo = dataInfo;
        this._host = host;
        this._port = port;
    }

    DataRelayToManageProcess(String rtuCode, JSONObject dataInfo) {
        this._rtuCode = rtuCode;
        this._dataInfo = dataInfo;
    }

    @Override
    public Integer call() throws Exception {
        try {
            LogHelper.info("notify taishan system " + _rtuCode + " " + _dataInfo.toJSONString());
            if (null != _dataInfo) {
                _dataInfo.put(GBConstants.BASE_KEY_RTU, _rtuCode);

                String func = _dataInfo.getString("func");
                if (!func.equals("2F") && !func.equals("E1") && !func.equals("2F") && !func.equals("C0") && !func.equals("32") && !func.equals("30") && !func.equals("33") && !func.equals("34")) {
                    //   JSONObject result = null;
                    //  try {
                    JSONObject info = new JSONObject();

                    info.put("agreement", _dataInfo.getString("agreement"));
                    info.put("cmd", _dataInfo.getString("func"));
                    info.put("rtu", _dataInfo.getString("rtuCode"));
                    info.put("text", _dataInfo.getJSONArray("data"));
                    DataProcessHolder.newInstance().dataProcess.mqttServer.send(info.toJSONString());
//                        RestTemplate restTemplate = new RestTemplate();
//                        result = restTemplate.postForObject(new URI(DataProcessHolder.newInstance().dataProcess.config.getTaishanNotifyUrl() + "/rtuManage/query/notify"), _dataInfo, JSONObject.class);
//                        if (result != null) {
//                            if (result.getString("code").equals("0")) {
                    // result.put("msg", "已通知泰山系统");
                    //  }
                    //}
//                    } catch (Exception e) {
//                        LogHelper.error(e);
//                        e.printStackTrace();
//                    }
                }
            } else {
                LogHelper.info("taishan system notify " + _rtuCode + " 消息体为空，不转发到泰山系统");
            }
        } catch (Exception e) {
            LogHelper.error(e);
//            e.printStackTrace();
        }
        return 0;
    }
}
