socket.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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_device_info:['seeting','default'],
  12. get_deviation_data:["developer","seeting"],
  13. set_deviation:"developer",
  14. get_mcu_other_info:"developer",
  15. set_mcu_other_info:"developer",
  16. send_command:"developer",
  17. smart_shooter_get_camera_property:"seeting",
  18. detail_progress:"PhotographyDetail",
  19. segment_progress:"PhotographyDetail",
  20. upper_footer_progress:"PhotographyDetail",
  21. scene_progress:"PhotographyDetail",
  22. upload_goods_progress:"PhotographyDetail",
  23. detail_result_progress:"PhotographyDetail",
  24. "get_dynamic_config":"seeting",
  25. "set_dynamic_config":"seeting",
  26. "get_mcu_info":"seeting",
  27. }
  28. // MCU 错误日志配置(从 config 读取,默认100条)
  29. let mcuErrorLogConfig = {
  30. maxLogs: 100, // 最大日志条数
  31. };
  32. // 初始化时从配置读取
  33. try {
  34. const config = readConfigFile();
  35. if (config.mcuErrorLog && typeof config.mcuErrorLog.maxLogs === 'number') {
  36. mcuErrorLogConfig.maxLogs = config.mcuErrorLog.maxLogs;
  37. }
  38. } catch (e) {
  39. console.log('读取 MCU 日志配置失败,使用默认值');
  40. }
  41. // MCU 错误日志存储
  42. const mcuErrorLogs = [];
  43. let mcuErrorUnreadCount = 0;
  44. const previewPath = path.join(app.getPath("userData"),'preview','liveview.png');
  45. // 确保目录存在的函数
  46. function ensureDirectoryExistence(filePath) {
  47. const dir = path.dirname(filePath);
  48. if (fs.existsSync(dir)) {
  49. return true;
  50. }
  51. ensureDirectoryExistence(path.dirname(dir));
  52. fs.mkdirSync(dir);
  53. }
  54. function livePreview(data){
  55. if(data.msg === '预览数据发送' && data.code === 1){
  56. ensureDirectoryExistence(previewPath);
  57. const tempFilePath = `${previewPath}.tmp`;
  58. fs.writeFile(tempFilePath, data.data.smart_shooter_preview, 'base64', (err) => {
  59. if (err) {
  60. Log.error('写入临时文件失败:', err);
  61. } else {
  62. fs.rename(tempFilePath, previewPath, (renameErr) => {
  63. if (renameErr) {
  64. } else {
  65. }
  66. });
  67. }
  68. });
  69. }
  70. }
  71. const pySocket = function () {
  72. this.app = null;
  73. // 重连配置
  74. this.reconnectConfig = {
  75. maxRetries: 5, // 最大重连次数
  76. retryInterval: 3000, // 重连间隔(毫秒)
  77. maxRetryInterval: 30000, // 最大重连间隔
  78. retryMultiplier: 1.5 // 重连间隔递增倍数
  79. };
  80. this.reconnectAttempts = 0; // 当前重连次数
  81. this.isReconnecting = false; // 是否正在重连
  82. this.shouldReconnect = true; // 是否应该重连(用于手动断开时阻止重连)
  83. // 心跳配置
  84. this.heartbeatConfig = {
  85. interval: 10000, // 心跳间隔(10秒)
  86. timeout: 30000, // 心跳超时时间(30秒)
  87. maxMissed: 3 // 最大允许丢失的心跳次数
  88. };
  89. this.heartbeatTimer = null; // 心跳定时器
  90. this.heartbeatTimeout = null; // 心跳超时定时器
  91. this.missedHeartbeats = 0; // 丢失的心跳次数
  92. this.lastHeartbeatTime = 0; // 最后一次心跳时间
  93. // 启动心跳机制
  94. this.startHeartbeat = function() {
  95. this.stopHeartbeat(); // 先停止之前的心跳
  96. console.log('启动心跳机制,间隔:', this.heartbeatConfig.interval + 'ms');
  97. this.heartbeatTimer = setInterval(() => {
  98. if (app.socket && app.socket.readyState === WebSocket.OPEN) {
  99. this.sendPing();
  100. this.lastHeartbeatTime = Date.now();
  101. // 设置心跳超时检测
  102. this.setHeartbeatTimeout();
  103. }
  104. }, this.heartbeatConfig.interval);
  105. };
  106. // 停止心跳机制
  107. this.stopHeartbeat = function() {
  108. if (this.heartbeatTimer) {
  109. clearInterval(this.heartbeatTimer);
  110. this.heartbeatTimer = null;
  111. }
  112. if (this.heartbeatTimeout) {
  113. clearTimeout(this.heartbeatTimeout);
  114. this.heartbeatTimeout = null;
  115. }
  116. this.missedHeartbeats = 0;
  117. console.log('停止心跳机制');
  118. };
  119. // 设置心跳超时检测
  120. this.setHeartbeatTimeout = function() {
  121. if (this.heartbeatTimeout) {
  122. clearTimeout(this.heartbeatTimeout);
  123. }
  124. this.heartbeatTimeout = setTimeout(() => {
  125. this.missedHeartbeats++;
  126. console.log(`心跳超时,丢失次数: ${this.missedHeartbeats}/${this.heartbeatConfig.maxMissed}`);
  127. if (this.missedHeartbeats >= this.heartbeatConfig.maxMissed) {
  128. console.log('心跳超时次数过多,主动断开连接');
  129. if (app.socket) {
  130. app.socket.close();
  131. }
  132. }
  133. }, this.heartbeatConfig.timeout);
  134. };
  135. // 处理心跳响应
  136. this.handleHeartbeatResponse = function() {
  137. this.missedHeartbeats = 0;
  138. if (this.heartbeatTimeout) {
  139. clearTimeout(this.heartbeatTimeout);
  140. this.heartbeatTimeout = null;
  141. }
  142. console.log('收到心跳响应');
  143. };
  144. // 重连逻辑函数
  145. this.attemptReconnect = async function() {
  146. if (!this.shouldReconnect || this.isReconnecting) {
  147. return;
  148. }
  149. if (this.reconnectAttempts >= this.reconnectConfig.maxRetries) {
  150. Log.info('达到最大重连次数,停止重连');
  151. this.isReconnecting = false;
  152. this.reconnectAttempts = 0;
  153. return;
  154. }
  155. this.isReconnecting = true;
  156. this.reconnectAttempts++;
  157. // 计算重连间隔(指数退避)
  158. const interval = Math.min(
  159. this.reconnectConfig.retryInterval * Math.pow(this.reconnectConfig.retryMultiplier, this.reconnectAttempts - 1),
  160. this.reconnectConfig.maxRetryInterval
  161. );
  162. Log.info(`第${this.reconnectAttempts}次重连尝试,${interval}ms后开始...`);
  163. setTimeout(async () => {
  164. try {
  165. Log.info('开始重连...');
  166. await this.init();
  167. Log.info('重连成功');
  168. this.isReconnecting = false;
  169. this.reconnectAttempts = 0;
  170. } catch (error) {
  171. Log.info('重连失败:', error);
  172. this.isReconnecting = false;
  173. // 继续尝试重连
  174. this.attemptReconnect();
  175. }
  176. }, interval);
  177. };
  178. this.init = async function (this_app) {
  179. if(this_app) this.app = this_app;
  180. await new Promise(async (resolve,reject) => {
  181. const win = CoreWindow.getMainWindow()
  182. if(app.socket){
  183. resolve(true);
  184. win.webContents.send('controller.socket.connect_open', true);
  185. return;
  186. }
  187. // 重置重连状态
  188. this.shouldReconnect = true;
  189. this.isReconnecting = false;
  190. app.socket = new WebSocket('ws://'+pyapp+':7074/ws');
  191. // 监听连接成功事件
  192. app.socket.on('open', () => {
  193. Log.info('socket open')
  194. resolve(true);
  195. win.webContents.send('controller.socket.connect_open', true);
  196. // 启动心跳机制
  197. this.startHeartbeat();
  198. });
  199. // 监听消息事件
  200. app.socket.on('message', (data) => {
  201. try {
  202. let this_data = JSON.parse(data.toString());
  203. if(!['blue_tooth','smart_shooter_enable_preview','smart_shooter_getinfo'].includes(this_data.msg_type)){
  204. console.log('message');
  205. console.log(this_data);
  206. // Log.info(this_data);
  207. }
  208. if(this_data.msg_type){
  209. let notAllMessage = false
  210. switch (this_data.msg_type){
  211. case 'smart_shooter_enable_preview':
  212. notAllMessage = true;
  213. livePreview(this_data);
  214. break;
  215. case 'pong':
  216. // 处理心跳响应
  217. this.handleHeartbeatResponse();
  218. notAllMessage = true;
  219. break;
  220. case 'print_mcu_error_data':
  221. // 处理 MCU 错误日志
  222. this.handleMcuErrorData(this_data, win);
  223. notAllMessage = true;
  224. break;
  225. }
  226. if(notAllMessage) return;
  227. let channel = 'controller.socket.message_'+this_data.msg_type;
  228. if(typeToMessage[this_data.msg_type]){
  229. if(typeof typeToMessage[this_data.msg_type] === 'object'){
  230. typeToMessage[this_data.msg_type].map(item=>{
  231. if(item === 'default'){
  232. win.webContents.send(channel, this_data);
  233. }else{
  234. if(this.app.electron[item]) {
  235. this.app.electron[item].webContents.send(channel, this_data);
  236. }
  237. }
  238. })
  239. }else{
  240. if(this.app.electron[typeToMessage[this_data.msg_type]]) {
  241. this.app.electron[typeToMessage[this_data.msg_type]].webContents.send(channel, this_data);
  242. }
  243. }
  244. }else{
  245. win.webContents.send(channel, this_data);
  246. }
  247. }
  248. }catch (e){
  249. console.log(e)
  250. }
  251. });
  252. // 监听连接关闭事件
  253. app.socket.on('close', (e) => {
  254. Log.info('socket close');
  255. Log.info(e);
  256. win.webContents.send('controller.socket.disconnect', null);
  257. // 停止心跳机制
  258. this.stopHeartbeat();
  259. app.socket = null;
  260. // 启动重连机制
  261. this.attemptReconnect();
  262. });
  263. // 监听错误事件
  264. app.socket.on('error', (err) => {
  265. Log.info('socket error:', err);
  266. win.webContents.send('controller.socket.disconnect', null);
  267. // 停止心跳机制
  268. this.stopHeartbeat();
  269. app.socket = null;
  270. // 启动重连机制
  271. this.attemptReconnect();
  272. reject(true);
  273. });
  274. })
  275. }
  276. this.sendPing = function () {
  277. const message = JSON.stringify({ data: 'node', type: 'ping' });
  278. this.sendMessage(message);
  279. }
  280. this.sendMessage = async function (message) {
  281. console.log('socket.=========================sendMessage');
  282. console.log('socket.sendMessage');
  283. console.log(message);
  284. console.log(app.socket?.readyState);
  285. if(!app.socket){
  286. await this.init()
  287. }
  288. // 检查连接状态
  289. if (app.socket?.readyState === WebSocket.OPEN) {
  290. console.log('send');
  291. app.socket.send(message); // 使用 send() 发送
  292. }
  293. }
  294. this.disconnect = function () {
  295. // 设置标志,阻止重连
  296. this.shouldReconnect = false;
  297. this.isReconnecting = false;
  298. this.reconnectAttempts = 0;
  299. // 停止心跳机制
  300. this.stopHeartbeat();
  301. if (app.socket) {
  302. app.socket.close(); // 使用 close() 方法
  303. app.socket = null;
  304. }
  305. }
  306. // 重新启用重连功能
  307. this.enableReconnect = function () {
  308. this.shouldReconnect = true;
  309. this.reconnectAttempts = 0;
  310. this.isReconnecting = false;
  311. }
  312. this.onSocketMessage = async function (message_type,callback) { // 监听消息事件
  313. return new Promise(async (resolve,reject) => {
  314. app.socket.on('message', onSocketMessage);
  315. async function onSocketMessage(data){
  316. try {
  317. let this_data = JSON.parse(data.toString());
  318. if(this_data.msg_type === message_type){
  319. app.socket.off('message', onSocketMessage);
  320. callback(this_data)
  321. resolve()
  322. }
  323. }catch (e){
  324. Log.error(e)
  325. reject(e)
  326. }
  327. }
  328. })
  329. }
  330. // 处理 MCU 错误数据
  331. this.handleMcuErrorData = function(data, win) {
  332. const logEntry = {
  333. message: data.data?.message || '',
  334. current_time: data.data?.current_time || new Date().toLocaleString('zh-CN', { hour12: false }).replace(/\//g, '-'),
  335. raw: data // 保留原始数据
  336. };
  337. // 添加到日志数组(限制最大条数)
  338. mcuErrorLogs.unshift(logEntry);
  339. if (mcuErrorLogs.length > mcuErrorLogConfig.maxLogs) {
  340. mcuErrorLogs.pop();
  341. }
  342. // 未读数 +1
  343. mcuErrorUnreadCount++;
  344. // 发送未读状态变更通知
  345. win.webContents.send('controller.mcu.errorLogUnread', { count: mcuErrorUnreadCount });
  346. // 同时将原始消息转发给前端(保持原有功能)
  347. win.webContents.send('controller.socket.message_print_mcu_error_data', data);
  348. };
  349. // 获取 MCU 错误日志列表
  350. this.getMcuErrorLogs = function() {
  351. return [...mcuErrorLogs];
  352. };
  353. // 获取未读数
  354. this.getMcuErrorUnreadCount = function() {
  355. return mcuErrorUnreadCount;
  356. };
  357. // 清除未读状态
  358. this.clearMcuErrorUnread = function() {
  359. mcuErrorUnreadCount = 0;
  360. return true;
  361. };
  362. // 设置最大日志条数(供外部调用)
  363. this.setMcuErrorLogMax = function(max) {
  364. mcuErrorLogConfig.maxLogs = max;
  365. // 如果当前日志超过限制,裁剪
  366. while (mcuErrorLogs.length > mcuErrorLogConfig.maxLogs) {
  367. mcuErrorLogs.pop();
  368. }
  369. return true;
  370. };
  371. return this;
  372. }
  373. module.exports = pySocket;