-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.swift
397 lines (377 loc) · 14.7 KB
/
test.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import Foundation
import Promise
/// 进度文件全部操作(需要服务端合并, 产生新的记录), 文件不再上传 cos 直接 body 参数给服务器
func progressFileMergeHandle() -> Promise<Void> {
guard self.tcpExceptionState == .None else {
return .value
}
// 重新学习不需要上传进度
guard syncScene != .reset else {
return .value
}
return .value
.done {
self.monitorLogs?.setCardItem(.fail, forKey: .ProgressFromDevice)
}
.then {
self.getProgressFiles()
}
.done {
self.monitorLogs?.setCardItem(.success, forKey: .ProgressFromDevice)
self.monitorLogs?.setCardItem(.fail, forKey: .ProgressNoticeServer)
}
.then { _ -> Promise<[ProgressFileSeverModel]> in
self.base64OrCloudHandle()
}
.filterValues {
$0.base64.isEmpty == false || $0.url.isEmpty == false
}
.then { models -> Guarantee<([ProgressFileSeverModel], Bool)> in
return self.checkForUseAllSyncProgressFile().map { (models, $0) }
}
.then { value -> Promise<Void> in
guard value.0.isEmpty == false else {
return .value
}
if value.1 {
return self.noticeServerProgressUpload(model: value.0.first)
} else {
return self.noticeServerProgressUpload(models: value.0)
}
}
.done {
self.monitorLogs?.setCardItem(.success, forKey: .ProgressNoticeServer)
}
}
/// 检查是否使用打包进度文件上传
func checkForUseAllSyncProgressFile() -> Guarantee<Bool> {
.value(allSyncProgressFile?.isSupported == true)
}
/// 检查是否使用云存储
func checkUploadWithCloud() -> Promise<Bool> {
self.checkForUseAllSyncProgressFile()
.then { value -> Promise<Bool> in
var dataCount = 0
if value {
let data = self.allSyncProgressFile?.data ?? Data()
dataCount = data.count
MMLog.log("批量进度文件大小\(dataCount)", prefix: .studyCard)
} else {
dataCount = self
.processModels
.map { $0.file?.data?.count ?? 0 }
.reduce(0) { $0 + $1 }
MMLog.log("单本进度文件循环总大小\(dataCount)", prefix: .studyCard)
}
return .value(dataCount > 2 * 1024 * 1024)
}
}
/// 从硬件获取所有进度文件
func getProgressFiles() -> Promise<Void> {
self.getProgressFilesNew()
.recover { [weak self] error -> Promise<Void> in
guard let `self` = self else {
throw error
}
let cardError = error as? StudyCardManager.StudyCardError
switch cardError {
case .getRecordCommonNoSupport:
MMLog.log("硬件获取进度文件不支持批量指令", prefix: .studyCard)
return .loopProcess(dataSource: self.processModels, handler: self.getProgressFile(processModel:))
default:
throw error
}
}
}
/// 使用获取所有进度文件指令获取文件, 返回值为是否支持该指令, 不支持该指令则继续原有逻辑
func getProgressFilesNew() -> Promise<Void> {
Promise { [weak self] seal in
self?.seal = seal
guard let using = self?.usingDevice else {
seal.reject(studyCard: .noUsingDevice)
return
}
guard let processModels = self?.processModels,
processModels.count > 1
else {
// 只有一本书在处理的时候, 说明只有一本书, 或者正在下载一本书, 此时不需要批量上传进度
MMLog.log("只有一本书不需要走批量", prefix: .studyCard)
seal.reject(studyCard: .getRecordCommonNoSupport)
return
}
// seal.reject(studyCard: .getRecordCommonNoSupport)
// return;
// 单词卡可能出现把 ping 回包插入 tar 中, 所以暂时停止 ping
self?.stopPingBlue()
// 保证清空上一次的缓存, 二次上传进度的时候, 如果不为 nil, 设备拿不到进度会因为缓存继续上传进度
self?.allSyncProgressFile = nil
MMLog.log("从学习卡批量获取进度文件", prefix: .studyCard)
MMDeviceApi.getAllSyncRecords(using) { result in
self?.startPingBlue()
MMLog.log(
"批量获取学习卡进度文件结果\(result.isSuccess), 是否支持批量\(result.isSupported)",
prefix: .studyCard
)
self?.allSyncProgressFile = result
if result.isSupported {
seal.fulfill()
} else {
seal.reject(studyCard: .getRecordCommonNoSupport)
}
}
}
}
/// 从硬件获取进度文件
func getProgressFile(processModel: ProcessModel) -> Promise<Void> {
Promise { [weak self] seal in
self?.seal = seal
guard let using = self?.usingDevice else {
seal.reject(studyCard: .noUsingDevice)
return
}
let book = processModel.book
// 单词卡可能出现把 ping 回包插入 tar 中, 所以暂时停止 ping
self?.stopPingBlue()
// 保证清空上一次的缓存, 二次上传进度的时候, 如果不为 nil, 设备拿不到进度会因为缓存继续上传进度
processModel.file = nil
MMLog.log("从学习卡获取进度文件 \(book.bookId)", prefix: .studyCard)
MMDeviceApi.getSyncRecord(
using,
bookType: book.bookType,
bookId: book.bookId
) { records, success in
defer {
self?.startPingBlue()
seal.fulfill()
MMLog.log("获取学习卡进度文件结果\(success)", prefix: .studyCard)
}
guard success else {
return
}
var file = UploadFile()
file.isSuccess = true
file.data = records.oriData
processModel.file = file
}
}
}
/// 进度文件使用 base64 或者云文件处理
func base64OrCloudHandle() -> Promise<[ProgressFileSeverModel]> {
self.checkUploadWithCloud()
.then { value -> Promise<[ProgressFileSeverModel]> in
if value {
return self.uploadProgressFileHandle()
} else {
return self.uploadProgressFileBase64Handle()
}
}
}
/// 进度文件处理成 base64
func uploadProgressFileBase64Handle() -> Promise<[ProgressFileSeverModel]> {
self.checkForUseAllSyncProgressFile()
.then { value -> Promise<[ProgressFileSeverModel]> in
if value {
return self.uploadAllSyncProgressFileBase64().map { [$0] }
} else {
return Promise<[ProgressFileSeverModel]>.loopProcess(
dataSource: self.processModels,
handler: self.uploadProgressFileBase64(processModel:)
)
}
}
}
/// 单本进度文件处理成 base64
func uploadProgressFileBase64(processModel: ProcessModel) -> Promise<ProgressFileSeverModel> {
Promise { seal in
guard let using = usingDevice else {
seal.reject(studyCard: .noUsingDevice)
return
}
guard let data = processModel.file?.data else {
seal.fulfill(.init())
return
}
let book = processModel.book
var result = ProgressFileSeverModel()
result.md5 = data.md5String
result.base64 = data.base64EncodedString()
result.bookId = book.bookId
result.bookType = book.bookType
result.deviceType = using.deviceType ?? ""
result.deviceSN = using.snCode ?? ""
MMLog.log("进度文件 Base64 成功", book, prefix: .studyCard)
seal.fulfill(result)
}
}
/// 批量进度文件转 base64
func uploadAllSyncProgressFileBase64() -> Promise<ProgressFileSeverModel> {
Promise { seal in
guard let using = usingDevice else {
seal.reject(studyCard: .noUsingDevice)
return
}
guard let data = allSyncProgressFile?.data else {
seal.fulfill(.init())
return
}
var result = ProgressFileSeverModel()
result.md5 = data.md5String
result.base64 = data.base64EncodedString()
result.deviceType = using.deviceType ?? ""
result.deviceSN = using.snCode ?? ""
MMLog.log("进度文件 Base64 成功", prefix: .studyCard)
seal.fulfill(result)
}
}
/// 上传进度文件到 cos
func uploadProgressFileHandle() -> Promise<[ProgressFileSeverModel]> {
self.checkForUseAllSyncProgressFile()
.then { value -> Promise<[ProgressFileSeverModel]> in
if value {
return self.uploadAllSyncProgressFile().map { [$0] }
} else {
return Promise<[ProgressFileSeverModel]>.loopProcess(
dataSource: self.processModels,
handler: self.uploadProgressFile(processModel:)
)
}
}
}
/// 单本上传进度文件到 cos
func uploadProgressFile(processModel: ProcessModel) -> Promise<ProgressFileSeverModel> {
Promise { seal in
guard let using = usingDevice else {
seal.reject(studyCard: .noUsingDevice)
return
}
guard let data = processModel.file?.data else {
seal.fulfill(.init())
return
}
let book = processModel.book
let userId = UserModel.sharedInstance()?.userId ?? ""
let name = "Book_\(userId)_\(Int(Date().timeIntervalSince1970 * 1000))".md5
MMLog.log("上传进度文件到 cos", prefix: .studyCard)
OSManager
.manager
.upload(
datas: [data],
objectKeys: [name],
module: .wordbook) {
guard let url = OSManager.manager.getCompleteURL(with: name, type: .wordbook) else {
seal.fulfill(.init())
return
}
var result = ProgressFileSeverModel()
result.md5 = data.md5String
result.url = url
result.bookId = book.bookId
result.bookType = book.bookType
result.deviceType = using.deviceType ?? ""
result.deviceSN = using.snCode ?? ""
MMLog.log("上传进度到云成功", prefix: .studyCard)
seal.fulfill(result)
} failure: { error in
MMLog.log("上传进度到云失败: \(error?.localizedDescription ?? "")", prefix: .studyCard)
seal.fulfill(.init())
} progress: { _ in
}
}
}
/// 批量进度文件上传到 cos
func uploadAllSyncProgressFile() -> Promise<ProgressFileSeverModel> {
Promise { seal in
guard let using = usingDevice else {
seal.reject(studyCard: .noUsingDevice)
return
}
guard let data = allSyncProgressFile?.data else {
seal.fulfill(.init())
return
}
let userId = UserModel.sharedInstance()?.userId ?? ""
let name = "Book_\(userId)_\(Int(Date().timeIntervalSince1970 * 1000))".md5
MMLog.log("上传进度文件到 cos", prefix: .studyCard)
OSManager
.manager
.upload(
datas: [data],
objectKeys: [name],
module: .wordbook) {
guard let url = OSManager.manager.getCompleteURL(with: name, type: .wordbook) else {
seal.fulfill(.init())
return
}
var result = ProgressFileSeverModel()
result.md5 = data.md5String
result.url = url
result.deviceType = using.deviceType ?? ""
result.deviceSN = using.snCode ?? ""
MMLog.log("上传进度到云成功", prefix: .studyCard)
seal.fulfill(result)
} failure: { error in
MMLog.log("上传进度到云失败: \(error?.localizedDescription ?? "")", prefix: .studyCard)
seal.fulfill(.init())
} progress: { _ in
}
}
}
/// 通知服务端进度上传
/// - Parameter models: 进度结果模型数组
func noticeServerProgressUpload(models: [ProgressFileSeverModel]) -> Promise<Void> {
Promise { [weak self] seal in
guard models.isEmpty == false else {
seal.fulfill()
return
}
guard let using = self?.usingDevice else {
seal.reject(studyCard: .noUsingDevice)
return
}
let param: [String: Any] = [
"progressList": models.mm.toJSON(),
"deviceSN": using.snCode ?? "",
"deviceType": using.deviceType ?? ""
]
MMLog.log("通知服务端进度上传", models.map { $0.bookId }, prefix: .studyCard)
APIRequest.mmPost(
withParms: param,
request: .kUrl_studyen_studycard_batchUploadStudyProgress
) { rsp in
MMLog.log("通知服务端上传进度成功", rsp.content, prefix: .studyCard)
seal.fulfill()
guard rsp.isSuccess() == false else {
return
}
MMToast.showText(rsp.msg)
} failureBlock: {
MMLog.log("通知服务端上传进度失败: \($0.code) - \($0.msg)", prefix: .studyCard)
seal.fulfill()
}
}
}
/// 通知服务端进度上传
/// - Parameter model: 进度结果模型
func noticeServerProgressUpload(model: ProgressFileSeverModel?) -> Promise<Void> {
Promise { seal in
guard let param: [String: Any] = model?.mm.toJSON() else {
seal.fulfill()
return
}
MMLog.log("通知服务端进度上传", model?.bookId ?? 0, prefix: .studyCard)
APIRequest.mmPost(
withParms: param,
request: .kUrl_studyen_studycard_uploadStudyProgressFile
) { rsp in
MMLog.log("通知服务端上传进度成功", rsp.content, prefix: .studyCard)
seal.fulfill()
guard rsp.isSuccess() == false else {
return
}
MMToast.showText(rsp.msg)
} failureBlock: {
MMLog.log("通知服务端上传进度失败: \($0.code) - \($0.msg)", prefix: .studyCard)
seal.fulfill()
}
}
}