75d24c15
yangbin
123
|
1
2
3
4
5
6
7
8
9
10
|
//
// FirmwareUpdateVC.swift
// Twear
//
// Created by yangbin on 2021/12/25.
//
import UIKit
import Alamofire
import HandyJSON
|
be19e595
yangbin
9
|
11
12
|
import SSZipArchive
import MBProgressHUD
|
75d24c15
yangbin
123
|
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
class FirmwareUpdateVC: UIViewController, XMLParserDelegate {
@IBOutlet weak var updateBtn: UIButton!
@IBOutlet weak var updateImageView: UIImageView!
@IBOutlet weak var deviceImageView: UIImageView!
@IBOutlet weak var updateLabel: UILabel!
@IBOutlet weak var progressLabel: UILabel!
@IBOutlet weak var versionLabel: UILabel!
var latestVersion: String = ""
var downLoadUrlPath: String = ""
var id: String = ""
var device = CurDevice
var downloadRequest: DownloadRequest? //下载请求对象
|
be19e595
yangbin
9
|
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
UIApplication.shared.isIdleTimerDisabled = false
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
let navController = navigationController as? ZCNavigationController
navController?.enableScreenEdgePanGestureRecognizer(true)
}
override func navigationShouldPop() -> Bool {
if !IsDailSyncing {
return true
} else {
MBProgressHUD.showh(LocString("正在升级中,请稍后..."))
return false
}
}
|
75d24c15
yangbin
123
|
51
52
53
|
override func viewDidLoad() {
super.viewDidLoad()
title = LocString("固件升级")
|
be19e595
yangbin
9
|
54
55
56
57
|
UIApplication.shared.isIdleTimerDisabled = true
Alamofire.SessionManager.default.session.configuration.requestCachePolicy = .reloadIgnoringLocalCacheData
|
75d24c15
yangbin
123
|
58
59
60
61
62
63
|
BluetoothManager.shared.getFirmwareVersion {[weak self] version, type, error in
self?.device = CurDevice
self?.queryLatestVersion()
}
}
|
be19e595
yangbin
9
|
64
65
66
67
68
69
70
71
|
func updateUserInteraction(_ isSyncing: Bool) {
// collectionView.isUserInteractionEnabled = !isSyncing
IsDailSyncing = isSyncing
let navController = navigationController as? ZCNavigationController
navController?.enableScreenEdgePanGestureRecognizer(!isSyncing)
}
|
75d24c15
yangbin
123
|
72
73
74
75
76
77
78
79
80
81
82
83
84
|
func queryLatestVersion() {
Alamofire.request("http://hodafone.com.cn/qwear/update/\(device.type)/version.xml").responseData { responseData in
if let data = responseData.data {
self.parseXML(data)
}
}
}
private func checkUpdate() {
if device.version.checkVersion(latestVersion) {
updateBtn.isUserInteractionEnabled = true
updateBtn.backgroundColor = UIColor.rgbColorFromHex(0x00993E)
let string = NSMutableAttributedString(string: "\(LocString("发现新版本")):v \(latestVersion)")
|
be19e595
yangbin
9
|
85
|
string.addAttributes([.foregroundColor: UIColor.rgbColorFromHex(0x00993E)], range: NSMakeRange("\(LocString("发现新版本")):".count, string.string.length-"\(LocString("发现新版本")):".count))
|
75d24c15
yangbin
123
|
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
|
versionLabel.attributedText = string
} else {
updateBtn.isUserInteractionEnabled = false
updateBtn.backgroundColor = UIColor.rgbColorFromHex(0xCBCBCB)
let str1 = LocString("当前已是最新版本")
let string = NSMutableAttributedString(string: "\(str1)\nv \(device.version)")
string.addAttributes([.foregroundColor: UIColor.rgbColorFromHex(0x00993E)], range: NSMakeRange(str1.count, string.string.count-str1.count))
versionLabel.attributedText = string
}
}
@IBAction func update(_ sender: UIButton) {
print("开始更新")
updateBtn.isUserInteractionEnabled = false
updateBtn.backgroundColor = UIColor.rgbColorFromHex(0xCBCBCB)
deviceImageView.isHidden = true
updateLabel.isHidden = false
progressLabel.isHidden = false
downloadUpdateFile()
}
func downloadUpdateFile() {
// let url = URL(string: "http://hodafone.com.cn/qwear/update/\(id)/\(downLoadUrlPath)")
let version = latestVersion
var filePath = ""
|
be19e595
yangbin
9
|
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
var unzipPath = ""
// let dest: DownloadRequest.DownloadFileDestination = { _, _ in
// let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
// let fileURL = documentsURL.appendingPathComponent("update/firmware1.bin")
//// unzipPath = documentsURL.path//.appendingPathComponent("update").path
// return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
// }
let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)
let manager = FileManager.default
let documentsURL = manager.urls(for: .documentDirectory, in: .userDomainMask)[0]
let contentsOfPath = try? manager.contentsOfDirectory(atPath: documentsURL.path)
print(contentsOfPath)
let dPath = documentsURL.appendingPathComponent(downLoadUrlPath).path
if manager.fileExists(atPath: dPath) {
try? manager.removeItem(atPath: dPath)
// self.showAlert(title: LocString("提示"), message: LocString("升级失败,请重试"), cancelText: nil)
// return
|
75d24c15
yangbin
123
|
131
|
}
|
be19e595
yangbin
9
|
132
133
134
135
|
// MTB033B_0.1.7.bin
Alamofire.download("http://hodafone.com.cn/qwear/update/\(id)/\(downLoadUrlPath)", to: destination).downloadProgress { progress in
print("下载进度--\(progress.fractionCompleted)")
|
75d24c15
yangbin
123
|
136
137
138
|
}.responseData { response in
if let data = response.value {
print("下载成功")
|
be19e595
yangbin
9
|
139
|
self.syncFile(response.destinationURL!.path, unzipPath: unzipPath)
|
75d24c15
yangbin
123
|
140
141
|
// response.destinationURL
} else {
|
be19e595
yangbin
9
|
142
|
self.showAlert(title: LocString("提示"), message: LocString("升级失败,请重试"), cancelText: nil)
|
75d24c15
yangbin
123
|
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
}
}
//开始下载
// AF.download(url, interceptor: nil, to: dest).downloadProgress(closure: { (progress) in
//
// debugPrint("-------下载进度: \(progress.fractionCompleted)")
// }).responseData { (res) in
// if let data = res.value {
// //显示图片
// let image = UIImage(data: data)
// self.imgGif.image = image
// }
// }
}
|
be19e595
yangbin
9
|
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
func syncFile(_ path: String, unzipPath: String) {
let manager = FileManager.default
let contentsOfPath = try? manager.contentsOfDirectory(atPath: path)
print(contentsOfPath)
// path String "/var/mobile/Containers/Data/Application/38F71EF2-43E6-415C-85BF-D7F29FFBC772/Documents/update/firmware.bin"
// print(unzipPath)
// let attributes = try? manager.attributesOfItem(atPath: path) //结果为Dictionary类型
// print("文件大小:\(attributes![FileAttributeKey.size]!)")
// print(NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)[0])
// let success: Bool = SSZipArchive.unzipFile(atPath: path, toDestination: unzipPath)
// if !success {
// self.showAlert(title: "", message: "升级失败,请重试")
// return
// }
if !manager.fileExists(atPath: path) {
self.showAlert(title: LocString("提示"), message: LocString("升级失败,请重试"), cancelText: nil)
return
}
// return
updateUserInteraction(true)
|
75d24c15
yangbin
123
|
180
181
182
183
184
|
BluetoothManager.shared.syncFileWith(path: path, isFirmware: true) {[weak self] progress in
let n = progress/2 * 2
self?.updateImageView.image = UIImage(named: "update_\(n)")
self?.progressLabel.text = "\(progress)%"
} completion: {[weak self] error in
|
be19e595
yangbin
9
|
185
|
self?.updateUserInteraction(false)
|
75d24c15
yangbin
123
|
186
|
if error == nil {
|
be19e595
yangbin
9
|
187
188
|
self?.updateLabel.text = LocString("升级完成")
self?.updateBtn.setTitle(LocString("升级完成"), for: .normal)
|
75d24c15
yangbin
123
|
189
|
self?.updateImageView.image = UIImage(named: "update_100")
|
be19e595
yangbin
9
|
190
191
|
} else {
self?.showAlert(title: LocString("提示"), message: LocString("升级失败,请重试"), cancelText: nil)
|
75d24c15
yangbin
123
|
192
193
194
195
|
}
}
}
|
75d24c15
yangbin
123
|
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
|
func parseXML(_ data: Data) {
let parser = XMLParser(data: data)
parser.delegate = self
parser.parse()
}
func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) {
if elementName == "product"{
print(attributeDict)
if let least = attributeDict["least"] {
latestVersion = least
checkUpdate()
}
if let file = attributeDict["file"] {
downLoadUrlPath = file
}
if let id = attributeDict["id"] {
self.id = id
}
}
}
deinit {
print("deinit\(NSStringFromClass(type(of: self)))!!!!!!!")
}
}
// func compareVersion(nowVersion:String,newVersion:String) -> Bool {
//
// let nowArray = nowVersion.split(separator: ".")
//
// let newArray = newVersion.split(separator: ".")
//
// let big = nowArray.count > newArray.count ? newArray.count : nowArray.count
//
// for index in 0...big - 1 {
//
// let first = nowArray[index]
// let second = newArray[index]
// if Int(first)! < Int(second)! {
// return true
// }
// }
// return false
// }
|