@@ -139,11 +139,40 @@ var (
139139 )
140140)
141141
142+ type CloudConnectionState uint8
143+
144+ const (
145+ CloudConnectionStateNotConfigured CloudConnectionState = iota
146+ CloudConnectionStateDisconnected
147+ CloudConnectionStateConnecting
148+ CloudConnectionStateConnected
149+ )
150+
142151var (
152+ cloudConnectionState CloudConnectionState = CloudConnectionStateNotConfigured
153+ cloudConnectionStateLock = & sync.Mutex {}
154+
143155 cloudDisconnectChan chan error
144156 cloudDisconnectLock = & sync.Mutex {}
145157)
146158
159+ func setCloudConnectionState (state CloudConnectionState ) {
160+ cloudConnectionStateLock .Lock ()
161+ defer cloudConnectionStateLock .Unlock ()
162+
163+ if cloudConnectionState == CloudConnectionStateDisconnected &&
164+ (config .CloudToken == "" || config .CloudURL == "" ) {
165+ state = CloudConnectionStateNotConfigured
166+ }
167+
168+ previousState := cloudConnectionState
169+ cloudConnectionState = state
170+
171+ go waitCtrlAndRequestDisplayUpdate (
172+ previousState != state ,
173+ )
174+ }
175+
147176func wsResetMetrics (established bool , sourceType string , source string ) {
148177 metricConnectionLastPingTimestamp .WithLabelValues (sourceType , source ).Set (- 1 )
149178 metricConnectionLastPingDuration .WithLabelValues (sourceType , source ).Set (- 1 )
@@ -285,6 +314,8 @@ func runWebsocketClient() error {
285314 wsURL .Scheme = "wss"
286315 }
287316
317+ setCloudConnectionState (CloudConnectionStateConnecting )
318+
288319 header := http.Header {}
289320 header .Set ("X-Device-ID" , GetDeviceID ())
290321 header .Set ("X-App-Version" , builtAppVersion )
@@ -302,20 +333,26 @@ func runWebsocketClient() error {
302333 c , resp , err := websocket .Dial (dialCtx , wsURL .String (), & websocket.DialOptions {
303334 HTTPHeader : header ,
304335 OnPingReceived : func (ctx context.Context , payload []byte ) bool {
305- scopedLogger .Info ().Bytes ("payload" , payload ).Int ("length" , len (payload )).Msg ("ping frame received" )
336+ scopedLogger .Debug ().Bytes ("payload" , payload ).Int ("length" , len (payload )).Msg ("ping frame received" )
306337
307338 metricConnectionTotalPingReceivedCount .WithLabelValues ("cloud" , wsURL .Host ).Inc ()
308339 metricConnectionLastPingReceivedTimestamp .WithLabelValues ("cloud" , wsURL .Host ).SetToCurrentTime ()
309340
341+ setCloudConnectionState (CloudConnectionStateConnected )
342+
310343 return true
311344 },
312345 })
313346
314- // get the request id from the response header
315- connectionId := resp .Header .Get ("X-Request-ID" )
316- if connectionId == "" {
317- connectionId = resp .Header .Get ("Cf-Ray" )
347+ var connectionId string
348+ if resp != nil {
349+ // get the request id from the response header
350+ connectionId = resp .Header .Get ("X-Request-ID" )
351+ if connectionId == "" {
352+ connectionId = resp .Header .Get ("Cf-Ray" )
353+ }
318354 }
355+
319356 if connectionId == "" {
320357 connectionId = uuid .New ().String ()
321358 scopedLogger .Warn ().
@@ -332,6 +369,8 @@ func runWebsocketClient() error {
332369 if err != nil {
333370 if errors .Is (err , context .Canceled ) {
334371 cloudLogger .Info ().Msg ("websocket connection canceled" )
372+ setCloudConnectionState (CloudConnectionStateDisconnected )
373+
335374 return nil
336375 }
337376 return err
@@ -450,14 +489,14 @@ func RunWebsocketClient() {
450489 }
451490
452491 // If the network is not up, well, we can't connect to the cloud.
453- if ! networkState .Up {
454- cloudLogger .Warn ().Msg ("waiting for network to be up , will retry in 3 seconds" )
492+ if ! networkState .IsOnline () {
493+ cloudLogger .Warn ().Msg ("waiting for network to be online , will retry in 3 seconds" )
455494 time .Sleep (3 * time .Second )
456495 continue
457496 }
458497
459498 // If the system time is not synchronized, the API request will fail anyway because the TLS handshake will fail.
460- if isTimeSyncNeeded () && ! timeSyncSuccess {
499+ if isTimeSyncNeeded () && ! timeSync . IsSyncSuccess () {
461500 cloudLogger .Warn ().Msg ("system time is not synced, will retry in 3 seconds" )
462501 time .Sleep (3 * time .Second )
463502 continue
@@ -520,6 +559,8 @@ func rpcDeregisterDevice() error {
520559 cloudLogger .Info ().Msg ("device deregistered, disconnecting from cloud" )
521560 disconnectCloud (fmt .Errorf ("device deregistered" ))
522561
562+ setCloudConnectionState (CloudConnectionStateNotConfigured )
563+
523564 return nil
524565 }
525566
0 commit comments