Commit 1b40abcc by pye52

增加多设备兼容方案

1、现在会根据设备型号匹配对应的串口读取方案,当前支持设备为: 天波(TPS)及优卡特(T3)
2、提供IQRCodeScan接口,通过该接口以抽象各机型串口读取实现,所有实现类由QRCodeScanFactory实例化
3、同时导入Android提供的SerialPort类及对应的so库(已更新混淆文件,不混淆SerialPort类)
parent 4f7c6881
...@@ -165,9 +165,12 @@ ...@@ -165,9 +165,12 @@
#不混淆smartcanteen下的所有类 #不混淆smartcanteen下的所有类
-keep class com.bgycc.smartcanteen.** { *; } -keep class com.bgycc.smartcanteen.** { *; }
#不混淆tps550下的所有类 #不混淆tps下的所有类
-keep class com.telpo.tps550.api.** { *; } -keep class com.telpo.tps550.api.** { *; }
#不混淆serialport下的所有类
-keep class android_serialport_api.** { *; }
#okhttp #okhttp
-dontwarn okhttp3.** -dontwarn okhttp3.**
-keep class okhttp3.**{*;} -keep class okhttp3.**{*;}
......
package android_serialport_api;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Google官方代码
* 此类的作用为,JNI的调用,用来加载.so文件的
* 获取串口输入输出流
*/
public class SerialPort {
private FileDescriptor mFd;
private FileInputStream mFileInputStream;
private FileOutputStream mFileOutputStream;
static {
System.loadLibrary("serial_port");
}
public SerialPort(String devicePath, int baudRate, int flags) throws SecurityException {
this(new File(devicePath), baudRate, flags);
}
private SerialPort(File device, int baudRate, int flags) throws SecurityException {
if (!device.canRead() || !device.canWrite()) {
try {
Process su = Runtime.getRuntime().exec("/system/bin/su");
String cmd = "chmod 666 " + device.getAbsolutePath() + "\n" + "exit\n";
su.getOutputStream().write(cmd.getBytes());
if ((su.waitFor() != 0) || !device.canRead() || !device.canWrite()) {
throw new RuntimeException("没有串口权限");
}
} catch (Exception e) {
throw new RuntimeException("提权失败" + e.getMessage(), e);
}
}
mFd = open(device.getAbsolutePath(), baudRate, flags);
mFileInputStream = new FileInputStream(mFd);
mFileOutputStream = new FileOutputStream(mFd);
}
public InputStream getInputStream() {
return mFileInputStream;
}
public OutputStream getOutputStream() {
return mFileOutputStream;
}
private native static FileDescriptor open(String path, int baudrate, int flags);
public native void close();
}
package android_serialport_api;
import android.util.Log;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.util.Iterator;
import java.util.Vector;
/**
* Google官方代码
* 此类的作用为,寻找得到有效的串口的物理地址。
* 如果你本身就知道串口的地址如:ttyS1、ttyS2,那么这个类就可以不用了。
*
*/
public class SerialPortFinder {
public class Driver {
public Driver(String name, String root) {
mDriverName = name;
mDeviceRoot = root;
}
private String mDriverName;
private String mDeviceRoot;
Vector<File> mDevices = null;
public Vector<File> getDevices() {
if (mDevices == null) {
mDevices = new Vector<>();
File dev = new File("/dev");
File[] files = dev.listFiles();
int i;
for (i=0; i<files.length; i++) {
if (files[i].getAbsolutePath().startsWith(mDeviceRoot)) {
Log.d(TAG, "Found new device: " + files[i]);
mDevices.add(files[i]);
}
}
}
return mDevices;
}
public String getName() {
return mDriverName;
}
}
private static final String TAG = "SerialPort";
private Vector<Driver> mDrivers = null;
Vector<Driver> getDrivers() throws IOException {
if (mDrivers == null) {
mDrivers = new Vector<>();
LineNumberReader r = new LineNumberReader(new FileReader("/proc/tty/drivers"));
String l;
while((l = r.readLine()) != null) {
// Issue 3:
// Since driver name may contain spaces, we do not extract driver name with split()
String drivername = l.substring(0, 0x15).trim();
String[] w = l.split(" +");
if ((w.length >= 5) && (w[w.length-1].equals("serial"))) {
Log.d(TAG, "Found new driver " + drivername + " on " + w[w.length-4]);
mDrivers.add(new Driver(drivername, w[w.length-4]));
}
}
r.close();
}
return mDrivers;
}
public String[] getAllDevices() {
Vector<String> devices = new Vector<String>();
// Parse each driver
Iterator<Driver> itdriv;
try {
itdriv = getDrivers().iterator();
while(itdriv.hasNext()) {
Driver driver = itdriv.next();
Iterator<File> itdev = driver.getDevices().iterator();
while(itdev.hasNext()) {
String device = itdev.next().getName();
String value = String.format("%s (%s)", device, driver.getName());
devices.add(value);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return devices.toArray(new String[devices.size()]);
}
public String[] getAllDevicesPath() {
Vector<String> devices = new Vector<String>();
// Parse each driver
Iterator<Driver> itdriv;
try {
itdriv = getDrivers().iterator();
while(itdriv.hasNext()) {
Driver driver = itdriv.next();
Iterator<File> itdev = driver.getDevices().iterator();
while(itdev.hasNext()) {
String device = itdev.next().getAbsolutePath();
devices.add(device);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return devices.toArray(new String[devices.size()]);
}
}
package com.bgycc.smartcanteen.qrcode; package com.bgycc.smartcanteen.qrcode;
import com.blankj.utilcode.util.LogUtils; import com.blankj.utilcode.util.LogUtils;
import com.telpo.tps550.api.serial.Serial;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
...@@ -9,24 +8,14 @@ import java.util.Arrays; ...@@ -9,24 +8,14 @@ import java.util.Arrays;
import static com.bgycc.smartcanteen.utils.SmartCanteenUtils.TAG; import static com.bgycc.smartcanteen.utils.SmartCanteenUtils.TAG;
/** public abstract class BaseQRCodeScan implements IQRCodeScan {
* 读取串口数据
*/
public class QRCodeScan {
private Serial serial;
private InputStream serialPortIS; private InputStream serialPortIS;
public QRCodeScan(String filePath) throws Exception { void setup(InputStream serialPortIS) {
serial = new Serial(filePath, 115200, 0); this.serialPortIS = serialPortIS;
serialPortIS = serial.getInputStream();
} }
/** @Override
* 串口是否有数据
* @return <br/>
* true => 串口有数据
* false => 串口没有数据
*/
public boolean available() { public boolean available() {
if (serialPortIS == null) { if (serialPortIS == null) {
return false; return false;
...@@ -38,10 +27,7 @@ public class QRCodeScan { ...@@ -38,10 +27,7 @@ public class QRCodeScan {
} }
} }
/** @Override
* 读取串口数据,该方法会阻塞当前线程
* @return 从串口读取的字节
*/
public String scan() throws IOException { public String scan() throws IOException {
if (serialPortIS == null) { if (serialPortIS == null) {
return ""; return "";
...@@ -64,9 +50,7 @@ public class QRCodeScan { ...@@ -64,9 +50,7 @@ public class QRCodeScan {
return str; return str;
} }
/** @Override
* 关闭串口
*/
public void close() { public void close() {
if (serialPortIS != null) { if (serialPortIS != null) {
try { try {
...@@ -75,9 +59,6 @@ public class QRCodeScan { ...@@ -75,9 +59,6 @@ public class QRCodeScan {
LogUtils.w(TAG, "端口关闭失败: " + e.getMessage(), e); LogUtils.w(TAG, "端口关闭失败: " + e.getMessage(), e);
} }
} }
if (serial != null) {
serial.close();
}
} }
private boolean end(byte[] buf) { private boolean end(byte[] buf) {
......
package com.bgycc.smartcanteen.qrcode;
import java.io.IOException;
public interface IQRCodeScan {
/**
* 串口是否有数据
*
* @return <br/>
* true => 串口有数据
* false => 串口没有数据
*/
boolean available();
/**
* 读取串口数据,该方法会阻塞当前线程
*
* @return 从串口读取的字节
*/
String scan() throws IOException;
/**
* 关闭串口
*/
void close();
}
package com.bgycc.smartcanteen.qrcode;
import android.os.Build;
public class QRCodeScanFactory {
private static final String TPS = "TPS";
private static final String T3 = "T3";
private static final String TPS_PORT = "/dev/ttyS0";
private static final String T3_PORT = "/dev/ttyS6";
public static IQRCodeScan create() throws Exception {
if (Build.DEVICE.contains(TPS)) {
return new TPS(TPS_PORT);
} else if (Build.DEVICE.contains(T3)) {
return new T3(T3_PORT);
} else {
throw new RuntimeException("不明设备型号");
}
}
}
package com.bgycc.smartcanteen.qrcode;
import android_serialport_api.SerialPort;
public class T3 extends BaseQRCodeScan {
private SerialPort serial;
public T3(String filePath) throws Exception {
serial = new SerialPort(filePath, 115200, 0);
setup(serial.getInputStream());
}
@Override
public void close() {
super.close();
if (serial != null) {
serial.close();
}
}
}
package com.bgycc.smartcanteen.qrcode;
import com.telpo.tps550.api.serial.Serial;
/**
* 天波580C串口读取
*/
public class TPS extends BaseQRCodeScan {
private Serial serial;
public TPS(String filePath) throws Exception {
serial = new Serial(filePath, 115200, 0);
setup(serial.getInputStream());
}
@Override
public void close() {
super.close();
if (serial != null) {
serial.close();
}
}
}
...@@ -8,9 +8,10 @@ import androidx.lifecycle.ViewModel; ...@@ -8,9 +8,10 @@ import androidx.lifecycle.ViewModel;
import com.bgycc.smartcanteen.entity.Command; import com.bgycc.smartcanteen.entity.Command;
import com.bgycc.smartcanteen.entity.PayData; import com.bgycc.smartcanteen.entity.PayData;
import com.bgycc.smartcanteen.qrcode.IQRCodeScan;
import com.bgycc.smartcanteen.qrcode.QRCodeScanFactory;
import com.bgycc.smartcanteen.state.QRCodeState; import com.bgycc.smartcanteen.state.QRCodeState;
import com.bgycc.smartcanteen.executor.SCTaskExecutor; import com.bgycc.smartcanteen.executor.SCTaskExecutor;
import com.bgycc.smartcanteen.qrcode.QRCodeScan;
import com.bgycc.smartcanteen.repository.CommandRepository; import com.bgycc.smartcanteen.repository.CommandRepository;
import com.bgycc.smartcanteen.repository.PayDataRepository; import com.bgycc.smartcanteen.repository.PayDataRepository;
import com.bgycc.smartcanteen.utils.SmartCanteenUtils; import com.bgycc.smartcanteen.utils.SmartCanteenUtils;
...@@ -33,14 +34,13 @@ import static com.bgycc.smartcanteen.utils.SmartCanteenUtils.TAG; ...@@ -33,14 +34,13 @@ import static com.bgycc.smartcanteen.utils.SmartCanteenUtils.TAG;
*/ */
public class QRCodeViewModel extends ViewModel { public class QRCodeViewModel extends ViewModel {
private static final String RESPONSE_ACTION = "action"; private static final String RESPONSE_ACTION = "action";
private static final String PORT_PATH = "/dev/ttyS0";
private static final long DELAY = 1500; private static final long DELAY = 1500;
private PayDataRepository payDataRepository; private PayDataRepository payDataRepository;
private CommandRepository commandRepository; private CommandRepository commandRepository;
private String deviceSN; private String deviceSN;
private ScheduledFuture<?> scanFuture; private ScheduledFuture<?> scanFuture;
private QRCodeScan scan; private IQRCodeScan scan;
private MutableLiveData<QRCodeState> qrCodeState = new MutableLiveData<>(); private MutableLiveData<QRCodeState> qrCodeState = new MutableLiveData<>();
...@@ -58,7 +58,7 @@ public class QRCodeViewModel extends ViewModel { ...@@ -58,7 +58,7 @@ public class QRCodeViewModel extends ViewModel {
public void initialize() { public void initialize() {
try { try {
scan = new QRCodeScan(PORT_PATH); scan = QRCodeScanFactory.create();
scan(); scan();
} catch (Exception e) { } catch (Exception e) {
LogUtils.e(TAG, "串口初始化失败: " + e.getMessage(), e); LogUtils.e(TAG, "串口初始化失败: " + e.getMessage(), e);
......
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 sign in to comment