camera.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // electron/utils/readConfig.js
  2. const path = require('path');
  3. const fs = require('fs');
  4. const Log = require('ee-core/log');
  5. const { spawn } = require('child_process');
  6. const { liveShow, liveHide, setParams, capture, getParams,CMD,captureLive,closeOtherWindow,minimizeSmartShooter } = require('../api/camera');
  7. const { dialog } = require('electron'); // 引入 electron 的 dialog 模块
  8. const CoreWindow = require("ee-core/electron/window");
  9. const { readConfigFile, writeConfigFile } = require('../utils/config');
  10. const exe = {
  11. "digiCamControl":"CameraControl.exe",
  12. "SmartShooter":"SmartShooter5.exe",
  13. }
  14. function getExePath () {
  15. let exePath = ""
  16. if(readConfigFile().controlType === 'digiCamControl'){
  17. exePath = path.join( readConfigFile().controlPath || readConfigFile().digiCamControlPath, exe["digiCamControl"]);
  18. }else if(readConfigFile().controlType === 'SmartShooter'){
  19. exePath = path.join( readConfigFile().controlPath || readConfigFile().SmartShooterPath, exe["SmartShooter"]);
  20. }
  21. console.log('ex============ePath');
  22. console.log(exePath);
  23. return exePath
  24. }
  25. async function checkCameraControlCmdExists() {
  26. try {
  27. // 拼接 CameraControlCmd.exe 的完整路径
  28. let exePath = getExePath()
  29. // 检查文件是否存在
  30. const exists = await fs.promises.access(exePath, fs.constants.F_OK)
  31. .then(() => true)
  32. .catch(() => false);
  33. if (!exists) {
  34. // 弹出文件夹选择对话框
  35. const { canceled, filePaths } = await dialog.showOpenDialog({
  36. title: '选择 相机控制安装软件 文件夹',
  37. properties: ['openDirectory']
  38. });
  39. if (!canceled && filePaths.length > 0) {
  40. const selectedPath = filePaths[0];
  41. // Check if SmartShooter5.exe exists in the selected directory
  42. const hasExe = path.join(selectedPath, exe["SmartShooter"]);
  43. if (fs.existsSync(hasExe)) {
  44. writeConfigFile("controlType","SmartShooter")
  45. }else{
  46. writeConfigFile("controlType","digiCamControl")
  47. }
  48. writeConfigFile("controlPath",selectedPath)
  49. } else {
  50. console.error('用户未选择文件夹');
  51. return {
  52. status:-1,
  53. msg:"无法找到 CameraControlCmd.exe 或者 SmartShooter5.exe",
  54. }
  55. }
  56. }
  57. const res = await openCameraControlCmd();
  58. // 如果是SmartShooter,启动后最小化窗口
  59. if(readConfigFile().controlType === 'SmartShooter'){
  60. // 等待软件启动完成
  61. setTimeout(async () => {
  62. try {
  63. await minimizeSmartShooter();
  64. } catch (error) {
  65. console.log('最小化SmartShooter失败:', error);
  66. }
  67. }, 3000); // 等待3秒让软件完全启动
  68. }
  69. return res;
  70. } catch (error) {
  71. Log.error('检查 第三方相机控制器 是否存在时出错:', error);
  72. throw error;
  73. }
  74. }
  75. async function openCameraControlCmd(digiCamControlPath) {
  76. return new Promise(async (resolve, reject) => {
  77. try {
  78. // 获取 digiCamControl 文件夹路径
  79. // 拼接 CameraControlCmd.exe 的完整路径
  80. let exePath = getExePath()
  81. // 检查文件是否存在
  82. await fs.promises.access(exePath, fs.constants.F_OK);
  83. try {
  84. const child = spawn(exePath);
  85. child.stdout.on('data', (data) => {
  86. resolve(true)
  87. });
  88. child.on('close', (code) => {
  89. if (code === 0) {
  90. reject(false)
  91. }
  92. });
  93. } catch (error) {
  94. Log.error('error 第三方相机控制器:', error);
  95. throw error;
  96. }
  97. } catch (error) {
  98. Log.error('无法找到或运行 第三方相机控制器:', error);
  99. throw error;
  100. }
  101. })
  102. }
  103. async function closeCameraControlTips() {
  104. try {
  105. await closeOtherWindow()
  106. }catch (e) {
  107. console.log(e)
  108. }
  109. }
  110. module.exports = {
  111. checkCameraControlCmdExists,
  112. closeCameraControlTips,
  113. minimizeSmartShooter
  114. };