yangys
2024-09-03 52ff3bda72646a532c3297e4843bc4d5aee8b949
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package com.qianwen.mdc.collect.service;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;
 
import org.apache.iotdb.isession.pool.SessionDataSetWrapper;
import org.apache.iotdb.rpc.IoTDBConnectionException;
import org.apache.iotdb.rpc.StatementExecutionException;
import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
import org.apache.iotdb.tsfile.read.common.RowRecord;
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.alibaba.fastjson.JSONObject;
import com.qianwen.mdc.collect.config.IotDBSessionConfig;
import com.qianwen.mdc.collect.constants.IOTDBConstant;
import com.qianwen.mdc.collect.domain.TelemetryData;
import com.qianwen.mdc.collect.domain.TelemetryDataItem;
import com.qianwen.mdc.collect.utils.redis.RedisUtil;
 
/**
 * 采集数据处理入库
 */
@Service
public class CollectDataService {
    private static final Logger log = LoggerFactory.getLogger(CollectDataService.class);
    
    //private String DB_PREFIX = "root.f2.";
    private static final Map<Integer, String> PROCESS_PARAM_MAP = new HashMap<>();
    @Autowired
    private IotDBSessionConfig iotdbConfig;
    @Autowired
    private IotDBCommonService iotDBCommonService;
    
    private static String TEMPLATE_NAME = "process_param";
 
    static {
        PROCESS_PARAM_MAP.put(1, "STATE");
        PROCESS_PARAM_MAP.put(2, "OUTPUT");
        PROCESS_PARAM_MAP.put(3, "ALARM");
        PROCESS_PARAM_MAP.put(4, "PROGRAMNUM");
        PROCESS_PARAM_MAP.put(5, "OUTPUT");
        PROCESS_PARAM_MAP.put(6, "ALARM");
        PROCESS_PARAM_MAP.put(7, "ALARM");
    }
 
    
    public void handleCollectData(List<TelemetryData> telemetryDataList) {
 
        for (TelemetryData dt : telemetryDataList) {
            handleOneWorkstation(dt);
        }
 
    }
 
    /**
     * 处理一个工位的数据解析入库
     * @param dt
     */
    void handleOneWorkstation(TelemetryData dt) {
        String deviceId;// = DB_PREFIX+TEMPLATE_NAME + "_" + dt.getWorkstationId();
 
        // 挂载模板
        //iotDBCommonService.setTemmplateIsNotSet(TEMPLATE_NAME, deviceId);
 
        List<MeasurementSchema> schemas = new ArrayList<>();
        
        schemas.add(new MeasurementSchema("workstation_id", TSDataType.INT64));
        schemas.add(new MeasurementSchema("n", TSDataType.TEXT));
        schemas.add(new MeasurementSchema("v", TSDataType.TEXT));
        
        
        int rowIndex = 0;
        
        Map<String, List<TypedTelemetryData>> processParamsMap = parseTelemetryToTypedMapList(dt);
        
        
        String[] nameArr = processParamsMap.keySet().toArray(new String[0]);
        String name;
        for(int i=0;i<nameArr.length;i++) {
            name = nameArr[i];
            List<TypedTelemetryData> typeList = processParamsMap.get(name);
            
            deviceId = generateDeviceId(dt.getWorkstationId(),name);
            //System.out.println("deivcdid="+deviceId);
            iotDBCommonService.setTemmplateIfNotSet(TEMPLATE_NAME, deviceId);
            Tablet tablet = new Tablet(deviceId, schemas);
            for(TypedTelemetryData tdata : typeList) {
                rowIndex = tablet.rowSize++;
                tablet.addTimestamp(rowIndex, tdata.getTime());
                tablet.addValue("workstation_id",rowIndex,dt.getWorkstationId());
                tablet.addValue("n",rowIndex,tdata.getName());
                tablet.addValue("v",rowIndex,tdata.getValue());
            }
            
            try {
                iotdbConfig.getSessionPool().insertAlignedTablet(tablet);
                
                updateLastParam(dt.getWorkstationId(),typeList);
            } catch (Exception e) {
                log.error("IOTDB入库失败",e);
                e.printStackTrace();
            }
        }
        
 
    }
    
    /**
     * 填充最新参数的数据
     * @param typeList
     * @throws StatementExecutionException 
     * @throws IoTDBConnectionException 
     */
    void updateLastParam(long workstationId,List<TypedTelemetryData> typeList) throws IoTDBConnectionException, StatementExecutionException {
        if(typeList.isEmpty()){
            return;
        }
        long updateTime = typeList.get(0).getTime();
        List<MeasurementSchema> schemas = new ArrayList<>();
        
        schemas.add(new MeasurementSchema("update_time", TSDataType.INT64));
        schemas.add(new MeasurementSchema("workstation_id", TSDataType.INT64));
        schemas.add(new MeasurementSchema("param_json", TSDataType.TEXT));
        
        Tablet tablet = new Tablet("root.f2.last_process_param", schemas);
        for(TypedTelemetryData tdata: typeList) {
            
        }
    
        
        String sql = "select update_time,workstation_id,param_json from root.f2.last_process_param where workstation_id="+workstationId;
        SessionDataSetWrapper dsw = iotdbConfig.getSessionPool().executeQueryStatement(sql);
        
        if(dsw.hasNext()) {
            RowRecord rec = dsw.next();
            long time = rec.getTimestamp();
            
            String paramJsonStr = rec.getFields().get(2).getStringValue();
            
            tablet.rowSize = 1;
            tablet.addTimestamp(0, time);
            tablet.addValue("update_time", 0, updateTime);
            tablet.addValue("workstation_id", 0, workstationId);
            JSONObject paramObj = JSONObject.parseObject(paramJsonStr);
            for(TypedTelemetryData tdata: typeList) {
                
                if(paramObj.containsKey(tdata.getName())) {
                    JSONObject itemObj = paramObj.getJSONObject(tdata.getName());
                    itemObj.put("value", tdata.getValue());
                    itemObj.put("time", tdata.getTime());//采集时间
                    paramObj.put(tdata.getName(), itemObj);
                }else {
                    JSONObject itemObj = new JSONObject();
                    itemObj.put("value", tdata.getValue());
                    itemObj.put("time", tdata.getTime());//采集时间
                    paramObj.put(tdata.getName(), itemObj);
                    
                }
            }
            tablet.addValue("param_json", 0, paramObj.toJSONString());
            
            this.iotdbConfig.getSessionPool().insertAlignedTablet(tablet);
 
        }else {
            //没数据,新加入一条
            tablet.rowSize = 1;
            
            tablet.addTimestamp(0, updateTime);
            tablet.addValue("update_time", 0, updateTime);
            tablet.addValue("workstation_id", 0, workstationId);
            
            JSONObject paramObj = new JSONObject();
            for(TypedTelemetryData tdata: typeList) {
                JSONObject itemObj = new JSONObject();
                itemObj.put("value", tdata.getValue());
                itemObj.put("time", tdata.getTime());//采集时间
                paramObj.put(tdata.getName(), itemObj);
            }
 
            tablet.addValue("param_json", 0,paramObj.toJSONString());
            this.iotdbConfig.getSessionPool().insertAlignedTablet(tablet);
        }
        
    }
    
    /**
     * 解析数据,形成name - value对类型的数据列表,并用name进行分组
     * @param dt
     * @return 按name分组后的数据
     */
    Map<String, List<TypedTelemetryData>> parseTelemetryToTypedMapList(TelemetryData dt){
        List<TypedTelemetryData> list = new ArrayList<>();
        
        String propertyName;
        for (TelemetryDataItem dataItem : dt.getDataItems()) {
 
            for (Map<String, String> point : dataItem.getDataPoints()) {
                
                String[] keys = point.keySet().toArray(new String[0]);
                for(int i=0;i<keys.length;i++) {
                    TypedTelemetryData tpData = new TypedTelemetryData();
                    propertyName = keys[i];
                    tpData.setTime(dataItem.getTime());
                    tpData.setName(propertyName);
                    tpData.setValue(point.get(propertyName));
                    
                    list.add(tpData);
                }
          
            }
            
        }
        
        Map<String, List<TypedTelemetryData>> map = list.stream().collect(Collectors.groupingBy(TypedTelemetryData::getName));
        return map;
    }
    
    String generateDeviceId(long workstationId,String propertyName) {
        return IOTDBConstant.DB_PREFIX+TEMPLATE_NAME + "_" + workstationId+"_"+propertyName;
    }
}
 
/*
 * 分类的数据
 */
class TypedTelemetryData{
    private long time;
    private String name;
    private String value;
    public long getTime() {
        return time;
    }
    public void setTime(long time) {
        this.time = time;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getValue() {
        return value;
    }
    public void setValue(String value) {
        this.value = value;
    }
    @Override
    public String toString() {
        return "TypedTelemetryData [time=" + time + ", name=" + name + ", value=" + value + "]";
    }
    
    
}