/
SessionManager.swift
131 lines (122 loc) · 4.26 KB
/
SessionManager.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
//
// Copyright (c) 2015 Harry Cheung
//
import Foundation
import UIKit
// Singleton
private let _SessionManagerSharedInstance = SessionManager()
final class SessionManager {
var session: Session? = nil
var currentLap: Lap?
var bestLap: Lap?
var lastPoint: Point?
var bestIndex: Int = 0
var nextGate: Gate?
var gateIter: Int = 0
var splits: [Double] = []
var splitGaps: [Double] = []
var splitStartTime: Double = 0
var splitNumber: Int = 0
var track: Track?
var currentSplit: Int = 0
var lapNumber: Int = 0
var gap: Double = 0
class var instance: SessionManager { return _SessionManagerSharedInstance }
func startSession(track: Track) {
if session == nil {
self.track = track
session = Session(track: track)
currentLap = Lap(
session: session!,
track: track,
startTime: session!.startTime,
lapNumber: 0)
session!.laps.append(currentLap!)
splits = [Double](count: track.numSplits(), repeatedValue: 0)
splitGaps = [Double](count: track.numSplits(), repeatedValue: -1)
splitStartTime = session!.startTime
splitNumber = 0
currentSplit = 0
lapNumber = 0
gap = 0
bestIndex = 0
nextGate = track.start
}
}
func endSession() {
session = nil
currentLap = nil
bestLap = nil
}
func gps(#latitude: Double, longitude: Double, speed: Double, bearing: Double,
horizontalAccuracy: Double, verticalAccuracy: Double, timestamp: Double) {
var point = Point(latitude: latitude, longitude: longitude, speed: speed,
bearing: bearing, horizontalAccuracy: horizontalAccuracy,
verticalAccuracy: verticalAccuracy, timestamp: timestamp)
if lastPoint != nil {
var cross = nextGate!.crossed(start: lastPoint!, destination: point)
if cross != nil {
currentLap!.add(cross!)
currentLap!.splits[currentSplit] = cross!.splitTime
switch nextGate!.type {
case .START_FINISH, .FINISH:
if currentLap!.points[0].generated {
currentLap!.valid = true
if bestLap == nil || currentLap!.duration < bestLap!.duration {
bestLap = currentLap
}
}
fallthrough
case .START:
lapNumber += 1
currentLap = Lap(session: session!, track: track!, startTime: cross!.timestamp, lapNumber: lapNumber)
lastPoint = Point(latitude: cross!.latitudeDegrees(),
longitude: cross!.longitudeDegrees(),
speed: cross!.speed,
bearing: cross!.bearing,
horizontalAccuracy: cross!.hAccuracy,
verticalAccuracy: cross!.vAccuracy,
timestamp: cross!.timestamp)
lastPoint!.lapDistance = 0
lastPoint!.lapTime = 0
lastPoint!.generated = true
currentLap!.add(lastPoint!);
session!.laps.append(currentLap!)
gap = 0
for (index, gap) in enumerate(splitGaps) {
splitGaps[index] = 0
}
bestIndex = 0
currentSplit = 0
case .SPLIT:
if bestLap != nil {
splitGaps[currentSplit] = currentLap!.splits[currentSplit] - bestLap!.splits[currentSplit]
}
currentSplit++
}
splitStartTime = cross!.timestamp
nextGate = track!.gates[currentSplit]
}
if bestLap != nil && bestIndex < bestLap!.points.count {
while bestIndex < bestLap!.points.count {
let refPoint = bestLap!.points[bestIndex]
if refPoint.lapDistance > currentLap!.distance {
let lastRefPoint = bestLap!.points[bestIndex - 1]
let distanceToLastRefPoint = currentLap!.distance - lastRefPoint.lapDistance
if (distanceToLastRefPoint > 0) {
let sinceLastRefPoint = distanceToLastRefPoint / point.speed
gap = point.lapTime - sinceLastRefPoint - lastRefPoint.lapTime
splitGaps[splitNumber] = point.splitTime - sinceLastRefPoint - lastRefPoint.splitTime
}
break
}
bestIndex++
}
}
point.lapDistance = lastPoint!.lapDistance + lastPoint!.distanceTo(point)
point.setLapTime(currentLap!.startTime, splitStartTime: splitStartTime)
}
currentLap!.add(point)
lastPoint = point
}
}