device.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /**
  2. * Device Mock Handlers
  3. * 设备相关的 mock 处理器
  4. */
  5. /**
  6. * 打开设备 - 成功场景
  7. *
  8. * @description 打开指定设备
  9. * @method POST
  10. * @url /api/v1/auth/device/open
  11. * @access 需要认证
  12. *
  13. * @param {Object} requestBody - 请求体
  14. * @param {string} requestBody.deviceUri - 设备URI
  15. *
  16. * @returns {Object} 成功响应
  17. *
  18. * @example
  19. * mockOpenDeviceSuccess();
  20. * cy.wait('@openDeviceSuccess');
  21. *
  22. * @see docs/DR.md - 章节28
  23. */
  24. export function mockOpenDeviceSuccess() {
  25. cy.intercept('POST', '/api/v1/auth/device/open', {
  26. statusCode: 200,
  27. body: {
  28. code: "0x000000",
  29. description: "Success",
  30. solution: "",
  31. data: {
  32. "@type": "type.googleapis.com/google.protobuf.Empty",
  33. value: {}
  34. }
  35. }
  36. }).as('openDeviceSuccess');
  37. }
  38. /**
  39. * 执行设备Get操作 - 成功场景
  40. *
  41. * @description 执行设备的Get操作,获取设备状态或参数
  42. * @method POST
  43. * @url /api/v1/auth/device/get
  44. * @access 需要认证
  45. *
  46. * @param {Object} requestBody - 请求体
  47. * @param {string} requestBody.deviceUri - 设备URI
  48. * @param {string} requestBody.reqName - 请求名称
  49. *
  50. * @returns {Object} data - 设备状态数据(JSON字符串)
  51. *
  52. * @example
  53. * mockDeviceGetSuccess();
  54. * cy.wait('@deviceGetSuccess');
  55. *
  56. * @see docs/DR.md - 章节29
  57. */
  58. export function mockDeviceGetSuccess() {
  59. cy.intercept('POST', '/api/v1/auth/device/get', {
  60. statusCode: 200,
  61. body: {
  62. code: "0x000000",
  63. description: "Success",
  64. solution: "",
  65. data: JSON.stringify({
  66. IsDemo: { Value: "1" },
  67. GENERATORSTATUS: { Value: "4" },
  68. KV: { Value: "70.0" },
  69. MA: { Value: "125.0" }
  70. })
  71. }
  72. }).as('deviceGetSuccess');
  73. }
  74. /**
  75. * 执行设备Action操作 - 成功场景
  76. *
  77. * @description 执行设备的Action操作,控制设备行为
  78. * @method POST
  79. * @url /api/v1/auth/device/action
  80. * @access 需要认证
  81. *
  82. * @param {Object} requestBody - 请求体
  83. * @param {string} requestBody.deviceUri - 设备URI
  84. * @param {string} requestBody.reqName - 请求名称
  85. * @param {string} requestBody.reqParam - 请求参数
  86. *
  87. * @returns {Object} 成功响应
  88. *
  89. * @example
  90. * mockDeviceActionSuccess();
  91. * cy.wait('@deviceActionSuccess');
  92. *
  93. * @see docs/DR.md - 章节30
  94. */
  95. export function mockDeviceActionSuccess() {
  96. cy.intercept('POST', '/api/v1/auth/device/action', {
  97. statusCode: 200,
  98. body: {
  99. code: "0x000000",
  100. description: "Success",
  101. solution: "",
  102. data: {
  103. "@type": "type.googleapis.com/google.protobuf.Empty",
  104. value: {}
  105. }
  106. }
  107. }).as('deviceActionSuccess');
  108. }
  109. /**
  110. * 重置高压发生器 - 成功场景
  111. *
  112. * @description 重置高压发生器参数到初始状态
  113. * @method POST
  114. * @url /auth/device/action
  115. * @access 需要认证
  116. *
  117. * @param {Object} requestBody - 请求体
  118. * @param {string} requestBody.deviceUri - 设备URI (DIOS/DEVICE/Generator)
  119. * @param {string} requestBody.reqName - 请求名称 (RESET)
  120. *
  121. * @returns {Object} 成功响应
  122. *
  123. * @example
  124. * mockResetDeviceSuccess();
  125. * cy.wait('@resetDevice');
  126. */
  127. export function mockResetDeviceSuccess() {
  128. cy.intercept('POST', '*/auth/device/action', (req) => {
  129. if (req.body.reqName === 'RESET') {
  130. req.reply({
  131. statusCode: 200,
  132. body: {
  133. code: '0x000000',
  134. description: 'Success',
  135. data: {}
  136. }
  137. });
  138. }
  139. }).as('resetDevice');
  140. }
  141. /**
  142. * 重置高压发生器 - 失败场景
  143. *
  144. * @description 模拟设备重置失败
  145. * @method POST
  146. * @url /auth/device/action
  147. * @access 需要认证
  148. *
  149. * @param {string} errorCode - 错误码,默认为'0x010001'
  150. * @param {string} description - 错误描述,默认为'设备通信失败'
  151. *
  152. * @returns {Object} 失败响应
  153. *
  154. * @example
  155. * mockResetDeviceFail();
  156. * cy.wait('@resetDeviceFail');
  157. */
  158. export function mockResetDeviceFail(
  159. errorCode: string = '0x010001',
  160. description: string = '设备通信失败'
  161. ) {
  162. cy.intercept('POST', '/auth/device/action', {
  163. statusCode: 200,
  164. body: {
  165. code: errorCode,
  166. description: description,
  167. solution: '检查设备连接'
  168. }
  169. }).as('resetDeviceFail');
  170. }
  171. /**
  172. * 重置高压发生器 - 网络错误
  173. *
  174. * @description 模拟网络通信错误
  175. * @method POST
  176. * @url /auth/device/action
  177. * @access 需要认证
  178. *
  179. * @returns {Object} 网络错误
  180. *
  181. * @example
  182. * mockResetDeviceNetworkError();
  183. * cy.wait('@resetDeviceNetworkError');
  184. */
  185. export function mockResetDeviceNetworkError() {
  186. cy.intercept('POST', '/auth/device/action', (req) => {
  187. if (req.body.reqName === 'RESET') {
  188. req.reply({ forceNetworkError: true });
  189. }
  190. }).as('resetDeviceNetworkError');
  191. }
  192. /**
  193. * 重置高压发生器 - 延迟响应
  194. *
  195. * @description 模拟设备重置延迟响应(用于测试loading状态)
  196. * @method POST
  197. * @url /auth/device/action
  198. * @access 需要认证
  199. *
  200. * @param {number} delayMs - 延迟毫秒数
  201. *
  202. * @returns {Object} 延迟后的成功响应
  203. *
  204. * @example
  205. * mockResetDeviceDelay(2000); // 延迟2秒
  206. * cy.wait('@resetDeviceDelay');
  207. */
  208. export function mockResetDeviceDelay(delayMs: number) {
  209. cy.intercept('POST', '/auth/device/action', (req) => {
  210. if (req.body.reqName === 'RESET') {
  211. req.reply({
  212. statusCode: 200,
  213. body: {
  214. code: '0x000000',
  215. description: 'Success',
  216. data: {}
  217. },
  218. delay: delayMs
  219. });
  220. }
  221. }).as('resetDeviceDelay');
  222. }