paraSettingCoordinator.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /**
  2. * 响应用户的操作,不需要组件传递参数,这些操作对应的方法有:
  3. *
  4. 1. 增加KV
  5. 2. 减少KV
  6. 3. 增加MAS
  7. 4. 减少MAS
  8. 5. 增加MA
  9. 6. 减少MA
  10. 7. 增加MS
  11. 8. 减少MS
  12. 9. 增加Density
  13. 10. 减少Density
  14. 11. 增加Thickness
  15. 12. 减少Thickness
  16. 13. 设置APR
  17. * 以上方法内部逻辑是向服务发送设置曝光参数,KV MA MS MAS;间接调用api method,通过 调用 @/src/API/exam/APRActions.ts 中定义的方法完成调用,并不直接调用api
  18. * 接收device mqtt发来的新参数。device mqtt会触发事件 emitter.emit('NEW_KV',parsedMessage.CONTEXT); 这是接收KV新值的,还有对应的其他新曝光参数的新值
  19. * 把 device mqtt 发来的新参数传递给 @/src/states/exam/aprSlice.ts 。 dispatch thunk的 action,有fullfilled pending rejected ,比如 incMS.fulfilled ;考虑 device mqtt 发来新曝光参数会超时,超时的情况下向 aprSLice发送 rejected 。
  20. */
  21. import {
  22. incKv as incKvFromAction,
  23. decKv as decKvFromAction,
  24. incMas as incMasFromAction,
  25. decMas as decMasFromAction,
  26. incMa as incMaFromAction,
  27. decMa as decMaFromAction,
  28. incMs as incMsFromAction,
  29. decMs as decMsFromAction,
  30. incDensity as incDensityFromAction,
  31. decDensity as decDensityFromAction,
  32. incThickness as incThicknessFromAction,
  33. decThickness as decThicknessFromAction,
  34. setExposureModeFromAction,
  35. } from '../../API/exam/APRActions';
  36. import emitter from '../../utils/eventEmitter';
  37. import {
  38. incKV,
  39. decKV,
  40. incMAS,
  41. decMAS,
  42. incMA,
  43. decMA,
  44. incMS,
  45. decMS,
  46. incDensity,
  47. decDensity,
  48. incThickness,
  49. decThickness,
  50. setExposureModeThunk,
  51. } from '../../states/exam/aprSlice';
  52. import store from '../../states/store';
  53. class ParaSettingCoordinator {
  54. constructor() {
  55. this.dispatch = store.dispatch;
  56. this.initializeEventListeners();
  57. }
  58. dispatch;
  59. initializeEventListeners() {
  60. emitter.on('NEW_KV', (newValue) => this.handleNewKV(newValue));
  61. emitter.on('NEW_MAS', (newValue) => this.handleNewMAS(newValue));
  62. emitter.on('NEW_MA', (newValue) => this.handleNewMA(newValue));
  63. emitter.on('NEW_MS', (newValue) => this.handleNewMS(newValue));
  64. emitter.on('NEW_DENSITY', (newValue) => this.handleNewDensity(newValue));
  65. emitter.on('NEW_THICKNESS', (newValue) =>
  66. this.handleNewThickness(newValue)
  67. );
  68. emitter.on('NEW_EXPOSURE_MODE', (newValue) =>
  69. this.handleNewExposureMode(newValue)
  70. );
  71. }
  72. handleNewKV(newValue) {
  73. this.dispatch({
  74. type: incKV.fulfilled.type,
  75. payload: newValue,
  76. meta: { arg: newValue, requestId: '' },
  77. });
  78. }
  79. handleNewMAS(newValue) {
  80. console.info('有新的mAs从设备端到来。handleNewMAS', newValue);
  81. this.dispatch({
  82. type: incMAS.fulfilled.type,
  83. payload: newValue,
  84. meta: { arg: newValue, requestId: '' },
  85. });
  86. }
  87. handleNewMA(newValue) {
  88. this.dispatch({
  89. type: incMA.fulfilled.type,
  90. payload: newValue,
  91. meta: { arg: newValue, requestId: '' },
  92. });
  93. }
  94. handleNewMS(newValue) {
  95. this.dispatch({
  96. type: incMS.fulfilled.type,
  97. payload: newValue,
  98. meta: { arg: newValue, requestId: '' },
  99. });
  100. }
  101. handleNewDensity(newValue) {
  102. this.dispatch({
  103. type: incDensity.fulfilled.type,
  104. payload: newValue,
  105. meta: { arg: newValue, requestId: '' },
  106. });
  107. }
  108. handleNewThickness(newValue) {
  109. this.dispatch({
  110. type: incThickness.fulfilled.type,
  111. payload: newValue,
  112. meta: { arg: newValue, requestId: '' },
  113. });
  114. }
  115. handleNewExposureMode(newValue) {
  116. // 将数字转换为字符串: 0 -> 'time', 1 -> 'mAs'
  117. const mode = newValue === '0' ? 'time' : 'mAs';
  118. console.info('有新的曝光模式从设备端到来。handleNewExposureMode', newValue, '转换为:', mode);
  119. this.dispatch({
  120. type: setExposureModeThunk.fulfilled.type,
  121. payload: mode,
  122. meta: { arg: mode, requestId: '' },
  123. });
  124. }
  125. setExposureMode(mode: string) {
  126. console.info('[ParaSettingCoordinator] 切换曝光模式:', mode);
  127. // 1. Dispatch pending
  128. this.dispatch({
  129. type: setExposureModeThunk.pending.type,
  130. meta: { arg: mode, requestId: '' },
  131. });
  132. // 2. 调用API
  133. try {
  134. // 将UI字符串转换为API需要的数字: 'mAs' -> 1, 'time' -> 0
  135. let reqParam;
  136. if (mode === 'mAs') {
  137. // 构造参数对象
  138. reqParam = JSON.stringify({
  139. P0: "1"
  140. });
  141. } else if (mode === 'time') {
  142. // 构造参数对象
  143. reqParam = JSON.stringify({
  144. P0: "0"
  145. });
  146. }
  147. console.info('[ParaSettingCoordinator] 下发曝光模式参数:', reqParam);
  148. setExposureModeFromAction(reqParam);
  149. } catch (error) {
  150. console.error('[ParaSettingCoordinator] 切换曝光模式失败:', error);
  151. this.dispatch({
  152. type: setExposureModeThunk.rejected.type,
  153. payload: error,
  154. meta: { arg: mode, requestId: '' },
  155. });
  156. }
  157. }
  158. increaseKV() {
  159. this.dispatch({
  160. type: incKV.pending.type,
  161. meta: { arg: 0, requestId: '' },
  162. });
  163. try {
  164. incKvFromAction();
  165. } catch (error) {
  166. this.dispatch({
  167. type: incKV.rejected.type,
  168. payload: error,
  169. meta: { arg: 0, requestId: '' },
  170. });
  171. }
  172. }
  173. decreaseKV() {
  174. this.dispatch({
  175. type: decKV.pending.type,
  176. meta: { arg: 0, requestId: '' },
  177. });
  178. try {
  179. decKvFromAction();
  180. } catch (error) {
  181. this.dispatch({
  182. type: decKV.rejected.type,
  183. payload: error,
  184. meta: { arg: 0, requestId: '' },
  185. });
  186. }
  187. }
  188. increaseMAS() {
  189. this.dispatch({
  190. type: incMAS.pending.type,
  191. meta: { arg: 0, requestId: '' },
  192. });
  193. try {
  194. incMasFromAction();
  195. } catch (error) {
  196. this.dispatch({
  197. type: incMAS.rejected.type,
  198. payload: error,
  199. meta: { arg: 0, requestId: '' },
  200. });
  201. }
  202. }
  203. decreaseMAS() {
  204. this.dispatch({
  205. type: decMAS.pending.type,
  206. meta: { arg: 0, requestId: '' },
  207. });
  208. try {
  209. decMasFromAction();
  210. } catch (error) {
  211. this.dispatch({
  212. type: decMAS.rejected.type,
  213. payload: error,
  214. meta: { arg: 0, requestId: '' },
  215. });
  216. }
  217. }
  218. increaseMA() {
  219. this.dispatch({
  220. type: incMA.pending.type,
  221. meta: { arg: 0, requestId: '' },
  222. });
  223. try {
  224. incMaFromAction();
  225. } catch (error) {
  226. this.dispatch({
  227. type: incMA.rejected.type,
  228. payload: error,
  229. meta: { arg: 0, requestId: '' },
  230. });
  231. }
  232. }
  233. decreaseMA() {
  234. this.dispatch({
  235. type: decMA.pending.type,
  236. meta: { arg: 0, requestId: '' },
  237. });
  238. try {
  239. decMaFromAction();
  240. } catch (error) {
  241. this.dispatch({
  242. type: decMA.rejected.type,
  243. payload: error,
  244. meta: { arg: 0, requestId: '' },
  245. });
  246. }
  247. }
  248. increaseMS() {
  249. this.dispatch({
  250. type: incMS.pending.type,
  251. meta: { arg: 0, requestId: '' },
  252. });
  253. try {
  254. incMsFromAction();
  255. } catch (error) {
  256. this.dispatch({
  257. type: incMS.rejected.type,
  258. payload: error,
  259. meta: { arg: 0, requestId: '' },
  260. });
  261. }
  262. }
  263. decreaseMS() {
  264. this.dispatch({
  265. type: decMS.pending.type,
  266. meta: { arg: 0, requestId: '' },
  267. });
  268. try {
  269. decMsFromAction();
  270. } catch (error) {
  271. this.dispatch({
  272. type: decMS.rejected.type,
  273. payload: error,
  274. meta: { arg: 0, requestId: '' },
  275. });
  276. }
  277. }
  278. increaseDensity() {
  279. this.dispatch({
  280. type: incDensity.pending.type,
  281. meta: { arg: 0, requestId: '' },
  282. });
  283. try {
  284. incDensityFromAction();
  285. } catch (error) {
  286. this.dispatch({
  287. type: incDensity.rejected.type,
  288. payload: error,
  289. meta: { arg: 0, requestId: '' },
  290. });
  291. }
  292. }
  293. decreaseDensity() {
  294. this.dispatch({
  295. type: decDensity.pending.type,
  296. meta: { arg: 0, requestId: '' },
  297. });
  298. try {
  299. decDensityFromAction();
  300. } catch (error) {
  301. this.dispatch({
  302. type: decDensity.rejected.type,
  303. payload: error,
  304. meta: { arg: 0, requestId: '' },
  305. });
  306. }
  307. }
  308. increaseThickness() {
  309. this.dispatch({
  310. type: incThickness.pending.type,
  311. meta: { arg: 0, requestId: '' },
  312. });
  313. try {
  314. incThicknessFromAction();
  315. } catch (error) {
  316. this.dispatch({
  317. type: incThickness.rejected.type,
  318. payload: error,
  319. meta: { arg: 0, requestId: '' },
  320. });
  321. }
  322. }
  323. decreaseThickness() {
  324. this.dispatch({
  325. type: decThickness.pending.type,
  326. meta: { arg: 0, requestId: '' },
  327. });
  328. try {
  329. decThicknessFromAction();
  330. } catch (error) {
  331. this.dispatch({
  332. type: decThickness.rejected.type,
  333. payload: error,
  334. meta: { arg: 0, requestId: '' },
  335. });
  336. }
  337. }
  338. }
  339. export default new ParaSettingCoordinator();