| | |
| | | import com.moral.common.bean.PageResult;
|
| | | import com.moral.common.util.ExampleUtil;
|
| | | import com.moral.common.util.RedisUtils;
|
| | | import com.moral.mapper.MonitorPointMapper;
|
| | | import org.apache.commons.collections.MapUtils;
|
| | | import org.apache.commons.lang3.BooleanUtils;
|
| | | import org.apache.commons.lang3.StringUtils;
|
| | | import org.springframework.stereotype.Service;
|
| | | import org.springframework.transaction.annotation.Transactional;
|
| | | import org.springframework.util.ObjectUtils;
|
| | |
| | |
|
| | | @Resource
|
| | | private DeviceMapper deviceMapper;
|
| | |
|
| | | @Resource
|
| | | private MonitorPointMapper monitorPointMapper;
|
| | | @Resource
|
| | | private AccountService accountService;
|
| | | private Class ENTITY_CLASS = Device.class;
|
| | |
| | | private void loadDeviceState(List<Device> list){
|
| | | //从redis里取状态
|
| | | list.stream().map( device -> {
|
| | | String state = getSateFromRedis(device.getMonitorPointId(),device.getMac());
|
| | | device.setState(state);
|
| | | String mac = device.getMac();
|
| | | if(!StringUtils.isBlank(mac)){
|
| | | String state = getSateFromRedis(device.getMonitorPointId(),mac.toLowerCase());
|
| | | device.setState(state);
|
| | | }else{
|
| | | device.setState(Constants.DEVICE_STATE_OFFLINE);
|
| | | }
|
| | | return device;
|
| | | }).count();
|
| | | }
|
| | | private String getSateFromRedis(Integer mpId,String mac){
|
| | |
|
| | | Map<String,String> states = getStateMapFromRedis(mpId,mac);
|
| | | Map<String,String> stateMap = getStateMapFromRedis(mpId,mac);
|
| | | String state = null;
|
| | | if(states != null){
|
| | | state = states.get("state");
|
| | | if(stateMap != null){
|
| | | state = stateMap.get("state");
|
| | | }
|
| | | state = state == null ?Constants.DEVICE_STATE_OFFLINE:state;
|
| | | return state;
|
| | |
| | | }
|
| | | return device;
|
| | | }
|
| | |
|
| | | /*
|
| | | 刷新 redis 设备的信息
|
| | | */
|
| | | private void refreshDeviceInRedis(Device device){
|
| | | if(!StringUtils.isBlank(device.getMac())){
|
| | | Device simpleDevice = new Device();
|
| | | simpleDevice.setId(device.getId());
|
| | | simpleDevice.setDeviceVersion(device.getDeviceVersion());
|
| | | simpleDevice.setMac(device.getMac());
|
| | | simpleDevice.setMonitorPointId(device.getMonitorPointId());
|
| | | if(device.getMonitorPointId()!=null){
|
| | | List<Integer> orgIds = monitorPointMapper.selectOrganizationIds(device.getMonitorPointId());
|
| | | simpleDevice.setOrganizationIds(orgIds);
|
| | | }
|
| | | String key = "device_"+device.getMac();
|
| | | redisUtils.set(key,simpleDevice);
|
| | | }
|
| | | }
|
| | | @Override
|
| | | public PageBean queryByPageBean(PageBean pageBean) {
|
| | | Example example = ExampleUtil.generateExample(ENTITY_CLASS,pageBean);
|
| | |
| | | @Override
|
| | | public void addOrModify(Device device){
|
| | | try{
|
| | | //mac 转小写
|
| | | if(StringUtils.isBlank(device.getMac())){
|
| | | device.setMac(device.getMac().toLowerCase());
|
| | | }
|
| | | if(device.getId()==null){
|
| | | device.setIsDelete(Constants.IS_DELETE_FALSE);
|
| | | deviceMapper.insertSelective(device);
|
| | | }else{
|
| | | deviceMapper.updateByPrimaryKeySelective(device);
|
| | | //刷新redis里设备信息
|
| | | refreshDeviceInRedis(device);
|
| | | }
|
| | | }
|
| | | catch (Exception ex){
|
| | |
| | | @Override
|
| | | public List<Map<String,String>> queryDevicesState(List<String> macList,Boolean withData) {
|
| | | List<Map<String,String>> list = macList.stream().map(mac->{
|
| | | Device device = getDeviceWithOrgIdsByMac(mac);
|
| | | Map<String,String> resultMap = new HashMap<>();
|
| | | Map<String,String> stateMap = getStateMapFromRedis(device.getMonitorPointId(),mac);
|
| | | if(!MapUtils.isEmpty(stateMap)){
|
| | | resultMap.putAll(stateMap);
|
| | | }else{
|
| | | resultMap.put("state",Constants.DEVICE_STATE_OFFLINE);
|
| | | }
|
| | | if(BooleanUtils.isTrue(withData)){
|
| | | String dataKey = "data_"+mac;
|
| | | Map<String,String> dataMap = redisUtils.get(dataKey,new TypeReference<Map<String,String>>(){});
|
| | | if(!MapUtils.isEmpty(dataMap)){
|
| | | resultMap.putAll(dataMap);
|
| | | if(!StringUtils.isBlank(mac)){
|
| | | mac = mac.toLowerCase();
|
| | | Device device = getDeviceWithOrgIdsByMac(mac);
|
| | | Map<String,String> stateMap = getStateMapFromRedis(device.getMonitorPointId(),mac);
|
| | | if(!MapUtils.isEmpty(stateMap)){
|
| | | resultMap.putAll(stateMap);
|
| | | }else{
|
| | | resultMap.put("state",Constants.DEVICE_STATE_OFFLINE);
|
| | | resultMap.put("mac",mac);
|
| | | }
|
| | | }else{
|
| | | resultMap.put("mac",mac);
|
| | | //添加data
|
| | | if(BooleanUtils.isTrue(withData)){
|
| | | String dataKey = "data_"+mac;
|
| | | Map<String,String> dataMap = redisUtils.get(dataKey,new TypeReference<Map<String,String>>(){});
|
| | | if(!MapUtils.isEmpty(dataMap)){
|
| | | resultMap.putAll(dataMap);
|
| | | }
|
| | | }
|
| | | }
|
| | | return resultMap;
|
| | | }).collect(Collectors.toList());
|