@@ -11,53 +11,54 @@ import Foundation
1111public class AffectiveCloudClient {
1212 public weak var affectiveCloudDelegate : AffectiveCloudResponseDelegate ! {
1313 didSet {
14- self . cloudService. delegate = affectiveCloudDelegate
14+ self . cloudService? . delegate = affectiveCloudDelegate
1515 }
1616 }
1717
18- private let cloudService : AffectiveCloudServices
18+ private var cloudService : AffectiveCloudServices ?
1919 public init ( websocketURL: URL , appKey: String , appSecret: String , userID: String ) {
2020 self . cloudService = AffectiveCloudServices ( wssURL: websocketURL)
21- self . cloudService. client = self
22- self . cloudService. appKey = appKey
23- self . cloudService. userID = userID
24- self . cloudService. appSecret = appSecret
21+ self . cloudService? . client = self
22+ self . cloudService? . appKey = appKey
23+ self . cloudService? . userID = userID
24+ self . cloudService? . appSecret = appSecret
2525 self . websocketConnect ( )
2626 }
2727
2828 public init ( websocketURLString: String , appKey: String , appSecret: String , userID: String ) {
2929 self . cloudService = AffectiveCloudServices ( ws: websocketURLString)
30- self . cloudService. client = self
31- self . cloudService. appKey = appKey
32- self . cloudService. userID = userID
33- self . cloudService. appSecret = appSecret
30+ self . cloudService? . client = self
31+ self . cloudService? . appKey = appKey
32+ self . cloudService? . userID = userID
33+ self . cloudService? . appSecret = appSecret
3434 self . websocketConnect ( )
3535 }
3636
3737 //MARK: - websocket
3838 public func websocketConnect( ) {
39- self . cloudService. webSocketConnect ( )
39+ self . cloudService? . webSocketConnect ( )
4040 }
4141
4242 public func websocketDisconnect( ) {
43- self . cloudService. webSocketDisConnect ( )
43+ self . cloudService? . webSocketDisConnect ( )
4444 }
4545
4646 public func isWebsocketConnected( ) -> Bool {
47- if self . cloudService. state == . connected {
47+ if self . cloudService? . state == . connected {
4848 return true
4949 } else {
5050 return false
5151 }
5252 }
5353
5454 public func close( ) {
55- if let services = cloudService. affectiveService {
55+ if let services = cloudService? . affectiveService {
5656 finishAffectiveDataServices ( services: services)
5757
5858 }
5959 closeSession ( )
6060 websocketDisconnect ( )
61+ self . cloudService = nil
6162 }
6263
6364 //MARK: - session
@@ -69,27 +70,27 @@ public class AffectiveCloudClient {
6970 /// - Parameter userName: your username from Cloud Service platform
7071 /// - Parameter userID: the unique ID of your app user
7172 public func createAndAuthenticateSession( ) {
72- self . cloudService. sessionCreate ( )
73+ self . cloudService? . sessionCreate ( )
7374 }
7475
7576 /// close cloud service
7677 /// firstly close biodata and affective services, secondly close session finally close cloud service.
7778 public func closeSession( ) {
78- self . cloudService. sessionClose ( )
79+ self . cloudService? . sessionClose ( )
7980 }
8081
8182 /// restore cloud service session
8283 public func restoreSession( ) {
83- self . cloudService. sessionRestore ( )
84+ self . cloudService? . sessionRestore ( )
8485 }
8586
8687 //MARK: - biodata
8788 /// start biodata services with parameter
8889 /// - Parameter services: biodata services. ps: .eeg and .hr
8990 /// - Parameter tolerance: 0-4
9091 public func initBiodataServices( services: BiodataTypeOptions , _ tolerance: [ String : Any ] ? = nil ) {
91- self . cloudService. bioService = services
92- self . cloudService. bioTolerance = tolerance
92+ self . cloudService? . bioService = services
93+ self . cloudService? . bioTolerance = tolerance
9394 //self.cloudService.biodataInitial(options: services, tolerance: tolerance)
9495 }
9596
@@ -104,7 +105,9 @@ public class AffectiveCloudClient {
104105 /// Affective services: .attention、.relaxtion and .pleasure
105106 /// - Parameter data: the brain raw data from the hardware
106107 public func appendBiodata( eegData: Data ) {
107- guard self . cloudService. socket. isConnected else {
108+ guard let _ = self . cloudService else { return }
109+
110+ guard self . cloudService!. socket. isConnected else {
108111 _eegBuffer. removeAll ( )
109112 return
110113 }
@@ -121,7 +124,7 @@ public class AffectiveCloudClient {
121124
122125 for element in results {
123126 let data_int = element. map { Int ( $0) }
124- self . cloudService. biodataUpload ( options: . EEG, eegData: data_int)
127+ self . cloudService? . biodataUpload ( options: . EEG, eegData: data_int)
125128 if _eegBuffer. count >= _eegBufferSize {
126129 _eegBuffer. removeFirst ( _eegBufferSize)
127130 }
@@ -138,7 +141,9 @@ public class AffectiveCloudClient {
138141 /// Affective services: .arousal 、.pressure
139142 /// - Parameter data: the heart data from the hardware
140143 public func appendBiodata( hrData: Data ) {
141- guard self . cloudService. socket. isConnected else {
144+ guard let _ = self . cloudService else { return }
145+
146+ guard self . cloudService!. socket. isConnected else {
142147 _hrBuffer. removeAll ( )
143148 return
144149 }
@@ -156,7 +161,7 @@ public class AffectiveCloudClient {
156161 for element in results {
157162 let data_int = element. map { Int ( $0) }
158163 DLog ( " upload hr data " )
159- self . cloudService. biodataUpload ( options: . HeartRate, hrData: data_int)
164+ self . cloudService? . biodataUpload ( options: . HeartRate, hrData: data_int)
160165 if _hrBuffer. count >= _hrBufferSize {
161166 _hrBuffer. removeFirst ( _hrBufferSize)
162167 }
@@ -169,21 +174,21 @@ public class AffectiveCloudClient {
169174 /// - Parameter services: biodata service . reference: `BiodataSubscribeOptions`
170175 public func subscribeBiodataServices( services: BiodataParameterOptions ) {
171176 //self.cloudService.biodataSubscribe(parameters: services)
172- self . cloudService. bioSubscription = services
177+ self . cloudService? . bioSubscription = services
173178 }
174179
175180 /// unsubscribe the specificed service
176181 /// cloud service will stop response the analyzed data in the service.
177182 /// - Parameter services: biodata service . reference: `BiodataSubscribeOptions`
178183 public func unsubscribeBiodataServices( services: BiodataParameterOptions ) {
179- self . cloudService. biodataUnSubscribe ( parameters: services)
184+ self . cloudService? . biodataUnSubscribe ( parameters: services)
180185 }
181186
182187 /// generate the report according your service
183188 /// you will get the report data in `biodataReport(response: CSResponseJSONModel)` in CSResponseDelegate
184189 /// - Parameter services: biodata service . reference: `BiodataSubscribeOptions`
185190 public func getBiodataReport( services: BiodataTypeOptions ) {
186- self . cloudService. biodataReport ( options: services)
191+ self . cloudService? . biodataReport ( options: services)
187192 }
188193
189194 //MARK: - Emotion
@@ -192,34 +197,34 @@ public class AffectiveCloudClient {
192197 /// - Parameter services: emotion services list: like: .attention .relaxation .pleasure .pressure and .arousal
193198 public func startAffectiveDataServices( services: AffectiveDataServiceOptions ) {
194199 //self.cloudService.emotionStart(services: services)
195- self . cloudService. affectiveService = services
200+ self . cloudService? . affectiveService = services
196201 }
197202
198203 /// generate the report according your service
199204 /// you will get the report data in `affectiveReport(response: CSResponseJSONModel)` in CSResponseDelegate
200205 /// - Parameter services: emotion serivce
201206 public func getAffectiveDataReport( services: AffectiveDataServiceOptions ) {
202- self . cloudService. emotionReport ( services: services)
207+ self . cloudService? . emotionReport ( services: services)
203208 }
204209
205210 /// By subscribing the specificed service
206211 /// you will get the analyzed data in `affectiveSubscribe(response: CSResponseJSONModel)` in CSResponseDelegate
207212 /// - Parameter services: emotion service
208213 public func subscribeAffectiveDataServices( services: AffectiveDataSubscribeOptions ) {
209214 //self.cloudService.emotionSubscribe(services: services)
210- self . cloudService. affectiveSubscription = services
215+ self . cloudService? . affectiveSubscription = services
211216 }
212217
213218 /// unsubscribe the specificed service
214219 /// cloud service will stop response the analyzed data with the specified service.
215220 /// - Parameter services: emotion services
216221 public func unsubscribeAffectiveDataServices( services: AffectiveDataSubscribeOptions ) {
217- self . cloudService. emotionUnSubscribe ( services: services)
222+ self . cloudService? . emotionUnSubscribe ( services: services)
218223 }
219224
220225 /// close emotion service with the sepecified service
221226 /// - Parameter services: emotion services
222227 public func finishAffectiveDataServices( services: AffectiveDataServiceOptions ) {
223- self . cloudService. emotionClose ( services: services)
228+ self . cloudService? . emotionClose ( services: services)
224229 }
225230}
0 commit comments