CameraPreview.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <template>
  2. <div>
  3. <div class="cameraMain">
  4. <div class="cameraTree">
  5. <CameraTreeCom
  6. :loading="presetListStore.loading"
  7. :camera-tree="cameraTree || []"
  8. :total="codeShowList.length"
  9. :no-integration-num="noIntegrationNum"
  10. :no-networking-num="noNetworkingNum"
  11. />
  12. </div>
  13. <div class="cameraSettingWrapper">
  14. <div class="cameraView">
  15. <CameraViewSetting v-if="cameraDetailStore.cameraId" @change-tree-render="changeRender" />
  16. <div class="cameraPlaceholder" v-else>请选择左侧相机</div>
  17. </div>
  18. </div>
  19. </div>
  20. </div>
  21. </template>
  22. <script lang="ts" setup>
  23. import { onUnmounted, ref, watch } from 'vue';
  24. import CameraTreeCom from './components/CameraTree/CameraTree.vue';
  25. import CameraViewSetting from './components/CameraViewSetting/CameraViewSetting.vue';
  26. import useCameraDetailStore from './store/useCameraDetailStore';
  27. import useCameraAlgoStore from './store/useCameraAlgoStore';
  28. import usePresetListStore from './store/usePresetListStore';
  29. import useFenceStore from './store/useFenceStore';
  30. import { onMounted } from 'vue';
  31. import { IsPtz } from '@/api/camera/camera-overview';
  32. import { CameraTree, getCameraTree } from '@/api/camera/camera-preview';
  33. import useCameraStatus from './store/useCameraStatus';
  34. const cameraStatus = useCameraStatus();
  35. const { noNetworkingNum, openInterval, closeInterval } = cameraStatus;
  36. const cameraDetailStore = useCameraDetailStore();
  37. const cameraAlgoStore = useCameraAlgoStore();
  38. const fenceStore = useFenceStore();
  39. const presetListStore = usePresetListStore();
  40. const cameraTree = ref<CameraTree[]>([]);
  41. const codeShowList = ref<string[]>([]);
  42. const noIntegrationNum = ref<number>(0);
  43. function updateNetworkingState(data, targetData) {
  44. let integrationCount = 0;
  45. for (let i = 0; i < data.length; i++) {
  46. const node = data[i];
  47. const matchedNode = targetData.find((item) => item.cameraCode === node.code);
  48. if (matchedNode) {
  49. node.networkingState = matchedNode.status;
  50. }
  51. if (node.integrationState === 1) {
  52. integrationCount++;
  53. }
  54. if (node.children && node.children.length > 0) {
  55. const childIntegrationCount = updateNetworkingState(node.children, targetData);
  56. integrationCount += childIntegrationCount;
  57. }
  58. }
  59. noIntegrationNum.value = integrationCount;
  60. return integrationCount;
  61. }
  62. function updateRender(data, targetData, targetVal) {
  63. for (let i = 0; i < data.length; i++) {
  64. const node = data[i];
  65. if (node.id === targetData) {
  66. node.render = targetVal;
  67. }
  68. if (node.children && node.children.length > 0) {
  69. updateRender(node.children, targetData, targetVal);
  70. }
  71. }
  72. }
  73. function getCameraList(data) {
  74. let cameraList = [] as string[];
  75. for (let i = 0; i < data.length; i++) {
  76. const node = data[i];
  77. if (node.nodeType === 'camera') {
  78. cameraList.push(node.code);
  79. }
  80. if (node.children && node.children.length > 0) {
  81. const childCameraList = getCameraList(node.children);
  82. cameraList.push(...childCameraList);
  83. }
  84. }
  85. return cameraList;
  86. }
  87. const changeRender = (render: string | number) => {
  88. updateRender(cameraTree.value, cameraDetailStore.cameraId, render);
  89. };
  90. watch(
  91. () => cameraDetailStore.cameraId,
  92. async (cameraId) => {
  93. fenceStore.clear();
  94. if (cameraId) {
  95. const presetList = await presetListStore.getPresetList(cameraId);
  96. if (cameraTree.value.length === 0) {
  97. /** 如果当前树为空,那么切换相机的时候,要重新请求树结构 */
  98. const tree = await getCameraTree();
  99. cameraTree.value = tree as unknown as CameraTree[];
  100. }
  101. const detail = getCameraDetail(cameraTree.value, cameraDetailStore.cameraId);
  102. if (detail) {
  103. cameraDetailStore.setDetail(detail);
  104. if (detail?.isPtz === IsPtz.disabled) {
  105. presetListStore.currentPresetToken = presetList?.[0].token;
  106. }
  107. }
  108. cameraAlgoStore.getCameraAlgoList(cameraId);
  109. cameraAlgoStore.selectedAlgoId = null;
  110. } else {
  111. /** 没有相机的时候也要请求相机树 */
  112. const tree = await getCameraTree();
  113. cameraTree.value = tree as unknown as CameraTree[];
  114. }
  115. },
  116. {
  117. immediate: true,
  118. },
  119. );
  120. onMounted(() => {
  121. cameraAlgoStore.getAllAlgoList();
  122. getCameraTree().then((res) => {
  123. cameraTree.value = res;
  124. codeShowList.value = getCameraList(res);
  125. // openInterval(codeShowList.value, cameraTree.value, updateNetworkingState);
  126. // (targetData) => { updateNetworkingState(cameraTree.value, targetData) }
  127. openInterval(codeShowList.value, (targetData) => {
  128. updateNetworkingState(cameraTree.value, targetData);
  129. });
  130. });
  131. });
  132. onUnmounted(() => {
  133. /** 离开页面要清理掉所有的store */
  134. cameraDetailStore.clear();
  135. cameraAlgoStore.clear();
  136. fenceStore.clear();
  137. presetListStore.clear();
  138. closeInterval();
  139. });
  140. function getCameraDetail(tree: CameraTree[], cameraId: number): CameraTree | null {
  141. let detail: CameraTree | null = null;
  142. function getDetail(t: CameraTree[]) {
  143. t.forEach((x) => {
  144. if (x.nodeType === 'camera' && x.id === cameraId) {
  145. detail = x;
  146. } else if (x.children && x.children.length > 0) {
  147. getDetail(x.children);
  148. }
  149. });
  150. }
  151. getDetail(tree);
  152. return detail;
  153. }
  154. </script>
  155. <style lang="scss" scoped>
  156. .cameraParamsSetting {
  157. width: 350px;
  158. min-height: 300px;
  159. // border: 1px solid #ccc;
  160. }
  161. .cameraParamsSetting {
  162. width: 350px;
  163. min-height: 300px;
  164. // border: 1px solid #ccc;
  165. }
  166. .algorithmsSetting {
  167. flex: 1;
  168. border-left: 1px solid #ccc;
  169. padding-left: 20px;
  170. }
  171. .cameraMain {
  172. display: flex;
  173. background: #fff;
  174. height: calc(100vh - 90px);
  175. }
  176. .cameraTree {
  177. width: 250px;
  178. flex-shrink: 0;
  179. // height: 800px;
  180. // border: 1px solid #ccc;
  181. border: 1px solid #f0f2f5;
  182. margin: 5px;
  183. }
  184. .cameraPlaceholder {
  185. color: #333;
  186. text-align: center;
  187. margin-top: 100px;
  188. margin-left: 100px;
  189. }
  190. </style>