¶Ô±ÈÐÂÎļþ |
| | |
| | | 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; |
| | | } |
| | | } |