GHHKControlService 接口
package org.gh.ghhk.service;
public interface GHHKControlService {
boolean monitorControl(String payload);
}
GHHKControlServiceImpl 实现类
package org.gh.ghhk.service.impl;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sun.jna.NativeLong;
import lombok.RequiredArgsConstructor;
import org.gh.ghhk.net.HCNetSDK;
import org.gh.ghhk.service.GHHKControlService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
@RequiredArgsConstructor
@Service
public class GHHKControlServiceImpl implements GHHKControlService {
private static final String MONITORIP = "monitorIp";
private static final String MONITORCONTROLPORT = "monitorControlPort";
private static final String MONITORUSERNAME = "monitorUserName";
private static final String MONITORPASSWORD = "monitorPassWord";
private static final String DIR = "dir";
private static final String SPEED = "speed";
private static final String DATA = "data";
private static final String ACTIONS = "actions";
private static final Logger log = LoggerFactory.getLogger(GHHKControlServiceImpl.class);
@Override
public boolean monitorControl(String payload) {
try {
JSONObject jsonObject = JSONObject.parseObject(payload);
JSONObject dastaJsonObject = jsonObject.getJSONObject(DATA);
JSONArray actionsJsonArray = dastaJsonObject.getJSONArray(ACTIONS);
for (int i = 0; i < actionsJsonArray.size(); i++) {
JSONObject actionJsonObject = actionsJsonArray.getJSONObject(i);
String monitorIp = actionJsonObject.getString(MONITORIP);
Short monitorControlPort = actionJsonObject.getShort(MONITORCONTROLPORT);
String monitorUserName = actionJsonObject.getString(MONITORUSERNAME);
String monitorPassword = actionJsonObject.getString(MONITORPASSWORD);
int dir = actionJsonObject.getInteger(DIR);
int speed = actionJsonObject.getInteger(SPEED);
boolean result = this.processMonitorControl(monitorIp, monitorControlPort, monitorUserName, monitorPassword, dir, speed);
if (!result) {
// 如果某个操作失败,可以选择返回false或者继续执行
return false;
}
}
return true; // 如果所有操作都成功,返回true
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public boolean processMonitorControl(String monitorIp, Short monitorControlPort, String monitorUserName, String monitorPassword, Integer dir, int speed) throws InterruptedException {
int lUserID;// 用户句柄
int lPreviewHandle;// 预览句柄
HCNetSDK.NET_DVR_CLIENTINFO m_strClientInfo;// 用户参数
HCNetSDK hCNetSDK = HCNetSDK.INSTANCE;
log.info("加载完成");
boolean bool = hCNetSDK.NET_DVR_Init();
log.info("初始化结果:{}", bool);
HCNetSDK.NET_DVR_DEVICEINFO_V30 m_strDeviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V30();
lUserID = hCNetSDK.NET_DVR_Login_V30(
monitorIp,
monitorControlPort,
monitorUserName,
monitorPassword,
m_strDeviceInfo
);
log.info("用户登录句柄:{}", lUserID);
m_strClientInfo = new HCNetSDK.NET_DVR_CLIENTINFO();
m_strClientInfo.lChannel = new NativeLong(1);
lPreviewHandle = hCNetSDK.NET_DVR_RealPlay_V30(lUserID,
m_strClientInfo, null, null, true);
log.info("预览句柄:{}", lPreviewHandle);
boolean isOK = hCNetSDK.NET_DVR_PTZControl(lPreviewHandle, dir, speed);
log.info("控制结果:{}", isOK);
Thread.sleep(1000);
isOK = hCNetSDK.NET_DVR_PTZControl(lPreviewHandle, dir, 1);
log.info("控制结果:{}", isOK);
Thread.sleep(1000);
isOK = hCNetSDK.NET_DVR_Logout(lUserID);
log.info("控制结果:{}", isOK);
isOK = hCNetSDK.NET_DVR_Cleanup();
log.info("控制结果:{}", isOK);
return true;
}
}
订阅mqtt消息
@Override
public void messageArrived(String topic, MqttMessage message) {
String payload = new String(message.getPayload());
ghhkControlService.monitorControl(payload);
log.info("收到消息: {}", payload);
}
这里注意 控制摄像头一定要释放 登出 要不然摄像头控制几次不受控制了
isOK = hCNetSDK.NET_DVR_PTZControl(lPreviewHandle, dir, 1);
log.info("控制结果:{}", isOK);
Thread.sleep(1000);
isOK = hCNetSDK.NET_DVR_Logout(lUserID);
log.info("控制结果:{}", isOK);
isOK = hCNetSDK.NET_DVR_Cleanup();
log.info("控制结果:{}", isOK);