我是靠谱客的博主 无聊缘分,这篇文章主要介绍电信物联网平台,java后台对接电信北向应用,命令下发到设备,现在分享给大家,希望可以做个参考。

最近公司设备需要对接电信物联网平台,需要实现数据下发到设备

实现流程

第三方应用 ----- > 电信平台 ( 下发给设备) --------> 设备   ------ > 电信平台  ------ >  第三方应用(回传下发命令状态是否成)

第一步获取token 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/** * 登录认证获取token * Authentication锛実et token * */ @SuppressWarnings("unchecked") public static Map<String, String> login(HttpsUtil httpsUtil) throws Exception { String appId = Constant.APPID; String secret = Constant.SECRET; String urlLogin = "https://server:port/iocm/app/sec/v1.1.0/login"; Map<String, String> paramLogin = new HashMap<String, String>(); paramLogin.put("appId", appId); paramLogin.put("secret", secret); StreamClosedHttpResponse responseLogin = httpsUtil.doPostFormUrlEncodedGetStatusLine(urlLogin, paramLogin); System.out.println("app auth success,return accessToken:"); System.out.print(responseLogin.getStatusLine()); System.out.println(responseLogin.getContent()); Map<String, String> data = new HashMap<String, String>(); return JsonUtil.jsonString2SimpleObj(responseLogin.getContent(), data.getClass()); }

第二步订阅命令结果通知

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/** * 2.5.1 订阅平台业务数据,设备数据变化 * @param httpsUtil * @param accessToken * @throws Exception */ public static int deviceDataChanged(HttpsUtil httpsUtil,String accessToken,String callbackUrl,String notifyType) throws Exception{ String appId = Constant.APPID; // please replace the appId, when you use the demo. Map<String, Object> paramSubscribe = new HashMap<String, Object>(); paramSubscribe.put("appId", appId); paramSubscribe.put("callbackUrl", callbackUrl); paramSubscribe.put("notifyType", notifyType); String jsonRequest = JsonUtil.jsonObj2Sting(paramSubscribe); Map<String, String> header = new HashMap<String, String>(); header.put(Constant.HEADER_APP_KEY, appId); header.put(Constant.HEADER_APP_AUTH, "Bearer" + " " + accessToken); HttpResponse httpResponse = httpsUtil.doPostJson(Constant.paltform_data_changed_url, header, jsonRequest); String bodySubscribe = httpsUtil.getHttpResponseBody(httpResponse); System.out.println(httpResponse.getStatusLine()); System.out.println(bodySubscribe); return httpResponse.getStatusLine().getStatusCode(); }

通知类型,第三方应用可以根据通知类型对消息分别进行处理。

  1. bindDevice(绑定设备)
  2. deviceAdded(添加新设备)
  3. deviceInfoChanged(设备信息变化)
  4. deviceDataChanged(设备数据变化)
  5. deviceDatasChanged(设备数据批量变化)
  6. deviceDeleted(删除设备)
  7. messageConfirm(消息确认)
  8. commandRsp(命令响应)
  9. deviceEvent(设备事件)
  10. serviceInfoChanged(服务信息变化)
  11. ruleEvent(规则事件)
  12. deviceModelAdded(添加设备模型)
  13. deviceModelDeleted(删除设备模型)
  14. deviceDesiredPropertiesModifyStatusChanged(设备影子状态变更)

 第三步 实现消息下发

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/** * 下发数据到设备 * @create 2019年2月15日 下午4:33:57 * @since * @param httpsUtil * @param accessToken * @param deviceId * @param data * @throws Exception */ public static void postCommand(HttpsUtil httpsUtil,String accessToken,String deviceId,String data)throws Exception{ String urlPostAsynCmd = Constant.POST_ASYN_CMD; String appId = Constant.APPID; String callbackUrl = "http://xxxxx.com/service-api/dx/callbackUrl"; String serviceId = "BodyFatScaleInfo"; String method = "bodyfatscalecmd"; ObjectNode paras = JsonUtil.convertObject2ObjectNode("{"cmd_data_info":""+data+""}"); Map<String, Object> paramCommand = new HashMap<String, Object>(); paramCommand.put("serviceId", serviceId); paramCommand.put("method", method); paramCommand.put("paras", paras); Map<String, Object> paramPostAsynCmd = new HashMap<String, Object>(); paramPostAsynCmd.put("deviceId", deviceId); paramPostAsynCmd.put("command", paramCommand); paramPostAsynCmd.put("callbackUrl", callbackUrl); paramPostAsynCmd.put("expireTime", 86400); //有效期一年(一天) String jsonRequest = JsonUtil.jsonObj2Sting(paramPostAsynCmd); Map<String, String> header = new HashMap<String, String>(); header.put(Constant.HEADER_APP_KEY, appId); header.put(Constant.HEADER_APP_AUTH, "Bearer" + " " + accessToken); HttpResponse responsePostAsynCmd = httpsUtil.doPostJson(urlPostAsynCmd, header, jsonRequest); String responseBody = httpsUtil.getHttpResponseBody(responsePostAsynCmd); System.out.println("PostAsynCommand, response content:"); System.out.print(responsePostAsynCmd.getStatusLine()); System.out.println(responseBody); }

数据下发成功后如果设备在线会立即调用第三方平台命令通知接口返回命令下发状态。

就介绍到这里,如果有问题可以加左边 wx联系方式。

电信官方文档:

https://180.101.147.208:8093/assets/docCenter/helpcenter/helpPortal/Portal/helpcenter.html?manualName=UserGuide_CMCC&docSite=CMCC&page=Overview&lang=zh&fake_Url=zh-cn_topic_0118669796.html

最后

以上就是无聊缘分最近收集整理的关于电信物联网平台,java后台对接电信北向应用,命令下发到设备的全部内容,更多相关电信物联网平台内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(62)

评论列表共有 0 条评论

立即
投稿
返回
顶部