package com.lotaai.canguiayw;
|
|
import android.content.Context;
|
import android.graphics.Color;
|
import android.os.Bundle;
|
import android.os.Handler;
|
import android.os.Message;
|
import android.util.Log;
|
import android.view.LayoutInflater;
|
import android.view.View;
|
import android.view.ViewGroup;
|
import android.widget.AdapterView;
|
import android.widget.Button;
|
import android.widget.GridView;
|
import android.widget.ThemedSpinnerAdapter;
|
|
import androidx.fragment.app.Fragment;
|
|
import com.blankj.utilcode.util.LogUtils;
|
import com.liys.lswitch.BaseSwitch;
|
import com.liys.lswitch.LSwitch;
|
import com.lotaai.canguiayw.common.SettingConfig;
|
import com.lotaai.canguiayw.device.CanGuiCommand;
|
import com.lotaai.canguiayw.device.CanguiStatus;
|
import com.lotaai.canguiayw.device.DeviceMessage;
|
import com.lotaai.canguiayw.device.DeviceType;
|
import com.lotaai.canguiayw.device.MessageType;
|
import com.lotaai.canguiayw.view.GridTableAdapter;
|
|
import org.greenrobot.eventbus.EventBus;
|
import org.greenrobot.eventbus.Subscribe;
|
import org.greenrobot.eventbus.ThreadMode;
|
|
import java.util.ArrayList;
|
import java.util.Collections;
|
import java.util.List;
|
|
public class OpenDoorFragment extends Fragment {
|
|
private String TAG = "OpenDoorFragment";
|
private List<CanguiStatus> gridStatusList = new ArrayList<>();
|
private Context context;
|
private GridView gridtable;
|
private View root;
|
private List<CanguiStatus> selectedList = new ArrayList<>();
|
private LSwitch jiareSwitch;
|
private LSwitch xiaoduSwitch;
|
private LSwitch dengSwitch;
|
private Button openDoorBtn;
|
private Button zhugui;
|
private Button fugui001;
|
private Button fugui002;
|
private Button fugui003;
|
private Button fugui004;
|
private Button fugui005;
|
private int cureentSelectGui = 0;
|
private boolean isCanRun = false;
|
|
|
@Override
|
public void onCreate(Bundle savedInstanceState) {
|
EventBus.getDefault().register(this);
|
super.onCreate(savedInstanceState);
|
}
|
|
@Override
|
public void onDestroy() {
|
EventBus.getDefault().unregister(this);
|
super.onDestroy();
|
}
|
|
|
@Subscribe(threadMode = ThreadMode.ASYNC)
|
public void recvCommand(DeviceMessage mssage) {
|
try {
|
if (mssage.getMessageType() == MessageType.FANHUI.ordinal()) {
|
isCanRun = false;
|
}
|
} catch (Exception e) {
|
LogUtils.e(e);
|
}
|
}
|
|
|
@Override
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
Bundle savedInstanceState) {
|
if (root == null) {
|
root = inflater.inflate(R.layout.framgment_opendoor, container, false);
|
}
|
context = container.getContext();
|
initView();
|
intitSwitch();
|
initButton();
|
// SettingConfig.getInstance().getExecutor().execute(syncRunnable);
|
return root;
|
}
|
|
Runnable syncRunnable = new Runnable() {
|
@Override
|
public void run() {
|
while (isCanRun) {
|
try {
|
Thread.sleep(1000);
|
showHandler.sendEmptyMessage(1);
|
} catch (InterruptedException e) {
|
e.printStackTrace();
|
} catch (Exception e) {
|
|
}
|
}
|
}
|
};
|
|
private void initButton() {
|
zhugui = root.findViewById(R.id.zhu001);
|
fugui001 = root.findViewById(R.id.gui001);
|
fugui002 = root.findViewById(R.id.gui002);
|
fugui003 = root.findViewById(R.id.gui003);
|
fugui004 = root.findViewById(R.id.gui004);
|
fugui005 = root.findViewById(R.id.gui005);
|
for (int g = 0; g < SettingConfig.getInstance().statusList.size(); g++) {
|
if (SettingConfig.getInstance().statusList.get(g).getGuiHao() == 0) {
|
if (zhugui.getVisibility() != View.VISIBLE) {
|
zhugui.setVisibility(View.VISIBLE);
|
}
|
}
|
|
if (SettingConfig.getInstance().statusList.get(g).getGuiHao() == 1) {
|
if (fugui001.getVisibility() != View.VISIBLE) {
|
fugui001.setVisibility(View.VISIBLE);
|
}
|
}
|
|
if (SettingConfig.getInstance().statusList.get(g).getGuiHao() == 2) {
|
if (fugui002.getVisibility() != View.VISIBLE) {
|
fugui002.setVisibility(View.VISIBLE);
|
}
|
}
|
|
if (SettingConfig.getInstance().statusList.get(g).getGuiHao() == 3) {
|
if (fugui003.getVisibility() != View.VISIBLE) {
|
fugui003.setVisibility(View.VISIBLE);
|
}
|
}
|
|
if (SettingConfig.getInstance().statusList.get(g).getGuiHao() == 4) {
|
if (fugui004.getVisibility() != View.VISIBLE) {
|
fugui004.setVisibility(View.VISIBLE);
|
}
|
}
|
|
if (SettingConfig.getInstance().statusList.get(g).getGuiHao() == 5) {
|
if (fugui005.getVisibility() != View.VISIBLE) {
|
fugui005.setVisibility(View.VISIBLE);
|
}
|
}
|
}
|
|
zhugui.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View view) {
|
cancelAllBtn();
|
zhugui.setBackgroundColor(getResources().getColor(R.color.cheng));
|
cureentSelectGui = 0;
|
|
/* CanguiStatus cangui =new CanguiStatus();
|
cangui.setGuiHao(8);
|
cangui.setGridNo(88);
|
((CunOjbActivity) getActivity()).setData(cangui);*/
|
}
|
});
|
|
fugui001.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View view) {
|
cancelAllBtn();
|
fugui001.setBackgroundColor(getResources().getColor(R.color.cheng));
|
cureentSelectGui = 1;
|
initView();
|
}
|
});
|
|
fugui002.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View view) {
|
cancelAllBtn();
|
fugui002.setBackgroundColor(getResources().getColor(R.color.cheng));
|
cureentSelectGui = 2;
|
|
initView();
|
}
|
});
|
|
fugui003.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View view) {
|
cancelAllBtn();
|
fugui003.setBackgroundColor(getResources().getColor(R.color.cheng));
|
cureentSelectGui = 3;
|
|
initView();
|
}
|
});
|
|
fugui004.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View view) {
|
cancelAllBtn();
|
fugui004.setBackgroundColor(getResources().getColor(R.color.cheng));
|
cureentSelectGui = 4;
|
|
initView();
|
}
|
});
|
|
fugui005.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View view) {
|
cancelAllBtn();
|
fugui005.setBackgroundColor(getResources().getColor(R.color.cheng));
|
cureentSelectGui = 5;
|
|
initView();
|
}
|
});
|
}
|
|
public void cancelAllBtn() {
|
zhugui.setBackgroundColor(Color.GRAY);
|
fugui001.setBackgroundColor(Color.GRAY);
|
fugui002.setBackgroundColor(Color.GRAY);
|
fugui003.setBackgroundColor(Color.GRAY);
|
fugui004.setBackgroundColor(Color.GRAY);
|
fugui005.setBackgroundColor(Color.GRAY);
|
}
|
|
private void intitSwitch() {
|
jiareSwitch = root.findViewById(R.id.lswitchjiare);
|
xiaoduSwitch = root.findViewById(R.id.lswitchxiaodu);
|
dengSwitch = root.findViewById(R.id.lswitdeng);
|
openDoorBtn = root.findViewById(R.id.openDoorBtn);
|
jiareSwitch.setOnCheckedListener(new BaseSwitch.OnCheckedListener() {
|
@Override
|
public void onChecked(boolean isChecked) {
|
int caozuo = 1;
|
if (isChecked) {
|
caozuo = 2;
|
} else {
|
caozuo = 1;
|
}
|
for (int p = 0; p < selectedList.size(); p++) {
|
CanguiStatus grid = selectedList.get(p);
|
byte[] command = CanGuiCommand.getInstance().controlGridCommand(grid.getGuiHao(),
|
grid.getGridNo(), 0, 255, 0, 0, caozuo, 0);
|
DeviceMessage message = new DeviceMessage();
|
message.setMessageType(MessageType.SENDMESSAGE.ordinal());
|
message.setDeviceType(DeviceType.CANGUI.ordinal());
|
message.setMessageByte(command);
|
EventBus.getDefault().post(message);
|
}
|
}
|
});
|
|
xiaoduSwitch.setOnCheckedListener(new BaseSwitch.OnCheckedListener() {
|
@Override
|
public void onChecked(boolean isChecked) {
|
int caozuo = 1;
|
if (isChecked) {
|
caozuo = 2;
|
} else {
|
caozuo = 1;
|
}
|
for (int p = 0; p < selectedList.size(); p++) {
|
CanguiStatus grid = selectedList.get(p);
|
byte[] command = CanGuiCommand.getInstance().controlGridCommand(grid.getGuiHao(),
|
grid.getGridNo(), 0, 255, caozuo, 0, 0, 0);
|
DeviceMessage message = new DeviceMessage();
|
message.setMessageType(MessageType.SENDMESSAGE.ordinal());
|
message.setDeviceType(DeviceType.CANGUI.ordinal());
|
message.setMessageByte(command);
|
EventBus.getDefault().post(message);
|
}
|
}
|
});
|
|
dengSwitch.setOnCheckedListener(new BaseSwitch.OnCheckedListener() {
|
@Override
|
public void onChecked(boolean isChecked) {
|
int caozuo = 1;
|
if (isChecked) {
|
caozuo = 2;
|
} else {
|
caozuo = 1;
|
}
|
for (int p = 0; p < selectedList.size(); p++) {
|
CanguiStatus grid = selectedList.get(p);
|
byte[] command = CanGuiCommand.getInstance().controlGridCommand(grid.getGuiHao(),
|
grid.getGridNo(), 0, 255, 0, caozuo, 0, 0);
|
DeviceMessage message = new DeviceMessage();
|
message.setMessageType(MessageType.SENDMESSAGE.ordinal());
|
message.setDeviceType(DeviceType.CANGUI.ordinal());
|
message.setMessageByte(command);
|
EventBus.getDefault().post(message);
|
}
|
}
|
});
|
|
openDoorBtn.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View view) {
|
for (int p = 0; p < selectedList.size(); p++) {
|
CanguiStatus grid = selectedList.get(p);
|
LogUtils.i(TAG, grid.getGuiHao(), grid.getGridNo());
|
byte[] command = CanGuiCommand.getInstance().controlGridCommand(grid.getGuiHao(),
|
grid.getGridNo(), 1, 255, 0, 0, 0, 0);
|
DeviceMessage message = new DeviceMessage();
|
message.setMessageType(MessageType.SENDMESSAGE.ordinal());
|
message.setDeviceType(DeviceType.CANGUI.ordinal());
|
message.setMessageByte(command);
|
EventBus.getDefault().post(message);
|
}
|
}
|
});
|
}
|
|
//设置一下界面上的状态
|
private Handler showHandler = new Handler() {
|
@Override
|
public void handleMessage(Message msg) {
|
initView();
|
}
|
};
|
|
public void initView() {
|
gridStatusList.clear();
|
for (int i = 0; i < SettingConfig.getInstance().statusList.size(); i++) {
|
CanguiStatus ss = SettingConfig.getInstance().statusList.get(i);
|
if (ss.getGuiHao() == cureentSelectGui) {
|
CanguiStatus status = new CanguiStatus();
|
status.setGridNo(ss.getGridNo());
|
status.setGuiHao(ss.getGuiHao());
|
status.setJiareIsOpen(ss.isJiareIsOpen());
|
status.setDoorIsOpen(ss.isDoorIsOpen());
|
status.setXiaoDuIsOpen(ss.isXiaoDuIsOpen());
|
status.setDengGuangIsOpen(ss.isDengGuangIsOpen());
|
status.setSelect(false);
|
if (checkIsSelectedAndOrRemove(status, false)) {
|
status.setSelect(true);
|
}
|
gridStatusList.add(status);
|
}
|
}
|
Collections.sort(gridStatusList);
|
GridTableAdapter adapter = new GridTableAdapter(context, gridStatusList, R.layout.framgment_tablegrid_item);
|
gridtable = root.findViewById(R.id.gridtable);
|
gridtable.setAdapter(adapter);
|
gridtable.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
@Override
|
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
|
CanguiStatus cangui = gridStatusList.get(i);
|
((CunOjbActivity) getActivity()).setData(cangui);
|
|
/* View item = gridtable.getChildAt(i).findViewById(R.id.gridLineLayout);
|
if (checkIsSelectedAndOrRemove(cangui,true)) {
|
item.setBackgroundColor(Color.GRAY);
|
} else {
|
item.setBackgroundColor(getResources().getColor(R.color.cheng));
|
}*/
|
}
|
});
|
}
|
|
/**
|
* @param status
|
* @param isControl 是否从控制删除和添加,选中变不选,不选变选
|
* @return
|
*/
|
private boolean checkIsSelectedAndOrRemove(CanguiStatus status, boolean isControl) {
|
boolean isFind = false;
|
for (int i = 0; i < selectedList.size(); i++) {
|
if (selectedList.get(i).getGridNo() == status.getGridNo() && selectedList.get(i).getGuiHao() == status.getGuiHao()) {
|
isFind = true;
|
if (isControl) {
|
selectedList.remove(i);
|
}
|
break;
|
}
|
}
|
if (!isFind && isControl) {
|
selectedList.add(status);
|
}
|
return isFind;
|
}
|
}
|