|
|
@@ -371,337 +371,4 @@ public class SysDeviceServiceImpl extends ServiceImpl<SysDeviceMapper, SysDevice
|
|
|
public SysDevice findByHostCode(String code) {
|
|
|
return lambdaQuery().eq(SysDevice::getHostCode, code).last(Constants.LIMIT1).one();
|
|
|
}
|
|
|
-
|
|
|
- @Override
|
|
|
- public SysDevice findByHostCodeAndCodeAndOrgId(String equipmentCode, String channelCode, Long orgId) {
|
|
|
- return lambdaQuery()
|
|
|
- .eq(SysDevice::getHostCode, equipmentCode)
|
|
|
- .eq(SysDevice::getChannelCode, channelCode)
|
|
|
- .eq(SysDevice::getOrgId, orgId)
|
|
|
- .last(Constants.LIMIT1)
|
|
|
- .one();
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public SysDevice findByCodeAndOrgId(String code, Long orgId) {
|
|
|
- return lambdaQuery()
|
|
|
- .eq(SysDevice::getChannelCode, code)
|
|
|
- .eq(SysDevice::getOrgId, orgId)
|
|
|
- .last(Constants.LIMIT1)
|
|
|
- .one();
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public ReceiveErrorDto saveDirectHost(DataPageDto<SensorDto> request, String branchId, String msgId) {
|
|
|
- //验证基础分页信息
|
|
|
- ReceiveErrorDto dto = checkObjFieldIsNull(request);
|
|
|
-
|
|
|
-
|
|
|
- if (dto.getSuccess()) {
|
|
|
- //分页数据的存取
|
|
|
- final List<SensorDto> data = isCompleted(request, SensorDto.class);
|
|
|
-
|
|
|
- log.info("判断是否获取通用传感器全部信息分页数据:{}", data.size() > 0);
|
|
|
- if (data.size() == 0) {
|
|
|
- return dto;
|
|
|
- }
|
|
|
-
|
|
|
- new Thread(() -> {
|
|
|
- //删除直连主机传感器一个月前的状态数据
|
|
|
- // productDeviceMapper.deleteDirectHostHistory(DateUtil.offset(new Date(), DateField.MONTH, -1));
|
|
|
- }).start();
|
|
|
-
|
|
|
- List<Long> deletedDeviceIds = new ArrayList<>();
|
|
|
- List<SysDevice> devices = convertToDomain(data, msgId, branchId, deletedDeviceIds);
|
|
|
- this.removeByIds(deletedDeviceIds);
|
|
|
- this.saveOrUpdateBatch(devices);
|
|
|
- }
|
|
|
- return dto;
|
|
|
- }
|
|
|
-
|
|
|
- private List<SysDevice> convertToDomain(List<SensorDto> sensorDtos, String msgId, String branchId, List<Long> deletedDeviceIds) {
|
|
|
- List<SysDevice> domains = new ArrayList<>();
|
|
|
- List<NorthError> errors = new ArrayList<>();
|
|
|
- String path = "/api/{branchId}/data/sensorCommonList";
|
|
|
- Map<String, List<SysDevice>> productDeviceMap = getAllDevice();
|
|
|
- Set<String> uniqueSet = new HashSet<>();
|
|
|
- for (SensorDto sensorDto : sensorDtos) {
|
|
|
- NorthError error = null;
|
|
|
- SysDevice domain = new SysDevice();
|
|
|
-
|
|
|
- if (StringUtils.isEmpty(sensorDto.getOrgCode())) {
|
|
|
- error = new NorthError(msgId, branchId, path
|
|
|
- , sensorDto, "参数非法:未传orgCode");
|
|
|
- errors.add(error);
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- SysOrg org = orgService.findByCode(sensorDto.getOrgCode());
|
|
|
- if (org == null) {
|
|
|
- error = new NorthError(msgId, branchId, path
|
|
|
- , sensorDto, "参数非法:机构orgCode不存在:" + sensorDto.getOrgCode());
|
|
|
- errors.add(error);
|
|
|
- continue;
|
|
|
- }
|
|
|
- String key = org.getId() + sensorDto.getDeviceCode();
|
|
|
- if (uniqueSet.contains(key)) {
|
|
|
- errors.add(new NorthError(msgId, branchId, path
|
|
|
- , sensorDto, "数据来源中,同机构中设备编号重复。"));
|
|
|
- continue;
|
|
|
- }
|
|
|
- Long deviceType = convertDeviceType(sensorDto.getDeviceType());
|
|
|
- if (ObjectUtil.isNull(deviceType)) {
|
|
|
- errors.add(new NorthError(msgId, branchId, path
|
|
|
- , sensorDto, "无法识别的设备类型"));
|
|
|
- continue;
|
|
|
- }
|
|
|
- uniqueSet.add(key);
|
|
|
-
|
|
|
- List<SysDevice> exists = productDeviceMap.get(key);
|
|
|
- domain.setId(IDHelper.id());
|
|
|
- domain.setOnline(DeviceOnlineStatus.UNKNOW.ordinal());
|
|
|
- domain.setIsAlarm(DeviceAlarmStatus.NORMAL.getStatus());
|
|
|
- domain.setDoStatus(0);
|
|
|
- domain.setDelFlag("0");
|
|
|
- domain.setCreateTime(new Date());
|
|
|
-
|
|
|
- if (ObjectUtil.isNotEmpty(exists)) {
|
|
|
- if (exists.size() > 1) {
|
|
|
- errors.add(new NorthError(msgId, branchId, path, sensorDto, "同机构下设备编号重复,将取第一条"));
|
|
|
- }
|
|
|
- domain = exists.get(0);
|
|
|
- }
|
|
|
- domain.setUpdateTime(new Date());
|
|
|
- domain.setDeviceName(sensorDto.getDeviceName().replace("机房/基站环境", "物联环境"));
|
|
|
- domain.setCategoryId(deviceType);
|
|
|
- domain.setHostCode(sensorDto.getHostCode());
|
|
|
- domain.setChannelCode(sensorDto.getDeviceCode());
|
|
|
- domain.setOrgId(org.getId());
|
|
|
- domain.setOrgName(org.getName());
|
|
|
- domain.setOrgPath(org.getPath());
|
|
|
- domain.setSource(1);
|
|
|
- domains.add(domain);
|
|
|
- }
|
|
|
- if (errors.size() > 0) {
|
|
|
- remoteMediatorService.saveErrorData(errors, SecurityConstants.INNER);
|
|
|
- }
|
|
|
-
|
|
|
- productDeviceMap.keySet().stream().filter(k -> !uniqueSet.contains(k)).forEach(k -> productDeviceMap.get(k).forEach(d -> deletedDeviceIds.add(d.getId())));
|
|
|
-
|
|
|
- return domains;
|
|
|
- }
|
|
|
-
|
|
|
- private Map<String, List<SysDevice>> getAllDevice() {
|
|
|
- return baseMapper.selectList(new LambdaQueryWrapper<SysDevice>()
|
|
|
- .eq(SysDevice::getCategoryId, 1))
|
|
|
- .stream()
|
|
|
- .collect(Collectors.groupingBy(c -> c.getOrgId().toString()));
|
|
|
- }
|
|
|
-
|
|
|
- private Long convertDeviceType(String deviceCode) {
|
|
|
- Long type = null;
|
|
|
- switch (deviceCode) {
|
|
|
- case "4183"://温湿度
|
|
|
- type = 3712195941285888L; //todo
|
|
|
- break;
|
|
|
-// case "4181"://红外
|
|
|
-// type = 3713672266842116L;
|
|
|
-// break;
|
|
|
- case "4182": //烟感
|
|
|
- type = 3712194930442240L;
|
|
|
- break;
|
|
|
- case "4184": //水浸
|
|
|
- type = 3712196701503488L;
|
|
|
- break;
|
|
|
- case "4160"://智能电表
|
|
|
- type = 3712198166114160L;
|
|
|
- }
|
|
|
-
|
|
|
- return type;
|
|
|
- }
|
|
|
-
|
|
|
- public static <T> ReceiveErrorDto checkObjFieldIsNull(DataPageDto<T> request) {
|
|
|
- String packageGuid = request.getPackageGuid();
|
|
|
- if (StringUtils.isEmpty(packageGuid)) {
|
|
|
- return ReceiveErrorDto.error("packageGuid参数非法");
|
|
|
- }
|
|
|
- Long totalPage = request.getTotalPage();
|
|
|
- if (totalPage == null) {
|
|
|
- return ReceiveErrorDto.error("totalPage参数非法");
|
|
|
- }
|
|
|
- Long currPage = request.getCurrPage();
|
|
|
- if (currPage == null) {
|
|
|
- return ReceiveErrorDto.error("currPage参数非法");
|
|
|
- }
|
|
|
- if (ObjectUtil.isEmpty(request.getData())) {
|
|
|
- return ReceiveErrorDto.error("data信息为空");
|
|
|
- }
|
|
|
- return ReceiveErrorDto.success();
|
|
|
- }
|
|
|
-
|
|
|
- public <T> List<T> isCompleted(DataPageDto<T> request, Class<T> clazz) {
|
|
|
- List<T> resultList = new ArrayList<>();
|
|
|
- String guid = request.getPackageGuid();
|
|
|
- Long currPage = request.getCurrPage();
|
|
|
- Long totalPage = request.getTotalPage();
|
|
|
- List<?> dtoList = request.getData();
|
|
|
-
|
|
|
- /**
|
|
|
- * 生成每一页的redis key值
|
|
|
- */
|
|
|
- String key = guid + "_" + currPage;
|
|
|
-
|
|
|
- /**
|
|
|
- * 判断是否已经获取到该分页数据,如果已获取到,则直接返回
|
|
|
- */
|
|
|
- Boolean isExist = RedisUtils.hasKey(key);
|
|
|
-
|
|
|
- if (!isExist) {
|
|
|
- String data = JSON.toJSONString(dtoList);
|
|
|
- redisTemplate.opsForValue().set(key, data);
|
|
|
- redisTemplate.expire(key, 1, TimeUnit.DAYS);
|
|
|
- }
|
|
|
- //判断是否完整
|
|
|
- for (int i = 1; i <= totalPage; i++) {
|
|
|
- String ak = guid + "_" + i;
|
|
|
- Boolean isOk = RedisUtils.hasKey(ak);
|
|
|
- if (!isOk) {
|
|
|
- return resultList;
|
|
|
- }
|
|
|
- }
|
|
|
- //数据完整了将数据取出
|
|
|
- for (int i = 1; i <= totalPage; i++) {
|
|
|
- String ak = guid + "_" + i;
|
|
|
- Object obj = redisTemplate.opsForValue().get(ak);
|
|
|
- List<T> data = JSON.parseArray((String) obj, clazz);
|
|
|
- if (data != null) {
|
|
|
- resultList.addAll(data);
|
|
|
- }
|
|
|
- }
|
|
|
- for (int i = 1; i <= totalPage; i++) {
|
|
|
- String ak = guid + "_" + i;
|
|
|
- redisTemplate.delete(ak);
|
|
|
- }
|
|
|
-
|
|
|
- return resultList;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public ReceiveErrorDto saveDirectHostStatus(DataPageDto<SensorStatusDto> request, String branchId, String msgId) {
|
|
|
- ReceiveErrorDto dto = checkObjFieldIsNull(request);
|
|
|
-
|
|
|
- if (dto.getSuccess() && org.apache.commons.collections4.CollectionUtils.isNotEmpty(request.getData())) {
|
|
|
- new Thread(() -> {
|
|
|
- saveStatusAsync(request, msgId, branchId);
|
|
|
- }).start();
|
|
|
- }
|
|
|
-
|
|
|
- return dto;
|
|
|
- }
|
|
|
-
|
|
|
- private void saveStatusAsync(DataPageDto<SensorStatusDto> request, String msgId, String branchId) {
|
|
|
- String path = "/api/{branchId}/status/sensorCommon";
|
|
|
- List<NorthError> errors = new ArrayList<>();
|
|
|
-
|
|
|
- long c1 = System.currentTimeMillis();
|
|
|
- Map<String, List<SysDevice>> productDeviceMap = getAllDevice();
|
|
|
- System.out.println("获取设备用时:" + (System.currentTimeMillis() - c1));
|
|
|
- c1 = System.currentTimeMillis();
|
|
|
- System.out.println("获取设备状态用时:" + (System.currentTimeMillis() - c1));
|
|
|
- List<MediatorDeviceData> deviceDataList = new ArrayList<>();
|
|
|
- List<MediatorDeviceDataLog> deviceDataLogList = new ArrayList<>();
|
|
|
- for (SensorStatusDto status : request.getData()) {
|
|
|
- if (StringUtils.isEmpty(status.getOrgCode())) {
|
|
|
- errors.add(new NorthError(msgId, branchId, path
|
|
|
- , status, "参数非法:未传orgCode"));
|
|
|
- continue;
|
|
|
- }
|
|
|
- final SysOrg org = orgService.findByCode(status.getOrgCode());
|
|
|
- if (org == null) {
|
|
|
- errors.add(new NorthError(msgId, branchId, path
|
|
|
- , status, "参数非法:机构orgCode不存在:" + status.getOrgCode()));
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- String key = org.getId() + status.getDeviceCode();
|
|
|
- if (!productDeviceMap.containsKey(key)) {
|
|
|
- errors.add(new NorthError(msgId, branchId, path
|
|
|
- , status, "对应设备不存在" + status.getOrgCode()));
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- MediatorDeviceData deviceData = new MediatorDeviceData();
|
|
|
-
|
|
|
- deviceData.setDeviceName(status.getDeviceName());
|
|
|
- deviceData.setOrgId(org.getId());
|
|
|
- deviceData.setOrgPath(org.getPath());
|
|
|
- deviceData.setOrgCode(org.getGuid());
|
|
|
- deviceData.setOrgName(org.getName());
|
|
|
- deviceData.setUpdateTime(status.getUpdateTime());
|
|
|
- deviceData.setMulti(0);
|
|
|
- deviceData.setItems(handleInfos(status.getInfos()));
|
|
|
-
|
|
|
- MediatorDeviceDataLog deviceDataLog = new MediatorDeviceDataLog();
|
|
|
- BeanHelper.copyProperties(deviceDataLog, deviceData, "id");
|
|
|
- deviceDataLog.setCreateTime(new Date());
|
|
|
- deviceDataLogList.add(deviceDataLog);
|
|
|
- deviceDataList.add(deviceData);
|
|
|
- }
|
|
|
- if (errors.size() > 0) {
|
|
|
- remoteMediatorService.saveErrorData(errors, SecurityConstants.INNER);
|
|
|
- }
|
|
|
- remoteMediatorService.saveOrUpdateDeviceData(deviceDataList, SecurityConstants.INNER);
|
|
|
- remoteMediatorService.saveOrUpdateDeviceDataLog(deviceDataLogList, SecurityConstants.INNER);
|
|
|
-
|
|
|
- for (MediatorDeviceData deviceData : deviceDataList) {
|
|
|
- remoteMediatorService.analysisDeviceDataItem(deviceData, SecurityConstants.INNER);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private String handleInfos(String items) {
|
|
|
- JSONArray array = JSONArray.parseArray(items);
|
|
|
- JSONArray arrayNew = new JSONArray();
|
|
|
- for (Object o : array) {
|
|
|
- JSONObject jo = ((JSONObject) o);
|
|
|
- String name = jo.getString("name");
|
|
|
- ElectricityMeterAttributes value = ElectricityMeterAttributes.getEnumByName(name);
|
|
|
- if (ObjectUtil.isNotEmpty(value)) {
|
|
|
- String fieldCode = value.getAttrtCode();
|
|
|
- Long fieldId = value.getNum();
|
|
|
- jo.put("fieldCode", fieldCode);
|
|
|
- jo.put("fieldId", fieldId);
|
|
|
- jo.put("fieldName", name);
|
|
|
- jo.put("value", jo.getString("val"));
|
|
|
- jo.remove("name");
|
|
|
- jo.remove("val");
|
|
|
- arrayNew.add(jo);
|
|
|
- }
|
|
|
- /* switch (ElectricityMeterAttributes.valueOf(name)) {
|
|
|
- case "环境温度":
|
|
|
- fieldCode = "temperature";
|
|
|
- fieldId = 8074214277450756099L;
|
|
|
- break;
|
|
|
- case "环境湿度":
|
|
|
- fieldCode = "humidity";
|
|
|
- fieldId = 8074214277450756097L;
|
|
|
- break;
|
|
|
- case "烟感":
|
|
|
- fieldCode = "SmokeSensorState";
|
|
|
- fieldId = 8070947354577793026L;
|
|
|
- break;
|
|
|
- case "水浸":
|
|
|
- fieldCode = "status";
|
|
|
- fieldId = 8123843190642397184L;
|
|
|
- break;
|
|
|
- default:
|
|
|
- continue;
|
|
|
- }*/
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- return arrayNew.toJSONString();
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
}
|