mac
2024-06-12 d8d41b0623146ec890ec9191b613c0e8f13197dd
app/src/main/java/com/hdl/photovoltaic/other/HdlUniLogic.java
@@ -46,7 +46,7 @@
import com.hdl.photovoltaic.ui.bean.OidBean;
import com.hdl.photovoltaic.ui.bean.UnCountBean;
import com.hdl.photovoltaic.ui.me.AsRegardsActivity;
import com.hdl.photovoltaic.ui.me.PersonalDataActivity;
import com.hdl.photovoltaic.ui.me.CPersonalDataActivity;
import com.hdl.photovoltaic.ui.me.SetActivity;
import com.hdl.photovoltaic.ui.newC.MessageCenterList;
import com.hdl.photovoltaic.ui.newC.PowerStationsListEdit;
@@ -202,6 +202,11 @@
            } else if (HDLUniMP.UNI_EVENT_REPLY_DEVICE_MODEL.equals(topic)) {
                //设备模块
                switch (type) {
                    //modBus协议专用
                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_MODBUS_SEND: {
                        sendModBus(mode_type, data, callback);
                    }
                    break;
                    //添加逆变器到云端
                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_ADD: {
                        this.uniAddInverterDeviceToCloud(mode_type, data, callback);
@@ -225,11 +230,6 @@
                    //获取逆变器列表
                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_LIST: {
                        this.uniGetCurrentHomeLocalAndCloudGatewayList(mode_type, data, callback);
                    }
                    break;
                    //modBus协议专用
                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_MODBUS_SEND: {
                        sendModBus(mode_type, data, callback);
                    }
                    break;
                    //获取oid列表
@@ -277,7 +277,16 @@
                        this.setInitializeGateway(mode_type, data, callback);
                    }
                    break;
                    //读取设备远程信息
                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_REMOTEINFO: {
                        this.setInverterDeviceRemoteInfo(mode_type, data, callback);
                    }
                    break;
                    //设备名称修改(修改是云端oid名称,设备使用modbus协议修改)
                    case HDLUniMP.UNI_EVENT_REPLY_DEVICE_EDIT_NAME: {
                        this.uniOidEditName(mode_type, data, callback);
                    }
                    break;
                }
            } else if (HDLUniMP.UNI_EVENT_REPLY_WIFI_MODEL.equals(topic)) {
@@ -423,7 +432,7 @@
                    case HDLUniMP.UNI_EVENT_REPLY_MINE_EDIT_USER_INFO: {
                        Intent intent = new Intent();
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.setClass(HDLApp.getInstance(), PersonalDataActivity.class);
                        intent.setClass(HDLApp.getInstance(), CPersonalDataActivity.class);
                        HDLApp.getInstance().startActivity(intent);
                    }
                    break;
@@ -457,6 +466,11 @@
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.setClass(HDLApp.getInstance(), SetActivity.class);
                        HDLApp.getInstance().startActivity(intent);
                    }
                    break;
                    //成员列表
                    case HDLUniMP.UNI_EVENT_REPLY_MEMBER_LIST: {
                    }
                    break;
@@ -498,10 +512,7 @@
                    //uni主页点击返回通知
                    case HDLUniMP.UNI_EVENT_REPLY_OTHER_BACK: {
                        //EventBus事件分发
                        BaseEventBus baseEventBus = new BaseEventBus();
                        baseEventBus.setTopic(HDLUniMP.UNI_EVENT_REPLY_OTHER_MODEL);
                        baseEventBus.setType(HDLUniMP.UNI_EVENT_REPLY_OTHER_BACK);
                        EventBus.getDefault().post(baseEventBus);
                        HdlCommonLogic.getInstance().postEventBus(HDLUniMP.UNI_EVENT_REPLY_OTHER_MODEL, HDLUniMP.UNI_EVENT_REPLY_OTHER_BACK);
                    }
                    break;
@@ -529,6 +540,12 @@
    public void openUniMP(String path, JSONObject jsonObject) {
        JSONObject json = this.createdJSONObject(jsonObject, true);
        HDLUniMPSDKManager.getInstance().openUniMP(HDLUniMP.UNI_APP_ID, path, json, HdlUniLogic.this);
        HdlLogLogic.print("uni---组装uni发送数据格式---" + json, false);
    }
    public void openUniMPDelay(String path, JSONObject jsonObject) {
        JSONObject json = this.createdJSONObject(jsonObject, true);
        HDLUniMPSDKManager.getInstance().openUniMPDelay(HDLUniMP.UNI_APP_ID, path, json, HdlUniLogic.this);
        HdlLogLogic.print("uni---组装uni发送数据格式---" + json, false);
    }
@@ -794,7 +811,7 @@
        String module = "FW#" + imageId;//模块信息(HW:硬件)
        //升级本地优先->云端升级
        HdlDeviceLogic.getInstance().isLocalConnect(deviceMac, new CloudCallBeak<Boolean>() {
        HdlDeviceLogic.getInstance().isLocalConnect(homeId, deviceMac, new CloudCallBeak<Boolean>() {
            @Override
            public void onSuccess(Boolean b) {
                if (b) {
@@ -1046,7 +1063,7 @@
        String imageId = getKeyValue("imageId", getKeyValue("data", data));//固件镜像id
        String module = driverCode + "#" + imageId;
        //升级本地优先->云端升级
        HdlDeviceLogic.getInstance().isLocalConnect(deviceMac, new CloudCallBeak<Boolean>() {
        HdlDeviceLogic.getInstance().isLocalConnect(homeId, deviceMac, new CloudCallBeak<Boolean>() {
            @Override
            public void onSuccess(Boolean b) {
                if (b) {
@@ -1491,7 +1508,7 @@
//        com.alibaba.fastjson.JSONArray jsonArray = JSON.parseArray(data);
        try {
            String devices = getKeyValue("devices", getKeyValue("data", data));
            String homeId = UserConfigManage.getInstance().getHomeId(); //绑定成功之后通知
            String homeId = getKeyValue("homeId", getKeyValue("data", data));
            Gson gson = new Gson();
            Type typeOfT = new TypeToken<List<GatewayBean>>() {
            }.getType();
@@ -1560,7 +1577,10 @@
     */
    private void uniDelInverterDevice(String type, Object data, DCUniMPJSCallback callback) {
        String deviceId = getKeyValue("deviceId", getKeyValue("data", data));
        String homeId = UserConfigManage.getInstance().getHomeId();
        String homeId = getKeyValue("homeId", getKeyValue("data", data));
        if (TextUtils.isEmpty(homeId)) {
            homeId = UserConfigManage.getInstance().getHomeId();
        }
        HdlDeviceLogic.getInstance().delInverterDevice(homeId, deviceId, new CloudCallBeak<Boolean>() {
            @Override
            public void onSuccess(Boolean obj) {
@@ -1719,77 +1739,75 @@
     * @param callback uni回调
     */
    public void startTcpThreadSendAccountAndPassword(String type, String ssid, String password, DCUniMPJSCallback callback) {
        new Thread(
                () -> {
                    Socket socket = null;
                    OutputStream outputStreamTcp = null;
                    InputStream inputStreamTcp = null;
                    try {
                        socket = new Socket();
                        socket.setTcpNoDelay(true);
                        socket.setSoTimeout(10 * 1000);//10秒超时
                        socket.connect(new InetSocketAddress("192.168.8.1", 8586));
                        socket.setKeepAlive(true);
        new Thread(() -> {
            Socket socket = null;
            OutputStream outputStreamTcp = null;
            InputStream inputStreamTcp = null;
            try {
                socket = new Socket();
                socket.setTcpNoDelay(true);
                socket.setSoTimeout(10 * 1000);//10秒超时
                socket.connect(new InetSocketAddress("192.168.8.1", 8586));
                socket.setKeepAlive(true);
//                        logMessage("tcp连接状态成功", 0, "");
                        outputStreamTcp = socket.getOutputStream();
                        JsonObject jsonObject = new JsonObject();
                        jsonObject.addProperty("id", "102030");
                        jsonObject.addProperty("ssid", ssid);
                        jsonObject.addProperty("password", password);
                        jsonObject.addProperty("server_addr", HDLLinkPMUser.getInstance().getHomeRegionUrl());
                        jsonObject.addProperty("homeId", UserConfigManage.getInstance().getHomeId());
                        String sendData = "Topic:/user/id/custom/wifi/set\r\n";
                        sendData += "Length:" + jsonObject.toString().getBytes().length + "\r\n\r\n";
                        sendData += jsonObject.toString();
                        outputStreamTcp.write(sendData.getBytes());
                        outputStreamTcp.flush();
                        Thread.sleep(200);
                        inputStreamTcp = socket.getInputStream();
                        byte[] bytes = new byte[1204 * 2];
                        int len = inputStreamTcp.read(bytes);
                        if (len != -1) {
                            String str = new String(bytes, 0, bytes.length);
                            String[] strings = str.split("\r\n\r\n");
                            String[] topicAndLength = strings[0].split("\r\n");
                            String topic = topicAndLength[0];
                            JSONObject json = new JSONObject(strings[1]);
                outputStreamTcp = socket.getOutputStream();
                JsonObject jsonObject = new JsonObject();
                jsonObject.addProperty("id", "102030");
                jsonObject.addProperty("ssid", ssid);
                jsonObject.addProperty("password", password);
                jsonObject.addProperty("server_addr", HDLLinkPMUser.getInstance().getHomeRegionUrl());
                jsonObject.addProperty("homeId", UserConfigManage.getInstance().getHomeId());
                String sendData = "Topic:/user/id/custom/wifi/set\r\n";
                sendData += "Length:" + jsonObject.toString().getBytes().length + "\r\n\r\n";
                sendData += jsonObject.toString();
                outputStreamTcp.write(sendData.getBytes());
                outputStreamTcp.flush();
                Thread.sleep(200);
                inputStreamTcp = socket.getInputStream();
                byte[] bytes = new byte[1204 * 2];
                int len = inputStreamTcp.read(bytes);
                if (len != -1) {
                    String str = new String(bytes, 0, bytes.length);
                    String[] strings = str.split("\r\n\r\n");
                    String[] topicAndLength = strings[0].split("\r\n");
                    String topic = topicAndLength[0];
                    JSONObject json = new JSONObject(strings[1]);
//                            logMessage("tcp回复数据", 0, strings[0] + "\r\n" + json.toString());
                            if (topic.endsWith("wifi/set_reply") || topic.endsWith("wifi/result/notify")) {
                                if (callback != null) {
                                    uniCallbackData(type, json, callback);
                                }
                                if (json.has("mac")) { //回复成功再拿设备mac
                    if (topic.endsWith("wifi/set_reply") || topic.endsWith("wifi/result/notify")) {
                        if (callback != null) {
                            uniCallbackData(type, json, callback);
                        }
                        if (json.has("mac")) { //回复成功再拿设备mac
//                                    this.device_mac = json.getString("mac");
//                                    this.isSucceedTcp = true;//表示成功
                                }
                            }
                        }
                    } catch (Exception ignored) {
                    } finally {
                        try {
                            if (outputStreamTcp != null) {
                                outputStreamTcp.close();
                                outputStreamTcp = null;
                            }
                            if (inputStreamTcp != null) {
                                inputStreamTcp.close();
                                inputStreamTcp = null;
                            }
                            if (socket != null) {
                                socket.close();
                                socket = null;
                            }
                        } catch (Exception ignored) {
                        }
                    }
                }
        ).start();
            } catch (Exception ignored) {
            } finally {
                try {
                    if (outputStreamTcp != null) {
                        outputStreamTcp.close();
                        outputStreamTcp = null;
                    }
                    if (inputStreamTcp != null) {
                        inputStreamTcp.close();
                        inputStreamTcp = null;
                    }
                    if (socket != null) {
                        socket.close();
                        socket = null;
                    }
                } catch (Exception ignored) {
                }
            }
        }).start();
    }
    /**
     * 初始化你逆变器
     * 初始化逆变器
     *
     * @param type     账号
     * @param data     uni请求数据
@@ -1810,6 +1828,78 @@
        });
    }
    /**
     * 初始化逆变器
     *
     * @param type     账号
     * @param data     uni请求数据
     * @param callback uni回调
     */
    public void setInverterDeviceRemoteInfo(String type, Object data, DCUniMPJSCallback callback) {
        String homeId = getKeyValue("homeId", getKeyValue("data", data));
        String json = getKeyValue("list", getKeyValue("data", data));
        List<CloudInverterDeviceBean> newList = new ArrayList<>();
        try {
            List<GatewayBean> list = new Gson().fromJson(json, new TypeToken<List<GatewayBean>>() {
            }.getType());
            if (list != null && list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    GatewayBean gatewayBean = list.get(i);
                    CloudInverterDeviceBean inverterDeviceBean = new CloudInverterDeviceBean();
                    inverterDeviceBean.setCategorySecondName(gatewayBean.getCategorySecondName());
                    inverterDeviceBean.setOsn(gatewayBean.getDevice_mac());
                    inverterDeviceBean.setOmodel(gatewayBean.getDevice_model());
                    inverterDeviceBean.setOid(gatewayBean.getOid());
                    inverterDeviceBean.setAddresses(gatewayBean.getAddresses());
                    inverterDeviceBean.setSid(gatewayBean.getSid());
                    inverterDeviceBean.setDeviceId(gatewayBean.getDeviceId());
                    inverterDeviceBean.setName(gatewayBean.getDevice_name());
                    inverterDeviceBean.setDeviceStatus(gatewayBean.getDeviceStatus());
                    inverterDeviceBean.setSpk(gatewayBean.getSpk());
                    inverterDeviceBean.setDeviceType(gatewayBean.getDeviceType());
                    newList.add(inverterDeviceBean);
                }
            }
        } catch (Exception ignored) {
        }
        HdlDeviceLogic.getInstance().setDeviceRemoteInfo(newList, homeId, new CloudCallBeak<List<GatewayBean>>() {
            @Override
            public void onSuccess(List<GatewayBean> obj) {
                uniCallbackData(type, obj, callback);
            }
            @Override
            public void onFailure(HDLException e) {
                uniCallbackData(type, null, e.getCode(), e.getMsg(), callback);
            }
        });
    }
    /**
     * 修改设备名称逆变器BMS电池包
     *
     * @param type     账号
     * @param data     uni请求数据
     * @param callback uni回调
     */
    public void uniOidEditName(String type, Object data, DCUniMPJSCallback callback) {
        String homeId = getKeyValue("homeId", getKeyValue("data", data));
        String deviceType = getKeyValue("deviceType", getKeyValue("data", data));
        String deviceId = getKeyValue("deviceId", getKeyValue("data", data));
        String name = getKeyValue("name", getKeyValue("data", data));
        HdlDeviceLogic.getInstance().setRename(homeId, deviceType, deviceId, name, new CloudCallBeak<Boolean>() {
            @Override
            public void onSuccess(Boolean obj) {
                uniCallbackData(type, obj, callback);
            }
            @Override
            public void onFailure(HDLException e) {
                uniCallbackData(type, null, e.getCode(), e.getMsg(), callback);
            }
        });
    }
    /**
     * 发送modbus协议数据