Commit 8c32f10e authored by chenyuling's avatar chenyuling

优化代码

parent 1b2f9938
......@@ -60,4 +60,8 @@ public class LeaveAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
//Log.i(TAG, "setDataList: size="+this.peopleBeans.size());
this.notifyDataSetChanged();
}
public List<PersonEntity> getDataList(){
return this.peopleBeans;
}
}
......@@ -6,6 +6,8 @@ import com.raizlabs.android.dbflow.annotation.Table;
import com.raizlabs.android.dbflow.structure.BaseModel;
import com.srthinker.statinfo.database.AppDB;
import java.util.Objects;
@Table(database = AppDB.class)
public class ConfigEntity extends BaseModel {
@PrimaryKey(autoincrement = true)
......@@ -69,4 +71,17 @@ public class ConfigEntity extends BaseModel {
public void setType(int type) {
this.type = type;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ConfigEntity config = (ConfigEntity) o;
return id == config.id && isConnect == config.isConnect && type == config.type && Objects.equals(username, config.username) && Objects.equals(password, config.password) && Objects.equals(serverIp, config.serverIp);
}
@Override
public int hashCode() {
return Objects.hash(id, username, password, serverIp, isConnect, type);
}
}
......@@ -29,6 +29,7 @@ import java.util.concurrent.ScheduledExecutorService;
public class ApiManager {
private static final String TAG = "ApiManager";
private static final long GET_PASS_TIME = 60000;
private final String username;
private final String password;
private final String serverIp;
......@@ -42,6 +43,7 @@ public class ApiManager {
private PassApiImpl passApi;
private boolean isRunning = false; // 标记任务是否正在执行
private boolean isFirstStatus = false;
private int lastPassTotal;
private AuthApi authApi;
private PersonApiImpl personApi;
private boolean lastConnect = false;
......@@ -76,6 +78,7 @@ public class ApiManager {
isRunning = true;
isFirstStatus = true;
lastConnect = false;
lastPassTotal = 0;
handler.post(authThreadRunnable);
//ThreadPool.getInstance().getThreadPoolExecutor().execute(passRecordRunnable);
handler.post(passThreadRunnable);
......@@ -103,6 +106,7 @@ public class ApiManager {
//只要连接成功都传避免出现意外,因为鉴权成功后也只访问一次
if (callback != null) {
lastConnect = true;
isFirstStatus = false;
callback.onConnectStatus(true, type);
}
//updateLastConnect(true);
......@@ -164,9 +168,14 @@ public class ApiManager {
totalPassLists = new ArrayList<>();
pass_offset = 0;
queryPass();
if (callback != null) {
callback.getPassInfo(totalPassLists,type);
//Log.i(TAG, "run: totalPassLists.size()="+totalPassLists.size());
//Log.i(TAG, "run: lastPassTotal="+lastPassTotal);
if (totalPassLists.size()!=0&&totalPassLists.size()!= lastPassTotal){
if (callback != null) {
callback.getPassInfo(totalPassLists,type);
}
}
lastPassTotal = totalPassLists.size();
/*handler.postDelayed(this,60000);*/
}
};
......@@ -179,7 +188,7 @@ public class ApiManager {
return;
}
ThreadPool.getInstance().getThreadPoolExecutor().execute(passRecordRunnable);
handler.postDelayed(this,60000);
handler.postDelayed(this,GET_PASS_TIME);
}
};
......
......@@ -14,6 +14,7 @@ import com.srthinker.statinfo.listener.upper.UpperDevicesCallback;
import com.srthinker.statinfo.util.common.ThreadPool;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
......@@ -47,27 +48,39 @@ public class ApiQuest {
this.upperDevicesCallback = upperDevicesCallback;
}
private void initData(int type){
if (type==ENTER_TYPE){
isEnterAllConnect = false;
allEnterPass = new ArrayList<>();
}else if (type==LEAVE_TYPE){
isLeaveAllConnect = false;
allLeavePass = new ArrayList<>();
}
}
public void startApi(List<ConfigEntity> configList,int type){
if (configList != null) {
//把之前的所有的停掉
stopApiByType(type);
for (int i = 0; i < configList.size(); i++) {
ConfigEntity config = configList.get(i);
String username = config.getUsername();
String password = config.getPassword();
String serverIp = config.getServerIp();
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);
}
synchronized (this){
//把之前的所有的停掉
stopApiByType(type);
initData(type);
for (int i = 0; i < configList.size(); i++) {
ConfigEntity config = configList.get(i);
String username = config.getUsername();
String password = config.getPassword();
String serverIp = config.getServerIp();
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);
}
}
}
}
}
private void startEnterIp(String username, String password, String ip) {
......@@ -86,20 +99,22 @@ public class ApiQuest {
enterMgr.setOnDeviceCallback(new DeviceCallback() {
@Override
public void getPassInfo(List<PersonEntity> passEntities, int type) {
allEnterPass.addAll(passEntities);
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: 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);
synchronized (allEnterPass){
allEnterPass.addAll(passEntities);
/*for (PersonEntity enterPass : allEnterPass) {
Log.i(TAG, "去重前getPassInfo: enterPass.time="+enterPass.getTimestamp()+",name="+enterPass.getPerson_name());
}*/
PersonEntityComparator comparator = new PersonEntityComparator();
Collections.sort(allEnterPass,comparator);
LinkedHashSet<PersonEntity> mergeList = new LinkedHashSet<>(allEnterPass);
allEnterPass = new ArrayList<>(mergeList);
/*for (PersonEntity enterPass : allEnterPass) {
Log.i(TAG, "去重后getPassInfo: enterPass.time="+enterPass.getTimestamp()+",name="+enterPass.getPerson_name());
}*/
ArrayList<PersonEntity> allPassList = new ArrayList<>(allEnterPass);
if (upperDevicesCallback != null) {
upperDevicesCallback.getAllPassInfo(allPassList,type);
}
}
}
......@@ -153,14 +168,16 @@ 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);
synchronized (allLeavePass){
allLeavePass.addAll(passEntities);
PersonEntityComparator comparator = new PersonEntityComparator();
Collections.sort(allLeavePass,comparator);
LinkedHashSet<PersonEntity> mergeList = new LinkedHashSet<>(allLeavePass);
allLeavePass = new ArrayList<>(mergeList);
ArrayList<PersonEntity> allPassList = new ArrayList<>(allLeavePass);
if (upperDevicesCallback != null) {
upperDevicesCallback.getAllPassInfo(allPassList,type);
}
}
}
......@@ -221,8 +238,10 @@ public class ApiQuest {
public void queryPersons(QueryPersonCallback callback){
if (enterMgrHashMap.size()>0){
ApiManager oneEnterMgr = enterMgrHashMap.get(oneEnterIp);
oneEnterMgr.queryPersons();
oneEnterMgr.setOnQueryPersonsCallback(callback);
if (oneEnterMgr != null) {
oneEnterMgr.queryPersons();
oneEnterMgr.setOnQueryPersonsCallback(callback);
}
}
}
}
package com.srthinker.statinfo.presenter;
import com.srthinker.statinfo.database.entity.PersonEntity;
import com.srthinker.statinfo.util.common.DateUtil;
import java.util.Comparator;
public class PersonEntityComparator implements Comparator<PersonEntity> {
@Override
public int compare(PersonEntity p1, PersonEntity p2) {
//return time2.compareTo(time1);
return 0;
long time1 = DateUtil.getTime(p1.getTimestamp(), "HH:mm");
long time2 = DateUtil.getTime(p2.getTimestamp(), "HH:mm");
//从大到小排列
if (time1>time2){
return -1;
}else if (time1<time2){
return 1;
}else{
return 0;
}
}
}
......@@ -362,14 +362,14 @@ 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) {
/*for (PersonEntity passEntity : passEntities) {
Log.i(TAG, "updatePassInfo: 获取到的name="+passEntity.getPerson_name());
}
}*/
if (type ==ENTER_TYPE){
if (passEntities != null) {
if (enterAdapter != null) {
int size = passEntities.size();
if (size!=lastEnterSize){
if (enterBeans!=null&&size!=enterBeans.size()){
//更新进出情况
//enterBeans = passEntities; 全局变量直接赋值会出现问题
enterBeans.clear();
......@@ -390,6 +390,8 @@ public class MainActivity extends BaseActivity implements OnTimeUpdateListener,
groupEnterHashMap = getGroupSizeHashMap(passEntities);
updatePresentSize(groupEnterHashMap,groupLeaveHashMap);
}
/* Log.i(TAG, "updatePassInfo: enterBeans.size="+enterBeans.size());
Log.i(TAG, "updatePassInfo: adapter.size="+enterAdapter.getDataList().size());*/
}
}
......@@ -399,7 +401,7 @@ public class MainActivity extends BaseActivity implements OnTimeUpdateListener,
if (leaveAdapter != null) {
int size = passEntities.size();
//进出有变化时才更新
if (size!=lastLeaveSize){
if (leaveBeans!=null&&size!=leaveBeans.size()){
leaveBeans.clear();
leaveBeans.addAll(passEntities);
//更新进出情况
......@@ -419,6 +421,8 @@ public class MainActivity extends BaseActivity implements OnTimeUpdateListener,
groupLeaveHashMap = getGroupSizeHashMap(passEntities);
updatePresentSize(groupEnterHashMap,groupLeaveHashMap);
}
/*Log.i(TAG, "updatePassInfo: leaveBeans.size="+leaveBeans.size());
Log.i(TAG, "updatePassInfo: adapter.size="+leaveAdapter.getDataList().size());*/
}
}
......@@ -548,22 +552,6 @@ 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(() -> {
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);
......@@ -586,18 +574,10 @@ public class MainActivity extends BaseActivity implements OnTimeUpdateListener,
}else if (type == LEAVE_TYPE){
ApiQuest.getInstance().startLeaveApi(configList);
}*/
for (ConfigEntity config : configList) {
/*for (ConfigEntity config : configList) {
Log.i(TAG, "onUpdateConfig: pass="+config.getPassword());
Log.i(TAG, "onUpdateConfig: ip="+config.getServerIp());
}
/*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);
}
......@@ -629,32 +609,4 @@ public class MainActivity extends BaseActivity implements OnTimeUpdateListener,
}
});
}
/*@Override
public void getPassInfo(List<PersonEntity> passEntities, int type) {
runOnUiThread(()->{
updatePassInfo(passEntities, type);
});
}
@Override
public void onConnectStatus(boolean isConnect, int type) {
runOnUiThread(()->{
if (type==ENTER_TYPE){
if (!isConnect) {
mBinding.tvEnterError.setVisibility(View.VISIBLE);
}else{
mBinding.tvEnterError.setVisibility(View.GONE);
}
}else if (type == LEAVE_TYPE){
if (!isConnect) {
mBinding.tvLeaveError.setVisibility(View.VISIBLE);
}else{
mBinding.tvLeaveError.setVisibility(View.GONE);
}
}
});
}*/
}
\ No newline at end of file
......@@ -9,6 +9,7 @@ import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
......@@ -34,6 +35,7 @@ import java.util.stream.Collectors;
public class ConfigDlgFragment extends DialogFragment {
private static final String TAG = "ConfigDlgFragment";
private Context context;
private int type;
private com.srthinker.statinfo.databinding.DialogConfigBinding mBinding;
......@@ -98,12 +100,15 @@ public class ConfigDlgFragment extends DialogFragment {
ConfigEntity config = mLastConfig.get(i);
if (config != null) {
String serverIp = config.getServerIp();
boolean connect = config.isConnect();
if (i==0){
mBinding.evServerip.setContent(serverIp);
mBinding.evServerip.setStatus(connect);
}else{
addDeviceIpView();
EditView editView = devicesIpList.get(i);
editView.setContent(serverIp);
editView.setStatus(connect);
}
}
}
......@@ -156,21 +161,29 @@ public class ConfigDlgFragment extends DialogFragment {
config.setPassword(password);
config.setServerIp(ip);
config.setType(type);
configEntities.add(config);
if (mLastConfig != null&&mLastConfig.size()>i) {
ConfigEntity mLastEntity = mLastConfig.get(i);
try {
if (TextUtils.equals(mLastEntity.getServerIp(),ip)){
config.setId(mLastEntity.getId());
config.setConnect(mLastEntity.isConnect());
}
/*try {
String newConfigString = GsonUtil.toJsonString(config);
String lastConfigString = GsonUtil.toJsonString(mLastEntity);
Log.i(TAG, "updateConfig: newConfigString="+newConfigString);
Log.i(TAG, "updateConfig: lastConfigString="+lastConfigString);
if (!TextUtils.equals(newConfigString,lastConfigString)){
isUpdate = true;
}
} catch (Exception e) {
e.printStackTrace();
}
}*/
}
configEntities.add(config);
}
if (isUpdate || mLastConfig!=null&&allDeviceIps.size()!=mLastConfig.size()){
Log.i(TAG, "updateConfig: mLastConfig="+ GsonUtil.ListToJson(mLastConfig));
Log.i(TAG, "updateConfig: configEntity="+GsonUtil.ListToJson(configEntities));
if (mLastConfig!=null&&!mLastConfig.equals(configEntities)){
/*if (callback != null) {
SharedUtil.getInstance(context).writeShared(type,configBeanList);
callback.onUpdateConfig(configBeanList,type);
......
......@@ -108,6 +108,7 @@ public class DateUtil {
return sdf.format(d_date);
}
public static String getAddDate(String str, long day_num) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date old_date;
......@@ -137,6 +138,17 @@ public class DateUtil {
}
}
public static long getTime(String str,String formatStr){
SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
try {
Date date = sdf.parse(str);
return date.getTime();
} catch (ParseException e) {
e.printStackTrace();
return 0;
}
}
//计算某天是星期几
public static int getWeekIndex(String s_date) {
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
......
......@@ -61,4 +61,11 @@ public class EditView extends LinearLayout {
mBinding.etContent.setInputType(type);
}
public void setStatus(boolean status){
if (status){
mBinding.tvStatus.setVisibility(INVISIBLE);
}else{
mBinding.tvStatus.setVisibility(VISIBLE);
}
}
}
......@@ -26,7 +26,16 @@
android:textSize="@dimen/text_12_5"
android:layout_gravity="center_vertical"
android:gravity="center|right"
android:text=""
android:singleLine="true"
android:background="@null"
android:outlineProvider="none"/>
<TextView
android:id="@+id/tv_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/color_0287F9"
android:text="!"
android:paddingLeft="5dp"
android:visibility="invisible"/>
</LinearLayout>
\ No newline at end of file
package com.srthinker.statinfo;
import com.srthinker.statinfo.bean.ConfigBean;
import com.srthinker.statinfo.database.entity.ConfigEntity;
import com.srthinker.statinfo.database.entity.PersonEntity;
import org.junit.Test;
......@@ -55,4 +56,19 @@ public class EqualTest {
System.out.println("遍历bean的:name="+bean.getUsername());
}
}
@Test
public void configTest1(){
List<ConfigEntity> configList1 = new ArrayList<>();
List<ConfigEntity> configList2 = new ArrayList<>();
ConfigEntity configBean = new ConfigEntity();
configBean.setUsername("admin");
configBean.setServerIp("111");
ConfigEntity configBean1 = new ConfigEntity();
configBean1.setUsername("admin");
configBean1.setServerIp("111");
configList1.add(configBean);
configList2.add(configBean1);
System.out.println("是否相等="+configList1.equals(configList2));
}
}
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