You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
167 lines
6.7 KiB
167 lines
6.7 KiB
![]()
2 years ago
|
//
|
||
![]()
2 years ago
|
// DialPushViewModels.swift
|
||
![]()
1 year ago
|
// FireBoltt
|
||
![]()
2 years ago
|
//
|
||
|
// Created by lemo. on 2020/4/29.
|
||
|
// Copyright © 2020 Sheldon. All rights reserved.
|
||
|
//
|
||
|
|
||
|
import UIKit
|
||
|
|
||
|
struct DialModel {
|
||
|
let icon: String
|
||
|
let fileUrl: String
|
||
|
let appName: String
|
||
|
}
|
||
|
|
||
|
|
||
![]()
2 years ago
|
class DialPushViewModels: ViewModel, ViewModelType {
|
||
![]()
2 years ago
|
|
||
|
struct Input {
|
||
|
let selection: Observable<DialModel>
|
||
|
}
|
||
|
|
||
|
struct Output {
|
||
|
}
|
||
|
|
||
![]()
2 years ago
|
func transform(input: DialPushViewModels.Input) -> DialPushViewModels.Output {
|
||
![]()
2 years ago
|
input.selection
|
||
|
.subscribe(onNext: { [weak self] dialModel in
|
||
|
guard let `self` = self else { return }
|
||
|
// 二次确认
|
||
![]()
1 year ago
|
showAlert(currentViewController()!, MultiLanguageKey_FB.dialPushFB.localized, cancelText: "NO", confirmText: "YES") { (result) in
|
||
![]()
2 years ago
|
if result {
|
||
![]()
2 years ago
|
if !BluetoothService.shared.checkBleCmdEnable(isShow: true) {
|
||
![]()
2 years ago
|
return
|
||
|
}
|
||
|
SVProgressHUD.setDefaultMaskType(.black)
|
||
|
SVProgressHUD.show()
|
||
|
// 下载表盘
|
||
![]()
1 year ago
|
ProviderRequestDownload(APIManagerFireBoltt.downLoad(url: dialModel.fileUrl))
|
||
![]()
2 years ago
|
.subscribe(onNext: { [weak self] (result) in
|
||
|
let data = result as NSData
|
||
|
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
|
||
|
let filePath = String(format: "%@/watch6", paths)
|
||
|
if !data.write(toFile: filePath, atomically: true) {
|
||
![]()
1 year ago
|
SVProgressHUD.showError(withStatus: MultiLanguageKey_FB.networkErrorFB.localized)
|
||
![]()
2 years ago
|
return
|
||
|
}
|
||
![]()
1 year ago
|
if let deviceInfo = UserDefaultsManagerFrieBoltt.getDeviceInfo() {
|
||
![]()
2 years ago
|
switch deviceInfo.otaPlan {
|
||
|
case .jerry:
|
||
|
BluetoothService.shared.newJerryDialPushWithBleCmdType(filePath: filePath, url: dialModel.fileUrl, fileData: data as Data)
|
||
|
|
||
|
default:
|
||
|
self?.dialData = data
|
||
|
// 发送表盘指令
|
||
|
BluetoothService.shared.newDialPushWithBleCmdType(number: 1, type: 1, fileLenght: data.length)
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
})
|
||
|
.disposed(by: self.rx.disposeBag)
|
||
|
// // 获取表盘文件
|
||
|
// guard let filePath = Bundle.main.path(forResource: "watch\(ip.row + 1)", ofType: "bin"), let data = NSData(contentsOfFile: filePath)else {
|
||
|
// return
|
||
|
// }
|
||
|
// self?.dialData = data
|
||
|
// // 发送表盘指令
|
||
|
// SVProgressHUD.setDefaultMaskType(.black)
|
||
|
// SVProgressHUD.show()
|
||
|
// BluetoothService.shared.newDialPushWithBleCmdType(number: 1, type: 1, fileLenght: data.length)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
})
|
||
|
.disposed(by: rx.disposeBag)
|
||
|
|
||
|
return Output()
|
||
|
}
|
||
|
|
||
|
let relay = BehaviorRelay<[SectionModel<String, DialModel>]>(value: [])
|
||
|
private var dialData: NSData?
|
||
|
private var offset: Int = 1
|
||
|
|
||
|
override init() {
|
||
|
super.init()
|
||
|
loadDialData()
|
||
|
monitor()
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
deinit {
|
||
|
GCDTimer.shared.cancleTimer(WithTimerName: "sendDialData")
|
||
|
}
|
||
|
|
||
|
private func loadDialData() {
|
||
|
SVProgressHUD.show()
|
||
|
if let adapterNumber = GlobalDeviceProfileModel.shareInstance.firwareModel?.adapterNumber {
|
||
|
ProviderRequest(.dialList(appType: adapterNumber))
|
||
|
.subscribe(onNext: { [weak self] json in
|
||
|
SVProgressHUD.dismiss()
|
||
|
guard let `self` = self else { return }
|
||
|
if let data = json["data"].array, data.count > 0 {
|
||
|
let dials = data.map { DialModel(icon: $0["icon"].stringValue, fileUrl: $0["url"].stringValue, appName: $0["appName"].stringValue) }
|
||
|
self.relay.accept([SectionModel(model: "", items: dials)])
|
||
|
}
|
||
|
})
|
||
|
.disposed(by: rx.disposeBag)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private func monitor() {
|
||
|
/// 指令成功失败监听
|
||
|
Observable.of(kNotificationCenter.rx.notification(Notification.Name(rawValue: CmdSuccess)),
|
||
|
kNotificationCenter.rx.notification(Notification.Name(rawValue: CmdTimeout)))
|
||
|
.merge()
|
||
|
.subscribe(onNext: { [weak self] notification in
|
||
![]()
1 year ago
|
guard let `self` = self, let cmd = notification.object as? BleCMD_FireBoltt else { return }
|
||
![]()
2 years ago
|
let isSuccess = notification.name.rawValue == CmdSuccess
|
||
|
switch cmd {
|
||
|
case .newDialPush:
|
||
|
if isSuccess {
|
||
|
self.offset = 1
|
||
|
self.sendDialData()
|
||
|
return
|
||
|
}
|
||
![]()
1 year ago
|
SVProgressHUD.showError(withStatus: MultiLanguageKey_FB.setFailFB.localized)
|
||
![]()
2 years ago
|
case .falshSend:
|
||
|
self.sendDialData()
|
||
|
default: break
|
||
|
}
|
||
|
})
|
||
|
.disposed(by: rx.disposeBag)
|
||
|
}
|
||
|
|
||
|
/// 开始发送表盘
|
||
|
private func sendDialData() {
|
||
|
guard let dialData = dialData else { return }
|
||
|
var total = dialData.length / 200
|
||
|
if dialData.length % 200 != 0 {
|
||
|
total += 1
|
||
|
}
|
||
|
if offset > total {
|
||
![]()
1 year ago
|
SVProgressHUD.showSuccess(withStatus: MultiLanguageKey_FB.diapPushCompleteFB.localized)
|
||
![]()
2 years ago
|
self.offset = 1
|
||
|
return
|
||
|
}
|
||
|
var count = 200
|
||
|
if offset == total && dialData.length % 200 != 0 {
|
||
|
count = dialData.length % 200
|
||
|
}
|
||
![]()
1 year ago
|
SVProgressHUD.show(withStatus: "\(MultiLanguageKey_FB.dialPushFB.localized)" + String(format: " %.0lf%%", (CGFloat(offset) / CGFloat(total)) * 100))
|
||
![]()
2 years ago
|
let data = dialData.subdata(with: NSMakeRange((offset - 1) * 200, count))
|
||
|
BluetoothService.shared.falshSendWithBleCmdType(total: total, curren: self.offset, lenght: data.count, data: data)
|
||
|
LFLogs("sendDialData total:\(total) curren:\(offset) lenght:\(data.count)")
|
||
|
offset += 1
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
}
|