package com.netsdk.demo.customize;
|
|
import com.netsdk.demo.util.CaseMenu;
|
import com.netsdk.lib.NetSDKLib;
|
import com.netsdk.lib.ToolKits;
|
import com.netsdk.lib.callback.impl.DefaultDisconnectCallback;
|
import com.netsdk.lib.callback.impl.DefaultHaveReconnectCallBack;
|
import com.netsdk.lib.enumeration.EM_EVENT_IVS_TYPE;
|
import com.netsdk.lib.structure.*;
|
import com.sun.jna.Memory;
|
import com.sun.jna.Pointer;
|
import java.io.File;
|
import java.io.UnsupportedEncodingException;
|
import java.text.SimpleDateFormat;
|
import java.util.Objects;
|
import static com.netsdk.lib.Utils.getOsPrefix;
|
|
|
/**
|
* @author 291189
|
* @version 1.0
|
* @description ERR231127162
|
* @date 2023/12/2 10:17
|
*/
|
public class HygrothermographDemo {
|
|
|
// SDk对象初始化
|
public static final NetSDKLib netsdk = NetSDKLib.NETSDK_INSTANCE;
|
public static final NetSDKLib configsdk = NetSDKLib.CONFIG_INSTANCE;
|
|
// 判断是否初始化
|
private static boolean bInit = false;
|
// 判断log是否打开
|
private static boolean bLogOpen = false;
|
|
// 设备信息
|
private NetSDKLib.NET_DEVICEINFO_Ex deviceInfo = new NetSDKLib.NET_DEVICEINFO_Ex();
|
// 登录句柄
|
private NetSDKLib.LLong m_hLoginHandle = new NetSDKLib.LLong(0);
|
|
|
// 回调函数需要是静态的,防止被系统回收
|
// 断线回调
|
private static NetSDKLib.fDisConnect disConnectCB = DefaultDisconnectCallback.getINSTANCE();
|
// 重连回调
|
private static NetSDKLib.fHaveReConnect haveReConnectCB = DefaultHaveReconnectCallBack.getINSTANCE();
|
|
private NetSDKLib.LLong hygrothermographHandle = new NetSDKLib.LLong(0);
|
|
private NetSDKLib.LLong attachHandle=new NetSDKLib.LLong(0);
|
|
// 编码格式
|
public static String encode;
|
|
static {
|
String osPrefix = getOsPrefix();
|
if (osPrefix.toLowerCase().startsWith("win32-amd64")) {
|
encode = "GBK";
|
} else if (osPrefix.toLowerCase().startsWith("linux-amd64")) {
|
encode = "UTF-8";
|
}
|
}
|
|
/**
|
* 获取当前时间
|
*/
|
public static String GetDate() {
|
SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
return simpleDate.format(new java.util.Date()).replaceAll("[^0-9]", "-");
|
}
|
|
/**
|
* 初始化SDK库
|
*/
|
public static boolean Init() {
|
bInit = netsdk.CLIENT_Init(disConnectCB, null);// 进程启动时,初始化一次
|
if (!bInit) {
|
System.out.println("Initialize SDK failed");
|
return false;
|
}
|
// 配置日志
|
HygrothermographDemo.enableLog();
|
|
// 设置断线重连回调接口, 此操作为可选操作,但建议用户进行设置
|
netsdk.CLIENT_SetAutoReconnect(haveReConnectCB, null);
|
|
// 设置登录超时时间和尝试次数,可选
|
// 登录请求响应超时时间设置为3S
|
int waitTime = 3000;
|
// 登录时尝试建立链接 1 次
|
int tryTimes = 1;
|
netsdk.CLIENT_SetConnectTime(waitTime, tryTimes);
|
// 设置更多网络参数, NET_PARAM 的nWaittime , nConnectTryNum 成员与 CLIENT_SetConnectTime
|
// 接口设置的登录设备超时时间和尝试次数意义相同,可选
|
NetSDKLib.NET_PARAM netParam = new NetSDKLib.NET_PARAM();
|
// 登录时尝试建立链接的超时时间
|
netParam.nConnectTime = 10000;
|
// 设置子连接的超时时间
|
netParam.nGetConnInfoTime = 3000;
|
netsdk.CLIENT_SetNetworkParam(netParam);
|
return true;
|
}
|
|
/**
|
* 打开 sdk log
|
*/
|
private static void enableLog() {
|
NetSDKLib.LOG_SET_PRINT_INFO setLog = new NetSDKLib.LOG_SET_PRINT_INFO();
|
File path = new File("sdklog/");
|
if (!path.exists())
|
path.mkdir();
|
|
// 这里的log保存地址依据实际情况自己调整
|
String logPath = path.getAbsoluteFile().getParent() + "\\sdklog\\" + "sdklog" + GetDate() + ".log";
|
setLog.nPrintStrategy = 0;
|
setLog.bSetFilePath = 1;
|
System.arraycopy(logPath.getBytes(), 0, setLog.szLogFilePath, 0, logPath.getBytes().length);
|
System.out.println(logPath);
|
setLog.bSetPrintStrategy = 1;
|
bLogOpen = netsdk.CLIENT_LogOpen(setLog);
|
if (!bLogOpen)
|
System.err.println("Failed to open NetSDK log");
|
}
|
|
/**
|
* 高安全登录
|
*/
|
public void loginWithHighLevel() {
|
// 输入结构体参数
|
NetSDKLib.NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY pstlnParam = new NetSDKLib.NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY() {
|
{
|
szIP = m_strIpAddr.getBytes();
|
nPort = m_nPort;
|
szUserName = m_strUser.getBytes();
|
szPassword = m_strPassword.getBytes();
|
}
|
};
|
// 输出结构体参数
|
NetSDKLib.NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY pstOutParam = new NetSDKLib.NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY();
|
|
// 写入sdk
|
m_hLoginHandle = netsdk.CLIENT_LoginWithHighLevelSecurity(pstlnParam, pstOutParam);
|
if (m_hLoginHandle.longValue() == 0) {
|
System.err.printf("Login Device[%s] Port[%d]Failed. %s\n", m_strIpAddr, m_nPort,
|
ToolKits.getLastError());
|
} else {
|
deviceInfo = pstOutParam.stuDeviceInfo; // 获取设备信息
|
System.out.println("Login Success");
|
System.out.println("Device Address:" + m_strIpAddr);
|
System.out.println("设备包含:" + deviceInfo.byChanNum + "个通道");
|
}
|
}
|
|
/**
|
* 退出
|
*/
|
public void logOut() {
|
if (m_hLoginHandle.longValue() != 0) {
|
netsdk.CLIENT_Logout(m_hLoginHandle);
|
System.out.println("LogOut Success");
|
}
|
}
|
|
/**
|
* 清理sdk环境并退出
|
*/
|
public static void cleanAndExit() {
|
if (bLogOpen) {
|
netsdk.CLIENT_LogClose(); // 关闭sdk日志打印
|
}
|
netsdk.CLIENT_Cleanup(); // 进程关闭时,调用一次
|
System.exit(0);
|
}
|
|
|
public static Pointer GetStringToPointer(String src, String charset) {
|
Pointer pointer = null;
|
try {
|
byte[] b = src.getBytes(charset);
|
|
pointer = new Memory(b.length + 1);
|
pointer.clear(b.length + 1);
|
|
pointer.write(0, b, 0, b.length);
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
}
|
return pointer;
|
}
|
|
|
|
public void attachHygrothermograph(){
|
|
NET_IN_ATTACH_HYGROTHERMOGRAPH input=new NET_IN_ATTACH_HYGROTHERMOGRAPH();
|
|
input.cbNotifyHygrothermograph = NotifyHygrothermographCB.getInstance();
|
|
input.nFilterCount=1;
|
|
|
NET_ATTACH_HYGROTHERMOGRAPH_FILTER_INFO[] stuFilter
|
= input.stuFilter;
|
|
stuFilter[0].nChannel=0;
|
|
Pointer pointerInput=new Memory(input.size());
|
|
pointerInput.clear(input.size());
|
|
ToolKits.SetStructDataToPointer(input,pointerInput,0);
|
|
NET_OUT_ATTACH_HYGROTHERMOGRAPH outPut=new NET_OUT_ATTACH_HYGROTHERMOGRAPH();
|
|
Pointer pointerOutPut=new Memory(outPut.size());
|
|
pointerOutPut.clear(outPut.size());
|
|
ToolKits.SetStructDataToPointer(outPut,pointerOutPut,0);
|
|
|
|
hygrothermographHandle
|
= netsdk.CLIENT_AttachHygrothermograph(m_hLoginHandle, pointerInput, pointerOutPut, 3000);
|
|
if (hygrothermographHandle.longValue() == 0) {
|
System.err.printf("CLIENT_AttachHygrothermograph Failed. %s\n",
|
ToolKits.getLastError());
|
} else {
|
System.out.println("CLIENT_AttachHygrothermograph success ");
|
}
|
}
|
|
/**
|
* 订阅温湿度实时检测数据回调函数原型
|
*/
|
private static class NotifyHygrothermographCB implements NetSDKLib.fNotifyHygrothermograph {
|
|
private static NotifyHygrothermographCB instance;
|
|
private NotifyHygrothermographCB() {
|
|
}
|
|
public static NotifyHygrothermographCB getInstance() {
|
if (instance == null) {
|
synchronized (NotifyHygrothermographCB.class) {
|
if (instance == null) {
|
instance = new NotifyHygrothermographCB();
|
}
|
}
|
}
|
return instance;
|
}
|
|
|
@Override
|
public void invoke(NetSDKLib.LLong lHygrothermographHandle, Pointer pstuHygrothermographInfo, Pointer dwUser) {
|
System.out.println("订阅温湿度实时检测数据回调函数原型");
|
NET_HYGROTHERMOGRAPH_INFO info=new NET_HYGROTHERMOGRAPH_INFO();
|
|
|
ToolKits.GetPointerDataToStruct(pstuHygrothermographInfo,0,info);
|
|
System.out.println("fTemperature:"+info.fTemperature);
|
|
System.out.println("fHumidity:"+info.fHumidity);
|
|
System.out.println("nChannel:"+info.nChannel);
|
|
|
}
|
}
|
|
|
public void detachHygrothermograph(){
|
|
if (this.hygrothermographHandle.longValue() != 0) {
|
|
boolean b
|
= netsdk.CLIENT_DetachHygrothermograph(hygrothermographHandle);
|
|
if (!b ) {
|
System.err.printf("CLIENT_DetachHygrothermograph Failed. %s\n",
|
ToolKits.getLastError());
|
} else {
|
System.out.println("CLIENT_DetachHygrothermograph success ");
|
}
|
|
}
|
|
}
|
|
|
/**
|
* 订阅智能任务
|
*/
|
public NetSDKLib.LLong AttachEventRealLoadPic() {
|
// 先退订,设备不会对重复订阅作校验,重复订阅后会有重复的事件返回
|
if(attachHandle.longValue()!=0){
|
this.DetachEventRealLoadPic();
|
}
|
|
// 需要图片
|
int bNeedPicture = 1;
|
int channel = 0;
|
attachHandle = netsdk.CLIENT_RealLoadPictureEx(m_hLoginHandle, channel, EM_EVENT_IVS_TYPE.EVENT_IVS_ALL.getType(), bNeedPicture, AnalyzerDataCB.getInstance(), null, null);
|
|
if (attachHandle.longValue() != 0) {
|
System.out.printf("Chn[%d] CLIENT_RealLoadPictureEx Success\n", channel);
|
} else {
|
System.out.printf("Ch[%d] CLIENT_RealLoadPictureEx Failed!LastError = %s\n", channel,
|
ToolKits.getErrorCode());
|
}
|
|
return attachHandle;
|
}
|
|
/**
|
* 报警事件(智能)回调
|
*/
|
private static class AnalyzerDataCB implements NetSDKLib.fAnalyzerDataCallBack {
|
private final File picturePath;
|
private static AnalyzerDataCB instance;
|
|
private AnalyzerDataCB() {
|
picturePath = new File("./AnalyzerPicture/");
|
if (!picturePath.exists()) {
|
picturePath.mkdirs();
|
}
|
}
|
|
public static AnalyzerDataCB getInstance() {
|
if (instance == null) {
|
synchronized (AnalyzerDataCB.class) {
|
if (instance == null) {
|
instance = new AnalyzerDataCB();
|
}
|
}
|
}
|
return instance;
|
}
|
|
@Override
|
public int invoke(NetSDKLib.LLong lAnalyzerHandle, int dwAlarmType, Pointer pAlarmInfo, Pointer pBuffer, int dwBufSize,
|
Pointer dwUser, int nSequence, Pointer reserved) {
|
if (lAnalyzerHandle == null || lAnalyzerHandle.longValue() == 0) {
|
return -1;
|
}
|
|
switch (Objects.requireNonNull(EM_EVENT_IVS_TYPE.getEventType(dwAlarmType))) {
|
|
case EVENT_IVS_TEMPERATURE_ALARM: // 对应 NET_DEV_EVENT_TEMPERATURE_ALARM_INFO
|
{
|
System.out.println("智能----------------------------温度报警事件");
|
NET_DEV_EVENT_TEMPERATURE_ALARM_INFO msg = new NET_DEV_EVENT_TEMPERATURE_ALARM_INFO();
|
ToolKits.GetPointerData(pAlarmInfo, msg);
|
|
try {
|
int bRealUTC = msg.bRealUTC;
|
if(bRealUTC==1){
|
System.out.println( "温度报警事件 时间(UTC):" + msg.stuRealUTC.toStringTime());
|
}else {
|
System.out.println( "温度报警事件 时间(UTC):" + msg.stuUTC.toStringTime());
|
}
|
|
System.out.println( " 通道号:" + msg.nChannelID
|
+ " 事件名称:" + new String( msg.szName,encode)+",当前温度:"+msg.fCurrent+" 检测正常范围:"+msg.nRange[0]+"---"+msg.nRange[1]);
|
System.out.println("GPS信息:");
|
|
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
}
|
System.out.println("GPS信息:");
|
System.out.println("stuParkingInfo.longitude = " + msg.stuGPS.longitude);
|
System.out.println("stuParkingInfo.latidude = " + msg.stuGPS.latidude);
|
System.out.println("stuParkingInfo.height = " + msg.stuGPS.height);
|
System.out.println("stuParkingInfo.angle = " + msg.stuGPS.angle);
|
break;
|
}
|
|
case EVENT_IVS_HUMIDITY_ALARM: // 对应 NET_DEV_EVENT_HUMIDITY_ALARM_INFO
|
{
|
System.out.println("智能----------------------------湿度报警事件");
|
|
NET_DEV_EVENT_HUMIDITY_ALARM_INFO msg = new NET_DEV_EVENT_HUMIDITY_ALARM_INFO();
|
|
ToolKits.GetPointerData(pAlarmInfo, msg);
|
|
int bRealUTC = msg.bRealUTC;
|
if(bRealUTC==1){
|
System.out.println( "湿度报警事件 时间(UTC):" + msg.stuRealUTC.toStringTime());
|
}else {
|
System.out.println( "湿度报警事件 时间(UTC):" + msg.stuUTC.toStringTime());
|
}
|
|
|
System.out.println( " 通道号:" + msg.nChannelID
|
+ " 当前湿度值:" + msg.fCurrentValue+" 报警阈值:" + msg.fLimitValue+" 检测正常范围:"+msg.nRange[0]+"---"+msg.nRange[1]);
|
|
System.out.println("GPS信息:");
|
System.out.println("stuParkingInfo.longitude = " + msg.stuGPS.longitude);
|
System.out.println("stuParkingInfo.latidude = " + msg.stuGPS.latidude);
|
System.out.println("stuParkingInfo.height = " + msg.stuGPS.height);
|
System.out.println("stuParkingInfo.angle = " + msg.stuGPS.angle);
|
|
break;
|
}
|
|
default:
|
System.out.println("其他事件--------------------"+ dwAlarmType);
|
break;
|
}
|
return 0;
|
}
|
}
|
|
|
/**
|
* 停止侦听智能事件
|
*/
|
public void DetachEventRealLoadPic() {
|
if (this.attachHandle.longValue() != 0) {
|
netsdk.CLIENT_StopLoadPic(this.attachHandle);
|
}
|
}
|
|
|
|
|
public void RunTest()
|
{
|
System.out.println("Run Test");
|
CaseMenu menu = new CaseMenu();
|
menu.addItem(new CaseMenu.Item(this, "订阅温湿度实时检测数据", "attachHygrothermograph"));
|
menu.addItem(new CaseMenu.Item(this, "退订温湿度实时检测数据", "detachHygrothermograph"));
|
menu.addItem((new CaseMenu.Item(this , "智能订阅" , "AttachEventRealLoadPic")));
|
menu.addItem((new CaseMenu.Item(this , "取消智能订阅" , "DetachEventRealLoadPic")));
|
|
menu.run();
|
}
|
// 配置登陆地址,端口,用户名,密码
|
private String m_strIpAddr = "172.27.1.88";
|
private int m_nPort = 37777;
|
private String m_strUser = "admin";
|
private String m_strPassword = "admin123";
|
public static void main(String[] args) {
|
HygrothermographDemo demo=new HygrothermographDemo();
|
demo.InitTest();
|
demo.RunTest();
|
demo.EndTest();
|
|
}
|
|
|
/**
|
* 初始化测试
|
*/
|
public void InitTest() {
|
HygrothermographDemo.Init();
|
|
this.loginWithHighLevel();
|
}
|
|
/**
|
* 结束测试
|
*/
|
public void EndTest() {
|
System.out.println("End Test");
|
this.logOut(); // 登出设备
|
System.out.println("See You...");
|
BackLightDemo.cleanAndExit(); // 清理资源并退出
|
}
|
}
|