|  |  |  | 
|---|
|  |  |  | import com.moral.common.util.RedisUtils; | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  | 
|---|
|  |  |  | private void loadDeviceState(List<Device> list){ | 
|---|
|  |  |  | //从redis里取状态 | 
|---|
|  |  |  | list.stream().map( device -> { | 
|---|
|  |  |  | String state = getSateFromRedis(device.getMonitorPointId(),device.getMac()); | 
|---|
|  |  |  | String mac = Optional.of(device.getMac()).get(); | 
|---|
|  |  |  | String state = getSateFromRedis(device.getMonitorPointId(),mac.toLowerCase()); | 
|---|
|  |  |  | device.setState(state); | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  | 
|---|
|  |  |  | @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); | 
|---|
|  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public List<Map<String,String>> queryDevicesState(List<String> macList,Boolean withData) { | 
|---|
|  |  |  | List<Map<String,String>> list = macList.stream().map(mac->{ | 
|---|
|  |  |  | mac = mac.toLowerCase(); | 
|---|
|  |  |  | Device device = getDeviceWithOrgIdsByMac(mac); | 
|---|
|  |  |  | Map<String,String> resultMap = new HashMap<>(); | 
|---|
|  |  |  | Map<String,String> stateMap = getStateMapFromRedis(device.getMonitorPointId(),mac); | 
|---|
|  |  |  | 
|---|
|  |  |  | resultMap.putAll(stateMap); | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | resultMap.put("state",Constants.DEVICE_STATE_OFFLINE); | 
|---|
|  |  |  | resultMap.put("mac",mac); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(BooleanUtils.isTrue(withData)){ | 
|---|
|  |  |  | String dataKey = "data_"+mac; | 
|---|
|  |  |  | 
|---|
|  |  |  | if(!MapUtils.isEmpty(dataMap)){ | 
|---|
|  |  |  | resultMap.putAll(dataMap); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | resultMap.put("mac",mac); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return  resultMap; | 
|---|
|  |  |  | }).collect(Collectors.toList()); | 
|---|