@@ -14,15 +14,26 @@ import Cocoa
14
14
15
15
public class Remote {
16
16
public static let shared = Remote ( )
17
- static public var host = URL ( string: " https ://api.system-stats.com " ) ! // https://api.system-stats.com http://localhost:8008
17
+ static public var host = URL ( string: " http ://localhost:8008 " ) ! // https://api.system-stats.com http://localhost:8008
18
18
19
- public var state : Bool {
20
- get { Store . shared. bool ( key: " remote_state " , defaultValue: false ) }
19
+ public var monitoring : Bool {
20
+ get { Store . shared. bool ( key: " remote_monitoring " , defaultValue: false ) }
21
21
set {
22
- Store . shared. set ( key: " remote_state " , value: newValue)
22
+ Store . shared. set ( key: " remote_monitoring " , value: newValue)
23
23
if newValue {
24
24
self . start ( )
25
- } else {
25
+ } else if !self . control {
26
+ self . stop ( )
27
+ }
28
+ }
29
+ }
30
+ public var control : Bool {
31
+ get { Store . shared. bool ( key: " remote_control " , defaultValue: false ) }
32
+ set {
33
+ Store . shared. set ( key: " remote_control " , value: newValue)
34
+ if newValue {
35
+ self . start ( )
36
+ } else if !self . monitoring {
26
37
self . stop ( )
27
38
}
28
39
}
@@ -31,19 +42,18 @@ public class Remote {
31
42
public var isAuthorized : Bool = false
32
43
public var auth : RemoteAuth = RemoteAuth ( )
33
44
45
+ private let log : NextLog
34
46
private var ws : WebSocketManager = WebSocketManager ( )
35
47
private var wsURL : URL ?
36
48
private var isConnecting = false
37
49
38
50
public init ( ) {
51
+ self . log = NextLog . shared. copy ( category: " Remote " )
39
52
self . id = UUID ( uuidString: Store . shared. string ( key: " telemetry_id " , defaultValue: UUID ( ) . uuidString) ) ?? UUID ( )
40
53
41
- DispatchQueue . main. asyncAfter ( deadline: . now( ) + 1 ) {
42
- if self . state {
43
- self . start ( )
44
- } else {
45
- self . stop ( )
46
- }
54
+ if self . auth. hasCredentials ( ) {
55
+ info ( " Found auth credentials for remote monitoring, starting Remote... " , log: self . log)
56
+ self . start ( )
47
57
}
48
58
49
59
NotificationCenter . default. addObserver ( self , selector: #selector( self . successLogin) , name: . remoteLoginSuccess, object: nil )
@@ -56,40 +66,41 @@ public class Remote {
56
66
57
67
public func login( ) {
58
68
self . auth. login { url in
59
- guard let url else { return }
69
+ guard let url else {
70
+ error ( " Empty url when try to login " , log: self . log)
71
+ return
72
+ }
73
+ debug ( " Open \( url) to login to Stats Remote " , log: self . log)
60
74
NSWorkspace . shared. open ( url)
61
75
}
62
76
}
63
77
64
78
public func logout( ) {
65
79
self . auth. logout ( )
66
80
self . isAuthorized = false
67
- self . state = false
68
81
self . ws. disconnect ( )
69
- NotificationCenter . default. post ( name: . remoteState, object: nil , userInfo: [ " auth " : self . isAuthorized, " state " : self . state] )
82
+ debug ( " Logout successfully from Stats Remote " , log: self . log)
83
+ NotificationCenter . default. post ( name: . remoteState, object: nil , userInfo: [ " auth " : self . isAuthorized] )
70
84
}
71
85
72
86
public func send( key: String , value: Codable ) {
73
- guard self . state && self . isAuthorized,
74
- let blobData = try ? JSONEncoder ( ) . encode ( value) else { return }
87
+ guard self . monitoring && self . isAuthorized, let blobData = try ? JSONEncoder ( ) . encode ( value) else { return }
75
88
self . ws. send ( key: key, data: blobData)
76
89
}
77
90
78
91
@objc private func successLogin( ) {
79
92
self . isAuthorized = true
80
- NotificationCenter . default. post ( name: . remoteState, object: nil , userInfo: [ " auth " : self . isAuthorized, " state " : self . state] )
81
-
82
- if self . state {
83
- self . ws. connect ( )
84
- }
93
+ NotificationCenter . default. post ( name: . remoteState, object: nil , userInfo: [ " auth " : self . isAuthorized] )
94
+ self . ws. connect ( )
95
+ debug ( " Login successfully on Stats Remote " , log: self . log)
85
96
}
86
97
87
98
public func start( ) {
88
99
self . auth. isAuthorized { [ weak self] status in
89
100
guard let self else { return }
90
101
91
102
self . isAuthorized = status
92
- NotificationCenter . default. post ( name: . remoteState, object: nil , userInfo: [ " auth " : self . isAuthorized, " state " : self . state ] )
103
+ NotificationCenter . default. post ( name: . remoteState, object: nil , userInfo: [ " auth " : self . isAuthorized] )
93
104
94
105
if status {
95
106
self . ws. connect ( )
@@ -99,7 +110,7 @@ public class Remote {
99
110
100
111
private func stop( ) {
101
112
self . ws. disconnect ( )
102
- NotificationCenter . default. post ( name: . remoteState, object: nil , userInfo: [ " auth " : self . isAuthorized, " state " : self . state ] )
113
+ NotificationCenter . default. post ( name: . remoteState, object: nil , userInfo: [ " auth " : self . isAuthorized] )
103
114
}
104
115
}
105
116
@@ -128,8 +139,15 @@ public class RemoteAuth {
128
139
}
129
140
130
141
public func isAuthorized( completion: @escaping ( Bool ) -> Void ) {
142
+ if !self . hasCredentials ( ) {
143
+ completion ( false )
144
+ return
145
+ }
131
146
self . validate ( completion)
132
147
}
148
+ public func hasCredentials( ) -> Bool {
149
+ return !self . accessToken. isEmpty && !self . refreshToken. isEmpty
150
+ }
133
151
134
152
public func login( completion: @escaping ( URL ? ) -> Void ) {
135
153
self . registerDevice { device in
@@ -335,14 +353,17 @@ class WebSocketManager: NSObject {
335
353
private let reconnectDelay : TimeInterval = 3.0
336
354
private var pingTimer : Timer ?
337
355
private var reachability : Reachability = Reachability ( start: true )
356
+ private let log : NextLog
338
357
339
358
override init ( ) {
359
+ self . log = NextLog . shared. copy ( category: " Remote WS " )
360
+
340
361
super. init ( )
341
362
342
363
self . session = URLSession ( configuration: . default, delegate: self , delegateQueue: . main)
343
364
344
365
self . reachability. reachable = {
345
- if Remote . shared. state {
366
+ if Remote . shared. isAuthorized {
346
367
self . connect ( )
347
368
}
348
369
}
@@ -367,19 +388,25 @@ class WebSocketManager: NSObject {
367
388
self . webSocket? . resume ( )
368
389
self . receiveMessage ( )
369
390
self . isDisconnected = false
391
+ debug ( " connected successfully " , log: self . log)
370
392
}
371
393
}
372
394
373
395
public func disconnect( ) {
396
+ if self . webSocket == nil && !self . isConnected { return }
374
397
self . isDisconnected = true
375
398
self . webSocket? . cancel ( with: . normalClosure, reason: nil )
376
399
self . webSocket = nil
377
400
self . isConnected = false
401
+ debug ( " disconnected gracefully " , log: self . log)
378
402
}
379
403
380
404
private func reconnect( ) {
381
405
guard !self . isDisconnected else { return }
382
406
DispatchQueue . main. asyncAfter ( deadline: . now( ) + self . reconnectDelay) { [ weak self] in
407
+ if let log = self ? . log {
408
+ debug ( " trying to reconnect after some interruption " , log: log)
409
+ }
383
410
self ? . connect ( )
384
411
}
385
412
}
0 commit comments