wjc
2025-01-07 facf399aa49ac37def7ea00221c5bbd056765df2
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
package com.hdl.photovoltaic.other;
 
import android.text.TextUtils;
 
import com.hdl.linkpm.sdk.core.exception.HDLException;
import com.hdl.photovoltaic.config.UserConfigManage;
import com.hdl.photovoltaic.internet.HttpClient;
import com.hdl.photovoltaic.listener.CloudCallBeak;
 
import java.util.HashMap;
import java.util.Map;
 
/**
 * 本地json缓存逻辑
 */
public class HdlESLocalJsonLogic {
    private static volatile HdlESLocalJsonLogic sHdlESLocalJsonLogic;
 
    /**
     * 获取当前对象
     *
     * @return HdlAccountLogic
     */
    public static synchronized HdlESLocalJsonLogic getInstance() {
        if (sHdlESLocalJsonLogic == null) {
            synchronized (HdlESLocalJsonLogic.class) {
                if (sHdlESLocalJsonLogic == null) {
                    sHdlESLocalJsonLogic = new HdlESLocalJsonLogic();
                }
            }
 
        }
        return sHdlESLocalJsonLogic;
    }
 
 
    /**
     * 【逆变器】设备型号
     */
    public static final String deviceModel_ME_GSE_S5K = "ME-GSE-S5K";
    /**
     * 【负载控制中心】设备型号
     */
    public static final String deviceModel_ME_GSE_SPG63A = "ME-GSE-SPG63A";
    /**
     * 【BMS】设备型号
     */
    public static final String deviceModel_ME_GSE_BMS15K = "ME-GSE-BMS15K";
    /**
     * 【电池包】设备型号
     */
    public static final String deviceModel_ME_GSE_B5K = "ME-GSE-B5K";
 
    /**
     * 创建电站-逆变器参数配置
     */
    public static final String hdl_inv_ecu_quick = "https://hdl-hz-dev.oss-cn-hangzhou.aliyuncs.com/ecu/currentLanguage/inv/deviceModel/modbus_hdl_inv_ecu_quick_v1.5.1.json";
    /**
     * 创建电站-负载控制中心参数配置
     */
    public static final String hdl_lc_ecu_quick = "https://hdl-hz-dev.oss-cn-hangzhou.aliyuncs.com/ecu/currentLanguage/lc/deviceModel/modbus_hdl_lc_ecu_quick_v1.5.0.json";
    /**
     * 逆变器参数配置(config)
     */
    public static final String hdl_inv_ecu = "https://hdl-hz-dev.oss-cn-hangzhou.aliyuncs.com/ecu/currentLanguage/inv/deviceModel/modbus_hdl_inv_ecu_v1.5.1.json";
    /**
     * 逆变器详情-pv
     */
    public static final String hdl_inv_ecu_info_pv = "https://hdl-hz-dev.oss-cn-hangzhou.aliyuncs.com/ecu/currentLanguage/inv/deviceModel/info/modbus_hdl_inv_ecu_info_pv_v1.4.2.json";
    /**
     * 逆变器详情-输出
     */
    public static final String hdl_inv_ecu_info_output = "https://hdl-hz-dev.oss-cn-hangzhou.aliyuncs.com/ecu/currentLanguage/inv/deviceModel/info/modbus_hdl_inv_ecu_info_output_v1.4.2.json";
    /**
     * 逆变器详情-item项
     */
    public static final String hdl_inv_ecu_info_item = "https://hdl-hz-dev.oss-cn-hangzhou.aliyuncs.com/ecu/currentLanguage/inv/deviceModel/info/modbus_hdl_inv_ecu_info_item_v1.5.1.json";
    /**
     * 获取负载控制中心
     */
    public static final String hdl_lc_ecu_info_item = "https://hdl-hz-dev.oss-cn-hangzhou.aliyuncs.com/ecu/currentLanguage/lc/deviceModel/modbus_hdl_lc_ecu_info_item_v1.5.0.json";
    /**
     * 获取BMS信息
     */
    public static final String hdl_bms_ecu = "https://hdl-hz-dev.oss-cn-hangzhou.aliyuncs.com/ecu/currentLanguage/bms/deviceModel/modbus_hdl_bms_ecu_v1.4.2.json";
    /**
     * 电池包信息
     */
    public static final String hdl_battery_ecu = "https://hdl-hz-dev.oss-cn-hangzhou.aliyuncs.com/ecu/currentLanguage/battery/deviceModel/modbus_hdl_battery_ecu_v1.4.2.json";
 
    /**
     * 电池包-电芯信息
     */
    public static final String hdl_battery_ecu_cell = "https://hdl-hz-dev.oss-cn-hangzhou.aliyuncs.com/ecu/currentLanguage/battery/deviceModel/modbus_hdl_battery_ecu_cell_v1.4.2.json";
 
    /**
     * 逆变器详情-信号点
     */
    public static final String hdl_inv_ecu_info_signal = "https://hdl-hz-dev.oss-cn-hangzhou.aliyuncs.com/ecu/currentLanguage/inv/deviceModel/info/modbus_hdl_inv_ecu_info_signal_v1.4.1.json";
 
    /**
     * 获取逆变器所有json中文
     */
    public static final String hdl_inv_ecu_all_zh = "https://hdl-hz-dev.oss-cn-hangzhou.aliyuncs.com/ecu/zh/all_v1.5.1.json";
    /**
     * 获取逆变器所有json英文
     */
    public static final String hdl_inv_ecu_all_en = "https://hdl-hz-dev.oss-cn-hangzhou.aliyuncs.com/ecu/en/all_v1.5.1.json";
 
    /**
     * 初始化脚本url
     *
     * @return map
     */
    private Map<String, String> getUrlMap() {
        //注意(逆变器:ME-GSE-S5K,负载中心:ME-GSE-SPG63A,bms:ME-GSE-BMS15K,电池包:ME-GSE-B5K)设备型号不一样
        Map<String, String> map = new HashMap<>();
        map.put(this.getNewUrl(hdl_lc_ecu_info_item, deviceModel_ME_GSE_SPG63A), deviceModel_ME_GSE_SPG63A);//获取负载控制中心
        map.put(this.getNewUrl(hdl_lc_ecu_quick, deviceModel_ME_GSE_SPG63A), deviceModel_ME_GSE_SPG63A); //创建电站-负载控制中心参数配置
        map.put(this.getNewUrl(hdl_inv_ecu_quick, deviceModel_ME_GSE_S5K), deviceModel_ME_GSE_S5K);//创建电站-逆变器参数配置
        map.put(this.getNewUrl(hdl_inv_ecu, deviceModel_ME_GSE_S5K), deviceModel_ME_GSE_S5K);//逆变器参数配置
        map.put(this.getNewUrl(hdl_inv_ecu_info_pv, deviceModel_ME_GSE_S5K), deviceModel_ME_GSE_S5K);// 逆变器详情-pv
        map.put(this.getNewUrl(hdl_inv_ecu_info_output, deviceModel_ME_GSE_S5K), deviceModel_ME_GSE_S5K);//逆变器详情-输出
        map.put(this.getNewUrl(hdl_inv_ecu_info_item, deviceModel_ME_GSE_S5K), deviceModel_ME_GSE_S5K);//逆变器详情-item项
        map.put(this.getNewUrl(hdl_inv_ecu_info_signal, deviceModel_ME_GSE_S5K), deviceModel_ME_GSE_S5K);//逆变器详情-信号点
        map.put(this.getNewUrl(hdl_bms_ecu, deviceModel_ME_GSE_BMS15K), deviceModel_ME_GSE_BMS15K);//获取BMS信息
        map.put(this.getNewUrl(hdl_battery_ecu, deviceModel_ME_GSE_B5K), deviceModel_ME_GSE_B5K);//电池包信息
        map.put(this.getNewUrl(hdl_battery_ecu_cell, deviceModel_ME_GSE_B5K), deviceModel_ME_GSE_B5K);//电池包-电芯信息
        return map;
    }
 
    /**
     * 向云端获取Json【所有】脚本
     */
    public void getAllHdlESLocalJson() {
        for (Map.Entry<String, String> entry : getUrlMap().entrySet()) {
            this.getHdlESLocalJson(entry.getKey(), entry.getValue(), null);
        }
    }
 
 
    /**
     * 向云端获取Json【单个】脚本
     *
     * @param url         地址
     * @param deviceModel 设备型号
     */
    public void getHdlESLocalJson(String url, String deviceModel, CloudCallBeak<String> callBeak) {
        HttpClient.getInstance().requestJsonHttpGet(url, new CloudCallBeak<String>() {
            @Override
            public void onSuccess(String str) {
                saveJson(url, deviceModel, str);
                HdlLogLogic.print("获取云端脚本---" + url + "\r\n" + str, false);
                if (callBeak != null) {
                    callBeak.onSuccess(str);
                }
            }
 
            @Override
            public void onFailure(HDLException e) {
                HdlLogLogic.print(e.getMessage(), e.getCode(), false);
                if (callBeak != null) {
                    callBeak.onFailure(e);
                }
            }
        });
    }
 
    /**
     * 读取本地json数据
     *
     * @param url         地址
     * @param deviceModel 设备型号
     */
    public String readHdlESLocalJson(String url, String deviceModel) {
        return this.readJson(url, deviceModel);
    }
 
    /**
     * 处理url返回新的url
     *
     * @param url         地址
     * @param deviceModel 设备类型
     * @return 返回地址
     */
    private String getNewUrl(String url, String deviceModel) {
        return url.replace("currentLanguage", UserConfigManage.getInstance().getCurrentAppLanguage()).replace("deviceModel", deviceModel);
    }
 
 
    /**
     * 保存json数据
     *
     * @param fileUrlOrFileName 地址或者文件名
     * @param deviceModel       设备型号(逆变器,负载中心,bms,电池包)
     * @param json              内容
     */
    private void saveJson(String fileUrlOrFileName, String deviceModel, String json) {
        try {
            if (TextUtils.isEmpty(json)) {
                return;
            }
            String fileName = fileUrlOrFileName;
            if (fileUrlOrFileName.contains("/")) {
                //表示链接,截取最后一个索引表示文件名
                fileName = fileUrlOrFileName.split("/")[fileUrlOrFileName.split("/").length - 1];
            }
            if (TextUtils.isEmpty(fileName)) {
                return;
            }
            //英文文件路径
            String fullPath = HdlFileLogic.getInstance().getHdlESLocalJsonEnFilePath(deviceModel, fileName);
            if (UserConfigManage.getInstance().isZh()) {
                //中文文件路径
                fullPath = HdlFileLogic.getInstance().getHdlESLocalJsonZhFilePath(deviceModel, fileName);
            }
 
            HdlFileLogic.getInstance().writeFile(fullPath, json);
        } catch (Exception ignored) {
        }
    }
 
    /**
     * 读取json数据
     *
     * @param fileUrlOrFileName 地址或者文件名
     * @param deviceModel       设备型号(逆变器,负载中心,bms,电池包)
     */
    private String readJson(String fileUrlOrFileName, String deviceModel) {
        try {
            String fileName = fileUrlOrFileName;
            if (fileUrlOrFileName.contains("/")) {
                //表示链接,截取最后一个索引表示文件名
                fileName = fileUrlOrFileName.split("/")[fileUrlOrFileName.split("/").length - 1];
            }
            if (TextUtils.isEmpty(fileName)) {
                return "";
            }
            //英文文件路径
            String fullPath = HdlFileLogic.getInstance().getHdlESLocalJsonEnFilePath(deviceModel, fileName);
            if (UserConfigManage.getInstance().isZh()) {
                //中文文件路径
                fullPath = HdlFileLogic.getInstance().getHdlESLocalJsonZhFilePath(deviceModel, fileName);
            }
            return HdlFileLogic.getInstance().readFile(fullPath);
        } catch (Exception ignored) {
            return "";
        }
    }
 
}