yangys
2024-11-13 04d53749b21921c9bceebe120d170c2ee6e533af
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
package com.qianwen.mdc.collect.service;
 
import java.util.List;
 
import org.apache.iotdb.rpc.IoTDBConnectionException;
import org.apache.iotdb.rpc.StatementExecutionException;
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;
 
 
/**
 * iotdb通用服务
 */
@Service
public class IotDBCommonService {
    private static final Logger logger = LoggerFactory.getLogger(IotDBCommonService.class);
    
    @Autowired
    private IotDBSessionConfig iotdbCfg;
 
    /**
     * 模板是否挂载到指定路径了
     * @param template 模板名称
     * @param path 设备路径
     * @return
     */
    public boolean isTemplateSetOnPath(String template,String path) {
        List<String> pathlist;
        try {
            pathlist = iotdbCfg.getSessionPool().showPathsTemplateSetOn(template);
            //logger.info("pathlist"+pathlist);
            return pathlist.contains(path);
        } catch (StatementExecutionException|IoTDBConnectionException e) {
            logger.error("获取模板使用错误",e);
        } 
        
        return false;
    }
    
    /**
     * 如果deviceId指定的路径没有挂载模板则执行挂载
     * @param template 模板名称
     * @param deviceId 设备路径
     */
    public void setTemmplateIfNotSet(String template,String deviceId) {
        if(!isTemplateSetOnPath(template, deviceId)) {
             try {
                 iotdbCfg.getSessionPool().setSchemaTemplate(template, deviceId);
             } catch (Exception e) {
                 logger.error("为deviceId设置模板错误,template="+template+",deviceId="+deviceId,e);
            }
         }
    }
}