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
package com.socket;
 
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.drm.DrmStore;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
 
import com.fgkj.action.ServiceModel;
import com.fgkj.dao.ActionUtil;
import com.fgkj.dao.DBHelper;
import com.fgkj.dto.Battinf;
import com.fgkj.impl.BattinfImpl;
import com.util.Com;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
public class FBS9600S_DeviceService {
    public static final int MAXCONCENTRATORCOUNT = 2;                                  //最多连接的汇集器数量
    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){
            model.code = 1;
            model.data = allBattDatt.get(battindex-1).state;
        }
        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("startBattResTest");
        }
    }
 
    //启动内阻测试
    @JavascriptInterface
    public void stopBattResTest(String index){
        int battindex = Integer.parseInt(index);
        if(battindex>0 && battindex <= MAXCONCENTRATORCOUNT){
            allBattDatt.get(battindex-1).stopBattResTest("stopBattResTest");
        }
    }
 
    //启动内阻测试
    @JavascriptInterface
    public void reStartSystem(String index){
        int battindex = Integer.parseInt(index);
        if(battindex>0 && battindex <= MAXCONCENTRATORCOUNT){
            allBattDatt.get(battindex-1).reStartSystem("reStartSystem");
        }
    }
 
 
    /**
     * 匹配电池信息配置中的信息
     * @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(i);
                }
            }
            if(battinf != null){
                battinfs.add(battinf);
                battData.isInstall = battinf.getStation_install() == 1;                               //是否是已安装
                battData.state.monCount = battinf.getMonCount();                                      //当前单体数目
            }else{
                Battinf temp = new Battinf();
                temp.setStation_install(battData.isInstall?1:0);
                temp.setBattGroupId(i+1);
                temp.setBattFloatCurrent(3);
                temp.setBattGroupName1("电池组");
                temp.setBattGroupName("电池组"+(i+1));
                temp.setBattInUseDate(new Date());
                temp.setBattProducer("");
                temp.setMonCapStd(100);
                temp.setMonCount(24);
                temp.setMonVolStd(2);
                temp.setMonResStd(0.2);
                temp.setMonSerStd(5000);
                temp.setMonTmpStd(24);
                battinfs.add(temp);
            }
        }
        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();
                }
            }
        }
    }
}