Commit 1b2f9938 authored by chenyuling's avatar chenyuling

添加多台设备IP

parent 642df084
......@@ -54,7 +54,7 @@
android:name=".uis.MainActivity"
android:configChanges="keyboardHidden|orientation"
android:exported="true"
android:windowSoftInputMode="stateHidden|adjustPan">
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
......
package com.srthinker.statinfo.adapter;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;
......@@ -22,7 +21,6 @@ public class EnterAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
public EnterAdapter(Context context, List<PersonEntity> pData){
this.context = context;
this.personEntities = pData;
Log.i(TAG, "EnterAdapter: 更新列表,size="+ pData.size());
}
@NonNull
@Override
......@@ -35,9 +33,9 @@ public class EnterAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
ItemHolder itemHolder = (ItemHolder) holder;
PersonEntity peopleBean = personEntities.get(position);
Log.i(TAG, "onBindViewHolder: peopleBeans.size="+ personEntities.size());
// Log.i(TAG, "onBindViewHolder: peopleBeans.size="+ personEntities.size());
if (peopleBean!=null){
Log.i(TAG, "onBindViewHolder: name="+peopleBean.getPerson_name());
// Log.i(TAG, "onBindViewHolder: name="+peopleBean.getPerson_name());
itemHolder.mBinding.tvInfo.setText(peopleBean.getTimestamp()+" "+peopleBean.getPerson_name());
}
}
......@@ -58,10 +56,14 @@ public class EnterAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
}
public void setDataList(List<PersonEntity> peopleBeans){
Log.i(TAG, "setDataList: 更新列表,size="+peopleBeans.size());
//Log.i(TAG, "setDataList: 更新列表,size="+peopleBeans.size());
// this.peopleBeans=new ArrayList<>();
this.personEntities.clear();
this.personEntities = peopleBeans;
this.personEntities.addAll(peopleBeans);
this.notifyDataSetChanged();
}
public List<PersonEntity> getDataList(){
return personEntities;
}
}
......@@ -12,8 +12,9 @@ import com.srthinker.statinfo.databinding.ItemLeaveBinding;
import java.util.List;
public class LeaveAdapter extends RecyclerView.Adapter {
public class LeaveAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final String TAG = "LeaveAdapter";
private final Context context;
private List<PersonEntity> peopleBeans;
......@@ -42,7 +43,7 @@ public class LeaveAdapter extends RecyclerView.Adapter {
return peopleBeans!=null&&peopleBeans.size()>0?peopleBeans.size():0;
}
private class ItemHolder extends RecyclerView.ViewHolder {
private static class ItemHolder extends RecyclerView.ViewHolder {
private final com.srthinker.statinfo.databinding.ItemLeaveBinding mBinding;
......@@ -52,8 +53,11 @@ public class LeaveAdapter extends RecyclerView.Adapter {
}
}
public void setDateList(List<PersonEntity> peopleBeans){
this.peopleBeans = peopleBeans;
notifyDataSetChanged();
public void setDataList(List<PersonEntity> peopleBeans){
//this.peopleBeans = peopleBeans;
this.peopleBeans.clear();
this.peopleBeans.addAll(peopleBeans);
//Log.i(TAG, "setDataList: size="+this.peopleBeans.size());
this.notifyDataSetChanged();
}
}
......@@ -6,5 +6,5 @@ import java.util.List;
public interface UpperDevicesCallback {
void getAllPassInfo(List<PersonEntity> passAllEntities, int type);
void onAllConnectStatus(boolean hasDisConnect,int type);
void onAllConnectStatus(boolean hasConnect,int type);
}
......@@ -75,6 +75,7 @@ public class ApiManager {
public void start(){
isRunning = true;
isFirstStatus = true;
lastConnect = false;
handler.post(authThreadRunnable);
//ThreadPool.getInstance().getThreadPoolExecutor().execute(passRecordRunnable);
handler.post(passThreadRunnable);
......@@ -82,68 +83,56 @@ public class ApiManager {
private Runnable authRunnable = new Runnable() {
@Override
public void run() {
//进行设备登录
// LoginResp loginResp = authApi.login("admin", "a12345");
LoginResp loginResp = null;
//boolean authConnect = false;
try {
loginResp = authApi.login(username, password);
}catch (ApiClientException e2){
} catch (Exception e) {
e.printStackTrace();
if (lastConnect){
if (callback != null) {
callback.onConnectStatus(false,type);
lastConnect = false;
}
}
Log.i(TAG, "run: authRunnable抛异常:ip="+serverIp);
if (isRunning){
handler.postDelayed(authThreadRunnable,2000);
}
}finally {
if (isFirstStatus){
if (callback != null) {
callback.onConnectStatus(false,type);
lastConnect = false;
}
isFirstStatus = false;
}
}
if (loginResp != null) {
if(loginResp.getStatus() != 200) {
if (lastConnect) {
if (callback != null) {
callback.onConnectStatus(false,type);
lastConnect = false;
}
}
updateLastConnect(false);
//登录出错,打印错误信息
System.out.println(JSONObject.toJSONString(loginResp));
return;
}
sessionId = loginResp.getSessionId();
if (!lastConnect){
if (callback!=null){
callback.onConnectStatus(true,type);
//只要连接成功都传避免出现意外,因为鉴权成功后也只访问一次
if (callback != null) {
lastConnect = true;
callback.onConnectStatus(true, type);
}
}
//updateLastConnect(true);
Log.i(TAG, "run: sessionId==>"+ sessionId);
}
}catch (ApiClientException e2){
updateLastConnect(false);
} catch (Exception e) {
e.printStackTrace();
updateLastConnect(false);
Log.i(TAG, "run: authRunnable抛异常:ip="+serverIp);
if (isRunning){
handler.postDelayed(authThreadRunnable,2000);
}
}
}
};
//第一次和状态有变化的时候传
private void updateLastConnect(boolean newConnectStatus) {
if (lastConnect != newConnectStatus || isFirstStatus) {
lastConnect = newConnectStatus;
if (callback != null) {
callback.onConnectStatus(lastConnect, type);
}
isFirstStatus = false;
}
}
public void queryPersons(){
ThreadPool.getInstance().getThreadPoolExecutor().execute(() -> {
totalPersonLists = new ArrayList<>();
......@@ -212,18 +201,6 @@ public class ApiManager {
PassQueryBean passQueryResp = null;
try {
passQueryResp = passApi.query(max_pass_limit,pass_offset,"desc", begin_time, end_time);
} catch (ApiClientException e2){
} catch (Exception e) {
Log.i(TAG, "run: passRecordRunnable抛异常");
e.printStackTrace();
if (lastConnect) {
if (callback != null) {
callback.onConnectStatus(false,type);
lastConnect = false;
}
}
}
if (passQueryResp!=null){
//Log.i(TAG, "run: passQueryResp="+passQueryResp.toString());
if (passQueryResp.getPaging()==null&&passQueryResp.getData()==null&&passQueryResp.getQuery_id()==null){
......@@ -249,12 +226,7 @@ public class ApiManager {
passBeans.add(passBean);
}
totalPassLists.addAll(passBeans);
if (!lastConnect){
if (callback != null) {
callback.onConnectStatus(true,type);
lastConnect = true;
}
}
updateLastConnect(true);
}
PassQueryBean.PagingBean paging = passQueryResp.getPaging();
......@@ -267,6 +239,14 @@ public class ApiManager {
}
}
}
} catch (ApiClientException e2){
updateLastConnect(false);
} catch (Exception e) {
Log.i(TAG, "run: passRecordRunnable抛异常");
updateLastConnect(false);
e.printStackTrace();
}
}
}
......
......@@ -18,7 +18,6 @@ import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class ApiQuest {
......@@ -57,14 +56,13 @@ public class ApiQuest {
String username = config.getUsername();
String password = config.getPassword();
String serverIp = config.getServerIp();
boolean isConnect = config.isConnect();
if (i==0&&type==ENTER_TYPE){
oneEnterIp = serverIp;
}
if (type==ENTER_TYPE){
startEnterIp(username,password,serverIp);
}else if (type==LEAVE_TYPE){
startLeaveIp(username,password,serverIp,isConnect);
startLeaveIp(username,password,serverIp);
}
}
......@@ -89,11 +87,17 @@ public class ApiQuest {
@Override
public void getPassInfo(List<PersonEntity> passEntities, int type) {
allEnterPass.addAll(passEntities);
Set<PersonEntity> mergeList = new LinkedHashSet<>(allEnterPass);
for (PersonEntity enterPass : allEnterPass) {
Log.i(TAG, "去重前getPassInfo: enterPass.time="+enterPass.getTimestamp()+",name="+enterPass.getPerson_name());
}
LinkedHashSet<PersonEntity> mergeList = new LinkedHashSet<>(allEnterPass);
allEnterPass = new ArrayList<>(mergeList);
for (PersonEntity enterPass : allEnterPass) {
Log.i(TAG, "getPassInfo: name="+enterPass.getPerson_name());
Log.i(TAG, "去重后getPassInfo: enterPass.time="+enterPass.getTimestamp()+",name="+enterPass.getPerson_name());
}
/*for (PersonEntity enterPass : allEnterPass) {
Log.i(TAG, "getPassInfo: name="+enterPass.getPerson_name());
}*/
if (upperDevicesCallback != null) {
upperDevicesCallback.getAllPassInfo(allEnterPass,type);
}
......@@ -103,15 +107,22 @@ public class ApiQuest {
public void onConnectStatus(boolean isConnect, int type) {
Log.i(TAG, "onConnectStatus: 接收到的连接状态:isConnect="+isConnect+",ip="+ip);
ThreadPool.getInstance().getThreadPoolExecutor().execute(() -> {
//修改状态
List<ConfigEntity> configEntities = ConfigHelper.getInstance().queryByTypeIp(type, ip);
synchronized (configEntities){
for (ConfigEntity configEntity : configEntities) {
configEntity.setConnect(isConnect);
}
//Log.i(TAG, "onConnectStatus: 修改后:ip"+configEntity.getServerIp()+",isConnect="+configEntity.isConnect());
//更新ip接口状态
ConfigHelper.getInstance().update(configEntities);
List<ConfigEntity> enterList = ConfigHelper.getInstance().queryByType(ENTER_TYPE);
ConfigHelper.getInstance().update(configEntity);
}
}
//获取最新状态
List<ConfigEntity> enterList = ConfigHelper.getInstance().queryByType(type);
isEnterAllConnect = true;
synchronized (enterList){
for (ConfigEntity config : enterList) {
//Log.i(TAG, "onConnectStatus: 数据库里面的数据:ip="+config.getServerIp()+",isConnect="+config.isConnect());
boolean connect = config.isConnect();
if (!connect){
isEnterAllConnect = false;
......@@ -121,12 +132,13 @@ public class ApiQuest {
if (upperDevicesCallback != null) {
upperDevicesCallback.onAllConnectStatus(isEnterAllConnect,type);
}
}
});
}
});
}
private void startLeaveIp(String username, String password, String ip,boolean isLastConnect) {
private void startLeaveIp(String username, String password, String ip) {
//如果之前有过则停掉
/*if (leaveMgrHashMap.get(ip)!=null){
ApiManager apiManager = leaveMgrHashMap.get(ip);
......@@ -141,29 +153,52 @@ public class ApiQuest {
leaveMgr.setOnDeviceCallback(new DeviceCallback() {
@Override
public void getPassInfo(List<PersonEntity> passEntities, int type) {
allLeavePass.addAll(passEntities);
LinkedHashSet<PersonEntity> mergeList = new LinkedHashSet<>(allLeavePass);
allLeavePass = new ArrayList<>(mergeList);
/*for (PersonEntity leavePass : allLeavePass) {
Log.i(TAG, "getPassInfo: name="+leavePass.getPerson_name());
}*/
if (upperDevicesCallback != null) {
upperDevicesCallback.getAllPassInfo(allLeavePass,type);
}
}
@Override
public void onConnectStatus(boolean isConnect, int type) {
Log.i(TAG, "onConnectStatus: 接收到的连接状态:isConnect="+isConnect+",ip="+ip);
ThreadPool.getInstance().getThreadPoolExecutor().execute(() -> {
//修改状态
List<ConfigEntity> configEntities = ConfigHelper.getInstance().queryByTypeIp(type, ip);
synchronized (configEntities){
for (ConfigEntity configEntity : configEntities) {
configEntity.setConnect(isConnect);
//Log.i(TAG, "onConnectStatus: 修改后:ip"+configEntity.getServerIp()+",isConnect="+configEntity.isConnect());
//更新ip接口状态
ConfigHelper.getInstance().update(configEntity);
}
});
}
/*public void startLeaveApi(ConfigBean configBean){
if (configBean != null) {
//如果之前曾经有过的,则停掉之前的
if (leaveMgr!=null){
stopLeaveApi();
//获取最新状态
List<ConfigEntity> leaveList = ConfigHelper.getInstance().queryByType(type);
isLeaveAllConnect = true;
synchronized (leaveList){
for (ConfigEntity config : leaveList) {
//Log.i(TAG, "onConnectStatus: 数据库里面的数据:ip="+config.getServerIp()+",isConnect="+config.isConnect());
boolean connect = config.isConnect();
if (!connect){
isLeaveAllConnect = false;
break;
}
leaveMgr = new ApiManager(configBean.getUsername(), configBean.getPassword(), configBean.getServerIp(), LEAVE_TYPE);
if (leaveMgr != null) {
leaveMgr.start();
leaveMgr.setOnDeviceCallback(this);
}
if (upperDevicesCallback != null) {
upperDevicesCallback.onAllConnectStatus(isLeaveAllConnect,type);
}
}*/
}
});
}
});
}
public void stopApi(){
stopApiByType(ENTER_TYPE);
......@@ -183,57 +218,11 @@ public class ApiQuest {
mgrHashMap.clear();
}
/*public void stopEnterApi(){
for (Map.Entry<String, ApiManager> enterMgr : enterMgrHashMap.entrySet()) {
ApiManager enter = enterMgr.getValue();
if (enter!=null){
enter.stop();
enter.setOnDeviceCallback(null);
enter = null;
}
enterMgrHashMap.remove(enterMgr.getKey());
}
}
public void stopLeaveApi(){
for (Map.Entry<String, ApiManager> leaveMgr : leaveMgrHashMap.entrySet()) {
ApiManager leave = leaveMgr.getValue();
if (leave!=null){
leave.stop();
leave.setOnDeviceCallback(null);
leave = null;
}
leaveMgrHashMap.remove(leaveMgr.getKey());
}
}*/
/*private void stopEnterApi(){
if (enterMgr != null) {
enterMgr.stop();
enterMgr.setOnDeviceCallback(null);
enterMgr.setOnQueryPersonsCallback(null);
enterMgr = null;
}
}*/
/*private void stopLeaveApi(){
if (leaveMgr != null) {
leaveMgr.stop();
leaveMgr.setOnDeviceCallback(null);
leaveMgr.setOnQueryPersonsCallback(null);
leaveMgr = null;
}
}*/
public void queryPersons(QueryPersonCallback callback){
if (enterMgrHashMap.size()>0){
ApiManager oneEnterMgr = enterMgrHashMap.get(oneEnterIp);
oneEnterMgr.queryPersons();
oneEnterMgr.setOnQueryPersonsCallback(callback);
}
/*if (enterMgr!=null){
enterMgr.queryPersons();
enterMgr.setOnQueryPersonsCallback(callback);
}*/
}
}
package com.srthinker.statinfo.presenter;
import com.srthinker.statinfo.database.entity.PersonEntity;
import java.util.Comparator;
public class PersonEntityComparator implements Comparator<PersonEntity> {
@Override
public int compare(PersonEntity p1, PersonEntity p2) {
//return time2.compareTo(time1);
return 0;
}
}
......@@ -361,21 +361,23 @@ public class MainActivity extends BaseActivity implements OnTimeUpdateListener,
}
private void updatePassInfo(List<PersonEntity> passEntities, int type) {
Log.i(TAG, "updatePassInfo: 获取的总数="+passEntities.size());
for (PersonEntity passEntity : passEntities) {
Log.i(TAG, "updatePassInfo: 获取到的name="+passEntity.getPerson_name());
}
if (type ==ENTER_TYPE){
enterBeans = passEntities;
if (enterBeans != null) {
if (passEntities != null) {
if (enterAdapter != null) {
int size = enterBeans.size();
Log.i(TAG, "updatePassInfo: 进来的人数="+size);
int size = passEntities.size();
if (size!=lastEnterSize){
//更新进出情况
enterAdapter.setDataList(enterBeans);
//enterBeans = passEntities; 全局变量直接赋值会出现问题
enterBeans.clear();
enterBeans.addAll(passEntities);
enterAdapter.setDataList(passEntities);
//更新人员情况(是否要根据id去重)
if (NEED_NO_REPET) {
List<PersonEntity> enterListNo = StatSize.filterList(enterBeans);
List<PersonEntity> enterListNo = StatSize.filterList(passEntities);
lastEnterSize = enterListNo.size();
}else{
lastEnterSize = size;
......@@ -385,24 +387,26 @@ public class MainActivity extends BaseActivity implements OnTimeUpdateListener,
mBinding.nvLeave.setNumber(lastLeaveSize);
mBinding.nvPresent.setNumber(lastPresentSize);
//更新分组情况(是否要根据id去重)
groupEnterHashMap = getGroupSizeHashMap(enterBeans);
groupEnterHashMap = getGroupSizeHashMap(passEntities);
updatePresentSize(groupEnterHashMap,groupLeaveHashMap);
}
}
}
}else if (type == LEAVE_TYPE){
leaveBeans = passEntities;
if (leaveBeans != null) {
//leaveBeans = passEntities;
if (passEntities != null) {
if (leaveAdapter != null) {
int size = passEntities.size();
//进出有变化时才更新
if (size!=lastLeaveSize){
leaveBeans.clear();
leaveBeans.addAll(passEntities);
//更新进出情况
leaveAdapter.setDateList(leaveBeans);
leaveAdapter.setDataList(passEntities);
//更新人员情况(是否要根据id去重)
if (NEED_NO_REPET) {
List<PersonEntity> leaveListNo = StatSize.filterList(leaveBeans);
List<PersonEntity> leaveListNo = StatSize.filterList(passEntities);
lastLeaveSize = leaveListNo.size();
}else{
lastLeaveSize = size;
......@@ -412,11 +416,12 @@ public class MainActivity extends BaseActivity implements OnTimeUpdateListener,
mBinding.nvLeave.setNumber(lastLeaveSize);
mBinding.nvPresent.setNumber(lastPresentSize);
//更新分组情况(是否要根据id去重)
groupLeaveHashMap = getGroupSizeHashMap(leaveBeans);
groupLeaveHashMap = getGroupSizeHashMap(passEntities);
updatePresentSize(groupEnterHashMap,groupLeaveHashMap);
}
}
}
}
}
......@@ -543,15 +548,28 @@ public class MainActivity extends BaseActivity implements OnTimeUpdateListener,
ApiQuest.startEnterApi(enterConfigList,this);
List<ConfigBean> leaveConfigList = SharedUtil.getInstance(this).readSharedList(LEAVE_TYPE, ConfigBean.class, "");
ApiQuest.startLeaveApi(leaveConfigList,this);*/
runOnUiThread(() -> {
/*runOnUiThread(() -> {
List<ConfigEntity> enterConfigList = ConfigHelper.getInstance().queryByType(ENTER_TYPE);
ApiQuest.getInstance().startApi(enterConfigList,ENTER_TYPE);
ApiQuest.getInstance().setOnUpperDevicesCallback(this);
List<ConfigEntity> leaveConfigList = ConfigHelper.getInstance().queryByType(LEAVE_TYPE);
ApiQuest.getInstance().startApi(leaveConfigList,LEAVE_TYPE);
ApiQuest.getInstance().setOnUpperDevicesCallback(this);
});*/
/*ThreadPool.getInstance().getThreadPoolExecutor().execute(() -> {
List<ConfigEntity> enterConfigList = ConfigHelper.getInstance().queryByType(ENTER_TYPE);
ApiQuest.getInstance().startApi(enterConfigList,ENTER_TYPE);
ApiQuest.getInstance().setOnUpperDevicesCallback(this);
List<ConfigEntity> leaveConfigList = ConfigHelper.getInstance().queryByType(LEAVE_TYPE);
ApiQuest.getInstance().startApi(leaveConfigList,LEAVE_TYPE);
ApiQuest.getInstance().setOnUpperDevicesCallback(this);
});*/
List<ConfigEntity> enterConfigList = ConfigHelper.getInstance().queryByType(ENTER_TYPE);
ApiQuest.getInstance().startApi(enterConfigList,ENTER_TYPE);
ApiQuest.getInstance().setOnUpperDevicesCallback(this);
List<ConfigEntity> leaveConfigList = ConfigHelper.getInstance().queryByType(LEAVE_TYPE);
ApiQuest.getInstance().startApi(leaveConfigList,LEAVE_TYPE);
ApiQuest.getInstance().setOnUpperDevicesCallback(this);
});
}
@Override
......@@ -572,10 +590,16 @@ public class MainActivity extends BaseActivity implements OnTimeUpdateListener,
Log.i(TAG, "onUpdateConfig: pass="+config.getPassword());
Log.i(TAG, "onUpdateConfig: ip="+config.getServerIp());
}
runOnUiThread(()->{
/*runOnUiThread(()->{
ApiQuest.getInstance().startApi(configList,type);
ApiQuest.getInstance().setOnUpperDevicesCallback(this);
});*/
/*ThreadPool.getInstance().getThreadPoolExecutor().execute(()->{
ApiQuest.getInstance().startApi(configList,type);
ApiQuest.getInstance().setOnUpperDevicesCallback(this);
});*/
ApiQuest.getInstance().startApi(configList,type);
ApiQuest.getInstance().setOnUpperDevicesCallback(this);
});
}
@Override
......@@ -586,17 +610,18 @@ public class MainActivity extends BaseActivity implements OnTimeUpdateListener,
}
@Override
public void onAllConnectStatus(boolean hasDisConnect, int type) {
public void onAllConnectStatus(boolean hasConnect, int type) {
Log.i(TAG, "onAllConnectStatus: 接收到的连接状态 hasConnect="+hasConnect);
runOnUiThread(()->{
if (type==ENTER_TYPE){
if (!hasDisConnect) {
if (!hasConnect) {
mBinding.tvEnterError.setVisibility(View.VISIBLE);
}else{
mBinding.tvEnterError.setVisibility(View.GONE);
}
}else if (type == LEAVE_TYPE){
if (!hasDisConnect) {
if (!hasConnect) {
mBinding.tvLeaveError.setVisibility(View.VISIBLE);
}else{
mBinding.tvLeaveError.setVisibility(View.GONE);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment