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.alarms.BattAlarmCheckThread;
|
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);
|
|
|
//电池告警线程
|
BattAlarmCheckThread battAlarmCheckThread = new BattAlarmCheckThread(dbHelper,allBattDatt);
|
battAlarmCheckThread.start();
|
|
}
|
|
//读取系统状态
|
@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();
|
}
|
}
|
}
|
}
|
}
|