|
Bogdan Timofte
authored
2 months ago
|
1
|
//
|
|
|
2
|
// Measurements.swift
|
|
|
3
|
// USB Meter
|
|
|
4
|
//
|
|
|
5
|
// Created by Bogdan Timofte on 07/05/2020.
|
|
|
6
|
// Copyright © 2020 Bogdan Timofte. All rights reserved.
|
|
|
7
|
//
|
|
|
8
|
|
|
|
9
|
import Foundation
|
|
|
10
|
import CoreGraphics
|
|
|
11
|
|
|
|
12
|
class Measurements : ObservableObject {
|
|
|
13
|
|
|
|
14
|
class Measurement : ObservableObject {
|
|
|
15
|
struct Point : Identifiable , Hashable {
|
|
|
16
|
var id : Int
|
|
|
17
|
var timestamp: Date
|
|
|
18
|
var value: Double
|
|
|
19
|
func point() -> CGPoint {
|
|
|
20
|
return CGPoint(x: timestamp.timeIntervalSince1970, y: value)
|
|
|
21
|
}
|
|
|
22
|
}
|
|
|
23
|
|
|
|
24
|
var points: [Point] = []
|
|
|
25
|
var context = ChartContext()
|
|
|
26
|
|
|
|
27
|
func removeValue(index: Int) {
|
|
|
28
|
points.remove(at: index)
|
|
|
29
|
context.reset()
|
|
|
30
|
for point in points {
|
|
|
31
|
context.include( point: point.point() )
|
|
|
32
|
}
|
|
|
33
|
self.objectWillChange.send()
|
|
|
34
|
}
|
|
|
35
|
|
|
|
36
|
func addPoint(timestamp: Date, value: Double) {
|
|
|
37
|
let newPoint = Measurement.Point(id: points.count, timestamp: timestamp, value: value)
|
|
|
38
|
points.append(newPoint)
|
|
|
39
|
context.include( point: newPoint.point() )
|
|
|
40
|
self.objectWillChange.send()
|
|
|
41
|
}
|
|
|
42
|
|
|
|
43
|
func reset() {
|
|
|
44
|
points.removeAll()
|
|
|
45
|
context.reset()
|
|
|
46
|
self.objectWillChange.send()
|
|
|
47
|
}
|
|
|
48
|
}
|
|
|
49
|
|
|
|
50
|
@Published var power = Measurement()
|
|
|
51
|
@Published var voltage = Measurement()
|
|
|
52
|
@Published var current = Measurement()
|
|
|
53
|
|
|
|
54
|
private var lastPointTimestamp = 0
|
|
|
55
|
|
|
|
56
|
private var itemsInSum: Double = 0
|
|
|
57
|
private var powerSum: Double = 0
|
|
|
58
|
private var voltageSum: Double = 0
|
|
|
59
|
private var currentSum: Double = 0
|
|
|
60
|
|
|
|
61
|
func remove(at idx: Int) {
|
|
|
62
|
power.removeValue(index: idx)
|
|
|
63
|
voltage.removeValue(index: idx)
|
|
|
64
|
current.removeValue(index: idx)
|
|
|
65
|
self.objectWillChange.send()
|
|
|
66
|
}
|
|
|
67
|
|
|
|
68
|
|
|
|
69
|
|
|
|
70
|
func addValues(timestamp: Date, power: Double, voltage: Double, current: Double) {
|
|
|
71
|
let valuesTimestamp = timestamp.timeIntervalSinceReferenceDate.intValue
|
|
|
72
|
if lastPointTimestamp == 0 {
|
|
|
73
|
lastPointTimestamp = valuesTimestamp
|
|
|
74
|
}
|
|
|
75
|
if lastPointTimestamp == valuesTimestamp {
|
|
|
76
|
itemsInSum += 1
|
|
|
77
|
powerSum += voltage
|
|
|
78
|
voltageSum += voltage
|
|
|
79
|
currentSum += current
|
|
|
80
|
}
|
|
|
81
|
else {
|
|
|
82
|
self.power.addPoint( timestamp: timestamp, value: powerSum / itemsInSum )
|
|
|
83
|
self.voltage.addPoint( timestamp: timestamp, value: voltageSum / itemsInSum )
|
|
|
84
|
self.current.addPoint( timestamp: timestamp, value: currentSum / itemsInSum )
|
|
|
85
|
lastPointTimestamp = valuesTimestamp
|
|
|
86
|
itemsInSum = 1
|
|
|
87
|
powerSum = power
|
|
|
88
|
voltageSum = voltage
|
|
|
89
|
currentSum = current
|
|
|
90
|
}
|
|
|
91
|
self.objectWillChange.send()
|
|
|
92
|
}
|
|
|
93
|
}
|