81041
2019-11-27 7c0c5ab7ecded4e1573c2c4e12f117b780260f5d
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package com.socket;
 
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
 
import com.concentrator.Concentrator_State;
import com.fgkj.action.ServiceModel;
import com.fgkj.dao.ActionUtil;
import com.fgkj.dao.DBHelper;
import com.fgkj.dto.Battinf;
import com.google.gson.Gson;
import com.util.Com;
 
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
 
public class FBS9600S_DeviceService {
    public final static String TAG = "FBS9600S_DeviceService";
 
    public static final int MAXCONCENTRATORCOUNT = 16;                                  //最多连接的汇集器数量
    public static final String JS_INTERFACE_NAME = "DeviceService";                     //JS调用类名
 
    public static List<BattDataThread> allBattDatt;
    public DBHelper dbHelper;
    public WebView webView;
    public FBS9600S_DeviceService(WebView webView,DBHelper dbHelper){
        this.dbHelper = dbHelper;
        this.webView = webView;
        allBattDatt = new ArrayList<>();
        for(int i=0;i<MAXCONCENTRATORCOUNT;i++){
            BattDataThread thread = new BattDataThread(i+1,this.dbHelper,webView);
            allBattDatt.add(thread);
            thread.start();
        }
        allBattDatt.get(0).isInstall = true;
 
        List<Battinf> list = queryAllBattInf(dbHelper);                     //查询数据库中的所有电池组信息
 
        List<Battinf> temps = initBattInfo(allBattDatt,list);
 
        insertBattinf(dbHelper,temps);
    }
 
    //读取系统状态
    @JavascriptInterface
    public void readSystemState(String index){
        int battindex = Integer.parseInt(index);
        if(battindex > 0 && battindex <= MAXCONCENTRATORCOUNT){
            allBattDatt.get(battindex - 1).readSystemState("readSystemState");
        }
    }
 
    //读取系统参数
    @JavascriptInterface
    public void readSystemParam(String index){
        int battindex = Integer.parseInt(index);
        if(battindex>0 && battindex <= MAXCONCENTRATORCOUNT){
            allBattDatt.get(battindex-1).readSystemParam("readSystemParam");
        }
    }
 
    //设置系统参数
    @JavascriptInterface
    public void writeSystemParam(String index,String json){
        int battindex = Integer.parseInt(index);
        if(battindex>0 && battindex <= MAXCONCENTRATORCOUNT){
            allBattDatt.get(battindex-1).writeSystemParam("writeSystemParam",json);
        }
    }
 
    //读取电池信息
    @JavascriptInterface
    public void readBattMonInfo(String index){
        int battindex = Integer.parseInt(index);
        ServiceModel model = new ServiceModel();
        if(battindex>0 && battindex <= MAXCONCENTRATORCOUNT){
            Concentrator_State state = allBattDatt.get(battindex-1).state;
            if(allBattDatt.get(battindex-1).isOutTime){
                state.isOutTime = 1;
            }else{
                state.isOutTime = 0;
            }
            model.code = 1;
            model.data = state;
        }
        //Log.e(TAG, "readBattMonInfo: "+ActionUtil.getGson().toJson(model) );
        ActionUtil.SendCallDataToJS(JS_INTERFACE_NAME,"readBattMonInfo",model,webView, ActionUtil.getGson());
    }
 
    //启动内阻测试
    @JavascriptInterface
    public void startBattResTest(String index){
        int battindex = Integer.parseInt(index);
        if(battindex>0 && battindex <= MAXCONCENTRATORCOUNT){
            allBattDatt.get(battindex-1).startBattResTest(JS_INTERFACE_NAME+"startBattResTest");
        }
    }
 
    //停止内阻测试
    @JavascriptInterface
    public void stopBattResTest(String index){
        int battindex = Integer.parseInt(index);
        if(battindex>0 && battindex <= MAXCONCENTRATORCOUNT){
            allBattDatt.get(battindex-1).stopBattResTest(JS_INTERFACE_NAME+"stopBattResTest");
        }
    }
 
    //重启系统
    @JavascriptInterface
    public void reStartSystem(String index){
        int battindex = Integer.parseInt(index);
        if(battindex>0 && battindex <= MAXCONCENTRATORCOUNT){
            allBattDatt.get(battindex-1).reStartSystem(JS_INTERFACE_NAME+"reStartSystem");
        }
    }
 
    //读取电池组充电个数
    @JavascriptInterface
    public void readBattCharCount(){
        int count = 0;
        for(int i=0;i<allBattDatt.size();i++){
            if(allBattDatt.get(i).state.battstate == Concentrator_State.WORKSTATE_CHARG){
                count ++;
            }
        }
        ServiceModel model = new ServiceModel();
        model.code = 1;
        model.data = count;
        ActionUtil.SendCallDataToJS(JS_INTERFACE_NAME,"readBattCharCount",model,webView, ActionUtil.getGson());
    }
 
    //读取电池组充电电池信息
    @JavascriptInterface
    public void readBattCharInfo(){
        List list = new ArrayList();
        for(int i=0;i<allBattDatt.size();i++){
            if(allBattDatt.get(i).state.battstate == Concentrator_State.WORKSTATE_CHARG){
                list.add(allBattDatt.get(i));
            }
        }
        ServiceModel model = new ServiceModel();
        model.code = 1;
        model.data = list;
        ActionUtil.SendCallDataToJS(JS_INTERFACE_NAME,"readBattCharInfo",model,webView, ActionUtil.getGson());
    }
 
    //读取电池组放电个数
    @JavascriptInterface
    public void readBattDiscCount(){
        int count = 0;
        for(int i=0;i<allBattDatt.size();i++){
            if(allBattDatt.get(i).state.battstate == Concentrator_State.WORKSTATE_DISCH){
                count ++;
            }
        }
        ServiceModel model = new ServiceModel();
        model.code = 1;
        model.data = count;
        ActionUtil.SendCallDataToJS(JS_INTERFACE_NAME,"readBattDiscCount",model,webView, ActionUtil.getGson());
    }
 
    //读取电池组放电电池信息
    @JavascriptInterface
    public void readBattDiscInfo(){
        //Log.e(TAG, "readBattDiscInfo: ##############################"+allBattDatt.size());
        List<HashMap> list = new ArrayList();
        for(int i=0;i<allBattDatt.size();i++){
            if(allBattDatt.get(i).state.battstate == Concentrator_State.WORKSTATE_DISCH){
                HashMap map = new HashMap();
                map.put("state",allBattDatt.get(i).state);
                map.put("battinf",allBattDatt.get(i).battinf);
                list.add(map);
            }
            //Log.e(TAG, "readBattDiscInfo: "+allBattDatt.get(i).cloneRtData() );
        }
        ServiceModel model = new ServiceModel();
        model.code = 1;
        model.data = list;
        //Gson gson = ActionUtil.getGson(Com.DTF_YMD_h_m_s);
        //Log.e("#############", "readBattDiscInfo: "+gson.toJson(model));
        ActionUtil.SendCallDataToJS(JS_INTERFACE_NAME,"readBattDiscInfo",model,this.webView, ActionUtil.getGson());
    }
 
    /**
     * 匹配电池信息配置中的信息
     * @param battDatas
     * @param list
     */
    public List<Battinf> initBattInfo(List<BattDataThread> battDatas,List<Battinf> list){
        List<Battinf> battinfs = new ArrayList<>();
        for(int i = 0;i<battDatas.size();i++){
            BattDataThread battData =  battDatas.get(i);
            Battinf battinf = null;
            for(int k = 0;k<list.size();k++){
                if(list.get(k).getBattGroupId() == battData.battIndex){
                    battinf = list.get(k);
                }
            }
            if(battinf != null){
                battinfs.add(battinf);
                battData.isInstall = battinf.getStation_install() == 1;                               //是否是已安装
                battData.state.monCount = battinf.getMonCount();                                      //当前单体数目
            }else{
                battinf = new Battinf();
                battinf.setStation_install(battData.isInstall?1:0);
                battinf.setBattGroupId(i+1);
                battinf.setBattFloatCurrent(3);
                battinf.setBattGroupName1("电池组");
                battinf.setBattGroupName("电池组"+(i+1));
                battinf.setBattInUseDate(new Date());
                battinf.setBattProducer("");
                battinf.setMonCapStd(100);
                battinf.setMonCount(24);
                battinf.setMonVolStd(2);
                battinf.setMonResStd(0.2);
                battinf.setMonSerStd(5000);
                battinf.setMonTmpStd(24);
                battinfs.add(battinf);
            }
            battData.battinf = battinf;
        }
        return battinfs;
    }
 
    /**
     *  查询数据库中的所有电池组信息
     * @param dbHelper
     * @return
     */
    public List<Battinf> queryAllBattInf(DBHelper dbHelper){
        List<Battinf> battinfs = new ArrayList<>();
        String sql_str = "SELECT * FROM tb_battinf ORDER BY BattGroupId ASC";
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        Cursor rs = null;
        try {
            rs = db.rawQuery(sql_str,null);
            while(rs.moveToNext()){
                Battinf battinf = new Battinf();
                battinf.setNum(rs.getInt(rs.getColumnIndex("num")));
                battinf.setMonVolStd(rs.getDouble(rs.getColumnIndex("MonVolStd")));
                battinf.setStation_install(rs.getInt(rs.getColumnIndex("station_install")));
                battinf.setMonTmpStd(rs.getDouble(rs.getColumnIndex("MonTmpStd")));
                battinf.setMonSerStd(rs.getDouble(rs.getColumnIndex("MonSerStd")));
                battinf.setMonResStd(rs.getDouble(rs.getColumnIndex("MonResStd")));
                battinf.setMonCount(rs.getInt(rs.getColumnIndex("MonCount")));
                battinf.setMonCapStd(rs.getDouble(rs.getColumnIndex("MonCapStd")));
                battinf.setBattProducer(rs.getString(rs.getColumnIndex("BattProducer")));
                Date date= ActionUtil.sdfwithOut.parse( rs.getString(rs.getColumnIndex("BattInUseDate")));
                battinf.setBattInUseDate(date);
                battinf.setBattGroupName1(rs.getString(rs.getColumnIndex("BattGroupName1")));
                battinf.setBattGroupName(rs.getString(rs.getColumnIndex("BattGroupName")));
                battinf.setBattFloatCurrent(rs.getDouble(rs.getColumnIndex("BattFloatCurrent")));
                battinf.setBattGroupId(rs.getInt(rs.getColumnIndex("BattGroupId")));
                battinfs.add(battinf);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(rs != null){
                    rs.close();
                }
                if(db != null){
                    db.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return battinfs;
    }
 
    /**
     * 插入电池组信息
     * @param dbHelper
     * @param list
     */
    public void insertBattinf(DBHelper dbHelper,List<Battinf> list){
        boolean start = true;
        String sql_str_start = " INSERT INTO tb_battinf(BattGroupId,BattGroupName,BattGroupName1,BattFloatCurrent,MonCount,MonCapStd,MonVolStd,MonResStd,MonSerStd,MonTmpStd,BattProducer,BattInUseDate,station_install) VALUES ";
        for(int i = 0;i<list.size();i++){
            if(list.get(i).getNum() == 0){
                Battinf temp = list.get(i);
                //需要录入的电池组
                if(start){
                    start = false;
                }else{
                    sql_str_start += ",";
                }
                sql_str_start += "("+temp.getBattGroupId()+",'"+temp.getBattGroupName()+"','"+temp.getBattGroupName1()+"',"+temp.getBattFloatCurrent()+","+temp.getMonCount()+","+temp.getMonCapStd()+","+temp.getMonVolStd()+","+temp.getMonResStd()+","+temp.getMonSerStd()+","+temp.getMonTmpStd()+",'"+temp.getBattProducer()+"','"+ Com.getDateTimeFormat(temp.getBattInUseDate(),Com.DTF_YMDhms)+"',"+temp.getStation_install()+")";
            }
        }
        if(!start){
            SQLiteDatabase db = null;
            try {
                db = dbHelper.getWritableDatabase();
                db.execSQL(sql_str_start);
            } catch (SQLException e) {
                e.printStackTrace();
            } finally{
                if(db != null){
                    db.close();
                }
            }
        }
    }
}