Skip to content

Instantly share code, notes, and snippets.

@SjoerdPerfors
Created November 13, 2017 12:52
Show Gist options
  • Save SjoerdPerfors/f8308593ed710cfa0bc2565bfe7f1003 to your computer and use it in GitHub Desktop.
Save SjoerdPerfors/f8308593ed710cfa0bc2565bfe7f1003 to your computer and use it in GitHub Desktop.
Example how I did the speedchange testing
//
// CITSManager.swift
// Flitsmeister
//
// Created by Sebastiaan de Weert on 20-03-17.
// Copyright © 2017 Flitsmeister B.V. All rights reserved.
//
import UIKit
import CITS
import CocoaLumberjackSwift
class CITSManager : NSObject, BMLocationServiceDelegate {
static let sharedInstance = CITSManager()
private let cits = BMCITS.instance()
private static let defaultSampleRate : TimeInterval = 5
private let locationService: BMLocationService
#if DEBUG
private var lastLocationRealtime_ : CLLocation?
#endif
override init() {
let strategy = BMLocationSamplingStrategy.init(samplingInterval: CITSManager.defaultSampleRate)
locationService = BMLocationService.init(locationSamplingStrategy: strategy)
super.init()
}
func start(WithSessionId sessionId : String) {
self.connectWithVehicleInfo(AndSessionId: sessionId)
self.locationService.delegate = self
self.locationService.startUpdating()
SignaleringController.sharedInstance().setCITSDelegate()
DDLogInfo("[CITSManager] Start")
}
func connectWithVehicleInfo(AndSessionId sessionId : String) {
var vehicleType: String = BMVehicleTypeCar
switch UserDefaultsManager.ParkingUserVehicleType {
case 0:
vehicleType = BMVehicleTypeCar
case 1:
vehicleType = BMVehicleTypeTruck
case 2:
vehicleType = BMVehicleTypeMoto
default:
break;
}
DDLogInfo("[CITSManager] Start met type: \(vehicleType)")
let vehicleInfo = BMVehicleInfo(
type: vehicleType,
subtype: BMVehicleSybtypeNone,
weight: Double(UserDefaultsManager.ParkingUserVehicleWeight),
length: 0,
height: Double(UserDefaultsManager.ParkingUserVehicleHeight),
width: Double(UserDefaultsManager.ParkingUserVehicleWidth)
)
BMCITS.environment = .production
#if DEBUG
/*
DEV = staging
TEST = acceptance
PROD
*/
BMCITS.environment = .dev
let sessionId = UUID.init().uuidString
#endif
if Template.isCITSmeisterApp(), let mail = AMIController.currentUser()?.getEmailadres() {
cits.connect(withUserID: mail, withVehicleInfo: vehicleInfo, withSessionID: sessionId)
} else {
cits.connect(withSessionID: sessionId, withVehicleInfo: vehicleInfo)
}
}
func stop() {
cits.disconnect()
self.locationService.stopUpdating()
DDLogInfo("[CITSManager] Stop")
}
func updateVehicleInformation() {
var vehicleType: String = BMVehicleTypeCar
switch UserDefaultsManager.ParkingUserVehicleType {
case 0:
vehicleType = BMVehicleTypeCar
case 1:
vehicleType = BMVehicleTypeTruck
case 2:
vehicleType = BMVehicleTypeMoto
default:
break;
}
let vehicleInfo = BMVehicleInfo(
type: vehicleType,
subtype: BMVehicleSybtypeNone,
weight: Double(UserDefaultsManager.ParkingUserVehicleWeight),
length: 0,
height: Double(UserDefaultsManager.ParkingUserVehicleHeight),
width: Double(UserDefaultsManager.ParkingUserVehicleWidth)
)
cits.updateVehicleInfo(vehicleInfo)
}
//MARK: - BMLocationServiceDelegate
func locationService(_ service: BMLocationService!, didReceiveLocationUpdates locations: [CLLocation]!) {
if self.cits.status == .connected {
print("\(Date.init()) Send location to CITS SDK")
#if DEBUG
if var lastLocation = locations.last {
if(lastLocationRealtime_ != nil &&
-lastLocationRealtime_!.timestamp.timeIntervalSinceNow < 60)
{
var speed = lastLocation.speed
if lastLocationRealtime_!.speed == 60 {
speed = 40
} else {
speed = 60
}
if (lastLocationRealtime_!.coordinate.latitude == lastLocation.coordinate.latitude &&
lastLocationRealtime_!.coordinate.longitude == lastLocation.coordinate.longitude) {
let course = lastLocationRealtime_!.course
lastLocation = CLLocation(coordinate: lastLocation.coordinate, altitude: lastLocation.altitude, horizontalAccuracy: 5, verticalAccuracy: 5, course: course, speed: speed, timestamp: lastLocation.timestamp);
} else {
//Dan hebben we geen course maar kunnen we wel berekenen.
let course : Double = lastLocationRealtime_!.headingForDirection(to: lastLocation);
lastLocation = CLLocation(coordinate: lastLocation.coordinate, altitude: lastLocation.altitude, horizontalAccuracy: 5, verticalAccuracy: 5, course: course, speed: speed, timestamp: lastLocation.timestamp);
}
}
lastLocationRealtime_ = lastLocation
var locations = locations!
locations.removeLast()
locations.append(lastLocation)
}
#endif
self.cits.update(locations)
}
}
func locationService(_ service: BMLocationService!, didFailWithError error: Error!) {
print("Got error from BMLocationService \(error)")
}
func locationServiceDidPauseLocationUpdates(_ service: BMLocationService!) {
}
func locationServiceDidResumeLocationUpdates(_ service: BMLocationService!) {
}
func locationService(_ service: BMLocationService!, didChange status: CLAuthorizationStatus) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment