-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathreader.swift
More file actions
190 lines (157 loc) · 5.47 KB
/
reader.swift
File metadata and controls
190 lines (157 loc) · 5.47 KB
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
//
// reader.swift
// Kit
//
// Created by Serhiy Mytrovtsiy on 10/04/2020.
// Using Swift 5.0.
// Running on macOS 10.15.
//
// Copyright © 2020 Serhiy Mytrovtsiy. All rights reserved.
//
import Cocoa
public protocol Reader_p {
var optional: Bool { get }
var popup: Bool { get }
func setup()
func read()
func terminate()
func start()
func pause()
func stop()
func replay()
func lock()
func unlock()
func initStoreValues(title: String)
func setInterval(_ value: Int)
}
public protocol ReaderInternal_p {
associatedtype T
var value: T? { get }
func read()
}
open class Reader<T: Codable>: NSObject, ReaderInternal_p {
public var log: NextLog {
NextLog.shared.copy(category: "\(String(describing: self))")
}
private let valueQueue = DispatchQueue(label: "eu.exelban.readerActiveQueue")
private var _value: T?
public var value: T? {
get { self.valueQueue.sync { self._value } }
set { self.valueQueue.sync { self._value = newValue } }
}
public var name: String {
String(NSStringFromClass(type(of: self)).split(separator: ".").last ?? "unknown")
}
public var interval: Double? = nil
public var defaultInterval: Int = 1
public var optional: Bool = false
public var popup: Bool = false
public var callbackHandler: (T?) -> Void
private let module: ModuleType
private var history: Bool
private var repeatTask: Repeater?
private var locked: Bool = true
private var initlizalized: Bool = false
private let activeQueue = DispatchQueue(label: "eu.exelban.readerActiveQueue")
private var _active: Bool = false
public var active: Bool {
get { self.activeQueue.sync { self._active } }
set { self.activeQueue.sync { self._active = newValue } }
}
private var lastDBWrite: Date? = nil
public init(_ module: ModuleType, popup: Bool = false, history: Bool = false, callback: @escaping (T?) -> Void = {_ in }) {
self.popup = popup
self.module = module
self.history = history
self.callbackHandler = callback
super.init()
DB.shared.setup(T.self, "\(module.stringValue)@\(self.name)")
if let lastValue = DB.shared.findOne(T.self, key: "\(module.stringValue)@\(self.name)") {
self.value = lastValue
callback(lastValue)
}
self.setup()
debug("Successfully initialize reader", log: self.log)
}
deinit {
DB.shared.insert(key: "\(self.module.stringValue)@\(self.name)", value: self.value, ts: self.history)
}
public func initStoreValues(title: String) {
guard self.interval == nil else { return }
let updateInterval = Store.shared.int(key: "\(title)_updateInterval", defaultValue: self.defaultInterval)
self.interval = Double(updateInterval)
}
public func callback(_ value: T?) {
let moduleKey = "\(self.module.stringValue)@\(self.name)"
self.value = value
if let value {
self.callbackHandler(value)
Remote.shared.send(key: moduleKey, value: value)
if let ts = self.lastDBWrite, let interval = self.interval, Date().timeIntervalSince(ts) > interval * 10 {
DB.shared.insert(key: moduleKey, value: value, ts: self.history)
self.lastDBWrite = Date()
} else if self.lastDBWrite == nil {
DB.shared.insert(key: moduleKey, value: value, ts: self.history)
self.lastDBWrite = Date()
}
}
}
open func read() {}
open func setup() {}
open func terminate() {}
open func start() {
if self.popup && self.locked {
DispatchQueue.global(qos: .background).async {
self.read()
}
return
}
if let interval = self.interval, self.repeatTask == nil {
if !self.popup && !self.optional {
debug("Set up update interval: \(Int(interval)) sec", log: self.log)
}
self.repeatTask = Repeater.init(seconds: Int(interval)) { [weak self] in
self?.read()
}
}
let shouldPrimeRead = !self.initlizalized || (self.popup && !self.active)
self.active = true
if shouldPrimeRead {
DispatchQueue.global(qos: .background).async {
self.read()
}
self.initlizalized = true
}
self.repeatTask?.start()
}
open func pause() {
self.repeatTask?.pause()
self.active = false
}
open func stop() {
self.repeatTask?.pause()
self.repeatTask = nil
self.active = false
self.initlizalized = false
}
public func setInterval(_ value: Int) {
debug("Set update interval: \(Int(value)) sec", log: self.log)
self.interval = Double(value)
self.repeatTask?.reset(seconds: value, restart: true)
}
public func replay() {
guard self.active, let value = self.value else { return }
self.callbackHandler(value)
}
public func save(_ value: T) {
DB.shared.insert(key: "\(self.module.stringValue)@\(self.name)", value: value, ts: self.history, force: true)
}
}
extension Reader: Reader_p {
public func lock() {
self.locked = true
}
public func unlock() {
self.locked = false
}
}