From 803697bd4f3ac5262e0acd69d6d7cc519ec45b2a Mon Sep 17 00:00:00 2001 From: vince <757871790@qq.com> Date: 星期一, 04 九月 2023 10:28:57 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/master' --- igds-protocol-modbus/src/main/java/com/ld/igds/modbus/service/HModbusService.java | 240 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 240 insertions(+), 0 deletions(-) diff --git a/igds-protocol-modbus/src/main/java/com/ld/igds/modbus/service/HModbusService.java b/igds-protocol-modbus/src/main/java/com/ld/igds/modbus/service/HModbusService.java new file mode 100644 index 0000000..9ade627 --- /dev/null +++ b/igds-protocol-modbus/src/main/java/com/ld/igds/modbus/service/HModbusService.java @@ -0,0 +1,240 @@ +package com.ld.igds.modbus.service; + +import com.bstek.bdf2.core.orm.hibernate.HibernateDao; +import com.ld.igds.constant.BizType; +import com.ld.igds.constant.Constant; +import com.ld.igds.constant.RedisConst; +import com.ld.igds.modbus.ModbusConstant; +import com.ld.igds.models.DeviceModbus; +import com.ld.igds.models.GasModbus; +import com.ld.igds.util.ContextUtil; +import com.ld.igds.util.RedisUtil; +import org.apache.commons.lang3.StringUtils; +import org.hibernate.Session; +import org.springframework.beans.BeanUtils; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + + +@Component +public class HModbusService extends HibernateDao { + + @Resource + private RedisUtil redisUtil; + + + public List<DeviceModbus> listAllDevice(String companyId) { + + String hql = " from " + DeviceModbus.class.getName() + " where 1=1 order by deviceCode+0"; + + return this.query(hql); + } + + public void flushCacheDevice(String companyId) { + + if (null == companyId) companyId = ContextUtil.getDefaultCompanyId(); + + List<DeviceModbus> listAll = this.listAllDevice(companyId); + + if (null == listAll || listAll.isEmpty()) return; + + + String key; + DeviceModbus entityDevice; + for (DeviceModbus hibDevice : listAll) { + entityDevice = new DeviceModbus(); + BeanUtils.copyProperties(hibDevice, entityDevice); + + if (null == hibDevice.getBizType()) hibDevice.setBizType(BizType.AREATION.getCode()); + if (BizType.AREATION.getCode().equals(hibDevice.getBizType())) { + key = RedisConst.buildKey(companyId, ModbusConstant.MODBUS_DEVICE_CACHE, hibDevice.getDeviceCode()); + } else { + key = RedisConst.buildKey(companyId, ModbusConstant.MODBUS_DEVICE_CACHE, hibDevice.getDeviceCode(), hibDevice.getBizType()); + } + redisUtil.set(key, entityDevice); + } + } + + public DeviceModbus getCacheDeviceModbus(String companyId, String deviceCode) { + return getCacheDeviceModbus(companyId, deviceCode, null); + } + + public DeviceModbus getCacheDeviceModbus(String companyId, String deviceCode, String bizType) { + String key = RedisConst.buildKey(companyId, ModbusConstant.MODBUS_DEVICE_CACHE, deviceCode); + if (null != bizType && !BizType.AREATION.equals(bizType)) { + key = RedisConst.buildKey(companyId, ModbusConstant.MODBUS_DEVICE_CACHE, deviceCode, bizType); + } + return (DeviceModbus) redisUtil.get(key); + } + + public void updateDataDevice(List<DeviceModbus> details) { + if (null == details || details.isEmpty()) return; + + Session session = this.getSessionFactory().openSession(); + try { + for (DeviceModbus device : details) { + + + device = updateInitData(device); + + if (null == device.getId()) { + device.setId(ContextUtil.getUUID()); + session.save(device); + } else { + session.update(device); + } + } + + flushCacheDevice(null); + + } catch (Exception e) { + + } finally { + session.flush(); + session.close(); + } + } + + private DeviceModbus updateInitData(DeviceModbus device) { + if (StringUtils.isEmpty(device.getOpen())) device.setOpen(Constant.YN_N); + if (StringUtils.isEmpty(device.getOpenFun())) device.setOpenFun(ModbusConstant.FUN_99); + + if (StringUtils.isEmpty(device.getOpenEnd())) device.setOpenEnd(Constant.YN_N); + if (StringUtils.isEmpty(device.getOpenEndFun())) device.setOpenEndFun(ModbusConstant.FUN_99); + + if (StringUtils.isEmpty(device.getOpenError())) device.setOpenError(Constant.YN_N); + if (StringUtils.isEmpty(device.getOpenErrorFun())) device.setOpenErrorFun(ModbusConstant.FUN_99); + + if (StringUtils.isEmpty(device.getClose())) device.setClose(Constant.YN_N); + if (StringUtils.isEmpty(device.getCloseFun())) device.setCloseFun(ModbusConstant.FUN_99); + + if (StringUtils.isEmpty(device.getCloseEnd())) device.setCloseEnd(Constant.YN_N); + if (StringUtils.isEmpty(device.getCloseEndFun())) device.setCloseEndFun(ModbusConstant.FUN_99); + + if (StringUtils.isEmpty(device.getCloseError())) device.setCloseError(Constant.YN_N); + if (StringUtils.isEmpty(device.getCloseErrorFun())) device.setCloseErrorFun(ModbusConstant.FUN_99); + + if (StringUtils.isEmpty(device.getStop())) device.setStop(Constant.YN_N); + if (StringUtils.isEmpty(device.getStopFun())) device.setStopFun(ModbusConstant.FUN_99); + + + if (StringUtils.isEmpty(device.getBizType())) device.setBizType(BizType.AREATION.getCode()); + + return device; + } + + public String delDataDevice(DeviceModbus data) { + Session session = this.getSessionFactory().openSession(); + try { + session.delete(data); + flushCacheDevice(null); + } catch (Exception e) { + } finally { + session.flush(); + session.close(); + } + return null; + } + + /** + * 鏍规嵁绫诲瀷鑾峰彇Modbus鑳借�楄澶囬厤缃俊鎭� + * @param bizType + * @param serId + * @return + */ + public List<DeviceModbus> getDataByBizType(String bizType, String serId) { + + String hql = " from " + DeviceModbus.class.getName() + " where bizType=:bizType and serId =:serId"; + + Map<String, Object> param = new HashMap<>(); + param.put("bizType", bizType); + param.put("serId", serId); + + return this.query(hql, param); + } + + + public DeviceModbus getGasData(String companyId, String depotId, String bizType) { + + String hql = " from " + DeviceModbus.class.getName() + " where bizType=:bizType and deviceCode =:deviceCode"; + + Map<String, Object> param = new HashMap<>(); + param.put("bizType", bizType); + param.put("deviceCode", depotId); + List<DeviceModbus> list = this.query(hql, param); + + if (null == list || list.isEmpty()) { + return null; + } + return list.get(0); + } + + + public List<GasModbus> listGasModBus(String depotId) { + String hql = " from " + GasModbus.class.getName(); + + if (null == depotId) { + return this.query(hql); + } else { + hql += " where depotId=:depotId order by passcode"; + Map<String, Object> param = new HashMap<>(); + param.put("depotId", depotId); + return this.query(hql, param); + } + } + + public void updateGasModbus(DeviceModbus data) { + Session session = this.getSessionFactory().openSession(); + try { + + data = updateInitData(data); + data.setBizType(BizType.GAS.getCode()); + + if (StringUtils.isEmpty(data.getId())) { + data.setId(ContextUtil.getUUID()); + session.save(data); + } else { + session.update(data); + } + flushCacheDevice(null); + + this.updateListGas(session, data); + + } catch (Exception e) { + } finally { + session.flush(); + session.close(); + } + } + + private void updateListGas(Session session, DeviceModbus data) { + List<GasModbus> list = data.getListGas(); + if (null == list || list.isEmpty()) return; + for (GasModbus gas : list) { + gas.setDepotId(data.getDeviceCode()); + if (StringUtils.isEmpty(gas.getId())) { + gas.setId(ContextUtil.getUUID()); + session.save(gas); + } else { + session.update(gas); + } + } + } + + + public String delDataGas(GasModbus data) { + Session session = this.getSessionFactory().openSession(); + try { + session.delete(data); + } catch (Exception e) { + } finally { + session.flush(); + session.close(); + } + return null; + } +} -- Gitblit v1.9.3