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.

452 lines
18 KiB

  1. package com.dreamworks.boditech.service;
  2. import com.dreamworks.boditech.driver.*;
  3. import com.dreamworks.boditech.driver.consumable.CsmLargeBufferTube;
  4. import com.dreamworks.boditech.driver.consumable.CsmPipetteTip;
  5. import com.dreamworks.boditech.driver.task.Task;
  6. import jakarta.annotation.Resource;
  7. import org.springframework.stereotype.Service;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. @Service
  11. public class DeviceService {
  12. @Resource
  13. public Device device;
  14. // pipette tips
  15. public List<CsmPipetteTip> pipetteTips;
  16. // buffer tubes
  17. public List<CsmLargeBufferTube> bufferTubes;
  18. // test cards
  19. public List<ConsumableTestCard> testCards;
  20. // tasks
  21. private final List<Task> tasks = new ArrayList<>();
  22. // task executor
  23. private TaskExecutor taskExecutor;
  24. // task executor thread
  25. private Thread taskExecutorThread;
  26. // reset to initial state
  27. public void reset() {
  28. }
  29. // start
  30. public void start() {
  31. this.device.appendPipetteTip(0, 10);
  32. this.device.appendPipetteTip(1, 10);
  33. this.device.appendPipetteTip(2, 10);
  34. this.device.appendBufferTubeA(0, 10);
  35. this.taskExecutor = new TaskExecutor(this.tasks, this.device);
  36. this.taskExecutorThread = new Thread(this.taskExecutor);
  37. this.taskExecutorThread.setName("task-executor");
  38. this.taskExecutorThread.start();
  39. }
  40. // stop
  41. public void stop() {
  42. this.taskExecutor.stop();
  43. try {
  44. this.taskExecutorThread.join();
  45. } catch (InterruptedException e) {
  46. throw new RuntimeException(e);
  47. }
  48. }
  49. // append new task
  50. public void taskAppend(Task task) {
  51. synchronized (this.tasks) {
  52. this.tasks.add(task);
  53. this.tasks.notifyAll();
  54. }
  55. }
  56. // private static final Logger LOG = LoggerFactory.getLogger(DeviceService.class);
  57. //
  58. // @Resource
  59. // private BoditechDeviceClient device;
  60. //
  61. // // device request list
  62. // private Map<Short, BoditechDeviceRequest> deviceRequests;
  63. // // request id
  64. // private short requestId = 0;
  65. //
  66. // /**
  67. // * initialize device service
  68. // */
  69. // @PostConstruct
  70. // public void init() {
  71. // LOG.info("init");
  72. // this.deviceRequests = new HashMap<Short, BoditechDeviceRequest>();
  73. // this.device.setOnMessageCallback(this::handleDeviceOnMessage);
  74. // }
  75. //
  76. // /**
  77. // * event handler for receiving binary message from device
  78. // * @param buffer message from device
  79. // */
  80. // public void handleDeviceOnMessage(ByteBuffer buffer) {
  81. // LOG.info("handleDeviceOnMessage");
  82. // buffer.order(ByteOrder.LITTLE_ENDIAN);
  83. // short reqId = buffer.getShort();
  84. // BoditechDeviceRequest request = this.deviceRequests.get(reqId);
  85. // if ( null == request ) {
  86. // return ;
  87. // }
  88. // request.setResponseBuffer(buffer);
  89. // }
  90. //
  91. // /**
  92. // * call device command
  93. // * @param mid module id
  94. // * @param cmd command definition
  95. // * @param params arguments for command
  96. // */
  97. // public ByteBuffer call( int mid, short[] cmd, int ... params ) {
  98. // this.requestId += 1;
  99. // if ( 30000 < this.requestId ) {
  100. // this.requestId = 1;
  101. // }
  102. //
  103. // // build request buffer and send to device
  104. // int requestBufferLength = 8 + params.length * 4;
  105. // ByteBuffer requestBuffer = ByteBuffer.allocate(requestBufferLength);
  106. // requestBuffer.order(ByteOrder.LITTLE_ENDIAN);
  107. // requestBuffer.putShort(this.requestId); // request id (2 bytes)
  108. // requestBuffer.putShort(cmd[0]); // main cmd (2 bytes)
  109. // requestBuffer.put((byte)cmd[1]); // sub cmd (1 byte)
  110. // requestBuffer.put((byte)0); // directive type
  111. // requestBuffer.putShort((short)mid); // module id (2 bytes)
  112. // for ( int param : params ) {
  113. // requestBuffer.putInt(param); // param (4 bytes)
  114. // }
  115. // byte[] bytes = requestBuffer.array();
  116. // this.device.send(bytes);
  117. // LOG.info("request({}) : send = {} ", this.requestId, bytes);
  118. //
  119. // // wait for response
  120. // BoditechDeviceRequest request = new BoditechDeviceRequest();
  121. // this.deviceRequests.put(this.requestId, request);
  122. // ByteBuffer responseBuffer = request.waitForResponse();
  123. // this.deviceRequests.remove(this.requestId);
  124. //
  125. // ByteBuffer responseData = responseBuffer.slice(8, responseBuffer.capacity() - 8);
  126. // LOG.info("request({}) : response = {} ", this.requestId, responseData);
  127. // return responseData;
  128. // }
  129. //
  130. // // motor enable / disable
  131. // public void motorEnable (int mid, boolean enable) {
  132. // LOG.info("motorEnable : mid={}, enable={}", mid, enable);
  133. // this.call(mid, BoditechDeviceCmd.MOTOR_ENABLE, enable ? 1 : 0);
  134. // }
  135. //
  136. // // motor easy move to
  137. // public void motorEasyMoveTo(int mid, int position) {
  138. // LOG.info("motorEasyMoveTo : mid={}, position={}", mid, position);
  139. // this.call(mid, BoditechDeviceCmd.MOTOR_EASY_MOVE_TO, position);
  140. // }
  141. //
  142. // // motor easy move by
  143. // public void motorEasyMoveBy(int mid, int distance) {
  144. // LOG.info("motorEasyMoveBy : mid={}, distance={}", mid, distance);
  145. // this.call(mid, BoditechDeviceCmd.MOTOR_EASY_MOVE_BY, distance);
  146. // }
  147. //
  148. // // motor easy move to zero
  149. // public void motorEasyMoveToZero(int mid, int direction) {
  150. // LOG.info("motorEasyMoveToZero : mid={}, direction={}", mid, direction);
  151. // this.call(mid, BoditechDeviceCmd.MOTOR_EASY_MOVE_TO_ZERO, direction);
  152. // }
  153. //
  154. // // motor easy rotate
  155. // public void motorEasyRotate(int mid, int direction) {
  156. // LOG.info("motorEasyRotate : mid={}, direction={}", mid, direction);
  157. // this.call(mid, BoditechDeviceCmd.MOTOR_EASY_ROTATE, direction);
  158. // }
  159. //
  160. // // motor easy set current pos
  161. // public void motorEasySetCurrentPos(int mid, int pos) {
  162. // LOG.info("motorEasySetCurrentPos : mid={}, pos={}", mid, pos);
  163. // this.call(mid, BoditechDeviceCmd.MOTOR_EASY_SET_CURRENT_POS, pos);
  164. // }
  165. //
  166. // // motor move by
  167. // public void motorMoveBy(int mid, int distance, int motorVelocity, int acc) {
  168. // LOG.info("motorMoveBy : mid={}, distance={}, motorVelocity={}, acc={}", mid, distance, motorVelocity, acc);
  169. // this.call(mid, BoditechDeviceCmd.MOTOR_MOVE_BY, distance, motorVelocity, acc);
  170. // }
  171. //
  172. // // motor move by acctime
  173. // public void motorMoveByAcctime(int mid, int distance, int motorVelocity, int acctime) {
  174. // LOG.info("motorMoveByAcctime : mid={}, distance={}, motorVelocity={}, acctime={}", mid, distance, motorVelocity, acctime);
  175. // this.call(mid, BoditechDeviceCmd.MOTOR_MOVE_BY_ACCTIME, distance, motorVelocity, acctime);
  176. // }
  177. //
  178. // // motor move to
  179. // public void motorMoveTo(int mid, int position, int motorVelocity, int acc) {
  180. // LOG.info("motorMoveTo : mid={}, position={}, motorVelocity={}, acc={}", mid, position, motorVelocity, acc);
  181. // this.call(mid, BoditechDeviceCmd.MOTOR_MOVE_TO, position, motorVelocity, acc);
  182. // }
  183. //
  184. // // motor move to acctime
  185. // public void motorMoveToAcctime(int mid, int position, int motorVelocity, int acctime) {
  186. // LOG.info("motorMoveToAcctime : mid={}, position={}, motorVelocity={}, acctime={}", mid, position, motorVelocity, acctime);
  187. // this.call(mid, BoditechDeviceCmd.MOTOR_MOVE_TO_ACCTIME, position, motorVelocity, acctime);
  188. // }
  189. //
  190. // // motor move to torque
  191. // public void motorMoveToTorque(int mid, int pos, int torque, int overtime) {
  192. // LOG.info("motorMoveToTorque : mid={}, pos={}, torque={}, overtime={}", mid, pos, torque, overtime);
  193. // this.call(mid, BoditechDeviceCmd.MOTOR_MOVE_TO_TORQUE, pos, torque, overtime);
  194. // }
  195. //
  196. // // motor move to zero backward
  197. // public void motorMoveToZeroBackward(int mid, int findzerospeed, int findzeroedgeSpeed, int acc, int overtime) {
  198. // LOG.info("motorMoveToZeroBackward : mid={}, findzerospeed={}, findzeroedgeSpeed={}, acc={}, overtime={}", mid, findzerospeed, findzeroedgeSpeed, acc, overtime);
  199. // this.call(mid, BoditechDeviceCmd.MOTOR_MOVE_TO_ZERO_BACKWARD, findzerospeed, findzeroedgeSpeed, acc, overtime);
  200. // }
  201. //
  202. // // motor move to zero backward and calculated shift
  203. // public void motorMoveToZeroBackwardAndCalculatedShift(int mid, int findzerospeed, int findzeroedgeSpeed, int acc, int overtime) {
  204. // LOG.info("motorMoveToZeroBackwardAndCalculatedShift : mid={}, findzerospeed={}, findzeroedgeSpeed={}, acc={}, overtime={}", mid, findzerospeed, findzeroedgeSpeed, acc, overtime);
  205. // this.call(mid, BoditechDeviceCmd.MOTOR_MOVE_TO_ZERO_BACKWARD_AND_CALCULATED_SHIFT, findzerospeed, findzeroedgeSpeed, acc, overtime);
  206. // }
  207. //
  208. // // motor move to zero forward
  209. // public void motorMoveToZeroForward(int mid, int findzerospeed, int findzeroedgeSpeed, int acc, int overtime) {
  210. // LOG.info("motorMoveToZeroForward : mid={}, findzerospeed={}, findzeroedgeSpeed={}, acc={}, overtime={}", mid, findzerospeed, findzeroedgeSpeed, acc, overtime);
  211. // this.call(mid, BoditechDeviceCmd.MOTOR_MOVE_TO_ZERO_FORWARD, findzerospeed, findzeroedgeSpeed, acc, overtime);
  212. // }
  213. //
  214. // // motor move to zero forward and calculated shift
  215. // public void motorMoveToZeroForwardAndCalculatedShift(int mid, int findzerospeed, int findzeroedgeSpeed, int acc, int overtime) {
  216. // LOG.info("motorMoveToZeroForwardAndCalculatedShift : mid={}, findzerospeed={}, findzeroedgeSpeed={}, acc={}, overtime={}", mid, findzerospeed, findzeroedgeSpeed, acc, overtime);
  217. // this.call(mid, BoditechDeviceCmd.MOTOR_MOVE_TO_ZERO_FORWARD_AND_CALCULATED_SHIFT, findzerospeed, findzeroedgeSpeed, acc, overtime);
  218. // }
  219. //
  220. // // motor rotate
  221. // public void motorRotate(int mid, int direction, int motorVelocity, int acc) {
  222. // LOG.info("motorRotate : mid={}, direction={}, motorVelocity={}, acc={}", mid, direction, motorVelocity, acc);
  223. // this.call(mid, BoditechDeviceCmd.MOTOR_ROTATE, direction, motorVelocity, acc);
  224. // }
  225. //
  226. // // motor rotate acctime
  227. // public void motorRotateAcctime(int mid, int direction, int motorVelocity, int acctime) {
  228. // LOG.info("motorRotateAcctime : mid={}, direction={}, motorVelocity={}, acctime={}", mid, direction, motorVelocity, acctime);
  229. // this.call(mid, BoditechDeviceCmd.MOTOR_ROTATE_ACCTIME, direction, motorVelocity, acctime);
  230. // }
  231. //
  232. // // motor rotate with torque
  233. // public void motorRotateWithTorque(int mid, int pos, int torque) {
  234. // LOG.info("motorRotateWithTorque : mid={}, pos={}, torque={}", mid, pos, torque);
  235. // this.call(mid, BoditechDeviceCmd.MOTOR_ROTATE_WITH_TORQUE, pos, torque);
  236. // }
  237. //
  238. // // motor calculated pos by move to zero
  239. // public void motorCalculatedPosByMoveToZero(int mid) {
  240. // LOG.info("motorCalculatedPosByMoveToZero : mid={}", mid);
  241. // this.call(mid, BoditechDeviceCmd.MOTOR_CALCULATED_POS_BY_MOVE_TO_ZERO);
  242. // }
  243. //
  244. // // motor read pos
  245. // public void motorReadPos(int mid) {
  246. // LOG.info("motorReadPos : mid={}", mid);
  247. // this.call(mid, BoditechDeviceCmd.MOTOR_READ_POS);
  248. // }
  249. //
  250. // // motor set current pos by change shift
  251. // public void motorSetCurrentPosByChangeShift(int mid, int pos) {
  252. // LOG.info("motorSetCurrentPosByChangeShift : mid={}, pos={}", mid, pos);
  253. // this.call(mid, BoditechDeviceCmd.MOTOR_SET_CURRENT_POS_BY_CHANGE_SHIFT, pos);
  254. // }
  255. //
  256. // // xymotor calculated pos by move to zero
  257. // public void xymotorCalculatedPosByMoveToZero(int mid) {
  258. // LOG.info("xymotorCalculatedPosByMoveToZero : mid={}", mid);
  259. // this.call(mid, BoditechDeviceCmd.XYMOTOR_CALCULATED_POS_BY_MOVE_TO_ZERO);
  260. // }
  261. //
  262. // // xymotor enable
  263. // public void xymotorEnable(int mid, boolean enable) {
  264. // LOG.info("xymotorEnable : mid={}, enable={}", mid, enable);
  265. // this.call(mid, BoditechDeviceCmd.XYMOTOR_ENABLE, enable ? 1 : 0);
  266. // }
  267. //
  268. // // xymotor move by
  269. // public void xymotorMoveBy(int mid, int dx, int dy, int motorVelocity) {
  270. // LOG.info("xymotorMoveBy : mid={}, dx={}, dy={}, motorVelocity={}", mid, dx, dy, motorVelocity);
  271. // this.call(mid, BoditechDeviceCmd.XYMOTOR_MOVE_BY, dx, dy, motorVelocity);
  272. // }
  273. //
  274. // // xymotor move to
  275. // public void xymotorMoveTo(int mid, int x, int y, int motorVelocity) {
  276. // LOG.info("xymotorMoveTo : mid={}, x={}, y={}, motorVelocity={}", mid, x, y, motorVelocity);
  277. // this.call(mid, BoditechDeviceCmd.XYMOTOR_MOVE_TO, x, y, motorVelocity);
  278. // }
  279. //
  280. // // xymotor move to zero
  281. // public void xymotorMoveToZero(int mid) {
  282. // LOG.info("xymotorMoveToZero : mid={}", mid);
  283. // this.call(mid, BoditechDeviceCmd.XYMOTOR_MOVE_TO_ZERO);
  284. // }
  285. //
  286. // // xymotor move to zero and calculated shift
  287. // public void xymotorMoveToZeroAndCalculatedShift(int mid) {
  288. // LOG.info("xymotorMoveToZeroAndCalculatedShift : mid={}", mid);
  289. // this.call(mid, BoditechDeviceCmd.XYMOTOR_MOVE_TO_ZERO_AND_CALCULATED_SHIFT);
  290. // }
  291. //
  292. // // xymotor read pos
  293. // public void xymotorReadPos(int mid) {
  294. // LOG.info("xymotorReadPos : mid={}", mid);
  295. // this.call(mid, BoditechDeviceCmd.XYMOTOR_READ_POS);
  296. // }
  297. //
  298. // // pipette ctrl init device
  299. // public void pipetteCtrlInitDevice(int mid) {
  300. // LOG.info("pipetteCtrlInitDevice : mid={}", mid);
  301. // this.call(mid, BoditechDeviceCmd.PIPETTE_CTRL_INIT_DEVICE);
  302. // }
  303. //
  304. // // pipette ctrl move to ul
  305. // public void pipetteCtrlMoveToUl(int mid, int ul) {
  306. // LOG.info("pipetteCtrlMoveToUl : mid={}, ul={}", mid, ul);
  307. // this.call(mid, BoditechDeviceCmd.PIPETTE_CTRL_MOVE_TO_UL, ul);
  308. // }
  309. //
  310. // // pipette ctrl put tip
  311. // public void pipetteCtrlPutTip(int mid) {
  312. // LOG.info("pipetteCtrlPutTip : mid={}", mid);
  313. // this.call(mid, BoditechDeviceCmd.PIPETTE_CTRL_PUT_TIP);
  314. // }
  315. //
  316. // // module active cfg
  317. // public void moduleActiveCfg(int mid) {
  318. // LOG.info("moduleActiveCfg : mid={}", mid);
  319. // this.call(mid, BoditechDeviceCmd.MODULE_ACTIVE_CFG);
  320. // }
  321. //
  322. // // module break
  323. // public void moduleBreak(int mid) {
  324. // LOG.info("moduleBreak : mid={}", mid);
  325. // this.call(mid, BoditechDeviceCmd.MODULE_BREAK);
  326. // }
  327. //
  328. // // module clear error
  329. // public void moduleClearError(int mid) {
  330. // LOG.info("moduleClearError : mid={}", mid);
  331. // this.call(mid, BoditechDeviceCmd.MODULE_CLEAR_ERROR);
  332. // }
  333. //
  334. // // module enable
  335. // public void moduleEnable(int mid, boolean enable) {
  336. // LOG.info("moduleEnable : mid={}, enable={}", mid, enable);
  337. // this.call(mid, BoditechDeviceCmd.MODULE_ENABLE, enable ? 1 : 0);
  338. // }
  339. //
  340. // // module factory reset
  341. // public void moduleFactoryReset(int mid) {
  342. // LOG.info("moduleFactoryReset : mid={}", mid);
  343. // this.call(mid, BoditechDeviceCmd.MODULE_FACTORY_RESET);
  344. // }
  345. //
  346. // // module flush cfg
  347. // public void moduleFlushCfg(int mid) {
  348. // LOG.info("moduleFlushCfg : mid={}", mid);
  349. // this.call(mid, BoditechDeviceCmd.MODULE_FLUSH_CFG);
  350. // }
  351. //
  352. // // module get error
  353. // public void moduleGetError(int mid) {
  354. // LOG.info("moduleGetError : mid={}", mid);
  355. // this.call(mid, BoditechDeviceCmd.MODULE_GET_ERROR);
  356. // }
  357. //
  358. // // module get inited flag
  359. // public void moduleGetInitedFlag(int mid) {
  360. // LOG.info("moduleGetInitedFlag : mid={}", mid);
  361. // this.call(mid, BoditechDeviceCmd.MODULE_GET_INITED_FLAG);
  362. // }
  363. //
  364. // // module get last exec status
  365. // public void moduleGetLastExecStatus(int mid) {
  366. // LOG.info("moduleGetLastExecStatus : mid={}", mid);
  367. // this.call(mid, BoditechDeviceCmd.MODULE_GET_LAST_EXEC_STATUS);
  368. // }
  369. //
  370. // // module get reg
  371. // public void moduleGetReg(int mid, int param_id) {
  372. // LOG.info("moduleGetReg : mid={}, param_id={}", mid, param_id);
  373. // this.call(mid, BoditechDeviceCmd.MODULE_GET_REG, param_id);
  374. // }
  375. //
  376. // // module get status
  377. // public void moduleGetStatus(int mid) {
  378. // LOG.info("moduleGetStatus : mid={}", mid);
  379. // this.call(mid, BoditechDeviceCmd.MODULE_GET_STATUS);
  380. // }
  381. //
  382. // // module ping
  383. // public void modulePing(int mid) {
  384. // LOG.info("modulePing : mid={}", mid);
  385. // this.call(mid, BoditechDeviceCmd.MODULE_PING);
  386. // }
  387. //
  388. // // module read adc
  389. // public void moduleReadAdc(int mid, int adc_id, int adcIndex) {
  390. // LOG.info("moduleReadAdc : mid={}, adc_id={}, adcIndex={}", mid, adc_id, adcIndex);
  391. // this.call(mid, BoditechDeviceCmd.MODULE_READ_ADC, adc_id, adcIndex);
  392. // }
  393. //
  394. // // module read raw
  395. // public void moduleReadRaw(int mid, int startIndex) {
  396. // LOG.info("moduleReadRaw : mid={}, startIndex={}", mid, startIndex);
  397. // this.call(mid, BoditechDeviceCmd.MODULE_READ_RAW, startIndex);
  398. // }
  399. //
  400. // // module read io
  401. // public void moduleReadIO(int mid) {
  402. // LOG.info("moduleReadIO : mid={}", mid);
  403. // this.call(mid, BoditechDeviceCmd.MODULE_READIO);
  404. // }
  405. //
  406. // // module set inited flag
  407. // public void moduleSetInitedFlag(int mid, int flag) {
  408. // LOG.info("moduleSetInitedFlag : mid={}, flag={}", mid, flag);
  409. // this.call(mid, BoditechDeviceCmd.MODULE_SET_INITED_FLAG, flag);
  410. // }
  411. //
  412. // // module set reg
  413. // public void moduleSetReg(int mid, int param_id, int param_value) {
  414. // LOG.info("moduleSetReg : mid={}, param_id={}, param_value={}", mid, param_id, param_value);
  415. // this.call(mid, BoditechDeviceCmd.MODULE_SET_REG, param_id, param_value);
  416. // }
  417. //
  418. // // module start
  419. // public void moduleStart(int mid) {
  420. // LOG.info("moduleStart : mid={}", mid);
  421. // this.call(mid, BoditechDeviceCmd.MODULE_START);
  422. // }
  423. //
  424. // // module stop
  425. // public void moduleStop(int mid) {
  426. // LOG.info("moduleStop : mid={}", mid);
  427. // this.call(mid, BoditechDeviceCmd.MODULE_STOP);
  428. // }
  429. //
  430. // // module write io
  431. // public void moduleWriteIO(int mid, int ioIndex, int io) {
  432. // LOG.info("moduleWriteIO : mid={}, ioIndex={}, io={}", mid, ioIndex, io);
  433. // this.call(mid, BoditechDeviceCmd.MODULE_WRITEIO, ioIndex, io);
  434. // }
  435. }