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.
 
 
 

166 lines
6.7 KiB

//
// DialPushViewModels.swift
// FireBoltt
//
// 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
}
class DialPushViewModels: ViewModel, ViewModelType {
struct Input {
let selection: Observable<DialModel>
}
struct Output {
}
func transform(input: DialPushViewModels.Input) -> DialPushViewModels.Output {
input.selection
.subscribe(onNext: { [weak self] dialModel in
guard let `self` = self else { return }
//
showAlert(currentViewController()!, MultiLanguageKey_FB.dialPushFB.localized, cancelText: "NO", confirmText: "YES") { (result) in
if result {
if !BluetoothService.shared.checkBleCmdEnable(isShow: true) {
return
}
SVProgressHUD.setDefaultMaskType(.black)
SVProgressHUD.show()
//
ProviderRequestDownload(APIManagerFireBoltt.downLoad(url: dialModel.fileUrl))
.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) {
SVProgressHUD.showError(withStatus: MultiLanguageKey_FB.networkErrorFB.localized)
return
}
if let deviceInfo = UserDefaultsManagerFrieBoltt.getDeviceInfo() {
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
guard let `self` = self, let cmd = notification.object as? BleCMD_FireBoltt else { return }
let isSuccess = notification.name.rawValue == CmdSuccess
switch cmd {
case .newDialPush:
if isSuccess {
self.offset = 1
self.sendDialData()
return
}
SVProgressHUD.showError(withStatus: MultiLanguageKey_FB.setFailFB.localized)
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 {
SVProgressHUD.showSuccess(withStatus: MultiLanguageKey_FB.diapPushCompleteFB.localized)
self.offset = 1
return
}
var count = 200
if offset == total && dialData.length % 200 != 0 {
count = dialData.length % 200
}
SVProgressHUD.show(withStatus: "\(MultiLanguageKey_FB.dialPushFB.localized)" + String(format: " %.0lf%%", (CGFloat(offset) / CGFloat(total)) * 100))
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
}
}