@@ -6,6 +6,115 @@ const OpenAI = require('openai');
66const https = require ( 'https' ) ;
77const ffmpeg = require ( 'fluent-ffmpeg' ) ;
88
9+ // Internet connectivity checking
10+ let isOnline = true ;
11+ let connectivityCheckInterval ;
12+ let connectivityRetryInterval ;
13+ const CONNECTIVITY_CHECK_INTERVAL = 10000 ; // 10 seconds
14+ const CONNECTIVITY_RETRY_INTERVAL = 30000 ; // 30 seconds when offline
15+ const CONNECTIVITY_TIMEOUT = 5000 ; // 5 seconds timeout
16+
17+ // Check internet connectivity by trying to reach Google
18+ function checkInternetConnectivity ( ) {
19+ return new Promise ( ( resolve ) => {
20+ const request = https . request ( {
21+ hostname : 'google.com' ,
22+ port : 443 ,
23+ path : '/' ,
24+ method : 'HEAD' ,
25+ timeout : CONNECTIVITY_TIMEOUT
26+ } , ( res ) => {
27+ resolve ( true ) ;
28+ } ) ;
29+
30+ request . on ( 'error' , ( ) => {
31+ resolve ( false ) ;
32+ } ) ;
33+
34+ request . on ( 'timeout' , ( ) => {
35+ request . destroy ( ) ;
36+ resolve ( false ) ;
37+ } ) ;
38+
39+ request . setTimeout ( CONNECTIVITY_TIMEOUT ) ;
40+ request . end ( ) ;
41+ } ) ;
42+ }
43+
44+ // Handle connectivity state changes
45+ async function handleConnectivityChange ( online ) {
46+ const wasOnline = isOnline ;
47+ isOnline = online ;
48+
49+ logger . info ( `Connectivity status changed: ${ online ? 'online' : 'offline' } ` ) ;
50+
51+ if ( mainWindow ) {
52+ mainWindow . webContents . send ( 'connectivity-status' , { isOnline : online } ) ;
53+ }
54+
55+ // If we just went offline, start more frequent retry checking
56+ if ( wasOnline && ! online ) {
57+ logger . info ( 'Device went offline, starting retry interval' ) ;
58+ startConnectivityRetryInterval ( ) ;
59+ }
60+ // If we just came back online, stop retry checking and resume normal checking
61+ else if ( ! wasOnline && online ) {
62+ logger . info ( 'Device came back online, stopping retry interval' ) ;
63+ stopConnectivityRetryInterval ( ) ;
64+ }
65+ }
66+
67+ // Start normal connectivity checking (every 10 seconds)
68+ function startConnectivityChecking ( ) {
69+ logger . info ( 'Starting connectivity checking' ) ;
70+
71+ // Initial check
72+ checkInternetConnectivity ( ) . then ( handleConnectivityChange ) ;
73+
74+ // Set up regular checking
75+ connectivityCheckInterval = setInterval ( async ( ) => {
76+ if ( isOnline ) { // Only do regular checks when we think we're online
77+ const online = await checkInternetConnectivity ( ) ;
78+ if ( online !== isOnline ) {
79+ await handleConnectivityChange ( online ) ;
80+ }
81+ }
82+ } , CONNECTIVITY_CHECK_INTERVAL ) ;
83+ }
84+
85+ // Start aggressive retry checking (every 30 seconds when offline)
86+ function startConnectivityRetryInterval ( ) {
87+ if ( connectivityRetryInterval ) {
88+ clearInterval ( connectivityRetryInterval ) ;
89+ }
90+
91+ connectivityRetryInterval = setInterval ( async ( ) => {
92+ logger . debug ( 'Retry connectivity check (offline state)' ) ;
93+ const online = await checkInternetConnectivity ( ) ;
94+ if ( online !== isOnline ) {
95+ await handleConnectivityChange ( online ) ;
96+ }
97+ } , CONNECTIVITY_RETRY_INTERVAL ) ;
98+ }
99+
100+ // Stop retry checking
101+ function stopConnectivityRetryInterval ( ) {
102+ if ( connectivityRetryInterval ) {
103+ clearInterval ( connectivityRetryInterval ) ;
104+ connectivityRetryInterval = null ;
105+ }
106+ }
107+
108+ // Stop all connectivity checking
109+ function stopConnectivityChecking ( ) {
110+ logger . info ( 'Stopping connectivity checking' ) ;
111+ if ( connectivityCheckInterval ) {
112+ clearInterval ( connectivityCheckInterval ) ;
113+ connectivityCheckInterval = null ;
114+ }
115+ stopConnectivityRetryInterval ( ) ;
116+ }
117+
9118// Debug logging setup
10119const debugLogsDir = path . join ( app . getPath ( 'userData' ) , 'logs' ) ;
11120const debugLogFile = path . join ( debugLogsDir , 'debug.log' ) ;
@@ -107,7 +216,7 @@ let openai;
107216let updateCheckInterval ;
108217
109218// Current app version
110- const CURRENT_VERSION = '1.5.1 ' ;
219+ const CURRENT_VERSION = '1.5.2 ' ;
111220const UPDATE_CHECK_URL = 'https://raw.githubusercontent.com/jay-bman725/AutoCaption/refs/heads/main/version' ;
112221const CHANGELOG_URL = 'https://raw.githubusercontent.com/jay-bman725/AutoCaption/refs/heads/main/changelog.md' ;
113222
@@ -435,6 +544,9 @@ async function createWindow() {
435544 // Start automatic update checking
436545 await startAutoUpdateCheck ( ) ;
437546
547+ // Start connectivity checking
548+ startConnectivityChecking ( ) ;
549+
438550 logger . info ( 'Showing main window' ) ;
439551 mainWindow . show ( ) ;
440552 } ) ;
@@ -515,6 +627,7 @@ app.whenReady().then(async () => {
515627app . on ( 'window-all-closed' , ( ) => {
516628 logger . info ( 'All windows closed' ) ;
517629 stopAutoUpdateCheck ( ) ;
630+ stopConnectivityChecking ( ) ;
518631 if ( process . platform !== 'darwin' ) {
519632 logger . info ( 'Quitting application' ) ;
520633 app . quit ( ) ;
@@ -829,6 +942,30 @@ ipcMain.handle('get-platform', async () => {
829942 }
830943} ) ;
831944
945+ // Connectivity management
946+ ipcMain . handle ( 'retry-connectivity-check' , async ( ) => {
947+ try {
948+ logger . info ( 'Manual connectivity retry requested' ) ;
949+ const online = await checkInternetConnectivity ( ) ;
950+ if ( online !== isOnline ) {
951+ await handleConnectivityChange ( online ) ;
952+ }
953+ return { success : true , isOnline : online } ;
954+ } catch ( error ) {
955+ logger . error ( 'Error during manual connectivity retry:' , error ) ;
956+ return { success : false , error : error . message } ;
957+ }
958+ } ) ;
959+
960+ ipcMain . handle ( 'get-connectivity-status' , async ( ) => {
961+ try {
962+ return { success : true , isOnline } ;
963+ } catch ( error ) {
964+ logger . error ( 'Error getting connectivity status:' , error ) ;
965+ return { success : false , error : error . message } ;
966+ }
967+ } ) ;
968+
832969// Onboarding IPC handlers
833970ipcMain . handle ( 'get-onboarding-status' , async ( ) => {
834971 try {
0 commit comments