Skip to content

Instantly share code, notes, and snippets.

@330132662
Created March 26, 2021 05:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 330132662/04ca91efd188a64eabeffd50117fab88 to your computer and use it in GitHub Desktop.
Save 330132662/04ca91efd188a64eabeffd50117fab88 to your computer and use it in GitHub Desktop.
package com.jh.abc.listener;
import android.content.Context;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import com.hjq.demo.R;
import com.jh.abc.hardware.mt520.com.mingte.mt520.CheckClick;
import com.jh.abc.hardware.mt520.com.mingte.mt520.StatusCard;
import com.jh.abc.util.DebugLogUtil;
import java.io.IOException;
import android_serialport_api.MT520Command;
/**
* 每个柜子 触摸操作响应,。
*/
public class CabinetTouchListener implements View.OnTouchListener {
public void setCabStatusListener(CabStatusListener cabStatusListener) {
this.cabStatusListener = cabStatusListener;
}
CabStatusListener cabStatusListener;
public CabinetTouchListener(Context context) {
this.context = context;
mReader = new MT520Command();
mIDReader = new MT520Command();
}
private Context context;
static final int DEFAULT_BUF_SIZE = 2048;
private final int MAXPORTNUM = 1;
private long firstClickTime;
private long secondClickTime;
private long stillTime;
private boolean isUp = false;
private boolean isDoubleClick = false;
int idelay = 0;
private Boolean bSearching = false;
private Boolean bIDReading = false;
float data = 0;
String strComm = "/dev/ttyS";
int iPortOpen = -1;
String PortName;
public Handler myMessageHandler;
private MT520Command mReader;
private MT520Command mIDReader;
private byte[] status = new byte[DEFAULT_BUF_SIZE];
@Override
public boolean onTouch(View v, MotionEvent event) {
StatusCard mykeycopy;
mykeycopy = (StatusCard) v;
int itemp = ((StatusCard) v).getiIDnum();
// Log.d("Cell Click", "id "+ itemp);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
isUp = false;
if (firstClickTime == 0 & secondClickTime == 0) {//第一次点击
firstClickTime = System.currentTimeMillis();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (!isUp) {
Log.d("Cell 长按", "id " + itemp);
onCellDoor(itemp);
firstClickTime = 0;
secondClickTime = 0;
isDoubleClick = false;
} else {
if (!isDoubleClick) {
Log.d("Cell 点击", "id " + itemp);
onCellStatus(itemp);
}
isDoubleClick = false;
firstClickTime = 0;
secondClickTime = 0;
}
}
}, 500);
} else {
secondClickTime = System.currentTimeMillis();
stillTime = secondClickTime - firstClickTime;
if (stillTime < 500) {//两次点击小于0.5秒
Log.d("Cell 双击", "id " + itemp);
onCellLED(itemp);
isDoubleClick = true;
firstClickTime = 0;
secondClickTime = 0;
}
}
break;
case MotionEvent.ACTION_UP:
isUp = true;
break;
default:
break;
}
return true;
}
public void onCellDoor(int mCellnum) { //开门
if (!CheckClick.isClickEvent()) {
return;
}
// ShowStatus("keys"); 更新UI
// ShowInformation("Cell Door :" + mCellnum);
bSearching = true;
new Thread(new Runnable() {
int iTempPortnum = 1;
@Override
public void run() {
while (bSearching) {
try {
if (TryOpenDoor(iTempPortnum, mCellnum) == 0) {
bSearching = false;
} else {
iTempPortnum += 1;
if (iTempPortnum > MAXPORTNUM) {
bSearching = false;
break;
}
}
} catch (InterruptedException | IOException e) {
e.printStackTrace();
}
}
}
}).start();
}
byte CellDoorStatus = 1;
byte[] DoorStatusList = new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,};
private int TryOpenDoor(int iPortnum, int Cellnum) throws IOException, InterruptedException {
// DebugLogUtil.getInstance().Debug("TryOpenDoor");
int iPortLocal = -1;
String tempStr;
int[] Tempdate = new int[20];
int iTemplen = 0;
PortName = strComm + String.format("%d", iPortnum);
try {
iPortLocal = mReader.OpenComm(PortName, 115200);
} catch (IOException e) {
e.printStackTrace();
}
if (iPortLocal == 0) {
try {
iPortLocal = mReader.Open4Cell((byte) Cellnum, status, Tempdate);
} catch (InterruptedException | IOException e) {
e.printStackTrace();
DebugLogUtil.getInstance().Error(e.getLocalizedMessage());
}
if (iPortLocal != 0) {
mReader.CloseComm();
// show_Toast("开门失败!" + iPortLocal, 0);
} else {
iPortOpen = 1;
mReader.CloseComm();
iTemplen = Tempdate[0];
iPortOpen = 1;
if (status[0] == 'Y') {
tempStr = context.getString(R.string.OKStatus);
CellDoorStatus ^= 1;
DoorStatusList[Cellnum + 1] = CellDoorStatus;
} else {
tempStr = context.getString(R.string.ErrStatus);
}
// ShowStatus("设备状态: " + PortName.substring(PortName.length() - 5, PortName.length()) + " " + tempStr);
if (CellDoorStatus == 1) {
tempStr = context.getString(R.string.CloseStatus);
} else {
tempStr = context.getString(R.string.OpenStatus);
}
DebugLogUtil.getInstance().Debug(context.getString(R.string.CellDoor) + " " + Cellnum + "):" + tempStr);
ShowInformation("设备信息(" + context.getString(R.string.CellDoor) + " " + Cellnum + "):" + tempStr);
SetCellDoorStatus(Cellnum, context.getString(R.string.CellDoor) + ":" + tempStr);
return 0;
}
} else {
DebugLogUtil.getInstance().Error(PortName + "串口打开失败");
// show_Toast(PortName + "串口打开失败", 1);
}
DebugLogUtil.getInstance().Error("未能链接仓门控制");
// show_Toast("未能链接仓门控制", 0);
return -1; // not found scanner
}
private int TryCellStatus(int iPortnum, int Cellnum) throws IOException, InterruptedException {
// DebugLogUtil.getInstance().Debug("TryCellStatus");
int iPortLocal = -1;
String tempStr;
String cardIdStr;
String doorstr;
byte tempLED = 0;
int[] Tempdate = new int[20];
int iTemplen = 0;
PortName = strComm + String.format("%d", iPortnum);
try {
iPortLocal = mReader.OpenComm(PortName, 115200);
} catch (IOException e) {
e.printStackTrace();
}
// SerialPortFinder serialPortFinder = new SerialPortFinder();
// String[] list = serialPortFinder.getAllDevices();
// Log.e("2222222",list.toString());
if (iPortLocal == 0) {
try {
iPortLocal = mReader.GetStatus4Cell((byte) Cellnum, status, Tempdate);
} catch (InterruptedException | IOException e) {
e.printStackTrace();
}
if (iPortLocal != 0) {
mReader.CloseComm();
// show_Toast(getString(R.string.EnqCell) + iPortLocal, 0);
} else {
iPortOpen = 1;
mReader.CloseComm();
iTemplen = Tempdate[0];
cardIdStr = mReader.byteArrayToHex(status, 2, 4).toUpperCase();
if (status[0] == 'Y') {
tempStr = context.getString(R.string.OKStatus);
} else {
tempStr = context.getString(R.string.ErrStatus);
// ShowStatus("设备状态: " + PortName.substring(PortName.length() - 5, PortName.length()) + " " + tempStr);
SetCellDoorStatus(Cellnum, context.getString(R.string.ErrStatus));
SetCellIDStatus(Cellnum, context.getString(R.string.ErrStatus));
SetCellLED_Status(Cellnum, (byte) 5);
return 0x0ffe1;
}
// ShowStatus("设备状态: " + PortName.substring(PortName.length() - 5, PortName.length()) + " " + tempStr);
if ((status[1] & (byte) 0x01) != 0) {
tempStr = context.getString(R.string.CellDoor) + context.getString(R.string.CloseStatus);
} else {
tempStr = context.getString(R.string.CellDoor) + context.getString(R.string.OpenStatus);
}
doorstr = tempStr;
if ((status[1] & 0x04) != 0) {
tempStr += "\n" + context.getString(R.string.CellLED) + context.getString(R.string.OpenStatus);
tempLED = 1;
} else {
tempStr += "\n" + context.getString(R.string.CellLED) + context.getString(R.string.CloseStatus);
tempLED = 0;
}
LEDStatusList[Cellnum] = tempLED;
if ((status[1] & 0x02) != 0) {
tempStr += "\n" + context.getString(R.string.CellIDStatus) + context.getString(R.string.ValidStatus);
tempStr += "\n ID:" + cardIdStr;
} else {
tempStr += "\n" + context.getString(R.string.CellIDStatus) + context.getString(R.string.EmptyStatus);
cardIdStr = context.getString(R.string.EmptyStatus);
}
// ShowInformation("设备信息(" + getString(R.string.EnqCell) + ":" + Cellnum + "):" + tempStr);
SetCellDoorStatus(Cellnum, doorstr);
SetCellIDStatus(Cellnum, cardIdStr);
SetCellLED_Status(Cellnum, tempLED);
return 0;
}
} else {
// show_Toast(PortName + "串口打开失败", 1);
}
// show_Toast("未能链接设备", 0);
return -1; // not found scanner
}
public void onCellStatus(int mCellnum) {
// DebugLogUtil.getInstance().Debug("onCellStatus");
if (!CheckClick.isClickEvent()) {
return;
}
// ShowStatus("keys");
// ShowInformation("Cell Status:" + mCellnum);
bSearching = true;
new Thread(new Runnable() {
int iTempPortnum = 1;
@Override
public void run() {
while (bSearching) {
try {
if (TryCellStatus(iTempPortnum, mCellnum) == 0) {
bSearching = false;
} else {
iTempPortnum += 1;
if (iTempPortnum > MAXPORTNUM) {
bSearching = false;
break;
}
}
} catch (InterruptedException | IOException e) {
e.printStackTrace();
}
}
}
}).start();
}
public void onCellLED(int mCellnum) {
// DebugLogUtil.getInstance().Debug("onCellLED");
if (!CheckClick.isClickEvent()) {
return;
}
// ShowStatus("keys");
// ShowInformation("Cell LED");
bSearching = true;
new Thread(new Runnable() {
int iTempPortnum = 1;
@Override
public void run() {
while (bSearching) {
try {
if (TryCellLED(iTempPortnum, mCellnum) == 0) {
bSearching = false;
} else {
iTempPortnum += 1;
if (iTempPortnum > MAXPORTNUM) {
bSearching = false;
break;
}
}
} catch (InterruptedException | IOException e) {
e.printStackTrace();
}
}
}
}).start();
}
byte CellLEDStatus = 1;
byte[] LEDStatusList = new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,};
private int TryCellLED(int iPortnum, int Cellnum) throws IOException, InterruptedException {
// DebugLogUtil.getInstance().Debug("TryCellLED");
int iPortLocal = -1;
String tempStr;
int[] Tempdate = new int[20];
int iTemplen = 0;
PortName = strComm + String.format("%d", iPortnum);
try {
iPortLocal = mReader.OpenComm(PortName, 115200);
} catch (IOException e) {
e.printStackTrace();
}
if (iPortLocal == 0) {
try {
//status[0] = CellLEDStatus;
LEDStatusList[Cellnum] ^= 1;
status[0] = LEDStatusList[Cellnum];
iPortLocal = mReader.LED4Cell((byte) Cellnum, status, Tempdate);
} catch (InterruptedException | IOException e) {
e.printStackTrace();
}
if (iPortLocal != 0) {
mReader.CloseComm();
// show_Toast("开灯失败!" + iPortLocal, 0);
} else {
iPortOpen = 1;
mReader.CloseComm();
iTemplen = Tempdate[0];
tempStr = mReader.Hexbyte2Str(status, iTemplen);
// ShowStatus("设备状态: " + PortName.substring(PortName.length()-5, PortName.length())
// +"\n" + tempStr.substring(7, tempStr.length()));
// ShowInformation("设备信息(VER): " +"\n" + mReader.Hexbyte2Str(status,18) );
// Message m = new Message();
// m.what = MESSAGE_STATE_CHANGE;// MESSAGE_EXIT;
// m.obj = status;
// m.arg1 = iPortLocal;
// Activity4GridBox.this.myMessageHandler.sendMessage(m);
iPortOpen = 1;
if (status[0] == 'Y') {
tempStr = context.getString(R.string.OKStatus);
// CellLEDStatus ^= 1;
CellLEDStatus = LEDStatusList[Cellnum];
//LEDStatusList [Cellnum ] ^=1;
SetCellLED_Status(Cellnum, LEDStatusList[Cellnum]);
} else {
tempStr = context.getString(R.string.ErrStatus);
SetCellLED_Status(Cellnum, (byte) 5);
}
// ShowStatus("设备状态: " + PortName.substring(PortName.length() - 5, PortName.length()) + " " + tempStr);
// ShowInformation("设备信息(" + getString(R.string.CellLED) + " " + Cellnum + "):" + tempStr);
return 0;
}
} else {
// show_Toast(PortName + "串口打开失败", 1);
}
// show_Toast("未能链接设备", 0);
return -1; // not found scanner
}
private void SetCellLED_Status(int Cellnum, byte mLEDstatus) {
// 回传act 或者适配器
// DebugLogUtil.getInstance().Debug("灯光状态 -1开0关-" + mLEDstatus);
cabStatusListener.ledStatus(Cellnum, mLEDstatus == 1);
}
/**
* 门的状态
*
* @param Cellnum
* @param doorstatus
*/
private void SetCellDoorStatus(int Cellnum, String doorstatus) {
// DebugLogUtil.getInstance().Debug(Cellnum + ":" + doorstatus);
cabStatusListener.doorOpen(Cellnum, doorstatus.equals("仓位门开"));
if (DoorStatusList[Cellnum - 1] == 1) {
} else {
// 仓库状态
}
}
void ShowInformation(final String str) {
DebugLogUtil.getInstance().Debug(str);
}
/**
* 是否有物品
*
* @param Cellnum
* @param sIDstatus
*/
private void SetCellIDStatus(int Cellnum, String sIDstatus) {
cabStatusListener.isNothing(Cellnum, sIDstatus.equals("空"));
// DebugLogUtil.getInstance().Debug(Cellnum + ":" + sIDstatus);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment