forked from theiliad/iota-starter-carsharing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
API.swift
340 lines (305 loc) · 15.9 KB
/
API.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/**
* Copyright 2016 IBM Corp. All Rights Reserved.
*
* Licensed under the IBM License, a copy of which may be obtained at:
*
* http://www14.software.ibm.com/cgi-bin/weblap/lap.pl?li_formnum=L-DDIN-ADRVKF&popup=y&title=IBM%20IoT%20for%20Automotive%20Sample%20Starter%20Apps
*
* You may not use this file except in compliance with the license.
*/
import CoreLocation
import UIKit
import BMSCore
import BMSSecurity
let USER_DEFAULTS_KEY_APP_ROUTE = "appRoute"
let USER_DEFAULTS_KEY_PUSH_APP_GUID = "pushAppGuid"
let USER_DEFAULTS_KEY_PUSH_CLIENT_SECRET = "pushClientSecret"
let USER_DEFAULTS_KEY_MCA_TENANT_ID = "mcaTenantId"
struct API {
static var moveToRootOnError = true
//static let defaultAppURL = "https://iota-starter-server.mybluemix.net"
static let defaultPushAppGUID = ""
static let defaultPushClientSecret = ""
static let defaultMcaTenantId = ""
static var bmRegion = BMSClient.Region.usSouth
static var customRealm = "custauth"
static var connectedAppURL = defaultAppURL
static var connectedPushAppGUID = defaultPushAppGUID
static var connectedPushClientSecret = defaultPushClientSecret
static var connectedMcaTenantId = defaultMcaTenantId
static var carsNearby = "\(connectedAppURL)/user/carsnearby"
static var reservation = "\(connectedAppURL)/user/reservation"
static var reservations = "\(connectedAppURL)/user/activeReservations"
static var carControl = "\(connectedAppURL)/user/carControl"
static var driverStats = "\(connectedAppURL)/user/driverInsights/statistics"
static var trips = "\(connectedAppURL)/user/driverInsights"
static var tripBehavior = "\(connectedAppURL)/user/driverInsights/behaviors"
static var latestTripBehavior = "\(connectedAppURL)/user/driverInsights/behaviors/latest"
static var tripRoutes = "\(connectedAppURL)/user/triproutes"
static var tripAnalysisStatus = "\(connectedAppURL)/user/driverInsights/tripanalysisstatus"
static var credentials = "\(connectedAppURL)/user/device/credentials"
static func setURIs(appURL: String) {
carsNearby = "\(appURL)/user/carsnearby"
reservation = "\(appURL)/user/reservation"
reservations = "\(appURL)/user/activeReservations"
carControl = "\(appURL)/user/carControl"
driverStats = "\(appURL)/user/driverInsights/statistics"
trips = "\(appURL)/user/driverInsights"
tripBehavior = "\(appURL)/user/driverInsights/behaviors"
latestTripBehavior = "\(appURL)/user/driverInsights/behaviors/latest"
tripRoutes = "\(appURL)/user/triproutes"
tripAnalysisStatus = "\(appURL)/user/driverInsights/tripanalysisstatus"
credentials = "\(appURL)/user/device/credentials"
}
static func setDefaultServer () {
connectedAppURL = defaultAppURL
connectedPushAppGUID = defaultPushAppGUID
connectedPushClientSecret = defaultPushClientSecret
connectedMcaTenantId = defaultMcaTenantId
moveToRootOnError = true
setURIs(connectedAppURL)
}
static func delegateCustomAuthHandler() -> Void {
let delegate = CustomAuthDelegate()
let mcaAuthManager = MCAAuthorizationManager.sharedInstance
do {
try mcaAuthManager.registerAuthenticationDelegate(delegate, realm: customRealm)
print("CustomeAuthDelegate was registered")
} catch {
print("error with register: \(error)")
}
return
}
static func doInitialize() {
let userDefaults = NSUserDefaults.standardUserDefaults()
let appRoute = userDefaults.valueForKey(USER_DEFAULTS_KEY_APP_ROUTE) as? String
let pushAppGUID = userDefaults.valueForKey(USER_DEFAULTS_KEY_PUSH_APP_GUID) as? String
let pushClientSecret = userDefaults.valueForKey(USER_DEFAULTS_KEY_PUSH_CLIENT_SECRET) as? String
let mcaTenantId = userDefaults.valueForKey(USER_DEFAULTS_KEY_MCA_TENANT_ID) as? String
moveToRootOnError = true
if(appRoute != nil){
connectedAppURL = appRoute!
connectedPushAppGUID = pushAppGUID == nil ? "" : pushAppGUID!
connectedPushClientSecret = pushClientSecret == nil ? "" : pushClientSecret!
connectedMcaTenantId = mcaTenantId == nil ? "" : mcaTenantId!
setURIs(connectedAppURL)
}
if connectedMcaTenantId != "" {
print("initialize and set up MCA")
let mcaAuthManager = MCAAuthorizationManager.sharedInstance
mcaAuthManager.initialize(tenantId: connectedMcaTenantId, bluemixRegion: bmRegion)
BMSClient.sharedInstance.authorizationManager = mcaAuthManager
delegateCustomAuthHandler()
// uncomment the next line if make that login is always necessary after restart this application
// MCAAuthorizationManager.sharedInstance.logout(nil)
} else {
print("non-MCA server")
}
}
static func login(requestAfterLogin: NSMutableURLRequest?, callback: ((NSHTTPURLResponse, [NSDictionary]) -> Void)?){
let customResourceURL = BMSClient.sharedInstance.bluemixAppRoute! + "/user/login"
let request = Request(url: customResourceURL, method: HttpMethod.GET)
print("get to /user/login")
let callBack: BMSCompletionHandler = {(response: Response?, error: NSError?) in
if error == nil {
print ("response :: \(response?.responseText), no error")
if let newRequest = requestAfterLogin {
self.doRequest(newRequest, callback: callback)
} else {
print ("error:: \(error.debugDescription)")
}
}
}
request.send(completionHandler: callBack)
}
static func handleError(error: NSError) {
doHandleError("Communication Error", message: "\(error)", moveToRoot: moveToRootOnError)
}
static func handleServerError(data:NSData, response: NSHTTPURLResponse) {
let responseString = String(data: data, encoding: NSUTF8StringEncoding)
let statusCode = response.statusCode
doHandleError("Server Error", message: "Status Code: \(statusCode) - \(responseString!)", moveToRoot: false)
}
static func doHandleError(title:String, message: String, moveToRoot: Bool) {
var vc: UIViewController?
if var topController = UIApplication.sharedApplication().keyWindow?.rootViewController {
while let presentedViewController = topController.presentedViewController {
topController = presentedViewController
}
vc = topController
} else {
let window:UIWindow?? = UIApplication.sharedApplication().delegate?.window
vc = window!!.rootViewController!
}
let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert)
let okAction = UIAlertAction(title: "OK", style: .Cancel) { action -> Void in
alert.removeFromParentViewController()
if(moveToRoot){
UIApplication.sharedApplication().cancelAllLocalNotifications()
// reset view back to Get Started
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateInitialViewController()! as UIViewController
UIApplication.sharedApplication().windows[0].rootViewController = controller
}
}
alert.addAction(okAction)
dispatch_async(dispatch_get_main_queue(), {
vc!.presentViewController(alert, animated: true, completion: nil)
})
}
static func getUUID() -> String {
if let uuid = NSUserDefaults.standardUserDefaults().stringForKey("iota-starter-uuid") {
return uuid
} else {
let value = NSUUID().UUIDString
NSUserDefaults.standardUserDefaults().setValue(value, forKey: "iota-starter-uuid")
return value
}
}
// convert NSMutableURLRequest to BMSCore Request
static private func toBMSRequest(request: NSMutableURLRequest) -> Request {
let bmsRequest = Request(url: request.URL!.absoluteString!, headers: request.allHTTPHeaderFields, queryParameters: request.allHTTPHeaderFields, method: HttpMethod(rawValue: request.HTTPMethod)!)
print("toBMSRequest url: \(request.URL!.absoluteString)")
return bmsRequest
}
static private func toJsonArray(data: NSData) -> [NSMutableDictionary] {
var jsonArray: [NSMutableDictionary] = []
do {
if let tempArray:[NSMutableDictionary] = try NSJSONSerialization.JSONObjectWithData(data, options: [NSJSONReadingOptions.MutableContainers]) as? [NSMutableDictionary] {
jsonArray = tempArray
} else {
if let temp = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as? NSMutableDictionary {
jsonArray.append(temp)
}
}
} catch {
print("data returned wasn't array of json")
/*
do {
if let temp = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary {
jsonArray[0] = temp
}
} catch {
print("data returned wasn't json")
}
*/
}
return jsonArray
}
static func doRequest(request: NSMutableURLRequest, callback: ((NSHTTPURLResponse, [NSDictionary]) -> Void)?) {
print("\(request.HTTPMethod) to \(request.URL!)")
request.setValue(getUUID(), forHTTPHeaderField: "iota-starter-uuid")
print("using UUID: \(getUUID())")
if connectedMcaTenantId != "" {
print("doRequest(BMS)")
let bmsRequest = toBMSRequest(request)
// Convert callback for NSURLSession dataTaskWithRequest(request) to callback for BMSCore sendWithCompletionHandler() or sendData()
let bmsCallback: BMSCompletionHandler = {(response: Response?, error: NSError?) in
if error == nil {
let nsResponse = NSHTTPURLResponse(URL: request.URL!, statusCode: response!.statusCode!, HTTPVersion: "HTTP/?.?", headerFields: response!.headers as! [String : String])!
print("response = \(response!.statusCode!) \(response!.headers)")
print("responseString = \(response!.responseText)")
let jsonArray = toJsonArray(response!.responseData!)
let statusCode = response!.statusCode
print("statusCode was \(statusCode)")
switch statusCode! {
case 401:
fallthrough
case 500..<600:
self.handleServerError(response!.responseData!, response: nsResponse)
break
case 200..<400:
if !checkAPIVersion(nsResponse) {
doHandleError("API Version Error", message: "API version between the server and mobile app is inconsistent. Please upgrade your server or mobile app.", moveToRoot: true)
return;
}
fallthrough
default:
callback?(nsResponse, jsonArray)
moveToRootOnError = false
}
} else {
print ("error: \(error.debugDescription)")
}
}
if request.HTTPBody == nil {
print("doRequest(BMS) no HTTPBody")
bmsRequest.send(completionHandler: bmsCallback)
} else {
print("doRequest(BMS) HTTPBody \(NSString(data: request.HTTPBody!, encoding: NSUTF8StringEncoding) as? String)")
bmsRequest.send(requestBody: request.HTTPBody!, completionHandler: bmsCallback)
}
} else {
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
guard error == nil && data != nil else {
print("error=\(error!)")
handleError(error!)
return
}
print("response = \(response!)")
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("responseString = \(responseString!)")
let jsonArray = toJsonArray(data!)
let httpStatus = response as? NSHTTPURLResponse
print("statusCode was \(httpStatus!.statusCode)")
let statusCode = httpStatus?.statusCode
switch statusCode! {
case 401:
self.login(request, callback: callback)
break
case 500..<600:
self.handleServerError(data!, response: (response as? NSHTTPURLResponse)!)
break
case 200..<400:
if !checkAPIVersion(response as! NSHTTPURLResponse) {
doHandleError("API Version Error", message: "API version between the server and mobile app is inconsistent. Please upgrade your server or mobile app.", moveToRoot: true)
return;
}
fallthrough
default:
callback?((response as? NSHTTPURLResponse)!, jsonArray)
moveToRootOnError = false
}
}
task.resume()
}
}
static func checkAPIVersion(response:NSHTTPURLResponse)->Bool{
guard let apiVersion:String = response.allHeaderFields["iota-starter-car-sharing-version"] as? String else{
print("Server API 1.0 is not supported")
return false
}
let appVersion = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as! String
let splitedApiVersion = apiVersion.componentsSeparatedByString(".")
let splitedAppVersion = appVersion.componentsSeparatedByString(".")
return splitedApiVersion[0] == splitedAppVersion[0]
}
static func getLocation(lat: Double, lng: Double, label: UILabel) -> Void {
let gc: CLGeocoder = CLGeocoder()
let location = CLLocationCoordinate2D(latitude: lat, longitude: lng)
gc.reverseGeocodeLocation(CLLocation(latitude: location.latitude, longitude: location.longitude), completionHandler: {
(placemarks: [CLPlacemark]?, error: NSError?) -> Void in
dispatch_async(dispatch_get_main_queue(), {
if (placemarks!.count > 0) {
let placemark = placemarks![0]
if placemark.name != nil && placemark.locality != nil {
let attrs = [
NSFontAttributeName : UIFont.systemFontOfSize(12.0),
NSForegroundColorAttributeName : UIColor.blackColor().colorWithAlphaComponent(0.6),
NSUnderlineStyleAttributeName : 1,
]
let text = "\(placemark.name!), \(placemark.locality!)"
let attributedText = NSAttributedString(string: text, attributes: attrs)
label.text = attributedText.string
label.attributedText = attributedText
} else {
// TODO: localize
label.text = "unknown location"
}
label.textColor = UIColor.blackColor().colorWithAlphaComponent(0.6)
label.highlightedTextColor = UIColor.whiteColor()
}
})
})
}
}