开发前需要去官网注册,获取开发需要的appId 和 appSecret
乐橙官网
乐橙开发文档
话不多说,直接上代码。按照这个流程走的话,半天就能完全接入!
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
/** * 我们把这些固定的参数单独配置,后续有变动改这里就可以了 */ public class MonitorConstants {
/** * 监控 redis key */ public static final String MONITOR_CODE_KEY = "monitor_code_key";
/** * appId */ public static final String APP_ID = "xxx";
/** * appSecret */ public static final String APP_SECRET = "xxx"; } |
|
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 40 41 42 43 44 45 46 47 48 49 |
/** * 这里给的案例是一个设备组绑定一个监控。大家根据自己的业务情况去修改 */ @Data public class DeviceGroup implements Serializable {
private static final long serialVersionUID = xxx;
/** * 设备组id */ private Long id;
/** * 用户id */ private Long userId;
/** * 设备组名称 */ private String groupName;
/** * 创建时间 */ @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") private Date createTime;
/** * 监控编码 */ private String monitorCode;
/** * 监控链接 */ private String monitorUrl;
/** * 0-上,1-下,2-左,3-右 */ private String operation;
/** * 设备验证码或密码 */ private String code; } |
|
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 |
/** * 监控绑定 * * @param deviceGroup * @return */ @PutMapping("/monitorBind") public AjaxResult monitorBind(@RequestBody DeviceGroup deviceGroup) {
//判断设备组是否已经绑定监控 int countGroup = monitorService.getGroupMonitorCount(deviceGroup.getId()); if (countGroup > 0) { return error("设备组只能绑定一个监控!"); }
//根据监控编码判断监控是否被绑定 int count = monitorService.getMonitorIsBinding(deviceGroup.getMonitorCode()); if (count > 0) { return error("监控已被绑定,请检查!"); }
return toAjax(monitorService.monitorBind(deviceGroup)); }
/** * 监控操作 * * @param deviceGroup * @return */ @PostMapping("/controlMovePTZ") public AjaxResult controlMovePTZ(@RequestBody DeviceGroup deviceGroup) { monitorService.controlMovePTZ(deviceGroup); return success(); } |
|
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 |
/** * 监控绑定 * * @param deviceGroup * @return */ @Transactional(rollbackFor = Exception.class) @Override public int monitorBind(DeviceGroup deviceGroup) {
//获取token String token = getToken();
//绑定设备 bindDevice(deviceGroup, token);
//创建设备源直播地址 String monitorUrl = bindDeviceLive(deviceGroup, token); if (monitorUrl == null) { throw new RuntimeException("创建设备源直播地址失败!"); }
deviceGroup.setMonitorUrl(monitorUrl);
return monitorMapper.monitorBind(deviceGroup); } |
|
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 |
/** * 获取token * * @return */ private String getToken() {
try { //从redis获取token Object cacheObject = redisCache.getCacheObject(MonitorConstants.MONITOR_CODE_KEY); if (cacheObject != null) { return cacheObject.toString(); } Map<String, Object> requestParams = paramsInit(Collections.emptyMap()); String requestData = JSONObject.toJSONString(requestParams);
String result = HttpClientUtil.getToken("https://openapi.lechange.cn/openapi/accessToken", requestData); JSONObject jsonObject = JSONObject.parseObject(result); if ("0".equals(JSONObject.from(jsonObject.get("result")).get("code"))) { String token = JSONObject.from(JSONObject.from(jsonObject.get("result")).get("data")).get("accessToken").toString(); redisCache.setCacheObject(MonitorConstants.MONITOR_CODE_KEY, token, 2, TimeUnit.DAYS); return token; } } catch (Exception e) { e.printStackTrace(); }
return null; } |
|
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 |
/** * 绑定设备 * * @param deviceGroup * @param token */ private void bindDevice(DeviceGroup deviceGroup, String token) {
try {
Map<String, Object> paramsMap = new HashMap<>(); paramsMap.put("token", token); paramsMap.put("deviceId", deviceGroup.getMonitorCode()); paramsMap.put("code", deviceGroup.getCode());
Map<String, Object> requestParams = paramsInit(paramsMap); String requestData = JSONObject.toJSONString(requestParams);
String result = HttpClientUtil.getToken("https://openapi.lechange.cn/openapi/bindDevice", requestData); JSONObject jsonObject = JSONObject.parseObject(result); if (!"0".equals(JSONObject.from(jsonObject.get("result")).get("code"))) { throw new RuntimeException(); } } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("绑定设备失败!"); } } |
|
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 |
/** * 创建设备源直播地址 * * @param deviceGroup * @param token * @return */ private String bindDeviceLive(DeviceGroup deviceGroup, String token) {
try {
Map<String, Object> paramsMap = new HashMap<>(); paramsMap.put("token", token); paramsMap.put("deviceId", deviceGroup.getMonitorCode()); paramsMap.put("channelId", "0"); paramsMap.put("streamId", 0);
Map<String, Object> requestParams = paramsInit(paramsMap); String requestData = JSONObject.toJSONString(requestParams);
String result = HttpClientUtil.getToken("https://openapi.lechange.cn/openapi/bindDeviceLive", requestData); JSONObject jsonObject = JSONObject.parseObject(result); if ("0".equals(JSONObject.from(jsonObject.get("result")).get("code"))) { JSONObject jo = JSONObject.from(JSONObject.from(jsonObject.get("result")).get("data")); JSONArray streams = JSONArray.parseArray(jo.get("streams").toString()); return JSONObject.from(streams.get(2)).get("hls").toString(); } } catch (Exception e) { e.printStackTrace(); }
return null; } |
|
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 |
/** * 封装请求参数 * * @param paramsMap * @return */ protected static Map<String, Object> paramsInit(Map<String, Object> paramsMap) { long time = System.currentTimeMillis() / 1000; String nonce = UUID.randomUUID().toString(); String id = UUID.randomUUID().toString();
StringBuilder paramString = new StringBuilder(); paramString.append("time:").append(time).append(","); paramString.append("nonce:").append(nonce).append(","); paramString.append("appSecret:").append(MonitorConstants.APP_SECRET);
String sign = ""; // 计算MD5得值 try { sign = DigestUtils.md5Hex(paramString.toString().trim().getBytes("UTF-8")); } catch (Exception e) { e.printStackTrace(); }
Map<String, Object> systemMap = new HashMap<String, Object>(); systemMap.put("ver", "1.0"); systemMap.put("sign", sign); systemMap.put("appId", MonitorConstants.APP_ID); systemMap.put("nonce", nonce); systemMap.put("time", time);
Map<String, Object> map = new HashMap<String, Object>(); map.put("system", systemMap); map.put("params", paramsMap); map.put("id", id); return map; } |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
/** * 监控操作 * * @param deviceGroup */ @Override public void controlMovePTZ(DeviceGroup deviceGroup) {
//获取token String token = getToken();
//监控操作 control(deviceGroup, token); } |
同上
|
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 |
/** * 云台移动控制 * * @param deviceGroup * @param token * @return */ private void control(DeviceGroup deviceGroup, String token) {
try {
Map<String, Object> paramsMap = new HashMap<>(); paramsMap.put("token", token); paramsMap.put("deviceId", deviceGroup.getMonitorCode()); paramsMap.put("channelId", "0"); paramsMap.put("operation", deviceGroup.getOperation()); paramsMap.put("duration", "1000");
Map<String, Object> requestParams = paramsInit(paramsMap); String requestData = JSONObject.toJSONString(requestParams);
String result = HttpClientUtil.getToken("https://openapi.lechange.cn/openapi/controlMovePTZ", requestData); JSONObject jsonObject = JSONObject.parseObject(result); if (!"0".equals(JSONObject.from(jsonObject.get("result")).get("code"))) { throw new RuntimeException(); } } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("监控移动控制失败!"); } } |
1.这里是把生成的直播地址存到数据库,只需要生成一次。后续除非是设备解绑,不然这个链接是长期有效的。
2.设备控制是给的默认1秒,每次上下左右移动的持续时长可以自定义。
3.这里只是给出代码案例,彦祖亦菲们根据自己的实际业务场景来修改。