package com.shxy.xyhkcamera.service.impl; import com.alibaba.druid.sql.visitor.functions.If; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.shxy.xyhkcamera.entity.DataEaifHEntity; import com.shxy.xyhkcamera.entity.ModevEntity; import com.shxy.xyhkcamera.hk.HCNetSDK; import com.shxy.xyhkcamera.hk.HKConfigEnum; import com.shxy.xyhkcamera.hk.HcNetSdkUtil; import com.shxy.xyhkcamera.interfacesss.Testint; import com.shxy.xyhkcamera.interfacesss.Testintimpl; import com.shxy.xyhkcamera.mapper.DataEaifHMapper; import com.shxy.xyhkcamera.service.AsyncService; import com.shxy.xyhkcamera.service.DataEaifHService; import com.shxy.xyhkcamera.service.ModevService; import com.shxy.xyhkcamera.service.TempMeasureService; import com.shxy.xyhkcamera.utils.CommonUtil; import com.sun.jna.Pointer; import com.sun.jna.ptr.IntByReference; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import java.math.BigDecimal; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.time.LocalDateTime; import java.util.Date; import java.util.List; @Service @Slf4j public class TempMeasureServiceImpl implements TempMeasureService { @Autowired ModevService modevService; @Autowired DataEaifHService dataEaifHService; @Autowired DataEaifHMapper dataEaifHMapper; @Autowired HcNetSdkUtil hcNetSdkUtil; @Autowired CommonUtil commonUtil; @Autowired AsyncService asyncService; // @Autowired // Testint testint; /** * 海康用户名 */ @Value("${hkconfig.user}") private String user; /** * 海康密码 */ @Value("${hkconfig.password}") private String password; private int handle = -1; private int test = 0; private Connection conn; @Override public void measure() { hcNetSdkUtil.init(); HCNetSDK instance = hcNetSdkUtil.createSDKInstance(); List list = modevService.list(Wrappers.lambdaQuery(ModevEntity.class).eq(ModevEntity::getModevtid, 9)); if (CollectionUtils.isNotEmpty(list)) { // for (int i = 0; i < list.size(); i++) { for (int i = 0; i < 1; i++) { // List list1 = dataEaifHService.list(Wrappers.lambdaQuery(DataEaifHEntity.class).eq(DataEaifHEntity::getDataId, 12891)); // DataEaifHEntity entitys = new DataEaifHEntity(); // entitys.setEqmid(384); // entitys.setMaxtemp((double) 0); // entitys.setAreaid(1); // entitys.setCapturetime(LocalDateTime.now()); // entitys.setMintemp((double) 0); // entitys.setAvgtemp((double) 0); // entitys.setType((byte) HKConfigEnum.BYRULECALIBTYPE.POINT.getMysqlvalue()); // entitys.setMid(Double.valueOf(0)); // dataEaifHService.save(entitys); // ModevEntity entitys = new ModevEntity(); // entitys.setModevtid(1919); // entitys.setZsbid(1); // entitys.setIedid(1); // entitys.setAddress(12112); // modevService.save(entitys); ModevEntity modevEntity = list.get(i); Integer id = modevEntity.getId(); String ip = modevEntity.getIp(); String port = modevEntity.getPort(); // Short aShort = Short.valueOf(port); // int admin = hcNetSdkUtil.login_V40(ip, aShort, user, password); int admin = hcNetSdkUtil.login_V40("192.168.1.64", (short) 8000, user, password); if (admin != -1) { log.info("登录成功 " + "IP:" + ip + " " + port); HCNetSDK.NET_DVR_REALTIME_THERMOMETRY_COND cond = new HCNetSDK.NET_DVR_REALTIME_THERMOMETRY_COND(); cond.read(); cond.dwSize = cond.size(); cond.byRuleID = 1; cond.dwChan = 2; cond.wInterval = 0; cond.write(); HCNetSDK.FRemoteConfigCallBack cbStateCallBack = new HCNetSDK.FRemoteConfigCallBack() { @Override public void invoke(int dwType, Pointer lpBuffer, int dwBufLen, Pointer pUserData) { // log.info("111111111" + Integer.toHexString(dwType)); // new Thread(new Runnable() { // @Override // public void run() { // log.info("3333333" + Integer.toHexString(dwType)); // DataEaifHEntity entitys = new DataEaifHEntity(); // entitys.setEqmid(384); // entitys.setMaxtemp((double) 0); // entitys.setAreaid(1); // entitys.setCapturetime(LocalDateTime.now()); // entitys.setMintemp((double) 0); // entitys.setAvgtemp((double) 0); // entitys.setType((byte) HKConfigEnum.BYRULECALIBTYPE.POINT.getMysqlvalue()); // entitys.setMid(Double.valueOf(0)); // dataEaifHService.save(entitys); // log.info("222222222222222" + Integer.toHexString(dwType)); // } // }).start(); test++; log.info("长连接返回: dwType:" + Integer.toHexString(dwType)); if (dwType == HKConfigEnum.NET_SDK_CALLBACK_TYPE.NET_SDK_CALLBACK_TYPE_DATA.getValue()) { HCNetSDK.NET_DVR_THERMOMETRY_UPLOAD data = new HCNetSDK.NET_DVR_THERMOMETRY_UPLOAD(); data.write(); Pointer pointer = data.getPointer(); pointer.write(0, lpBuffer.getByteArray(0, data.size()), 0, data.size()); data.read(); log.info("返回的数据:" + data.toString()); log.info("开始数据整理"); byte byRuleCalibType = data.byRuleCalibType; byte byThermometryUnit = data.byThermometryUnit; DataEaifHEntity entity = new DataEaifHEntity(); entity.setEqmid(id); entity.setAreaid(1); if (byThermometryUnit == HKConfigEnum.BYTHERMOMETRYUNIT.SSD.getValue()) { entity.setUnit((byte) HKConfigEnum.BYTHERMOMETRYUNIT.SSD.getMysqlvalue()); } else if (byThermometryUnit == HKConfigEnum.BYTHERMOMETRYUNIT.HSD.getValue()) { entity.setUnit((byte) HKConfigEnum.BYTHERMOMETRYUNIT.HSD.getMysqlvalue()); } else if (byThermometryUnit == HKConfigEnum.BYTHERMOMETRYUNIT.KEW.getValue()) { entity.setUnit((byte) HKConfigEnum.BYTHERMOMETRYUNIT.KEW.getMysqlvalue()); } log.info("数据整理1"); //数据库中1->点2->线3->框 if (byRuleCalibType == HKConfigEnum.BYRULECALIBTYPE.POINT.getValue()) { float fTemperature = data.struPointThermCfg.fTemperature; double maxtemp = new BigDecimal(fTemperature).setScale(4, BigDecimal.ROUND_DOWN).doubleValue(); entity.setMaxtemp(maxtemp); entity.setMintemp(maxtemp); entity.setAvgtemp(maxtemp); entity.setType((byte) HKConfigEnum.BYRULECALIBTYPE.POINT.getMysqlvalue()); entity.setMid(Double.valueOf(0)); } else if (byRuleCalibType == HKConfigEnum.BYRULECALIBTYPE.FRAME.getValue() || byRuleCalibType == HKConfigEnum.BYRULECALIBTYPE.LINE.getValue()) { HCNetSDK.NET_DVR_LINEPOLYGON_THERM_CFG struLinePolygonThermCfg = data.struLinePolygonThermCfg; float fAverageTemperature = struLinePolygonThermCfg.fAverageTemperature;//平均温度 float fMinTemperature = struLinePolygonThermCfg.fMinTemperature; float fMaxTemperature = struLinePolygonThermCfg.fMaxTemperature; float fTemperatureDiff = struLinePolygonThermCfg.fTemperatureDiff; entity.setMaxtemp(new BigDecimal(fMaxTemperature).setScale(4, BigDecimal.ROUND_DOWN).doubleValue()); entity.setMintemp(new BigDecimal(fMinTemperature).setScale(4, BigDecimal.ROUND_DOWN).doubleValue()); entity.setAvgtemp(new BigDecimal(fAverageTemperature).setScale(4, BigDecimal.ROUND_DOWN).doubleValue()); entity.setMid(new BigDecimal(fTemperatureDiff).setScale(4, BigDecimal.ROUND_DOWN).doubleValue()); if (byRuleCalibType == HKConfigEnum.BYRULECALIBTYPE.FRAME.getValue()) { entity.setType((byte) HKConfigEnum.BYRULECALIBTYPE.FRAME.getMysqlvalue()); } else if (byRuleCalibType == HKConfigEnum.BYRULECALIBTYPE.LINE.getValue()) { entity.setType((byte) HKConfigEnum.BYRULECALIBTYPE.LINE.getMysqlvalue()); } } log.info("数据整理2"); entity.setStd(Double.valueOf(0)); entity.setCapturetime(LocalDateTime.now()); log.info("开始插入"); log.info("数据是:" + entity.toString()); asyncService.executeAsyncTask(entity); // Testintimpl testintimpl = new Testintimpl(); // testintimpl.test(entity); // String sql = "INSERT INTO data_eaif_h(eqmid,areaid,capturetime,maxtemp,mintemp,avgtemp,unit,type,mid,std) values('" + 666 + "','1','" + new Date() + "','" + 1 + "','" + 1 + "','" + 1 + "','" + 1 + "','" + 1 + "','" + 1 + "','" + 1 + "')"; // System.out.println(sql); // help.getConnection(); // int ret = help.excuteUpdate(sql); // log.info("啦啦啦" + ret); // try { // conn.close(); // } catch (SQLException throwables) { // throwables.printStackTrace(); // } // if (test == 1) { // DataEaifHEntity entitys = new DataEaifHEntity(); // entitys.setEqmid(384); // entitys.setMaxtemp((double) 0); // entitys.setAreaid(1); // entitys.setCapturetime(LocalDateTime.now()); // entitys.setMintemp((double) 0); // entitys.setAvgtemp((double) 0); // entitys.setType((byte) HKConfigEnum.BYRULECALIBTYPE.POINT.getMysqlvalue()); // entitys.setMid(Double.valueOf(0)); // dataEaifHService.save(entitys); //// //// ModevEntity entitys = new ModevEntity(); //// entitys.setModevtid(1919); //// entitys.setZsbid(1); //// entitys.setIedid(1); //// entitys.setAddress(12112); //// modevService.save(entitys); // } if (handle != -1) { log.info("开始退出"); instance.NET_DVR_StopRemoteConfig(handle); instance.NET_DVR_Logout(admin); } } else { if (handle != -1) { instance.NET_DVR_StopRemoteConfig(handle); instance.NET_DVR_Logout(admin); } } } }; //NET_DVR_GET_REALTIME_THERMOMETRY 实时温度检测 NET_DVR_GET_MANUALTHERM_INFO 手动测温实时获取 handle = instance.NET_DVR_StartRemoteConfig(admin, HCNetSDK.NET_DVR_GET_REALTIME_THERMOMETRY, cond.getPointer(), cond.size(), cbStateCallBack, null); if (handle < 0) { int j = instance.NET_DVR_GetLastError(); IntByReference intByReference = new IntByReference(j); log.error("长连接连接失败 " + "错误码:" + j + " 错误信息:" + instance.NET_DVR_GetErrorMsg(intByReference)); } else { log.info("长连接连接成功!!!"); } // try { // Thread.sleep(5000); // } catch (InterruptedException e) { // e.printStackTrace(); // } } else { log.error("登录失败 " + "IP:" + ip + " " + port); } } } else { log.info("尚未配置红外设备!!!"); } } }