You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

291 lines
6.9 KiB

package com.dreamworks.boditech.service;
import com.dreamworks.boditech.driver.*;
import com.dreamworks.boditech.driver.consumable.*;
import com.dreamworks.boditech.driver.entity.*;
import com.dreamworks.boditech.driver.task.*;
import com.dreamworks.boditech.driver.task.TaskStopReset;
import com.dreamworks.boditech.entity.parameter.ParamTestTubeRackTaskSave;
import com.dreamworks.boditech.entity.parameter.ParamTestTubeRackTaskSaveTube;
import com.dreamworks.boditech.utils.AppError;
import com.dreamworks.boditech.utils.AppRuntimeException;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.*;
@Service
public class DeviceService {
@Resource
public Device device;
@Resource
public RuntimeVariableService runtimeVariableService;
@Resource
private ActionLogService actionLog;
@Resource
private TestService testService;
@Resource
private ProjectService projectService;
// tasks
private final List<Task> tasks = new ArrayList<>();
// task executor
private Executor taskExecutor;
// task executor thread
private Thread taskExecutorThread;
/**
* reset device
*/
public void reset() {
TaskStartReset task = new TaskStartReset();
task.mode = TaskStartReset.MODE_NORMAL;
String deviceWorkingStatus = this.runtimeVariableService.getString("DeviceWorkingStatus");
if ( "RUNNING".equals(deviceWorkingStatus) ) {
task.mode = TaskStartReset.MODE_ERROR;
}
Executor.executeTask(this.device, task);
this.runtimeVariableService.setString("DeviceWorkingStatus","RUNNING");
}
/**
* load consumable resources
*/
public void load() {
this.actionLog.log("device.load");
TaskLoad task = new TaskLoad();
Executor.executeTask(this.device, task);
}
/**
* unload consumable resources
*/
public void unload() {
if ( this.isExecutorRunning() ) {
throw new AppRuntimeException(AppError.DEVICE_BUSY);
}
this.actionLog.log("device.unload");
this.device.testCards.clear();
this.device.bufferTubes.clear();
this.device.largeBufferTubes.clear();
this.projectService.unload();
}
/**
* start task executor
*/
public void start() {
if ( null != this.taskExecutor ) {
throw new AppRuntimeException(AppError.DEVICE_ALREADY_STARTED);
}
if ( !this.device.testCards.getIsLoaded() ) {
throw new AppRuntimeException(AppError.DEVICE_CONSUMABLE_NOT_LOADED);
}
if (this.device.pipetteTips.isEmpty()
|| this.device.testCards.isEmpty()
|| this.device.bufferTubes.isEmpty()
) {
throw new AppRuntimeException(AppError.DEVICE_CONSUMABLE_NOT_ENOUGH);
}
this.actionLog.log("device.start");
this.taskExecutor = new Executor(this.testService, this.device);
this.taskExecutorThread = new Thread(this.taskExecutor);
this.taskExecutorThread.setName("task-executor");
this.taskExecutorThread.start();
}
// stop
public void stop() {
if ( null == this.taskExecutor ) {
throw new AppRuntimeException(AppError.DEVICE_NOT_STARTED);
}
this.taskExecutor.stop();
try {
this.taskExecutorThread.join();
} catch (InterruptedException e) {
throw new AppRuntimeException(AppError.DEVICE_STOP_FAILED);
}
TaskStopReset task = new TaskStopReset();
Executor.executeTask(this.device, task);
this.runtimeVariableService.setString("DeviceWorkingStatus", "STOPPED");
this.taskExecutor = null;
}
// pause
public void pause() {
if ( null == this.taskExecutor ) {
throw new AppRuntimeException(AppError.DEVICE_NOT_STARTED);
}
this.taskExecutor.pause();
}
// resume
public void resume() {
if ( null == this.taskExecutor ) {
throw new AppRuntimeException(AppError.DEVICE_NOT_STARTED);
}
this.taskExecutor.resume();
}
// reboot the device
public void reboot() {
if ( null != this.taskExecutor ) {
throw new AppRuntimeException(AppError.DEVICE_BUSY);
}
try {
Runtime.getRuntime().exec("reboot");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
// power off the device
public void poweroff() {
if ( null != this.taskExecutor ) {
throw new AppRuntimeException(AppError.DEVICE_BUSY);
}
try {
Runtime.getRuntime().exec("sync");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
// get task executor
public Executor getTaskExecutor() {
return this.taskExecutor;
}
/**
* get is executor running
* @return is executor running
*/
public Boolean isExecutorRunning() {
if ( null == this.taskExecutor ) {
return false;
}
return Executor.STATUS_RUNNING.equals(this.taskExecutor.getStatus());
}
// get executor working status
public String getExecutorWorkingStatus() {
if ( null == this.taskExecutor ) {
return null;
}
return this.taskExecutor.workingStatus;
}
// scan and load test cards
public void testCardLoad() {
TaskTestCardLoad task = new TaskTestCardLoad();
Executor.executeTask(this.device, task);
}
// update test cards by box
public void testCardUpdateByBox(ParamTestCardUpdateByBox update) {
this.device.testCards.testCardUpdateByBox(update);
}
// update pipette tips
public void pipetteTipUpdateByBox(ParamPipetteTipUpdate param) {
this.device.pipetteTips.updateByBox(param);
}
// load large buffer tubes
public List<CsmLargeBufferTube> largeBufferTubeLoad() {
TaskLargeBufferTubeLoad task = new TaskLargeBufferTubeLoad();
Executor.executeTask(this.device, task);
return this.device.largeBufferTubes.getAll();
}
// update large buffer tubes
public void largeBufferTubeUpdate(ParamBufferTubeUpdateByBox param) {
this.device.largeBufferTubes.update(param);
}
// load buffer tubes
public void bufferTubeLoad() {
TaskBufferTubeLoad task = new TaskBufferTubeLoad();
Executor.executeTask(this.device, task);
}
// update buffer tubes by box
public void bufferTubeUpdateByBox(ParamBufferTubeUpdateByBox param ) {
this.device.bufferTubes.updateByBox(param);
}
// get status
public Map<String, Object> status() {
return new HashMap<>();
}
// append task
public void taskAppend(Task task) {
synchronized (this.tasks) {
this.tasks.add(task);
this.tasks.notifyAll();
}
}
}