| | |
| | | } 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); |
| | |
| | | //获取逆变器列表 |
| | | 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列表 |
| | |
| | | //初始化逆变器 |
| | | case HDLUniMP.UNI_EVENT_REPLY_DEVICE_INITIALIZEINVERTER: { |
| | | this.setInitializeGateway(mode_type, data, callback); |
| | | } |
| | | break; |
| | | //读取设备远程信息 |
| | | case HDLUniMP.UNI_EVENT_REPLY_DEVICE_REMOTEINFO: { |
| | | this.setInverterDeviceRemoteInfo(mode_type, data, callback); |
| | | } |
| | | break; |
| | | |
| | |
| | | //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; |
| | |
| | | * @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请求数据 |
| | |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * 初始化逆变器 |
| | | * |
| | | * @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); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 发送modbus协议数据 |