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.TextView;
|
import android.widget.ThemedSpinnerAdapter;
|
|
import androidx.fragment.app.Fragment;
|
|
import com.blankj.utilcode.util.CacheDiskUtils;
|
import com.blankj.utilcode.util.GsonUtils;
|
import com.blankj.utilcode.util.LogUtils;
|
import com.liys.lswitch.BaseSwitch;
|
import com.liys.lswitch.LSwitch;
|
import com.lotaai.canguiayw.common.HttpLoggerInterceptor;
|
import com.lotaai.canguiayw.common.HttpUrlDefine;
|
import com.lotaai.canguiayw.common.SettingConfig;
|
import com.lotaai.canguiayw.data.BaseDto;
|
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 com.lotaai.canguiayw.view.GridTableAdapterNew;
|
import com.pranavpandey.android.dynamic.toasts.DynamicToast;
|
|
import org.greenrobot.eventbus.EventBus;
|
import org.greenrobot.eventbus.Subscribe;
|
import org.greenrobot.eventbus.ThreadMode;
|
|
import java.io.IOException;
|
import java.util.ArrayList;
|
import java.util.Collections;
|
import java.util.List;
|
import java.util.concurrent.TimeUnit;
|
|
import okhttp3.Call;
|
import okhttp3.Callback;
|
import okhttp3.FormBody;
|
import okhttp3.OkHttpClient;
|
import okhttp3.Request;
|
import okhttp3.RequestBody;
|
import okhttp3.Response;
|
|
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 TextView tv_close;
|
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);
|
tv_close = root.findViewById(R.id.tv_close);
|
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();
|
}
|
});
|
|
tv_close.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View view) {
|
requireActivity().finish();
|
}
|
});
|
}
|
|
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);
|
if (gridStatusList.size() == 8){
|
CanguiStatus e = new CanguiStatus();
|
e.setGridNo(-1);
|
gridStatusList.add(4, e);
|
}
|
GridTableAdapterNew adapter = new GridTableAdapterNew(context, gridStatusList, R.layout.item_box_new);
|
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);
|
if (cangui.getGridNo()!=-1){
|
//存餐
|
takeCheckCode(((CunOjbActivity) getActivity()).pickupCode,0,cangui.getGridNo(),((CunOjbActivity) getActivity()).isHand);
|
}
|
// ((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));
|
}*/
|
}
|
});
|
}
|
|
public void takeCheckCode(String code, final int guino, final int gridno, int isHand) {
|
HttpLoggerInterceptor loggingInterceptor = new HttpLoggerInterceptor(true);
|
OkHttpClient client = new OkHttpClient.Builder()
|
.connectTimeout(20, TimeUnit.SECONDS)//响应时间,读取时间
|
.readTimeout(20, TimeUnit.SECONDS)
|
.callTimeout(20, TimeUnit.SECONDS)
|
.addInterceptor(loggingInterceptor)//添加日志拦截器
|
.build();
|
|
LogUtils.i(CacheDiskUtils.getInstance().getString(SettingConfig.getInstance().Cache_Device_Code), code);
|
RequestBody body = new FormBody.Builder()
|
.build();
|
|
final Request request = new Request.Builder()
|
.url(HttpUrlDefine.checkStatus + "?pickNum=" + code + "&boxNum=" + guino + "&cartNum=" + gridno+ "&isHand=" + isHand+ "&sn=" + CacheDiskUtils.getInstance().getString(SettingConfig.getInstance().Cache_Device_Code))
|
/* .addHeader("language", "zh-CN")*/
|
.post(body)
|
.build();
|
client.newCall(request).enqueue(new Callback() {
|
@Override
|
public void onFailure(Call call, IOException e) {
|
requireActivity().runOnUiThread(new Runnable() {
|
@Override
|
public void run() {
|
DynamicToast.makeError(context, "Request error!", 3).show();
|
}
|
});
|
|
}
|
|
@Override
|
public void onResponse(Call call, Response response) throws IOException {
|
String result = response.body().string();
|
LogUtils.i("获取到的数据:" + result);
|
final BaseDto baseDto = GsonUtils.fromJson(result, BaseDto.class);
|
requireActivity().runOnUiThread(new Runnable() {
|
@Override
|
public void run() {
|
if (baseDto.getCode() == 200) {
|
byte[] command = CanGuiCommand.getInstance().controlGridCommand(guino,
|
gridno, 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);
|
// new Handler(Looper.getMainLooper()).postDelayed(() -> finish(), 3000);
|
((CunOjbActivity)requireActivity()).showResult("Place meal into the box.");
|
} else {
|
DynamicToast.makeError(context, baseDto.getMsg(), 3).show();
|
}
|
}
|
});
|
|
|
}
|
});
|
}
|
|
|
/**
|
* @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;
|
}
|
}
|