10 changed files with 229 additions and 40 deletions
-
3src/main/java/a8k/appbase/appret/AppRet.java
-
62src/main/java/a8k/service/FrontEndEventRouter.java
-
4src/main/java/a8k/service/UIEventProcesser.java
-
6src/main/java/a8k/service/appeventbus/appevent/AppEvent.java
-
26src/main/java/a8k/service/appeventbus/appevent/NewAppIDCardDetectEvent.java
-
40src/main/java/a8k/service/hardware/canbus/A8kCanBusService.java
-
4src/main/java/a8k/service/hardware/canbus/protocol/CmdId.java
-
51src/main/java/a8k/service/project_mgr/ProjectItemMgrService.java
-
17src/main/java/a8k/utils/ByteArray.java
-
56src/main/java/a8k/utils/wq/ZWorkQueue.java
@ -0,0 +1,62 @@ |
|||
package a8k.service; |
|||
|
|||
import a8k.appbase.AppEventListener; |
|||
import a8k.appbase.appret.AppRet; |
|||
import a8k.controler.engineer.utils.EngineerPageTab; |
|||
import a8k.controler.engineer.utils.EnginnerPageAction; |
|||
import a8k.service.appeventbus.AppEventBusService; |
|||
import a8k.service.appeventbus.appevent.A8kHardwareReport; |
|||
import a8k.service.appeventbus.appevent.AppEvent; |
|||
import jakarta.annotation.PostConstruct; |
|||
import jakarta.annotation.Resource; |
|||
import org.slf4j.Logger; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import java.util.Queue; |
|||
|
|||
@Component |
|||
@EngineerPageTab(name = "FrontEndEventRouter") |
|||
public class FrontEndEventRouter implements AppEventListener { |
|||
static Logger logger = org.slf4j.LoggerFactory.getLogger(FrontEndEventRouter.class); |
|||
|
|||
@Resource |
|||
AppEventBusService eventBus; |
|||
|
|||
private final Queue<AppEvent> appEventQueue = new java.util.concurrent.ConcurrentLinkedQueue<>(); |
|||
|
|||
|
|||
@PostConstruct |
|||
public void init() { |
|||
eventBus.regListener(this); |
|||
} |
|||
|
|||
public Boolean filterEvent(AppEvent event) { |
|||
if (event instanceof A8kHardwareReport) { |
|||
return true; |
|||
} |
|||
return false; |
|||
} |
|||
|
|||
synchronized public AppEvent _pollAppEvent() { |
|||
return appEventQueue.poll(); |
|||
} |
|||
|
|||
@Override public void onAppEvent(AppEvent event) { |
|||
if (filterEvent(event)) { |
|||
return; |
|||
} |
|||
|
|||
appEventQueue.add(event); |
|||
if (appEventQueue.size() >= 10) { |
|||
logger.warn("too many events in queue, drop some"); |
|||
_pollAppEvent(); |
|||
} |
|||
} |
|||
|
|||
@EnginnerPageAction(name = "pollAppEvent") |
|||
public AppRet<AppEvent> pollAppEvent() { |
|||
AppEvent event = _pollAppEvent(); |
|||
return AppRet.success(event); |
|||
} |
|||
|
|||
} |
@ -1,4 +0,0 @@ |
|||
package a8k.service; |
|||
|
|||
public class UIEventProcesser { |
|||
} |
@ -1,8 +1,8 @@ |
|||
package a8k.service.appeventbus.appevent; |
|||
|
|||
public class AppEvent { |
|||
public String name; |
|||
public AppEvent(String name) { |
|||
this.name = name; |
|||
public String typeName; |
|||
public AppEvent(String typeName) { |
|||
this.typeName = typeName; |
|||
} |
|||
} |
@ -0,0 +1,26 @@ |
|||
package a8k.service.appeventbus.appevent; |
|||
|
|||
import a8k.db.ProjectInfo; |
|||
|
|||
public class NewAppIDCardDetectEvent extends AppEvent { |
|||
Boolean newIdCardInfo; |
|||
ProjectInfo projectInfo; |
|||
|
|||
public NewAppIDCardDetectEvent() { |
|||
super(NewAppIDCardDetectEvent.class.getSimpleName()); |
|||
} |
|||
|
|||
public NewAppIDCardDetectEvent(Boolean newIdCardInfo, ProjectInfo projectInfo) { |
|||
super(NewAppIDCardDetectEvent.class.getSimpleName()); |
|||
this.newIdCardInfo = newIdCardInfo; |
|||
this.projectInfo = projectInfo; |
|||
} |
|||
|
|||
public Boolean getNewIdCardInfo() { |
|||
return newIdCardInfo; |
|||
} |
|||
|
|||
public ProjectInfo getProjectInfo() { |
|||
return projectInfo; |
|||
} |
|||
} |
@ -0,0 +1,56 @@ |
|||
package a8k.utils.wq; |
|||
|
|||
import org.slf4j.Logger; |
|||
import org.slf4j.LoggerFactory; |
|||
|
|||
import java.util.concurrent.LinkedBlockingQueue; |
|||
import java.util.concurrent.ExecutorService; |
|||
import java.util.concurrent.Executors; |
|||
|
|||
public class ZWorkQueue { |
|||
static final Logger logger = LoggerFactory.getLogger(ZWorkQueue.class); |
|||
|
|||
private final LinkedBlockingQueue<Runnable> workQueue; |
|||
private final ExecutorService executor; |
|||
|
|||
int threadPoolSize; |
|||
|
|||
public ZWorkQueue(int queueSize, int threadPoolSize) { |
|||
workQueue = new LinkedBlockingQueue<>(queueSize); |
|||
executor = Executors.newFixedThreadPool(threadPoolSize); |
|||
this.threadPoolSize = threadPoolSize; |
|||
// 启动消费者线程 |
|||
startWorkerThreads(); |
|||
} |
|||
|
|||
private void startWorkerThreads() { |
|||
for (int i = 0; i < threadPoolSize; i++) { |
|||
executor.submit(() -> { |
|||
while (!Thread.currentThread().isInterrupted()) { |
|||
try { |
|||
// 从队列中获取任务 |
|||
Runnable task = workQueue.take(); |
|||
task.run(); |
|||
} catch (InterruptedException ignored) { |
|||
} catch (Exception e) { |
|||
logger.error("Error while running task", e); |
|||
} |
|||
} |
|||
}); |
|||
} |
|||
} |
|||
|
|||
public void addTask(Runnable task) { |
|||
try { |
|||
// 将任务添加到队列中 |
|||
workQueue.put(task); |
|||
} catch (InterruptedException e) { |
|||
Thread.currentThread().interrupt(); |
|||
} |
|||
} |
|||
|
|||
public void shutdown() { |
|||
// 关闭线程池 |
|||
executor.shutdown(); |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue