package com.casic.missiles.nbiot; import com.casic.missiles.core.base.response.SuccessResponseData; import com.casic.missiles.modular.system.model.BusNbDevice; import com.casic.missiles.util.HttpsUtil; import com.casic.missiles.util.JsonUtil; import com.casic.missiles.util.StreamClosedHttpResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.HashMap; import java.util.Map; /** * Register Directly Connected Device : * This interface is used by NAs to register devices on the IoT platform. * After the registration is successful, * the IoT platform allocates a device ID for the device,which is used as the unique identifier of the device. * Unregistered devices are not allowed to access the IoT platform. * In NB-IoT scenarios, the Set device info interface needs to be invoked to set device information after the registration is successful. */ public class DeviceRegister { public static Logger log = LoggerFactory.getLogger(DeviceRegister.class); public static SuccessResponseData registerDevice(String appId, String appkey, String accessToken, String baseUrl, BusNbDevice device) throws Exception { // Two-Way Authentication HttpsUtil httpsUtil = new HttpsUtil(); httpsUtil.initSSLConfigForTwoWay(); //Please make sure that the following parameter values have been modified in the Constant file. String urlReg = baseUrl + Constant.REGISTER_DEVICE ; System.out.println("URL:"+urlReg); //please replace the verifyCode and nodeId and timeout, when you use the demo. String verifyCode = device.getVerifyCode(); String nodeId = device.getNodeid(); Integer timeout = device.getTimeout(); String isSecure = device.getIsSecure(); Map<String, Object> paramReg = new HashMap<>(); paramReg.put("verifyCode", verifyCode.toUpperCase()); paramReg.put("nodeId", nodeId.toUpperCase()); paramReg.put("timeout", timeout); // paramReg.put("isSecure", isSecure); String jsonRequest = JsonUtil.jsonObj2Sting(paramReg); //头header Map<String, String> header = new HashMap<>(); header.put(Constant.HEADER_APP_KEY, appId); header.put(Constant.HEADER_APP_AUTH, "Bearer" + " " + accessToken); StreamClosedHttpResponse responseReg = httpsUtil.doPostJsonGetStatusLine(urlReg, header, jsonRequest); System.out.println("RegisterDirectlyConnectedDevice, response content:"); System.out.println(responseReg.getStatusLine()); log.info("NBCUCC_regsister_response:"+responseReg.getStatusLine()); log.info(responseReg.getContent()); int code = responseReg.getStatusLine().getStatusCode(); if (code == 200) { Map<String, String> data = new HashMap<>(); data = JsonUtil.jsonString2SimpleObj(responseReg.getContent(), data.getClass()); String deviceId = data.get("deviceId"); return new SuccessResponseData(200, deviceId, "注册设备成功"); } else { return new SuccessResponseData(500, null, "注册设备失败,错误码为" + code); } } public static SuccessResponseData modifyDevice(String appId, String appkey, String accessToken, String baseUrl, BusNbDevice device) throws Exception { // Two-Way Authentication HttpsUtil httpsUtil = new HttpsUtil(); httpsUtil.initSSLConfigForTwoWay(); String deviceId = device.getNbDeviceId(); String urlModifyDeviceInfo = baseUrl + Constant.MODIFY_DEVICE_INFO + "/" + deviceId; String name = device.getName(); String manufacturerId = device.getManufacturerId(); String manufacturerName = device.getManufacturerName(); String deviceType = device.getDeviceType(); String model = device.getModel(); String protocolType = device.getProtocalType(); Map<String, Object> paramReg = new HashMap<>(); paramReg.put("name", name); paramReg.put("manufacturerId", manufacturerId); paramReg.put("manufacturerName", manufacturerName); paramReg.put("deviceType", deviceType); paramReg.put("model", model); paramReg.put("protocolType", protocolType); String jsonRequest = JsonUtil.jsonObj2Sting(paramReg); //头header Map<String, String> header = new HashMap<>(); header.put(Constant.HEADER_APP_KEY, appId); header.put(Constant.HEADER_APP_AUTH, "Bearer" + " " + accessToken); StreamClosedHttpResponse responseModifyDeviceInfo = httpsUtil.doPutJsonGetStatusLine(urlModifyDeviceInfo, header, jsonRequest); System.out.println("ModifyDeviceInfo, response content:"); System.out.println(responseModifyDeviceInfo.getStatusLine()); log.info("NBCUCC_modify_response:" + responseModifyDeviceInfo.getStatusLine()); log.info(responseModifyDeviceInfo.getContent()); int code = responseModifyDeviceInfo.getStatusLine().getStatusCode(); if (code == 204) { return new SuccessResponseData(204, null, "修改设备信息成功"); } else { return new SuccessResponseData(500, null, "修改设备信息失败,错误码为" + code); } } public static SuccessResponseData deleteDevice(String appId, String appkey, String accessToken, String baseUrl, String deviceId) throws Exception { // Two-Way Authentication HttpsUtil httpsUtil = new HttpsUtil(); httpsUtil.initSSLConfigForTwoWay(); //please replace the deviceId, when you use the demo. String urlDelete = baseUrl+Constant.DELETE_DEVICE + "/" +deviceId; Map<String, String> header = new HashMap<>(); header.put(Constant.HEADER_APP_KEY, appId); header.put(Constant.HEADER_APP_AUTH, "Bearer" + " " + accessToken); StreamClosedHttpResponse responseDelete = httpsUtil.doDeleteGetStatusLine(urlDelete, header); System.out.println("DeleteDirectlyConnectedDevice, response content:"); System.out.println(responseDelete.getStatusLine().getStatusCode()); log.info("NBCUCC_delete_response:"+responseDelete.getStatusLine()); log.info(responseDelete.getContent()); int code = responseDelete.getStatusLine().getStatusCode(); if(code==204){ return new SuccessResponseData(204,null,"删除设备成功"); }else{ return new SuccessResponseData(500,null,"删除设备失败,错误码为"+code); } } }