|
@@ -9,6 +9,9 @@ import java.util.Map;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
|
+import com.bz.smart_city.dto.DeviceDataQueryDTO;
|
|
|
+import com.bz.smart_city.service.DeviceDataService;
|
|
|
+import com.zcxk.zoniot.smartcity.common.model.DeviceData;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.BeansException;
|
|
|
import org.springframework.beans.factory.InitializingBean;
|
|
@@ -16,6 +19,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
import org.springframework.context.ApplicationContextAware;
|
|
|
+import org.springframework.data.domain.Sort;
|
|
|
import org.springframework.kafka.core.KafkaTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
@@ -67,8 +71,6 @@ public class DeviceDataTimeOutServiceImpl implements DeviceDataTimeOutService, I
|
|
|
DeviceErrorMapper deviceErrorMapper;
|
|
|
@Autowired
|
|
|
DeviceMapper deviceMapper;
|
|
|
- @Autowired
|
|
|
- DeviceDataDimMapper deviceDataDimMapper;
|
|
|
@Resource
|
|
|
SnowflakeIdWorker idWorker;
|
|
|
@Resource
|
|
@@ -86,6 +88,9 @@ public class DeviceDataTimeOutServiceImpl implements DeviceDataTimeOutService, I
|
|
|
@Autowired
|
|
|
KafkaTemplate<String, Object> kafkaTemplate;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ DeviceDataService deviceDataService;
|
|
|
+
|
|
|
static Map<String, AlarmType> alarmTypes = new HashMap<String, AlarmType>();
|
|
|
|
|
|
@Override
|
|
@@ -97,26 +102,28 @@ public class DeviceDataTimeOutServiceImpl implements DeviceDataTimeOutService, I
|
|
|
Device param = new Device();
|
|
|
param.setDeviceType(deviceTypeId);
|
|
|
List<Integer> statusList = new ArrayList<Integer>();
|
|
|
- statusList.add(1);
|
|
|
- statusList.add(2);
|
|
|
+
|
|
|
+ statusList.add(1);
|
|
|
+ statusList.add(2);
|
|
|
|
|
|
- statusList.add(4);
|
|
|
+ statusList.add(4);
|
|
|
param.setDeviceStatusList(statusList);
|
|
|
int total = deviceMapper.countDevice(param);
|
|
|
|
|
|
int pageSize = 1000;
|
|
|
|
|
|
int totalPage = total / pageSize;
|
|
|
- if (total % pageSize != 0)
|
|
|
+ if (total % pageSize != 0){
|
|
|
totalPage++;
|
|
|
+ }
|
|
|
List<DeviceError> errorList = new ArrayList<DeviceError>();
|
|
|
- for (int i = 1; i <= totalPage; i++) {
|
|
|
- int start = (i - 1) * pageSize;
|
|
|
- List<DeviceDto> deviceList = deviceMapper.getDeviceList(param, start, pageSize);
|
|
|
+ for (int i = 1; i <= totalPage; i++) {
|
|
|
+ int start = (i - 1) * pageSize;
|
|
|
+ List<DeviceDto> deviceList = deviceMapper.getDeviceList(param, start, pageSize);
|
|
|
|
|
|
for (DeviceDto device : deviceList) {
|
|
|
|
|
|
- Long deviceId = device.getId();
|
|
|
+ Long deviceId = device.getId();
|
|
|
|
|
|
String deviceDataJSONStr = redisUtil.get(String.valueOf(deviceId));
|
|
|
Date date = DateTimeUtil.yesterdayLastDate();
|
|
@@ -125,80 +132,100 @@ public class DeviceDataTimeOutServiceImpl implements DeviceDataTimeOutService, I
|
|
|
JSONObject dataObj = (JSONObject) JSON.parse(deviceDataJSONStr);
|
|
|
|
|
|
JSONObject timeData = (JSONObject) dataObj.get("TIME");
|
|
|
- String timeStr = (String) timeData.get("measuringVaule");
|
|
|
+
|
|
|
+ String timeStr = (String) timeData.get("measuringData");
|
|
|
|
|
|
Date cacheDate = DateTimeUtil.parseDate(timeStr, "yyyyMMddHHmmss");
|
|
|
|
|
|
- if (cacheDate.before(DateTimeUtil.before(date, new Double(period * 3600 * 1000).longValue()))) {
|
|
|
+
|
|
|
+ if (cacheDate.before(DateTimeUtil.before(date, new Double(period * 3600 * 1000).longValue()))) {
|
|
|
DeviceError alarm = createTimeOutDerviceError(device);
|
|
|
if (alarm != null) {
|
|
|
errorList.add(alarm);
|
|
|
Device obj = new Device();
|
|
|
obj.setId(device.getId());
|
|
|
- obj.setDeviceStatus(2);
|
|
|
+
|
|
|
+ obj.setDeviceStatus(2);
|
|
|
deviceMapper.updateByPrimaryKeySelective(obj);
|
|
|
result++;
|
|
|
}
|
|
|
- } else if (cacheDate.before(DateTimeUtil.before(date, new Double(24 * 3600 * 1000).longValue()))) {
|
|
|
+ }
|
|
|
+
|
|
|
+ else if (cacheDate.before(DateTimeUtil.before(date, new Double(24 * 3600 * 1000).longValue()))) {
|
|
|
Device obj = new Device();
|
|
|
obj.setId(device.getId());
|
|
|
- obj.setDeviceStatus(4);
|
|
|
+
|
|
|
+ obj.setDeviceStatus(4);
|
|
|
deviceMapper.updateByPrimaryKeySelective(obj);
|
|
|
|
|
|
- if (device.getDeviceStatus() == 2) {
|
|
|
+ if (device.getDeviceStatus() == 2) {
|
|
|
+
|
|
|
waterMeterErrorDaysMapper.reset(deviceId);
|
|
|
- } else if (device.getDeviceStatus() == 2) {
|
|
|
+ } else if (device.getDeviceStatus() == 2) {
|
|
|
+
|
|
|
waterMeterErrorDaysMapper.addDays(deviceId);
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
- DeviceDataDim data = getLastDeviceData(deviceId);
|
|
|
+ DeviceData data = getLastDeviceData(deviceId);
|
|
|
if (data != null) {
|
|
|
- Date sendTime = data.getSendTime();
|
|
|
- if (sendTime.before(DateTimeUtil.before(date, new Double(period * 3600 * 1000).longValue()))) {
|
|
|
+ Date sendTime = data.getReceiveTime();
|
|
|
+
|
|
|
+ if (sendTime.before(DateTimeUtil.before(date, new Double(period * 3600 * 1000).longValue()))) {
|
|
|
DeviceError alarm = createTimeOutDerviceError(device);
|
|
|
if (alarm != null) {
|
|
|
errorList.add(alarm);
|
|
|
Device obj = new Device();
|
|
|
obj.setId(device.getId());
|
|
|
- obj.setDeviceStatus(2);
|
|
|
+
|
|
|
+ obj.setDeviceStatus(2);
|
|
|
deviceMapper.updateByPrimaryKeySelective(obj);
|
|
|
result++;
|
|
|
}
|
|
|
- } else if (sendTime.before(DateTimeUtil.before(date, new Double(24 * 3600 * 1000).longValue()))) {
|
|
|
+ }
|
|
|
+
|
|
|
+ else if (sendTime.before(DateTimeUtil.before(date, new Double(24 * 3600 * 1000).longValue()))) {
|
|
|
Device obj = new Device();
|
|
|
obj.setId(device.getId());
|
|
|
- obj.setDeviceStatus(4);
|
|
|
+
|
|
|
+ obj.setDeviceStatus(4);
|
|
|
deviceMapper.updateByPrimaryKeySelective(obj);
|
|
|
|
|
|
|
|
|
- if (device.getDeviceStatus() == 2) {
|
|
|
+ if (device.getDeviceStatus() == 2) {
|
|
|
+
|
|
|
waterMeterErrorDaysMapper.reset(deviceId);
|
|
|
- } else if (device.getDeviceStatus() == 2) {
|
|
|
+ } else if (device.getDeviceStatus() == 2) {
|
|
|
+
|
|
|
waterMeterErrorDaysMapper.addDays(deviceId);
|
|
|
}
|
|
|
}
|
|
|
- } else {
|
|
|
- if (deviceCreateDate.before(DateTimeUtil.before(date, new Double(period * 3600 * 1000).longValue()))) {
|
|
|
+ } else {
|
|
|
+
|
|
|
+ if (deviceCreateDate.before(DateTimeUtil.before(date, new Double(period * 3600 * 1000).longValue()))) {
|
|
|
DeviceError alarm = createTimeOutDerviceError(device);
|
|
|
if (alarm != null) {
|
|
|
errorList.add(alarm);
|
|
|
Device obj = new Device();
|
|
|
obj.setId(device.getId());
|
|
|
- obj.setDeviceStatus(2);
|
|
|
+
|
|
|
+ obj.setDeviceStatus(2);
|
|
|
deviceMapper.updateByPrimaryKeySelective(obj);
|
|
|
result++;
|
|
|
}
|
|
|
} else if (deviceCreateDate.before(DateTimeUtil.before(date, new Double(24 * 3600 * 1000).longValue()))) {
|
|
|
Device obj = new Device();
|
|
|
obj.setId(device.getId());
|
|
|
- obj.setDeviceStatus(4);
|
|
|
+
|
|
|
+ obj.setDeviceStatus(4);
|
|
|
deviceMapper.updateByPrimaryKeySelective(obj);
|
|
|
|
|
|
|
|
|
- if (device.getDeviceStatus() == 2) {
|
|
|
+ if (device.getDeviceStatus() == 2) {
|
|
|
+
|
|
|
waterMeterErrorDaysMapper.reset(deviceId);
|
|
|
- } else if (device.getDeviceStatus() == 2) {
|
|
|
+ } else if (device.getDeviceStatus() == 2) {
|
|
|
+
|
|
|
waterMeterErrorDaysMapper.addDays(deviceId);
|
|
|
}
|
|
|
}
|
|
@@ -215,7 +242,7 @@ public class DeviceDataTimeOutServiceImpl implements DeviceDataTimeOutService, I
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
- Thread.sleep(3000l);
|
|
|
+ Thread.sleep(3000L);
|
|
|
} catch (Exception e) {
|
|
|
log.error("runtime error", e);
|
|
|
}
|
|
@@ -229,7 +256,7 @@ public class DeviceDataTimeOutServiceImpl implements DeviceDataTimeOutService, I
|
|
|
errorList.clear();
|
|
|
}
|
|
|
log.info("DeviceDataTimeOutService createTimeOutAlarm cost ="
|
|
|
- + (new Date().getTime() - startTime.getTime()) / 1000 + " seconds");
|
|
|
+ + (System.currentTimeMillis()- startTime.getTime()) / 1000 + " seconds");
|
|
|
log.info("end DeviceDataTimeOutService createTimeOutAlarm, result = " + result);
|
|
|
return result;
|
|
|
}
|
|
@@ -237,39 +264,50 @@ public class DeviceDataTimeOutServiceImpl implements DeviceDataTimeOutService, I
|
|
|
|
|
|
* 处理告警数据
|
|
|
*
|
|
|
- * @param device
|
|
|
- * @param errors
|
|
|
- * @param lastReceiveTime
|
|
|
+ * @param
|
|
|
+ * @param
|
|
|
+ * @param
|
|
|
*/
|
|
|
protected DeviceError processAlarm(DeviceError error) {
|
|
|
DeviceError rtnError = null;
|
|
|
|
|
|
Long deviceId = error.getDeviceId();
|
|
|
List<DeviceError> lastErrorList = getDeviceLastErrorList(deviceId);
|
|
|
- if (lastErrorList == null || lastErrorList.size() == 0) {
|
|
|
+ if (lastErrorList == null || lastErrorList.size() == 0) {
|
|
|
+
|
|
|
rtnError = saveNewDeviceError(error);
|
|
|
} else {
|
|
|
|
|
|
boolean isSame = false;
|
|
|
DeviceError originalError = null;
|
|
|
for (DeviceError lde : lastErrorList) {
|
|
|
- if (2 == lde.getHandleStatus() && lde.getAlarmTypeId() == error.getAlarmTypeId()) {
|
|
|
+
|
|
|
+ if (2 == lde.getHandleStatus() && lde.getAlarmTypeId().equals( error.getAlarmTypeId())) {
|
|
|
isSame = true;
|
|
|
originalError = lde;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
- if (!isSame) {
|
|
|
+ if (!isSame) {
|
|
|
+
|
|
|
rtnError = saveNewDeviceError(error);
|
|
|
- } else {
|
|
|
-
|
|
|
+ } else {
|
|
|
+
|
|
|
+
|
|
|
Date lastAlarmTime = Jdk8DateUtils.getLocalDateTimeToDate(lastErrorList.get(0).getLastAlarmTime());
|
|
|
Date currentAlarmTime = Jdk8DateUtils.getLocalDateTimeToDate(error.getAlarmTime());
|
|
|
- List<DeviceDataDim> receiveDatas = deviceDataDimMapper.getDeviceDataBetweenDate(lastAlarmTime,
|
|
|
- currentAlarmTime, deviceId);
|
|
|
- if (receiveDatas != null && receiveDatas.size() != 0) {
|
|
|
+ DeviceDataQueryDTO param = new DeviceDataQueryDTO();
|
|
|
+ param.setDeviceId(deviceId);
|
|
|
+ param.setEndDate(currentAlarmTime);
|
|
|
+ param.setStartDate(lastAlarmTime);
|
|
|
+ List<DeviceData> receiveDatas = deviceDataService.queryDeviceDataList(param);
|
|
|
+
|
|
|
+
|
|
|
+ if (receiveDatas != null && receiveDatas.size() != 0) {
|
|
|
+
|
|
|
rtnError = saveNewDeviceError(error);
|
|
|
- } else {
|
|
|
+ } else {
|
|
|
+
|
|
|
rtnError = updateExistDeviceError(error, originalError);
|
|
|
}
|
|
|
}
|
|
@@ -304,7 +342,7 @@ public class DeviceDataTimeOutServiceImpl implements DeviceDataTimeOutService, I
|
|
|
|
|
|
* 查看最后一次上报的告警信息
|
|
|
*
|
|
|
- * @param device
|
|
|
+ * @param
|
|
|
* @return
|
|
|
*/
|
|
|
protected List<DeviceError> getDeviceLastErrorList(Long deviceId) {
|
|
@@ -378,8 +416,9 @@ public class DeviceDataTimeOutServiceImpl implements DeviceDataTimeOutService, I
|
|
|
target = alarmType;
|
|
|
}
|
|
|
}
|
|
|
- if (target != null)
|
|
|
+ if(target != null){
|
|
|
alarmTypes.put(key, target);
|
|
|
+ }
|
|
|
}
|
|
|
return target;
|
|
|
}
|
|
@@ -393,12 +432,18 @@ public class DeviceDataTimeOutServiceImpl implements DeviceDataTimeOutService, I
|
|
|
return alarmTypeMapper.queryAlaramType(param);
|
|
|
}
|
|
|
|
|
|
- protected DeviceDataDim getLastDeviceData(Long deviceId) {
|
|
|
- DeviceDataDim result = null;
|
|
|
- DeviceDataDim p = new DeviceDataDim();
|
|
|
- p.setDeviceId(deviceId);
|
|
|
+ protected DeviceData getLastDeviceData(Long deviceId) {
|
|
|
+ DeviceData result = null;
|
|
|
+
|
|
|
+
|
|
|
|
|
|
- List<DeviceDataDim> datas = deviceDataDimMapper.queryDeviceDataDim(p, 1);
|
|
|
+ DeviceDataQueryDTO param = new DeviceDataQueryDTO();
|
|
|
+ param.setSort(new Sort(Sort.Direction.DESC, "receiveTime"));
|
|
|
+ param.setDeviceId(deviceId);
|
|
|
+ param.setPageIndex(0);
|
|
|
+ param.setPageSize(1);
|
|
|
+ List<DeviceData> datas = deviceDataService.queryDeviceDataList(param);
|
|
|
+
|
|
|
if (datas.size() > 0) {
|
|
|
result = datas.get(0);
|
|
|
}
|
|
@@ -442,51 +487,64 @@ public class DeviceDataTimeOutServiceImpl implements DeviceDataTimeOutService, I
|
|
|
JSONObject dataObj = (JSONObject) JSON.parse(deviceDataJSONStr);
|
|
|
|
|
|
JSONObject timeData = (JSONObject) dataObj.get("TIME");
|
|
|
- String timeStr = (String) timeData.get("measuringVaule");
|
|
|
+
|
|
|
+ String timeStr = (String) timeData.get("measuringVaule");
|
|
|
|
|
|
Date cacheDate = DateTimeUtil.parseDate(timeStr, "yyyyMMddHHmmss");
|
|
|
- if (cacheDate.before(DateTimeUtil.before(date, new Double(period * 3600 * 1000).longValue()))) {
|
|
|
+
|
|
|
+ if (cacheDate.before(DateTimeUtil.before(date, new Double(period * 3600 * 1000).longValue()))) {
|
|
|
alarm = createTimeOutDerviceError(device);
|
|
|
if (alarm != null) {
|
|
|
Device obj = new Device();
|
|
|
obj.setId(device.getId());
|
|
|
- obj.setDeviceStatus(2);
|
|
|
+
|
|
|
+ obj.setDeviceStatus(2);
|
|
|
deviceMapper.updateByPrimaryKeySelective(obj);
|
|
|
}
|
|
|
- } else if (cacheDate.before(DateTimeUtil.before(date, new Double(24 * 3600 * 1000).longValue()))) {
|
|
|
+ } else if (cacheDate.before(DateTimeUtil.before(date, new Double(24 * 3600 * 1000).longValue()))) {
|
|
|
+
|
|
|
Device obj = new Device();
|
|
|
obj.setId(device.getId());
|
|
|
- obj.setDeviceStatus(4);
|
|
|
+
|
|
|
+ obj.setDeviceStatus(4);
|
|
|
deviceMapper.updateByPrimaryKeySelective(obj);
|
|
|
|
|
|
- if (device.getDeviceStatus() == 2) {
|
|
|
+ if (device.getDeviceStatus() == 2) {
|
|
|
+
|
|
|
waterMeterErrorDaysMapper.reset(deviceId);
|
|
|
- } else if (device.getDeviceStatus() == 2) {
|
|
|
+ } else if (device.getDeviceStatus() == 2) {
|
|
|
+
|
|
|
waterMeterErrorDaysMapper.addDays(deviceId);
|
|
|
}
|
|
|
}
|
|
|
- } else {
|
|
|
- DeviceDataDim data = getLastDeviceData(deviceId);
|
|
|
+ } else {
|
|
|
+
|
|
|
+ DeviceData data = getLastDeviceData(deviceId);
|
|
|
if (data != null) {
|
|
|
- Date sendTime = data.getSendTime();
|
|
|
+ Date sendTime = data.getReceiveTime();
|
|
|
if (sendTime.before(DateTimeUtil.before(date, new Double(period * 3600 * 1000).longValue()))) {
|
|
|
alarm = createTimeOutDerviceError(device);
|
|
|
if (alarm != null) {
|
|
|
Device obj = new Device();
|
|
|
obj.setId(device.getId());
|
|
|
- obj.setDeviceStatus(2);
|
|
|
+ obj.setDeviceStatus(2);
|
|
|
+
|
|
|
deviceMapper.updateByPrimaryKeySelective(obj);
|
|
|
}
|
|
|
- } else if (sendTime.before(DateTimeUtil.before(date, new Double(24 * 3600 * 1000).longValue()))) {
|
|
|
+ } else if (sendTime.before(DateTimeUtil.before(date, new Double(24 * 3600 * 1000).longValue()))) {
|
|
|
+
|
|
|
Device obj = new Device();
|
|
|
obj.setId(device.getId());
|
|
|
- obj.setDeviceStatus(4);
|
|
|
+ obj.setDeviceStatus(4);
|
|
|
+
|
|
|
deviceMapper.updateByPrimaryKeySelective(obj);
|
|
|
|
|
|
|
|
|
- if (device.getDeviceStatus() == 2) {
|
|
|
+ if (device.getDeviceStatus() == 2) {
|
|
|
+
|
|
|
waterMeterErrorDaysMapper.reset(deviceId);
|
|
|
- } else if (device.getDeviceStatus() == 2) {
|
|
|
+ } else if (device.getDeviceStatus() == 2) {
|
|
|
+
|
|
|
waterMeterErrorDaysMapper.addDays(deviceId);
|
|
|
}
|
|
|
}
|
|
@@ -496,19 +554,24 @@ public class DeviceDataTimeOutServiceImpl implements DeviceDataTimeOutService, I
|
|
|
if (alarm != null) {
|
|
|
Device obj = new Device();
|
|
|
obj.setId(device.getId());
|
|
|
- obj.setDeviceStatus(2);
|
|
|
+
|
|
|
+ obj.setDeviceStatus(2);
|
|
|
deviceMapper.updateByPrimaryKeySelective(obj);
|
|
|
}
|
|
|
- } else if (deviceCreateDate.before(DateTimeUtil.before(date, new Double(24 * 3600 * 1000).longValue()))) {
|
|
|
+ } else if (deviceCreateDate.before(DateTimeUtil.before(date, new Double(24 * 3600 * 1000).longValue()))) {
|
|
|
+
|
|
|
Device obj = new Device();
|
|
|
obj.setId(device.getId());
|
|
|
- obj.setDeviceStatus(4);
|
|
|
+ obj.setDeviceStatus(4);
|
|
|
+
|
|
|
deviceMapper.updateByPrimaryKeySelective(obj);
|
|
|
|
|
|
|
|
|
- if (device.getDeviceStatus() == 2) {
|
|
|
+ if (device.getDeviceStatus() == 2) {
|
|
|
+
|
|
|
waterMeterErrorDaysMapper.reset(deviceId);
|
|
|
- } else if (device.getDeviceStatus() == 2) {
|
|
|
+ } else if (device.getDeviceStatus() == 2) {
|
|
|
+
|
|
|
waterMeterErrorDaysMapper.addDays(deviceId);
|
|
|
}
|
|
|
}
|