socket.js 5.5 KB

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