device.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. import store from '@/store'
  2. import request, { tenantRequest } from '@/utils/request'
  3. import {
  4. add,
  5. update,
  6. del,
  7. confirm,
  8. messageSend,
  9. send,
  10. confirmAndSend,
  11. addTenant,
  12. addTenantOrOrg,
  13. addUser
  14. } from './base'
  15. import {
  16. AssetType,
  17. SupportedAlarmStrategies
  18. } from '@/constant'
  19. export function getRatiosWithUser () {
  20. return tenantRequest({
  21. url: '/device/resolutionRatio',
  22. method: 'GET',
  23. params: addTenantOrOrg({})
  24. }).then(({ data }) => {
  25. return {
  26. data: Object.keys(data).map(key => {
  27. return {
  28. value: key,
  29. label: `${key} ${data[key].slice(0, 3).map(device => device.name).join(', ')}`
  30. }
  31. })
  32. }
  33. })
  34. }
  35. export function getRatios () {
  36. return tenantRequest({
  37. url: '/device/resolutionRatio',
  38. method: 'GET',
  39. params: addTenant({})
  40. }).then(({ data }) => {
  41. return {
  42. data: Object.keys(data).map(key => {
  43. return {
  44. value: key,
  45. label: key
  46. }
  47. })
  48. }
  49. })
  50. }
  51. export function getTimeline (deviceId, options) {
  52. return request({
  53. url: `/content/deviceCalender/${deviceId}`,
  54. method: 'GET',
  55. ...options
  56. }).then(({ data }) => JSON.parse(data.eventDetail) || [])
  57. }
  58. export function getTimelineByRange (deviceId, startTime, endTime, options) {
  59. return request({
  60. url: `/content/deviceCalender/${deviceId}`,
  61. method: 'GET',
  62. params: {
  63. startTime,
  64. endTime
  65. },
  66. ...options
  67. }).then(({ data }) => JSON.parse(data.eventDetail) || [])
  68. }
  69. export function addDevice (data) {
  70. return add({
  71. url: '/device',
  72. method: 'POST',
  73. data
  74. })
  75. }
  76. export function updateDevice (data) {
  77. return update({
  78. url: '/device',
  79. method: 'PUT',
  80. data
  81. })
  82. }
  83. function deleteDeviceById (id) {
  84. return messageSend({
  85. url: `/device/${id}`,
  86. method: 'DELETE'
  87. }, '删除')
  88. }
  89. export function deleteDevice ({ id, name, masterId }) {
  90. if (__SUB_DEVICE__) {
  91. if (masterId) {
  92. return confirm(`删除备份设备【${name}】?`).then(() => deleteDeviceById(id))
  93. }
  94. return send({
  95. url: `/device/${id}/standbyDevice`,
  96. method: 'GET',
  97. params: { pageNum: 1, pageSize: 1 }
  98. }).then(({ data }) => confirm(
  99. data.length
  100. ? `删除主设备【${name}】后备份设备也将删除`
  101. : `删除设备【${name}】?`
  102. )).then(() => deleteDeviceById(id))
  103. }
  104. return confirm(`删除设备【${name}】?`).then(() => deleteDeviceById(id))
  105. }
  106. export function getDevices (query, options) {
  107. return getDevicesByQuery(addTenantOrOrg(query), options)
  108. }
  109. export function getDevicesByQuery (query, options) {
  110. const { tenant, org, ...params } = query
  111. if (tenant || org && org === store.getters.tenant) {
  112. return getDevicesByTenant(tenant || org, params, options)
  113. }
  114. return getDevicesByOrg(org, params, options)
  115. }
  116. export function getDevicesByTenant (tenant, query, options) {
  117. const { pageNum: pageIndex, pageSize, ...params } = query
  118. return tenantRequest({
  119. url: '/device/tenant/page',
  120. method: 'GET',
  121. params: {
  122. tenant,
  123. pageIndex,
  124. pageSize,
  125. ...params
  126. },
  127. ...options
  128. })
  129. }
  130. export function getDevicesByOrg (org, query, options) {
  131. const { pageNum: pageIndex, pageSize, ...params } = query
  132. return tenantRequest({
  133. url: '/device/relation/page',
  134. method: 'GET',
  135. params: {
  136. org,
  137. pageIndex,
  138. pageSize,
  139. ...params
  140. },
  141. ...options
  142. })
  143. }
  144. export function activateDevice ({ id, name }) {
  145. return confirmAndSend('激活', name, {
  146. url: '/device/batch/activate',
  147. method: 'PUT',
  148. data: [id]
  149. })
  150. }
  151. export function deactivateDevice ({ id, name }) {
  152. return confirmAndSend('停用', name, {
  153. url: '/device/batch/deactivate',
  154. method: 'PUT',
  155. data: [id]
  156. })
  157. }
  158. export function getDevice (id) {
  159. return request({
  160. url: `/device/${id}`,
  161. method: 'GET'
  162. })
  163. }
  164. export function getSubDevices (query) {
  165. const { id, pageNum: pageIndex, pageSize, ...params } = query
  166. return request({
  167. url: `/device/${id}/standbyDevice`,
  168. method: 'GET',
  169. params: {
  170. pageIndex, pageSize,
  171. ...params
  172. }
  173. })
  174. }
  175. export function addSubDevice ({ id }, data) {
  176. return add({
  177. url: `/device/${id}/addStandby`,
  178. method: 'POST',
  179. data
  180. })
  181. }
  182. export function addProductType (data) {
  183. return add({
  184. url: '/productType',
  185. method: 'POST',
  186. data
  187. })
  188. }
  189. export function updateProductType (data) {
  190. return update({
  191. url: '/productType',
  192. method: 'put',
  193. data
  194. })
  195. }
  196. export function deleteProductType ({ id, name }) {
  197. return del({
  198. url: `/productType/${id}`,
  199. method: 'DELETE'
  200. }, name)
  201. }
  202. export function getProductTypes (query) {
  203. const { pageNum: pageIndex, pageSize, ...params } = query
  204. return tenantRequest({
  205. url: '/productType/list',
  206. method: 'GET',
  207. params: addTenant({
  208. pageIndex, pageSize,
  209. ...params
  210. })
  211. })
  212. }
  213. export function addProduct (data) {
  214. return add({
  215. url: '/product',
  216. method: 'POST',
  217. data
  218. })
  219. }
  220. export function updateProduct (data) {
  221. return update({
  222. url: '/product',
  223. method: 'put',
  224. data
  225. })
  226. }
  227. export function deleteProduct ({ id, name }) {
  228. return del({
  229. url: `/product/${id}`,
  230. method: 'DELETE'
  231. }, name)
  232. }
  233. export function getProducts (query) {
  234. const { pageNum: pageIndex, pageSize, ...params } = query
  235. return tenantRequest({
  236. url: '/product/list',
  237. method: 'GET',
  238. params: addTenant({
  239. pageIndex, pageSize,
  240. ...params
  241. })
  242. })
  243. }
  244. export function addDeviceGroup (data) {
  245. return add({
  246. url: '/deviceGroup',
  247. method: 'POST',
  248. data
  249. })
  250. }
  251. export function updateDeviceGroup (data) {
  252. return update({
  253. url: '/deviceGroup',
  254. method: 'PUT',
  255. data
  256. })
  257. }
  258. export function deleteDeviceGroup ({ id, name }) {
  259. return del({
  260. url: `/deviceGroup/${id}`,
  261. method: 'DELETE'
  262. }, name)
  263. }
  264. export function getDeviceGroups (query) {
  265. const { pageNum: pageIndex, pageSize, ...params } = query
  266. return tenantRequest({
  267. url: '/deviceGroup/list',
  268. method: 'GET',
  269. params: addUser({
  270. pageIndex, pageSize,
  271. ...params
  272. })
  273. })
  274. }
  275. export function getDevicesByGroup (id) {
  276. return tenantRequest({
  277. url: `/deviceGroup/${id}/device`,
  278. method: 'GET',
  279. params: addTenantOrOrg({})
  280. })
  281. }
  282. export function getDeviceTree () {
  283. return tenantRequest({
  284. url: '/deviceGroup/deviceTree',
  285. method: 'GET',
  286. params: addUser(addTenantOrOrg({}))
  287. })
  288. }
  289. export function addDevicesToGroup (id, deviceIds) {
  290. return add({
  291. url: `/deviceGroup/${id}/device`,
  292. method: 'POST',
  293. data: deviceIds
  294. })
  295. }
  296. export function deleteDeviceFromGroup (id, { id: deviceId, name }) {
  297. return confirmAndSend('移除', name, {
  298. url: `/deviceGroup/${id}/device`,
  299. method: 'DELETE',
  300. params: { deviceId }
  301. })
  302. }
  303. export function getDeviceStatistics () {
  304. return getDeviceStatisticsByPath(store.getters.org)
  305. }
  306. export function getDeviceStatisticsByPath (path) {
  307. return tenantRequest({
  308. url: '/device/listDeviceTotal',
  309. method: 'GET',
  310. params: path === store.getters.tenant
  311. ? { tenant: path }
  312. : { org: path }
  313. })
  314. }
  315. export function getDeviceAlarms (query) {
  316. const { pageNum: pageIndex, pageSize, ...params } = query
  317. return request({
  318. url: '/deviceException/list',
  319. method: 'GET',
  320. params: {
  321. pageIndex, pageSize,
  322. ...params
  323. }
  324. }).then(({ data, totalCount }) => {
  325. return {
  326. data: data.map(({ id, deviceName, level, pic, picUrl, type, handle, status, happenTime, bugMessage, ...item }) => {
  327. const alarm = {
  328. id, deviceName, level, happenTime,
  329. file: pic && picUrl
  330. ? {
  331. type: AssetType.IMAGE,
  332. url: picUrl,
  333. thumb: picUrl,
  334. origin: true
  335. }
  336. : null,
  337. type: bugMessage || `预警码【${type}】`,
  338. handle: ['应用重启', '设备重启', '恢复出厂', '未干预'][handle] || '-',
  339. status: handle <= 2 && status <= 2
  340. ? {
  341. type: ['primary', 'success', 'danger'][status],
  342. label: ['处理中', '成功', '失败'][status]
  343. }
  344. : { label: '-' }
  345. }
  346. SupportedAlarmStrategies.forEach(({ key }) => {
  347. alarm[key] = getTag(item[key])
  348. })
  349. return alarm
  350. }),
  351. totalCount
  352. }
  353. })
  354. }
  355. function getTag (value) {
  356. switch (value) {
  357. case 0:
  358. return {
  359. type: 'danger',
  360. label: '否'
  361. }
  362. case 1:
  363. return {
  364. type: 'success',
  365. label: '是'
  366. }
  367. default:
  368. return { label: '-' }
  369. }
  370. }
  371. export function getTasks (query) {
  372. const { pageNum: pageIndex, pageSize, ...params } = query
  373. return request({
  374. url: '/device/functionTask',
  375. method: 'GET',
  376. params: {
  377. pageIndex, pageSize,
  378. ...params
  379. }
  380. })
  381. }
  382. export function addTask (task) {
  383. return add({
  384. url: '/device/functionTask',
  385. method: 'POST',
  386. data: task
  387. })
  388. }
  389. export function updateTask (task) {
  390. return update({
  391. url: '/device/functionTask',
  392. method: 'PUT',
  393. data: task
  394. })
  395. }
  396. export function deleteTask ({ taskId }) {
  397. return del({
  398. url: `/device/functionTask/${taskId}`,
  399. method: 'DELETE'
  400. })
  401. }
  402. export function activateTask ({ taskId }) {
  403. return confirmAndSend('启用', null, {
  404. url: '/device/functionTask/resume',
  405. method: 'PUT',
  406. data: [taskId]
  407. })
  408. }
  409. export function deactivateTask ({ taskId }) {
  410. return confirmAndSend('停用', null, {
  411. url: '/device/functionTask/pause',
  412. method: 'PUT',
  413. data: [taskId]
  414. })
  415. }
  416. export function getShadow (deviceId) {
  417. return request({
  418. url: `/device/shadow/${deviceId}`,
  419. method: 'GET'
  420. })
  421. }
  422. export function getRecordConfig (deviceId, config) {
  423. return request({
  424. url: '/deviceStream/config',
  425. method: 'GET',
  426. params: { deviceId },
  427. ...config
  428. })
  429. }
  430. export function addRecordConfig (data, config) {
  431. return request({
  432. url: '/deviceStream/config',
  433. method: 'POST',
  434. data,
  435. ...config
  436. })
  437. }
  438. export function updateRecordConfig (data, config) {
  439. return request({
  440. url: '/deviceStream/config',
  441. method: 'PUT',
  442. data,
  443. ...config
  444. })
  445. }
  446. export function authCode (stream) {
  447. return request({
  448. url: `/deviceStream/${stream}/authCode`,
  449. method: 'GET'
  450. })
  451. }