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

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. package com.dreamworks.boditech.service;
  2. import com.dreamworks.boditech.driver.*;
  3. import com.dreamworks.boditech.driver.consumable.*;
  4. import com.dreamworks.boditech.driver.entity.*;
  5. import com.dreamworks.boditech.driver.task.*;
  6. import com.dreamworks.boditech.driver.task.TaskStopReset;
  7. import com.dreamworks.boditech.entity.parameter.ParamTestTubeRackTaskSave;
  8. import com.dreamworks.boditech.entity.parameter.ParamTestTubeRackTaskSaveTube;
  9. import com.dreamworks.boditech.utils.AppError;
  10. import com.dreamworks.boditech.utils.AppRuntimeException;
  11. import jakarta.annotation.Resource;
  12. import org.springframework.stereotype.Service;
  13. import java.io.IOException;
  14. import java.util.*;
  15. @Service
  16. public class DeviceService {
  17. @Resource
  18. public Device device;
  19. @Resource
  20. public RuntimeVariableService runtimeVariableService;
  21. @Resource
  22. private ActionLogService actionLog;
  23. @Resource
  24. private TestService testService;
  25. @Resource
  26. private ProjectService projectService;
  27. // tasks
  28. private final List<Task> tasks = new ArrayList<>();
  29. // task executor
  30. private Executor taskExecutor;
  31. // task executor thread
  32. private Thread taskExecutorThread;
  33. /**
  34. * reset device
  35. */
  36. public void reset() {
  37. TaskStartReset task = new TaskStartReset();
  38. task.mode = TaskStartReset.MODE_NORMAL;
  39. String deviceWorkingStatus = this.runtimeVariableService.getString("DeviceWorkingStatus");
  40. if ( "RUNNING".equals(deviceWorkingStatus) ) {
  41. task.mode = TaskStartReset.MODE_ERROR;
  42. }
  43. Executor.executeTask(this.device, task);
  44. this.runtimeVariableService.setString("DeviceWorkingStatus","RUNNING");
  45. }
  46. /**
  47. * load consumable resources
  48. */
  49. public void load() {
  50. this.actionLog.log("device.load");
  51. TaskLoad task = new TaskLoad();
  52. Executor.executeTask(this.device, task);
  53. }
  54. /**
  55. * unload consumable resources
  56. */
  57. public void unload() {
  58. if ( this.isExecutorRunning() ) {
  59. throw new AppRuntimeException(AppError.DEVICE_BUSY);
  60. }
  61. this.actionLog.log("device.unload");
  62. this.device.testCards.clear();
  63. this.device.bufferTubes.clear();
  64. this.device.largeBufferTubes.clear();
  65. this.projectService.unload();
  66. }
  67. /**
  68. * start task executor
  69. */
  70. public void start() {
  71. if ( null != this.taskExecutor ) {
  72. throw new AppRuntimeException(AppError.DEVICE_ALREADY_STARTED);
  73. }
  74. if ( !this.device.testCards.getIsLoaded() ) {
  75. throw new AppRuntimeException(AppError.DEVICE_CONSUMABLE_NOT_LOADED);
  76. }
  77. if (this.device.pipetteTips.isEmpty()
  78. || this.device.testCards.isEmpty()
  79. || this.device.bufferTubes.isEmpty()
  80. ) {
  81. throw new AppRuntimeException(AppError.DEVICE_CONSUMABLE_NOT_ENOUGH);
  82. }
  83. this.actionLog.log("device.start");
  84. this.taskExecutor = new Executor(this.testService, this.device);
  85. this.taskExecutorThread = new Thread(this.taskExecutor);
  86. this.taskExecutorThread.setName("task-executor");
  87. this.taskExecutorThread.start();
  88. }
  89. // stop
  90. public void stop() {
  91. if ( null == this.taskExecutor ) {
  92. throw new AppRuntimeException(AppError.DEVICE_NOT_STARTED);
  93. }
  94. this.taskExecutor.stop();
  95. try {
  96. this.taskExecutorThread.join();
  97. } catch (InterruptedException e) {
  98. throw new AppRuntimeException(AppError.DEVICE_STOP_FAILED);
  99. }
  100. TaskStopReset task = new TaskStopReset();
  101. Executor.executeTask(this.device, task);
  102. this.runtimeVariableService.setString("DeviceWorkingStatus", "STOPPED");
  103. this.taskExecutor = null;
  104. }
  105. // pause
  106. public void pause() {
  107. if ( null == this.taskExecutor ) {
  108. throw new AppRuntimeException(AppError.DEVICE_NOT_STARTED);
  109. }
  110. this.taskExecutor.pause();
  111. }
  112. // resume
  113. public void resume() {
  114. if ( null == this.taskExecutor ) {
  115. throw new AppRuntimeException(AppError.DEVICE_NOT_STARTED);
  116. }
  117. this.taskExecutor.resume();
  118. }
  119. // reboot the device
  120. public void reboot() {
  121. if ( null != this.taskExecutor ) {
  122. throw new AppRuntimeException(AppError.DEVICE_BUSY);
  123. }
  124. try {
  125. Runtime.getRuntime().exec("reboot");
  126. } catch (IOException e) {
  127. throw new RuntimeException(e);
  128. }
  129. }
  130. // power off the device
  131. public void poweroff() {
  132. if ( null != this.taskExecutor ) {
  133. throw new AppRuntimeException(AppError.DEVICE_BUSY);
  134. }
  135. try {
  136. Runtime.getRuntime().exec("sync");
  137. } catch (IOException e) {
  138. throw new RuntimeException(e);
  139. }
  140. }
  141. // get task executor
  142. public Executor getTaskExecutor() {
  143. return this.taskExecutor;
  144. }
  145. /**
  146. * get is executor running
  147. * @return is executor running
  148. */
  149. public Boolean isExecutorRunning() {
  150. if ( null == this.taskExecutor ) {
  151. return false;
  152. }
  153. return Executor.STATUS_RUNNING.equals(this.taskExecutor.getStatus());
  154. }
  155. // get executor working status
  156. public String getExecutorWorkingStatus() {
  157. if ( null == this.taskExecutor ) {
  158. return null;
  159. }
  160. return this.taskExecutor.workingStatus;
  161. }
  162. // scan and load test cards
  163. public void testCardLoad() {
  164. TaskTestCardLoad task = new TaskTestCardLoad();
  165. Executor.executeTask(this.device, task);
  166. }
  167. // update test cards by box
  168. public void testCardUpdateByBox(ParamTestCardUpdateByBox update) {
  169. this.device.testCards.testCardUpdateByBox(update);
  170. }
  171. // update pipette tips
  172. public void pipetteTipUpdateByBox(ParamPipetteTipUpdate param) {
  173. this.device.pipetteTips.updateByBox(param);
  174. }
  175. // load large buffer tubes
  176. public List<CsmLargeBufferTube> largeBufferTubeLoad() {
  177. TaskLargeBufferTubeLoad task = new TaskLargeBufferTubeLoad();
  178. Executor.executeTask(this.device, task);
  179. return this.device.largeBufferTubes.getAll();
  180. }
  181. // update large buffer tubes
  182. public void largeBufferTubeUpdate(ParamBufferTubeUpdateByBox param) {
  183. this.device.largeBufferTubes.update(param);
  184. }
  185. // load buffer tubes
  186. public void bufferTubeLoad() {
  187. TaskBufferTubeLoad task = new TaskBufferTubeLoad();
  188. Executor.executeTask(this.device, task);
  189. }
  190. // update buffer tubes by box
  191. public void bufferTubeUpdateByBox(ParamBufferTubeUpdateByBox param ) {
  192. this.device.bufferTubes.updateByBox(param);
  193. }
  194. // get status
  195. public Map<String, Object> status() {
  196. return new HashMap<>();
  197. }
  198. // append task
  199. public void taskAppend(Task task) {
  200. synchronized (this.tasks) {
  201. this.tasks.add(task);
  202. this.tasks.notifyAll();
  203. }
  204. }
  205. }