Parcourir la source

add&update:修改PtzService为HKService;新增云台抓图接口。

miloma93@163.com il y a 3 mois
Parent
commit
c79e7b908e

+ 11 - 5
src/main/java/com/sw/patroleditor/controller/PTZController.java

@@ -4,7 +4,7 @@ import com.sw.patroleditor.common.ResultData;
 import com.sw.patroleditor.component.rosBridge.RobotDataCallBack;
 import com.sw.patroleditor.domain.model.PTZData;
 import com.sw.patroleditor.domain.model.Position3D;
-import com.sw.patroleditor.service.PTZCtrlService;
+import com.sw.patroleditor.service.HKService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import org.springframework.web.bind.annotation.GetMapping;
@@ -14,26 +14,32 @@ import org.springframework.web.bind.annotation.RestController;
 
 import javax.annotation.Resource;
 
-@Api(tags = {"云台控制"})
+@Api(tags = {"云台功能"})
 @RestController
 public class PTZController {
 
     @Resource
-    private PTZCtrlService ptzCtrlService;
+    private HKService hkService;
     @Resource
     private RobotDataCallBack robotDataCallBack;
 
     @ApiOperation(value = "云台转动/递进")
     @PutMapping("/ptz/move")
     public ResultData move(@RequestBody PTZData ptzData) {
-        return ptzCtrlService.continuousChanged(ptzData);
+        return hkService.continuousChanged(ptzData);
     }
 
 
     @ApiOperation(value = "3D定位")
     @PutMapping("/ptz/position3D")
     public ResultData position3D(@RequestBody Position3D position3D) {
-        return ptzCtrlService.position3DCtrl(position3D);
+        return hkService.position3DCtrl(position3D);
+    }
+
+    @ApiOperation(value = "可见光抓图")
+    @GetMapping("/ptz/picture")
+    public ResultData picture() {
+        return hkService.picture();
     }
 
     @ApiOperation(value = "机器人信息")

+ 3 - 1
src/main/java/com/sw/patroleditor/service/PTZCtrlService.java → src/main/java/com/sw/patroleditor/service/HKService.java

@@ -5,7 +5,7 @@ import com.sw.patroleditor.domain.model.FocusData;
 import com.sw.patroleditor.domain.model.PTZData;
 import com.sw.patroleditor.domain.model.Position3D;
 
-public interface PTZCtrlService {
+public interface HKService {
 
 
     ResultData continuousChanged(PTZData ptzData);
@@ -13,4 +13,6 @@ public interface PTZCtrlService {
     ResultData focusChanged(FocusData focusData);
 
     ResultData position3DCtrl(Position3D position);
+
+    ResultData picture();
 }

+ 33 - 8
src/main/java/com/sw/patroleditor/service/impl/PTZCtrlServiceImpl.java → src/main/java/com/sw/patroleditor/service/impl/HKServiceImpl.java

@@ -5,20 +5,22 @@ import com.sw.patroleditor.common.ResultData;
 import com.sw.patroleditor.domain.model.*;
 import com.sw.patroleditor.exception.BusinessException;
 import com.sw.patroleditor.exception.ErrorCode;
-import com.sw.patroleditor.service.PTZCtrlService;
+import com.sw.patroleditor.service.HKService;
 import com.sw.patroleditor.util.HkHttpUtils;
 import com.sw.patroleditor.util.XmlUtils;
 
 import org.springframework.stereotype.Service;
 
+import java.util.Base64;
+
 
 /**
- * 云台控制接口
+ * 海康接口
  *
  * @author machao
  */
 @Service
-public class PTZCtrlServiceImpl implements PTZCtrlService {
+public class HKServiceImpl implements HKService {
 
     HkHttpUtils httpUtils = new HkHttpUtils();
 
@@ -34,7 +36,7 @@ public class PTZCtrlServiceImpl implements PTZCtrlService {
         String uri = "/ISAPI/PTZCtrl/channels/" + bodyRequestModel.getChannel() + "/continuous";
         bodyRequestModel.setUri(uri);
         //发送指令
-        return sendCMD2Hik(ptzData, bodyRequestModel, PTZData.class);
+        return sendCtrlCMD2Hik(ptzData, bodyRequestModel, PTZData.class);
 
     }
 
@@ -48,7 +50,7 @@ public class PTZCtrlServiceImpl implements PTZCtrlService {
         BodyRequestModel bodyRequestModel = new BodyRequestModel();
         String uri = "/ISAPI/System/Video/inputs/channels/" + bodyRequestModel.getChannel() + "/focus";
         bodyRequestModel.setUri(uri);
-        return sendCMD2Hik(focusData, bodyRequestModel, FocusData.class);
+        return sendCtrlCMD2Hik(focusData, bodyRequestModel, FocusData.class);
 
     }
 
@@ -63,18 +65,41 @@ public class PTZCtrlServiceImpl implements PTZCtrlService {
         BodyRequestModel bodyRequestModel = new BodyRequestModel();
         String uri = "/ISAPI/PTZCtrl/channels/" + bodyRequestModel.getChannel() + "/position3D";
         bodyRequestModel.setUri(uri);
-        return sendCMD2Hik(position3D, bodyRequestModel, Position3D.class);
+        return sendCtrlCMD2Hik(position3D, bodyRequestModel, Position3D.class);
+    }
+
+    /**
+     * 可见光抓图
+     *
+     * @return
+     */
+    @Override
+    public ResultData picture() {
+        String resp = null;
+        BodyRequestModel bodyRequestModel = new BodyRequestModel();
+        String uri = "/ISAPI/Streaming/channels/" + bodyRequestModel.getChannel() + "/picture";
+        bodyRequestModel.setUri(uri);
+        //发送指令
+        try {
+            byte[] bytes = httpUtils.doPictureGet(bodyRequestModel);
+            //Base64编码
+            resp = Base64.getEncoder().encodeToString(bytes);
+            //System.out.println(resp);
+        } catch (Exception e) {
+            throw new BusinessException(ErrorCode.FAIL.getCode(), e.getMessage());
+        }
+        return ResultData.success(resp);
     }
 
 
     /**
-     * 利用摘要认证并按照ISAPI协议向HIK发送指令
+     * 利用摘要认证并按照ISAPI协议向HIK发送控制指令
      *
      * @param cmdData
      * @param bodyRequestModel
      * @return
      */
-    private ResultData sendCMD2Hik(Object cmdData, BodyRequestModel bodyRequestModel, Class<?>... classT) {
+    private ResultData sendCtrlCMD2Hik(Object cmdData, BodyRequestModel bodyRequestModel, Class<?>... classT) {
         try {
             String xml = XmlUtils.objectToXml(cmdData, classT);
             bodyRequestModel.setEntity(xml);

+ 24 - 0
src/main/java/com/sw/patroleditor/util/HkHttpUtils.java

@@ -40,6 +40,30 @@ public class HkHttpUtils {
     }
 
     /**
+     * 抓图请求
+     *
+     * @param config
+     * @return
+     * @throws Exception
+     */
+    public byte[] doPictureGet(BodyRequestModel config) throws Exception {
+        byte[] response = null;
+        CredentialsProvider credsProvider = new BasicCredentialsProvider();
+        AuthScope favourite_digest_realm = new AuthScope(config.getIp(), config.getPort());
+        UsernamePasswordCredentials usernamePasswordCredentials = new UsernamePasswordCredentials(config.getUsername(), config.getPassword());
+        credsProvider.setCredentials(favourite_digest_realm, usernamePasswordCredentials);
+        CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
+        HttpGet httpGet = new HttpGet(config.getAgreement() + "://" + config.getIp() + ":" + config.getPort() + config.getUri());
+
+        CloseableHttpResponse responseBody = httpclient.execute(httpGet);
+        HttpEntity responseEntity = responseBody.getEntity();
+        if (responseEntity != null) {
+            response = EntityUtils.toByteArray(responseEntity);
+        }
+        return response;
+    }
+
+    /**
      * delete请求
      *
      * @param config