socket.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. detail_result_progress:"PhotographyDetail"
  23. }
  24. const previewPath = path.join(app.getPath("userData"),'preview','liveview.png');
  25. // 确保目录存在的函数
  26. function ensureDirectoryExistence(filePath) {
  27. const dir = path.dirname(filePath);
  28. if (fs.existsSync(dir)) {
  29. return true;
  30. }
  31. ensureDirectoryExistence(path.dirname(dir));
  32. fs.mkdirSync(dir);
  33. }
  34. function livePreview(data){
  35. if(data.msg === '预览数据发送' && data.code === 1){
  36. ensureDirectoryExistence(previewPath);
  37. const tempFilePath = `${previewPath}.tmp`;
  38. fs.writeFile(tempFilePath, data.data.smart_shooter_preview, 'base64', (err) => {
  39. if (err) {
  40. Log.error('写入临时文件失败:', err);
  41. } else {
  42. fs.rename(tempFilePath, previewPath, (renameErr) => {
  43. if (renameErr) {
  44. } else {
  45. }
  46. });
  47. }
  48. });
  49. }
  50. }
  51. const pySocket = function () {
  52. this.app = null;
  53. // 重连配置
  54. this.reconnectConfig = {
  55. maxRetries: 5, // 最大重连次数
  56. retryInterval: 3000, // 重连间隔(毫秒)
  57. maxRetryInterval: 30000, // 最大重连间隔
  58. retryMultiplier: 1.5 // 重连间隔递增倍数
  59. };
  60. this.reconnectAttempts = 0; // 当前重连次数
  61. this.isReconnecting = false; // 是否正在重连
  62. this.shouldReconnect = true; // 是否应该重连(用于手动断开时阻止重连)
  63. // 心跳配置
  64. this.heartbeatConfig = {
  65. interval: 10000, // 心跳间隔(10秒)
  66. timeout: 30000, // 心跳超时时间(30秒)
  67. maxMissed: 3 // 最大允许丢失的心跳次数
  68. };
  69. this.heartbeatTimer = null; // 心跳定时器
  70. this.heartbeatTimeout = null; // 心跳超时定时器
  71. this.missedHeartbeats = 0; // 丢失的心跳次数
  72. this.lastHeartbeatTime = 0; // 最后一次心跳时间
  73. // 启动心跳机制
  74. this.startHeartbeat = function() {
  75. this.stopHeartbeat(); // 先停止之前的心跳
  76. console.log('启动心跳机制,间隔:', this.heartbeatConfig.interval + 'ms');
  77. this.heartbeatTimer = setInterval(() => {
  78. if (app.socket && app.socket.readyState === WebSocket.OPEN) {
  79. this.sendPing();
  80. this.lastHeartbeatTime = Date.now();
  81. // 设置心跳超时检测
  82. this.setHeartbeatTimeout();
  83. }
  84. }, this.heartbeatConfig.interval);
  85. };
  86. // 停止心跳机制
  87. this.stopHeartbeat = function() {
  88. if (this.heartbeatTimer) {
  89. clearInterval(this.heartbeatTimer);
  90. this.heartbeatTimer = null;
  91. }
  92. if (this.heartbeatTimeout) {
  93. clearTimeout(this.heartbeatTimeout);
  94. this.heartbeatTimeout = null;
  95. }
  96. this.missedHeartbeats = 0;
  97. console.log('停止心跳机制');
  98. };
  99. // 设置心跳超时检测
  100. this.setHeartbeatTimeout = function() {
  101. if (this.heartbeatTimeout) {
  102. clearTimeout(this.heartbeatTimeout);
  103. }
  104. this.heartbeatTimeout = setTimeout(() => {
  105. this.missedHeartbeats++;
  106. console.log(`心跳超时,丢失次数: ${this.missedHeartbeats}/${this.heartbeatConfig.maxMissed}`);
  107. if (this.missedHeartbeats >= this.heartbeatConfig.maxMissed) {
  108. console.log('心跳超时次数过多,主动断开连接');
  109. if (app.socket) {
  110. app.socket.close();
  111. }
  112. }
  113. }, this.heartbeatConfig.timeout);
  114. };
  115. // 处理心跳响应
  116. this.handleHeartbeatResponse = function() {
  117. this.missedHeartbeats = 0;
  118. if (this.heartbeatTimeout) {
  119. clearTimeout(this.heartbeatTimeout);
  120. this.heartbeatTimeout = null;
  121. }
  122. console.log('收到心跳响应');
  123. };
  124. // 重连逻辑函数
  125. this.attemptReconnect = async function() {
  126. if (!this.shouldReconnect || this.isReconnecting) {
  127. return;
  128. }
  129. if (this.reconnectAttempts >= this.reconnectConfig.maxRetries) {
  130. Log.info('达到最大重连次数,停止重连');
  131. this.isReconnecting = false;
  132. this.reconnectAttempts = 0;
  133. return;
  134. }
  135. this.isReconnecting = true;
  136. this.reconnectAttempts++;
  137. // 计算重连间隔(指数退避)
  138. const interval = Math.min(
  139. this.reconnectConfig.retryInterval * Math.pow(this.reconnectConfig.retryMultiplier, this.reconnectAttempts - 1),
  140. this.reconnectConfig.maxRetryInterval
  141. );
  142. Log.info(`第${this.reconnectAttempts}次重连尝试,${interval}ms后开始...`);
  143. setTimeout(async () => {
  144. try {
  145. Log.info('开始重连...');
  146. await this.init();
  147. Log.info('重连成功');
  148. this.isReconnecting = false;
  149. this.reconnectAttempts = 0;
  150. } catch (error) {
  151. Log.info('重连失败:', error);
  152. this.isReconnecting = false;
  153. // 继续尝试重连
  154. this.attemptReconnect();
  155. }
  156. }, interval);
  157. };
  158. this.init = async function (this_app) {
  159. if(this_app) this.app = this_app;
  160. await new Promise(async (resolve,reject) => {
  161. const win = CoreWindow.getMainWindow()
  162. if(app.socket){
  163. resolve(true);
  164. win.webContents.send('controller.socket.connect_open', true);
  165. return;
  166. }
  167. // 重置重连状态
  168. this.shouldReconnect = true;
  169. this.isReconnecting = false;
  170. app.socket = new WebSocket('ws://'+pyapp+':7074/ws');
  171. // 监听连接成功事件
  172. app.socket.on('open', () => {
  173. Log.info('socket open')
  174. resolve(true);
  175. win.webContents.send('controller.socket.connect_open', true);
  176. // 启动心跳机制
  177. this.startHeartbeat();
  178. });
  179. // 监听消息事件
  180. app.socket.on('message', (data) => {
  181. try {
  182. let this_data = JSON.parse(data.toString());
  183. if(!['blue_tooth','smart_shooter_enable_preview','smart_shooter_getinfo'].includes(this_data.msg_type)){
  184. console.log('message');
  185. console.log(this_data);
  186. // Log.info(this_data);
  187. }
  188. if(this_data.msg_type){
  189. let notAllMessage = false
  190. switch (this_data.msg_type){
  191. case 'smart_shooter_enable_preview':
  192. notAllMessage = true;
  193. livePreview(this_data);
  194. break;
  195. case 'pong':
  196. // 处理心跳响应
  197. this.handleHeartbeatResponse();
  198. notAllMessage = true;
  199. break;
  200. }
  201. if(notAllMessage) return;
  202. let channel = 'controller.socket.message_'+this_data.msg_type;
  203. if(typeToMessage[this_data.msg_type]){
  204. if(typeof typeToMessage[this_data.msg_type] === 'object'){
  205. typeToMessage[this_data.msg_type].map(item=>{
  206. if(item === 'default'){
  207. win.webContents.send(channel, this_data);
  208. }else{
  209. if(this.app.electron[item]) {
  210. this.app.electron[item].webContents.send(channel, this_data);
  211. }
  212. }
  213. })
  214. }else{
  215. if(this.app.electron[typeToMessage[this_data.msg_type]]) {
  216. this.app.electron[typeToMessage[this_data.msg_type]].webContents.send(channel, this_data);
  217. }
  218. }
  219. }else{
  220. win.webContents.send(channel, this_data);
  221. }
  222. }
  223. }catch (e){
  224. console.log(e)
  225. }
  226. });
  227. // 监听连接关闭事件
  228. app.socket.on('close', (e) => {
  229. Log.info('socket close');
  230. Log.info(e);
  231. win.webContents.send('controller.socket.disconnect', null);
  232. // 停止心跳机制
  233. this.stopHeartbeat();
  234. app.socket = null;
  235. // 启动重连机制
  236. this.attemptReconnect();
  237. });
  238. // 监听错误事件
  239. app.socket.on('error', (err) => {
  240. Log.info('socket error:', err);
  241. win.webContents.send('controller.socket.disconnect', null);
  242. // 停止心跳机制
  243. this.stopHeartbeat();
  244. app.socket = null;
  245. // 启动重连机制
  246. this.attemptReconnect();
  247. reject(true);
  248. });
  249. })
  250. }
  251. this.sendPing = function () {
  252. const message = JSON.stringify({ data: 'node', type: 'ping' });
  253. this.sendMessage(message);
  254. }
  255. this.sendMessage = async function (message) {
  256. console.log('socket.=========================sendMessage');
  257. console.log('socket.sendMessage');
  258. console.log(message);
  259. console.log(app.socket?.readyState);
  260. if(!app.socket){
  261. await this.init()
  262. }
  263. // 检查连接状态
  264. if (app.socket?.readyState === WebSocket.OPEN) {
  265. console.log('send');
  266. app.socket.send(message); // 使用 send() 发送
  267. }
  268. }
  269. this.disconnect = function () {
  270. // 设置标志,阻止重连
  271. this.shouldReconnect = false;
  272. this.isReconnecting = false;
  273. this.reconnectAttempts = 0;
  274. // 停止心跳机制
  275. this.stopHeartbeat();
  276. if (app.socket) {
  277. app.socket.close(); // 使用 close() 方法
  278. app.socket = null;
  279. }
  280. }
  281. // 重新启用重连功能
  282. this.enableReconnect = function () {
  283. this.shouldReconnect = true;
  284. this.reconnectAttempts = 0;
  285. this.isReconnecting = false;
  286. }
  287. this.onSocketMessage = async function (message_type,callback) { // 监听消息事件
  288. return new Promise(async (resolve,reject) => {
  289. app.socket.on('message', onSocketMessage);
  290. async function onSocketMessage(data){
  291. try {
  292. let this_data = JSON.parse(data.toString());
  293. if(this_data.msg_type === message_type){
  294. app.socket.off('message', onSocketMessage);
  295. callback(this_data)
  296. resolve()
  297. }
  298. }catch (e){
  299. Log.error(e)
  300. reject(e)
  301. }
  302. }
  303. })
  304. }
  305. return this;
  306. }
  307. module.exports = pySocket;