socket.js 10 KB

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