feat: 增加海康长连接采集温度并保存入库功能

hc-jna3
huangfeng 10 hours ago
parent 1429d53c02
commit 8d64ad8e57

@ -2,6 +2,7 @@ package com.hcsdk;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import com.xydl.cac.model.StaticVariable;
import lombok.extern.slf4j.Slf4j;
@ -10,6 +11,7 @@ public class HCModule {
public static HCNetSDK hCNetSDK = null;
static int userID;
static int handle;
static FExceptionCallBack_Imp fExceptionCallBack;
static String libPath;
@ -33,7 +35,6 @@ public class HCModule {
public static void init() {
libPath = StaticVariable.jarPath + "/hklib/";
log.info("海康库路径=" + libPath);
createSDKInstance();
HCNetSDK.BYTE_ARRAY ptrByteArray1 = new HCNetSDK.BYTE_ARRAY(256);
@ -43,19 +44,22 @@ public class HCModule {
String strPath2 = libPath + "libssl.so.1.1";
System.arraycopy(strPath1.getBytes(), 0, ptrByteArray1.byValue, 0, strPath1.length());
ptrByteArray1.write();
hCNetSDK.NET_DVR_SetSDKInitCfg(3, ptrByteArray1.getPointer());
hCNetSDK.NET_DVR_SetSDKInitCfg(HCNetSDK.NET_SDK_INIT_CFG_LIBEAY_PATH, ptrByteArray1.getPointer());
System.arraycopy(strPath2.getBytes(), 0, ptrByteArray2.byValue, 0, strPath2.length());
ptrByteArray2.write();
hCNetSDK.NET_DVR_SetSDKInitCfg(4, ptrByteArray2.getPointer());
hCNetSDK.NET_DVR_SetSDKInitCfg(HCNetSDK.NET_SDK_INIT_CFG_SSLEAY_PATH, ptrByteArray2.getPointer());
String strPathCom = libPath + "HCNetSDKCom/";
HCNetSDK.NET_DVR_LOCAL_SDK_PATH struComPath = new HCNetSDK.NET_DVR_LOCAL_SDK_PATH();
System.arraycopy(strPathCom.getBytes(), 0, struComPath.sPath, 0, strPathCom.length());
struComPath.write();
hCNetSDK.NET_DVR_SetSDKInitCfg(2, struComPath.getPointer());
hCNetSDK.NET_DVR_SetSDKInitCfg(HCNetSDK.NET_SDK_INIT_CFG_SDK_PATH, struComPath.getPointer());
boolean r = hCNetSDK.NET_DVR_Init();
if (!r) {
log.error("海康热成像初始化失败,错误码为: " + hCNetSDK.NET_DVR_GetLastError());
int err = hCNetSDK.NET_DVR_GetLastError();
String msg = hCNetSDK.NET_DVR_GetErrorMsg(new IntByReference(err));
log.error("海康热成像初始化失败,库路径=" + libPath
+ ", 错误码为: " + err + ", 错误信息:" + msg);
}
Pointer pUser = null;
@ -108,10 +112,38 @@ public class HCModule {
// 执行登录操作
userID = hCNetSDK.NET_DVR_Login_V40(m_strLoginInfo, m_strDeviceInfo);
if (userID == -1) {
log.error("海康热成像登入失败" + ip + ":" + port
+ ", 用户名:" + user + ", 密码:" + psw + ", 错误码为: " + hCNetSDK.NET_DVR_GetLastError());
int err = hCNetSDK.NET_DVR_GetLastError();
String msg = hCNetSDK.NET_DVR_GetErrorMsg(new IntByReference(err));
log.error("海康热成像登入失败" + ip + ":" + port + ", 用户名:" + user + ", 密码:" + psw
+ ", 错误码为: " + err + ", 错误信息:" + msg);
return false;
}
return true; // 返回登录结果
}
public static boolean startRemoteConfig(HCNetSDK.FRemoteConfigCallBack callBack,
Integer channel, Integer tmId) {
HCNetSDK.NET_DVR_REALTIME_THERMOMETRY_COND cond = new HCNetSDK.NET_DVR_REALTIME_THERMOMETRY_COND();
cond.read();
cond.dwSize = cond.size();
cond.byRuleID = tmId.byteValue();
cond.dwChan = channel;
cond.wInterval = 1;
cond.byMode = 1;
cond.write();
log.info("开始采集通道" + channel + ", 规则" + tmId);
handle = hCNetSDK.NET_DVR_StartRemoteConfig(userID, HCNetSDK.NET_DVR_GET_REALTIME_THERMOMETRY,
cond.getPointer(), cond.size(), callBack, null);
if (handle < 0) {
int err = hCNetSDK.NET_DVR_GetLastError();
String msg = hCNetSDK.NET_DVR_GetErrorMsg(new IntByReference(err));
log.error("startRemoteConfig失败 " + ", 错误码为: " + err + ", 错误信息:" + msg);
return false;
}
return true;
}
public static void stopRemoteConfig() {
hCNetSDK.NET_DVR_StopRemoteConfig(handle);
}
}

@ -0,0 +1,61 @@
package com.xydl.cac.thermal;
import com.hcsdk.HCNetSDK;
import com.sun.jna.Pointer;
import com.xydl.cac.entity.NSensor;
import com.xydl.cac.entity.ThermalConfig;
import com.xydl.cac.model.StaticVariable;
import com.xydl.cac.service.DataService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
@Slf4j
public class HCRemoteConfigCallBack implements HCNetSDK.FRemoteConfigCallBack {
public boolean done;
public NSensor item;
public String time;
public ThermalConfig config;
@Resource
DataService dataService;
@Override
public void invoke(int dwType, Pointer lpBuffer, int dwBufLen, Pointer pUserData) {
if (done) {
return;
}
HCNetSDK.NET_DVR_THERMOMETRY_UPLOAD data = new HCNetSDK.NET_DVR_THERMOMETRY_UPLOAD();
Pointer pointer = data.getPointer();
pointer.write(0, lpBuffer.getByteArray(0, data.size()), 0, data.size());
data.read();
float fAverageTemperature = data.struLinePolygonThermCfg.fAverageTemperature;
float fMinTemperature = data.struLinePolygonThermCfg.fMinTemperature;
float fMaxTemperature = data.struLinePolygonThermCfg.fMaxTemperature;
log.info("海康热成像采集到max=" + fMaxTemperature + ", min=" + fMinTemperature + ", aver=" + fAverageTemperature);
done = true;
if (StringUtils.isNotBlank(config.getMaxtemp())) {
String value = String.valueOf(fMaxTemperature);
dataService.insertData(item.getTableName(), item.getDevId(), time, config.getMaxtemp(), value);
// 更新最新数据缓存
StaticVariable.updateLastData(item.getDevId(), config.getMaxtemp(), value, time);
}
if (StringUtils.isNotBlank(config.getMintemp())) {
String value = String.valueOf(fMinTemperature);
dataService.insertData(item.getTableName(), item.getDevId(), time, config.getMintemp(), value);
// 更新最新数据缓存
StaticVariable.updateLastData(item.getDevId(), config.getMintemp(), value, time);
}
if (StringUtils.isNotBlank(config.getAvertemp())) {
String value = String.valueOf(fAverageTemperature);
dataService.insertData(item.getTableName(), item.getDevId(), time, config.getAvertemp(), value);
// 更新最新数据缓存
StaticVariable.updateLastData(item.getDevId(), config.getAvertemp(), value, time);
}
}
}

@ -3,11 +3,8 @@ package com.xydl.cac.thermal;
import com.hcsdk.HCModule;
import com.xydl.cac.entity.NSensor;
import com.xydl.cac.entity.ThermalConfig;
import com.xydl.cac.model.StaticVariable;
import com.xydl.cac.service.DataService;
import com.xydl.cac.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.PreDestroy;
@ -20,7 +17,7 @@ import java.util.List;
public class HCThermalService {
@Resource
DataService dataService;
HCRemoteConfigCallBack _callBack;
int shutdown = 0;
@ -43,33 +40,23 @@ public class HCThermalService {
} else {
return;
}
String time = DateUtil.format(new Date());
for (NSensor item : list) {
// NetSDKLib.NET_RADIOMETRYINFO data = ThermalCameraModule.queryItemTemper(0, 0, sensor.getTmId(), sensor.getTmType());
// if (data == null) {
// log.warn("海康热成像没有采集到数据, 序号" + item.getTmId() + "测量项目" + sensor.getTmType());
// } else {
// log.info("海康热成像采集到序号" + item.getTmId() + "测量项目" + sensor.getTmType() + "的max="
// + data.fTemperMax + ", min=" + data.fTemperMin + ", aver=" + data.fTemperAver);
// if (StringUtils.isNotBlank(config.getMaxtemp())) {
// String value = String.valueOf(data.fTemperMax);
// dataService.insertData(item.getTableName(), item.getDevId(), time, config.getMaxtemp(), value);
// // 更新最新数据缓存
// StaticVariable.updateLastData(item.getDevId(), config.getMaxtemp(), value, time);
// }
// if (StringUtils.isNotBlank(config.getMintemp())) {
// String value = String.valueOf(data.fTemperMin);
// dataService.insertData(item.getTableName(), item.getDevId(), time, config.getMintemp(), value);
// // 更新最新数据缓存
// StaticVariable.updateLastData(item.getDevId(), config.getMintemp(), value, time);
// }
// if (StringUtils.isNotBlank(config.getAvertemp())) {
// String value = String.valueOf(data.fTemperAver);
// dataService.insertData(item.getTableName(), item.getDevId(), time, config.getAvertemp(), value);
// // 更新最新数据缓存
// StaticVariable.updateLastData(item.getDevId(), config.getAvertemp(), value, time);
// }
// }
_callBack.done = false;
_callBack.item = item;
_callBack.config = config;
_callBack.time = DateUtil.format(new Date());
r = HCModule.startRemoteConfig(_callBack, 2, item.getTmId());
if (r) {
int sec = 0;
while (shutdown == 0 && sec < 20 && !_callBack.done) {
try {
Thread.sleep(1000);
sec++;
} catch (Exception ignore) {
}
}
HCModule.stopRemoteConfig();
}
}
HCModule.logout();
} catch (Exception ex) {

Loading…
Cancel
Save