package com.qianwen.mdc.collect.service;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Map.Entry;
|
import java.util.stream.Collectors;
|
|
import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
|
import org.apache.iotdb.tsfile.write.record.Tablet;
|
import org.apache.iotdb.tsfile.write.schema.MeasurementSchema;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
|
import com.qianwen.mdc.collect.config.IotDBSessionConfig;
|
import com.qianwen.mdc.collect.constants.IOTDBConstant;
|
import com.qianwen.mdc.collect.entity.iotdb.Alarm;
|
/**
|
* 设备状态普通服务
|
*/
|
@Service
|
public class AlarmService{
|
private static final Logger log = LoggerFactory.getLogger(AlarmService.class);
|
|
@Autowired
|
private IotDBCommonService iotDBCommonService;
|
@Autowired
|
private IotDBSessionConfig iotdbConfig;
|
|
static final List<MeasurementSchema> schemas;
|
|
static {
|
schemas = new ArrayList<>();
|
|
schemas.add(new MeasurementSchema("workstation_id", TSDataType.INT64));
|
schemas.add(new MeasurementSchema("calendar_code", TSDataType.TEXT));
|
schemas.add(new MeasurementSchema("factory_year", TSDataType.INT32));
|
schemas.add(new MeasurementSchema("factory_month", TSDataType.INT32));
|
schemas.add(new MeasurementSchema("factory_week", TSDataType.INT32));
|
schemas.add(new MeasurementSchema("factory_date", TSDataType.INT32));
|
schemas.add(new MeasurementSchema("shift_index", TSDataType.INT32));
|
schemas.add(new MeasurementSchema("shift_time_type", TSDataType.INT32));
|
schemas.add(new MeasurementSchema("code", TSDataType.TEXT));
|
schemas.add(new MeasurementSchema("message", TSDataType.TEXT));
|
schemas.add(new MeasurementSchema("level", TSDataType.TEXT));
|
}
|
|
/**
|
* 保存告警数据(state_{workstationId})
|
* @param stateList
|
*/
|
public void saveAlarms(List<Alarm> alarmList) {
|
//将数据按照工位id分组
|
Map<Long,List<Alarm>> maps = alarmList.stream().collect(Collectors.groupingBy(Alarm::getWorkstationId));
|
String deviceId;
|
Long wid;
|
|
List<Alarm> alarms;
|
for(Entry<Long, List<Alarm>> entry: maps.entrySet()) {
|
wid = entry.getKey();
|
|
deviceId = IOTDBConstant.DB_PREFIX+"alarm_"+wid;
|
iotDBCommonService.setTemmplateIfNotSet(IOTDBConstant.TEMPLATE_ALARM, deviceId);//挂载模板
|
|
Tablet tablet = new Tablet(deviceId, schemas);
|
|
alarms = entry.getValue();
|
tablet.rowSize = alarms.size();
|
Alarm alarm;
|
for(int i=0;i<alarms.size();i++) {
|
alarm = alarms.get(i);
|
tablet.addTimestamp(i, alarm.getTime());
|
tablet.addValue("workstation_id", i, alarm.getWorkstationId());
|
tablet.addValue("calendar_code", i, alarm.getCalendarCode());
|
tablet.addValue("factory_year", i, alarm.getFactoryYear());
|
tablet.addValue("factory_month", i, alarm.getFactoryMonth());
|
tablet.addValue("factory_week", i, alarm.getFactoryWeek());
|
tablet.addValue("factory_date", i, alarm.getFactoryDate());
|
tablet.addValue("shift_index", i, alarm.getShiftIndex());
|
tablet.addValue("shift_time_type", i, alarm.getShiftTimeType());
|
tablet.addValue("code", i, alarm.getCode());
|
tablet.addValue("message", i, alarm.getMessage());
|
tablet.addValue("level", i, alarm.getLevel());
|
}
|
try {
|
//每个工位批量插入一次数据
|
this.iotdbConfig.getSessionPool().insertAlignedTablet(tablet);
|
} catch (Exception e) {
|
log.error("保存固定点数据异常",e);
|
}
|
|
}
|
}
|
|
}
|