socket.js 13 KB

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