yangys
2024-10-09 7ef593e1e3c35aaeecf9318f0b3941230d3ed002
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package com.qianwen.mdc.collect.service;
 
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
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.google.common.collect.Lists;
import com.qianwen.core.tool.utils.Func;
import com.qianwen.core.tool.utils.SpringUtil;
import com.qianwen.mdc.collect.cache.WorkstationCache;
import com.qianwen.mdc.collect.config.IotDBSessionConfig;
import com.qianwen.mdc.collect.constants.CommonConstant;
import com.qianwen.mdc.collect.constants.IOTDBConstant;
import com.qianwen.mdc.collect.dto.CalendarShiftInfoDTO;
import com.qianwen.mdc.collect.dto.WorkstationDTO;
import com.qianwen.mdc.collect.entity.iotdb.DeviceState;
import com.qianwen.mdc.collect.enums.FeedbackTimePointEnum;
import com.qianwen.mdc.collect.mapper.mgr.CalendarMapper;
import com.qianwen.mdc.collect.utils.LocalDateTimeUtils;
 
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
/**
 * 设备状态普通服务
 */
@Service
public class DeviceStateService{
    private static final Logger log = LoggerFactory.getLogger(DeviceStateService.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("value_collect", TSDataType.INT32));
 
        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("wcs", TSDataType.INT32));
        schemas.add(new MeasurementSchema("rps", TSDataType.INT32));
        schemas.add(new MeasurementSchema("is_fix_point", TSDataType.BOOLEAN));
        schemas.add(new MeasurementSchema("is_sync", TSDataType.BOOLEAN));
        schemas.add(new MeasurementSchema("is_plan", TSDataType.INT32));//TODO 这个属性应该是GlobalWcsOfRps中的值,如何填写?
        schemas.add(new MeasurementSchema("feedback_point_type", TSDataType.INT32));
        schemas.add(new MeasurementSchema("feedback_id", TSDataType.INT64));
        schemas.add(new MeasurementSchema("is_deleted", TSDataType.BOOLEAN));
        schemas.add(new MeasurementSchema("employee_id", TSDataType.INT64));
        
    }
    
    /**
     * 保存状态固定点数据(state_{workstationId})
     * @param stateList
     */
    public void saveDeviceStates(List<DeviceState> stateList) {
        //将数据按照工位id分组
        Map<Long,List<DeviceState>> maps = stateList.stream().collect(Collectors.groupingBy(DeviceState::getWorkstationId));
        String deviceId;
        Long wid;
        
        List<DeviceState> states;
        for(Entry<Long, List<DeviceState>>  entry: maps.entrySet()) {
            wid = entry.getKey();
            
            deviceId = IOTDBConstant.DB_PREFIX+"state_"+wid;
            iotDBCommonService.setTemmplateIfNotSet(IOTDBConstant.TEMPLATE_STATE, deviceId);//挂载模板
            
            Tablet tablet = new Tablet(deviceId, schemas);
            
            
            states = entry.getValue();
            tablet.rowSize = states.size();
            DeviceState state;
            
            
            final int MAX_COUNT = 1000;
            //int currentIdx = 0;
        
                
            int tblIndex = 0;
            for(int i=0; i < states.size(); i++) {
                state = states.get(i);
                tablet.addTimestamp(tblIndex, state.getTime());
                tablet.addValue("workstation_id", tblIndex, state.getWorkstationId());
                tablet.addValue("value_collect", tblIndex, state.getValueCollect());
                
                tablet.addValue("calendar_code", tblIndex, state.getCalendarCode());
                tablet.addValue("factory_year", tblIndex, state.getFactoryYear());
                tablet.addValue("factory_month", tblIndex, state.getFactoryMonth());
                tablet.addValue("factory_week", tblIndex, state.getFactoryWeek());
                tablet.addValue("factory_date", tblIndex, state.getFactoryDate());
                tablet.addValue("shift_index", tblIndex, state.getShiftIndex());
                tablet.addValue("shift_time_type", tblIndex, state.getShiftTimeType());
                tablet.addValue("wcs", tblIndex, state.getWcs());
                tablet.addValue("rps", tblIndex, state.getRps());
                tablet.addValue("is_fix_point", tblIndex, state.getIsFixPoint());
                tablet.addValue("is_sync", tblIndex, state.getIsSync());
                tablet.addValue("is_plan", tblIndex, state.getIsPlan());
                tablet.addValue("feedback_point_type", tblIndex, state.getFeedbackPointType());
                tablet.addValue("feedback_id", tblIndex, state.getFeedbackId());
                tablet.addValue("is_deleted", tblIndex, state.getIsDeleted());
                tablet.addValue("employee_id", tblIndex, state.getEmployeeId());
                
                tblIndex++;
                
                if(tblIndex >= MAX_COUNT) {
                    try {
                        //每个工位批量插入一次数据
                        this.iotdbConfig.getSessionPool().insertAlignedTablet(tablet);
                        log.info("保存设备状态完成");
                        tablet.reset();
                        tblIndex = 0;
                    } catch (Exception e) {
                        log.error("保存固定点数据异常",e);
                    } 
                }
            }
            
            if(tblIndex > 0) {
                try {
                    //每个工位批量插入一次数据
                    this.iotdbConfig.getSessionPool().insertAlignedTablet(tablet);
                    log.info("保存设备状态完成2");
                    tablet.reset();
                    tblIndex = 0;
                } catch (Exception e) {
                    log.error("保存固定点数据异常",e);
                } 
            }
            
            /*
            for(int i=0;i<states.size();i++) {
                state = states.get(i);
                tablet.addTimestamp(i, state.getTime());
                tablet.addValue("workstation_id", i, state.getWorkstationId());
                tablet.addValue("value_collect", i, state.getValueCollect());
                
                tablet.addValue("calendar_code", i, state.getCalendarCode());
                tablet.addValue("factory_year", i, state.getFactoryYear());
                tablet.addValue("factory_month", i, state.getFactoryMonth());
                tablet.addValue("factory_week", i, state.getFactoryWeek());
                tablet.addValue("factory_date", i, state.getFactoryDate());
                tablet.addValue("shift_index", i, state.getShiftIndex());
                tablet.addValue("shift_time_type", i, state.getShiftTimeType());
                tablet.addValue("wcs", i, state.getWcs());
                tablet.addValue("rps", i, state.getRps());
                tablet.addValue("is_fix_point", i, state.getIsFixPoint());
                tablet.addValue("is_sync", i, state.getIsSync());
                tablet.addValue("is_plan", i, state.getIsPlan());
                tablet.addValue("feedback_point_type", i, state.getFeedbackPointType());
                tablet.addValue("feedback_id", i, state.getFeedbackId());
                tablet.addValue("is_deleted", i, state.getIsDeleted());
                tablet.addValue("employee_id", i, state.getEmployeeId());
            }*/
            
            
        }
    }
    
}