// // DialPushViewModel.swift // Lookfit // // 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 DialPushViewModel: ViewModel, ViewModelType { struct Input { let selection: Observable } struct Output { } func transform(input: DialPushViewModel.Input) -> DialPushViewModel.Output { input.selection .subscribe(onNext: { [weak self] dialModel in guard let `self` = self else { return } // 二次确认 showAlert(currentViewController()!, MultiLanguageKey.dialPush.localized, cancelText: "NO", confirmText: "YES") { (result) in if result { if !BluetoothService.shared.checkBleCmdEnable() { return } SVProgressHUD.setDefaultMaskType(.black) SVProgressHUD.show() // 下载表盘 ProviderRequestDownload(APIManager.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.networkError.localized) return } if let deviceInfo = UserDefaultsManager.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]>(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 else { return } let isSuccess = notification.name.rawValue == CmdSuccess switch cmd { case .newDialPush: if isSuccess { self.offset = 1 self.sendDialData() return } SVProgressHUD.showError(withStatus: MultiLanguageKey.setFail.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.diapPushComplete.localized) self.offset = 1 return } var count = 200 if offset == total && dialData.length % 200 != 0 { count = dialData.length % 200 } SVProgressHUD.show(withStatus: "\(MultiLanguageKey.dialPush.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 } }