socket.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. const Log = require('ee-core/log');
  2. const CoreWindow = require('ee-core/electron/window');
  3. const WebSocket = require('ws'); // 引入原生 ws 库
  4. const { readConfigFile } = require('./config');
  5. const pyapp = readConfigFile().pyapp
  6. const { app } = require('electron');
  7. const path = require('path');
  8. const fs = require('fs');
  9. const typeToMessage = {
  10. run_mcu_single:['seeting','default'],
  11. get_deviation_data:"developer",
  12. set_deviation:"developer",
  13. get_mcu_other_info:"developer",
  14. set_mcu_other_info:"developer",
  15. send_command:"developer",
  16. smart_shooter_get_camera_property:"seeting",
  17. detail_progress:"PhotographyDetail",
  18. segment_progress:"PhotographyDetail"
  19. }
  20. const previewPath = path.join(app.getPath("userData"),'preview','liveview.png');
  21. // 确保目录存在的函数
  22. function ensureDirectoryExistence(filePath) {
  23. const dir = path.dirname(filePath);
  24. if (fs.existsSync(dir)) {
  25. return true;
  26. }
  27. ensureDirectoryExistence(path.dirname(dir));
  28. fs.mkdirSync(dir);
  29. }
  30. function livePreview(data){
  31. if(data.msg === '预览数据发送' && data.code === 1){
  32. ensureDirectoryExistence(previewPath);
  33. const tempFilePath = `${previewPath}.tmp`;
  34. fs.writeFile(tempFilePath, data.data.smart_shooter_preview, 'base64', (err) => {
  35. if (err) {
  36. Log.error('写入临时文件失败:', err);
  37. } else {
  38. fs.rename(tempFilePath, previewPath, (renameErr) => {
  39. if (renameErr) {
  40. } else {
  41. }
  42. });
  43. }
  44. });
  45. }
  46. }
  47. const pySocket = function () {
  48. this.app = null;
  49. this.init = async function (this_app) {
  50. if(this_app) this.app = this_app;
  51. await new Promise(async (resolve,reject) => {
  52. const win = CoreWindow.getMainWindow()
  53. if(app.socket){
  54. resolve(true);
  55. win.webContents.send('controller.socket.connect_open', true);
  56. return;
  57. }
  58. app.socket = new WebSocket('ws://'+pyapp+':7074/ws');
  59. // 监听连接成功事件
  60. app.socket.on('open', () => {
  61. console.log('socket open')
  62. resolve(true);
  63. win.webContents.send('controller.socket.connect_open', true);
  64. });
  65. // 监听消息事件
  66. app.socket.on('message', (data) => {
  67. try {
  68. let this_data = JSON.parse(data.toString());
  69. if(!['blue_tooth','smart_shooter_enable_preview','smart_shooter_getinfo'].includes(this_data.msg_type)){
  70. console.log('message');
  71. console.log(this_data);
  72. }
  73. if(this_data.msg_type){
  74. let notAllMessage = false
  75. switch (this_data.msg_type){
  76. case 'smart_shooter_enable_preview':
  77. notAllMessage = true;
  78. livePreview(this_data);
  79. break;
  80. }
  81. if(notAllMessage) return;
  82. let channel = 'controller.socket.message_'+this_data.msg_type;
  83. if(typeToMessage[this_data.msg_type]){
  84. if(typeof typeToMessage[this_data.msg_type] === 'object'){
  85. typeToMessage[this_data.msg_type].map(item=>{
  86. if(item === 'default'){
  87. win.webContents.send(channel, this_data);
  88. }else{
  89. if(this.app.electron[item]) this.app.electron[item].webContents.send(channel, this_data);
  90. }
  91. })
  92. }else{
  93. if(this.app.electron[typeToMessage[this_data.msg_type]]) this.app.electron[typeToMessage[this_data.msg_type]].webContents.send(channel, this_data);
  94. }
  95. }else{
  96. win.webContents.send(channel, this_data);
  97. }
  98. }
  99. }catch (e){
  100. console.log(e)
  101. }
  102. });
  103. // 监听连接关闭事件
  104. app.socket.on('close', () => {
  105. console.log('socket close');
  106. win.webContents.send('controller.socket.disconnect', null);
  107. app.socket = null
  108. });
  109. // 监听错误事件
  110. app.socket.on('error', (err) => {
  111. console.log('socket error');
  112. win.webContents.send('controller.socket.disconnect', null);
  113. reject(true);
  114. });
  115. })
  116. }
  117. this.sendPing = function () {
  118. const message = JSON.stringify({ data: 'node', type: 'ping' });
  119. this.sendMessage(message);
  120. }
  121. this.sendMessage = async function (message) {
  122. console.log('socket.=========================sendMessage');
  123. console.log('socket.sendMessage');
  124. console.log(message);
  125. console.log(app.socket?.readyState);
  126. if(!app.socket){
  127. await this.init()
  128. }
  129. // 检查连接状态
  130. if (app.socket?.readyState === WebSocket.OPEN) {
  131. console.log('send');
  132. app.socket.send(message); // 使用 send() 发送
  133. }
  134. }
  135. this.disconnect = function () {
  136. if (app.socket) {
  137. app.socket.close(); // 使用 close() 方法
  138. app.socket = null;
  139. }
  140. }
  141. this.onSocketMessage = async function (message_type,callback) { // 监听消息事件
  142. return new Promise(async (resolve,reject) => {
  143. app.socket.on('message', onSocketMessage);
  144. async function onSocketMessage(data){
  145. try {
  146. let this_data = JSON.parse(data.toString());
  147. if(this_data.msg_type === message_type){
  148. app.socket.off('message', onSocketMessage);
  149. callback(this_data)
  150. resolve()
  151. }
  152. }catch (e){
  153. Log.error(e)
  154. reject(e)
  155. }
  156. }
  157. })
  158. }
  159. return this;
  160. }
  161. module.exports = pySocket;