diff --git a/commonSPort/src/main/java/com/common/queue/Task/BaseTask.java b/commonSPort/src/main/java/com/common/queue/Task/BaseTask.java index 8b69f84..a51783c 100644 --- a/commonSPort/src/main/java/com/common/queue/Task/BaseTask.java +++ b/commonSPort/src/main/java/com/common/queue/Task/BaseTask.java @@ -13,25 +13,38 @@ public class BaseTask implements ITask { private TaskPriority mTaskPriority = TaskPriority.DEFAULT; //默认优先级 private int mSequence;// 入队次序 private Boolean mTaskStatus = false; // 标志任务状态,是否仍在展示 - protected WeakReference taskQueue;//阻塞队列 + protected WeakReference taskScheduler; + protected WeakReference taskQueue;//消息队列阻塞队列 protected int duration = 0; //任务执行时间 //此队列用来实现任务时间不确定的队列阻塞功能 private PriorityBlockingQueue blockQueue; + + //关联调度器 + public void attachScheduler(TaskScheduler taskScheduler) { + this.taskScheduler = new WeakReference<>(taskScheduler); + this.taskQueue = new WeakReference<>(taskScheduler.getQueue()); + } + //构造函数 public BaseTask() { - taskQueue = new WeakReference<>(BlockTaskQueue.getInstance()); blockQueue = new PriorityBlockingQueue<>(); } + //入队实现 @Override - public void enqueue() { - TaskScheduler.getInstance().enqueue(this); + public void enqueue() throws Exception { + if (taskScheduler == null) { + throw new Exception("必须关联调度器使用"); + } + taskScheduler.get().enqueue(this); } + //执行任务方法,此时标记为设为true,并且将当前任务记录下来 @Override public void doTask() { mTaskStatus = true; } + //任务执行完成,改变标记位,将任务在队列中移除,并且把记录清除 @Override public void finishTask() { @@ -39,53 +52,62 @@ public class BaseTask implements ITask { this.taskQueue.get().remove(this); Log.d(TAG, taskQueue.get().size() + ""); } + //设置任务优先级实现 @Override public ITask setPriority(TaskPriority mTaskPriority) { this.mTaskPriority = mTaskPriority; return this; } + //设置任务执行时间 public ITask setDuration(int duration) { this.duration = duration; return this; } + //获取任务优先级 @Override public TaskPriority getPriority() { return mTaskPriority; } + //获取任务执行时间 @Override public int getDuration() { return duration; } + //设置任务次序 @Override public void setSequence(int mSequence) { this.mSequence = mSequence; } + //获取任务次序 @Override public int getSequence() { return mSequence; } + // 获取任务状态 @Override public boolean getStatus() { return mTaskStatus; } + //阻塞任务执行 @Override public void blockTask() throws Exception { blockQueue.take(); //如果队列里面没数据,就会一直阻塞 } + //解除阻塞 - @Override - public void unLockBlock() { + @Override + public void unLockBlock() { blockQueue.add(1); //往里面随便添加一个数据,阻塞就会解除 - } - + } + /** * 排队实现 * 优先级的标准如下: @@ -99,6 +121,7 @@ public class BaseTask implements ITask { return me == it ? this.getSequence() - another.getSequence() : it.ordinal() - me.ordinal(); } + //输出一些信息 @Override public String toString() { diff --git a/commonSPort/src/main/java/com/common/queue/Task/ITask.java b/commonSPort/src/main/java/com/common/queue/Task/ITask.java index 458f798..f9e30ad 100644 --- a/commonSPort/src/main/java/com/common/queue/Task/ITask.java +++ b/commonSPort/src/main/java/com/common/queue/Task/ITask.java @@ -3,7 +3,7 @@ package com.common.queue.Task; public interface ITask extends Comparable { // 将该任务插入队列 - void enqueue(); + void enqueue() throws Exception; // 执行具体任务的方法 void doTask(); diff --git a/commonSPort/src/main/java/com/common/queue/TaskScheduler.java b/commonSPort/src/main/java/com/common/queue/TaskScheduler.java index 7b04d2a..e3f635b 100644 --- a/commonSPort/src/main/java/com/common/queue/TaskScheduler.java +++ b/commonSPort/src/main/java/com/common/queue/TaskScheduler.java @@ -6,25 +6,17 @@ public class TaskScheduler { private final String TAG = "TaskScheduler"; private BlockTaskQueue mTaskQueue = new BlockTaskQueue(); private TaskExecutor mExecutor; - -// private static class ShowDurationHolder { -// private final static TaskScheduler INSTANCE = new TaskScheduler(); -// } - + public TaskScheduler() { initExecutor(); } - + private void initExecutor() { mExecutor = new TaskExecutor(mTaskQueue); mExecutor.start(); } - -// public static TaskScheduler getInstance() { -// return ShowDurationHolder.INSTANCE; -// } - - public void enqueue(ITask task) { + + public void enqueue(ITask task) { //因为TaskScheduler这里写成单例,如果isRunning改成false的话,不判断一下,就会一直都是false if (!mExecutor.isRunning()) { mExecutor.startRunning(); @@ -32,13 +24,17 @@ public class TaskScheduler { //按照优先级插入队列 依次播放 mTaskQueue.add(task); } - + public void resetExecutor() { mExecutor.resetExecutor(); } - + public void clearExecutor() { mExecutor.clearExecutor(); } + + public BlockTaskQueue getQueue() { + return mTaskQueue; + } } \ No newline at end of file diff --git a/commonSPort/src/main/java/com/common/serialport/EasySerialPort.kt b/commonSPort/src/main/java/com/common/serialport/EasySerialPort.kt index ccb1fbc..70f913f 100644 --- a/commonSPort/src/main/java/com/common/serialport/EasySerialPort.kt +++ b/commonSPort/src/main/java/com/common/serialport/EasySerialPort.kt @@ -12,8 +12,9 @@ class EasySerialPort( private val mReceiver: (ByteArray) -> Unit ) : Runnable { private var mPort: SerialPort? = null - private var startReceiveMsg = true - private var autoRetryConnect = false + private var mStartReceiveMsg = true + private var mAutoRetryConnect = false + private val mTaskScheduler = TaskScheduler() init { openSerialPort(); @@ -22,7 +23,7 @@ class EasySerialPort( private fun openSerialPort() { mPort = SerialPortHelper.openSerialPort(portPath, baudRate) mPort?.let { - startReceiveMsg = true + mStartReceiveMsg = true Thread(this).start() } } @@ -31,7 +32,7 @@ class EasySerialPort( * 关闭串口 */ open fun closePort() { - startReceiveMsg = false + mStartReceiveMsg = false SerialPortHelper.closePort(mPort) } @@ -39,7 +40,7 @@ class EasySerialPort( * 循环读消息 */ override fun run() { - while (startReceiveMsg) { + while (mStartReceiveMsg) { try { val ips = mPort?.inputStream val readByte = ips?.available()?.let { ByteArray(it) } @@ -55,7 +56,7 @@ class EasySerialPort( "read msg error; path = " + portPath + ", error msg = " + e.message ) e.printStackTrace() - if (autoRetryConnect) { + if (mAutoRetryConnect) { closePort() openSerialPort() } @@ -78,26 +79,27 @@ class EasySerialPort( "write msg error; path = " + portPath + ", error msg = " + e.message ) e.printStackTrace() - if (autoRetryConnect) { + if (mAutoRetryConnect) { closePort() openSerialPort() } } } - TaskScheduler.getInstance().enqueue(task) + task.attachScheduler(mTaskScheduler) + task.enqueue() } /** * 开始自动重连 */ open fun enableAutoConnect() { - this.autoRetryConnect = true + this.mAutoRetryConnect = true } /** * 关闭自动重连 */ open fun disableAutoConnect() { - this.autoRetryConnect = true + this.mAutoRetryConnect = true } } \ No newline at end of file diff --git a/commonSPort/src/main/java/com/common/serialport/utils/CommonPortUtils.kt b/commonSPort/src/main/java/com/common/serialport/utils/CommonPortUtils.kt index 1128523..f308f5a 100644 --- a/commonSPort/src/main/java/com/common/serialport/utils/CommonPortUtils.kt +++ b/commonSPort/src/main/java/com/common/serialport/utils/CommonPortUtils.kt @@ -9,7 +9,7 @@ import com.common.serialport.EasySerialPort */ object CommonPortUtils { - private val portMaps = HashMap() + private val mPortMaps = HashMap() /** * 打开串口 @@ -22,11 +22,11 @@ object CommonPortUtils { baudRate: Int, mReceiver: (ByteArray) -> Unit ) { - if (portMaps.containsKey(portPath)) { + if (mPortMaps.containsKey(portPath)) { return } val easyPort = EasySerialPort(portPath, baudRate, mReceiver) - portMaps.put(portPath, easyPort) + mPortMaps.put(portPath, easyPort) } /** @@ -35,7 +35,7 @@ object CommonPortUtils { * @param data 消息byte数组 */ fun sendMsg(portPath: String, data: ByteArray) { - SinglePortUtils.easyPort?.write(data) + SinglePortUtils.mEasyPort?.write(data) } /** @@ -44,7 +44,7 @@ object CommonPortUtils { * @param data String类型消息,内部会toByteArray */ fun sendMsg(portPath: String, data: String) { - SinglePortUtils.easyPort?.write(data.toByteArray()) + SinglePortUtils.mEasyPort?.write(data.toByteArray()) } /** @@ -52,19 +52,19 @@ object CommonPortUtils { * @param portPath 串口地址 */ fun release(portPath: String) { - if (portMaps.containsKey(portPath)) { - portMaps.get(portPath)?.closePort() + if (mPortMaps.containsKey(portPath)) { + mPortMaps.get(portPath)?.closePort() } - portMaps.remove(portPath) + mPortMaps.remove(portPath) } /** * 释放所有串口 */ fun releaseAll() { - portMaps.forEach { + mPortMaps.forEach { it.value?.closePort() } - portMaps.clear() + mPortMaps.clear() } } \ No newline at end of file diff --git a/commonSPort/src/main/java/com/common/serialport/utils/SinglePortUtils.kt b/commonSPort/src/main/java/com/common/serialport/utils/SinglePortUtils.kt index 7d06f16..0fe21b0 100644 --- a/commonSPort/src/main/java/com/common/serialport/utils/SinglePortUtils.kt +++ b/commonSPort/src/main/java/com/common/serialport/utils/SinglePortUtils.kt @@ -8,7 +8,7 @@ import com.common.serialport.EasySerialPort * 操作单个串口工具类 */ object SinglePortUtils { - var easyPort: EasySerialPort? = null + var mEasyPort: EasySerialPort? = null /** * 初始化参数 @@ -21,7 +21,7 @@ object SinglePortUtils { baudRate: Int, mReceiver: (ByteArray) -> Unit ) { - easyPort = EasySerialPort(portPath, baudRate, mReceiver) + mEasyPort = EasySerialPort(portPath, baudRate, mReceiver) } /** @@ -30,7 +30,7 @@ object SinglePortUtils { * @param data 消息byte数组 */ fun sendMsg(data: ByteArray) { - easyPort?.write(data) + mEasyPort?.write(data) } /** @@ -39,13 +39,13 @@ object SinglePortUtils { * @param data String类型消息,内部会toByteArray */ fun sendMsg(data: String) { - easyPort?.write(data.toByteArray()) + mEasyPort?.write(data.toByteArray()) } /** * 释放串口 */ fun release(){ - easyPort?.closePort() + mEasyPort?.closePort() } } \ No newline at end of file