SBTSetBatteryVCtr.swift 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. //
  2. // SBTSetBatteryVCtr.swift
  3. // SolarBT
  4. //
  5. // Created by weclouds on 2019/3/20.
  6. // Copyright © 2019 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. import SwiftPopup
  10. class SBTSetBatteryVCtr: SwiftPopup {
  11. var batRateVolt : String?
  12. var isAuto : Bool? = false
  13. var isDemo :Bool?
  14. var peripheralName :String?
  15. var batStr :String?
  16. var batteryDict : [String:String]? = [String:String]()
  17. var selectedIndex : Int?
  18. //完成回调
  19. var completehandle:((String?)->Void)?
  20. var maxVolt :String?
  21. var voltageStr : String?{
  22. didSet{
  23. }
  24. }
  25. var batteryTypeStr :String?{
  26. didSet{
  27. }
  28. }
  29. var selectedVoltage : Int? = -1
  30. var selectedBatteyType: Int? = -1
  31. var cmdArr : [String]? = [String]()
  32. @IBOutlet weak var batterySettingLabel: UILabel!
  33. @IBOutlet weak var batteryTypeLabel: UILabel!
  34. @IBOutlet weak var systemVoltageLabel: UILabel!
  35. @IBOutlet weak var saveBtn: UIButton!
  36. @IBOutlet weak var cancelBtn: UIButton!
  37. @IBOutlet weak var systemVoltageBox: UIView!
  38. @IBOutlet weak var batteryTapeBox: UIView!
  39. @IBOutlet weak var closeLabel: UILabel!
  40. @IBOutlet weak var batteryType: UILabel!
  41. @IBOutlet weak var systemVoltage: UILabel!
  42. override func viewDidLoad() {
  43. super.viewDidLoad()
  44. createUI()
  45. addGesture()
  46. // Do any additional setup after loading the view.
  47. }
  48. override func viewDidAppear(_ animated: Bool) {
  49. super .viewDidAppear(animated)
  50. if self.selectedIndex == 0 {
  51. valtageTap()
  52. }else if selectedIndex == 1{
  53. batteryTap()
  54. }
  55. }
  56. //00
  57. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  58. // dismiss()
  59. }
  60. @IBAction func saveAction(_ sender: Any) {
  61. //~/Library/Logs/DiagnosticReports and has a prefix 'lldb-rpc-server
  62. if self.isDemo == false { //
  63. cmdArr?.removeAll()
  64. var oldVolt = "00"
  65. if self.batRateVolt == "Auto" || self.batRateVolt == "自动识别"{
  66. oldVolt = "00"
  67. }else{
  68. oldVolt = String(format: "%02X", Int(self.batRateVolt!)!)
  69. }
  70. //判断输入
  71. if selectedVoltage != -1 { //
  72. // let key = DeviceDictKey()
  73. var voltage = "" // 设置高八位
  74. if batteryType.text == "锂LI" || batteryType.text == "Lithium LI" {
  75. if self.maxVolt == "12" {
  76. // valtages = ["12V"]
  77. switch selectedVoltage {
  78. case 0: // 12
  79. voltage = "0C" + oldVolt
  80. default:
  81. break
  82. }
  83. }else if self.maxVolt == "24" {
  84. // valtages = ["12V","24V"]
  85. switch selectedVoltage {
  86. case 0: // 12
  87. voltage = "0C" + oldVolt
  88. case 1: //24
  89. voltage = "18" + oldVolt
  90. default:
  91. break
  92. }
  93. }else if self.maxVolt == "36" {
  94. //valtages = ["12V","24V","36V"]
  95. switch selectedVoltage {
  96. case 0: // 12
  97. voltage = "0C" + oldVolt
  98. case 1: //24
  99. voltage = "18" + oldVolt
  100. case 2: // 36
  101. voltage = "24" + oldVolt
  102. default:
  103. break
  104. }
  105. }else if self.maxVolt == "48" {
  106. // valtages = ["12V","24V","36V","48V"]
  107. switch selectedVoltage {
  108. case 0: // 12
  109. voltage = "0C" + oldVolt
  110. case 1: //24
  111. voltage = "18" + oldVolt
  112. case 2: // 36
  113. voltage = "24" + oldVolt
  114. case 3: //48
  115. voltage = "30" + oldVolt
  116. default:
  117. break
  118. }
  119. }else if self.maxVolt == "60" {
  120. //valtages = ["12V","24V","36V","48V","60V"]
  121. switch selectedVoltage {
  122. case 0: // 12
  123. voltage = "0C" + oldVolt
  124. case 1: //24
  125. voltage = "18" + oldVolt
  126. case 2: // 36
  127. voltage = "24" + oldVolt
  128. case 3: //48
  129. voltage = "30" + oldVolt
  130. case 4: //60
  131. voltage = "3C" + oldVolt
  132. default:
  133. break
  134. }
  135. }else if self.maxVolt == "72" {
  136. // valtages = ["12V","24V","36V","48V","60V","72V"]
  137. switch selectedVoltage {
  138. case 0: // 12
  139. voltage = "0C" + oldVolt
  140. case 1: //24
  141. voltage = "18" + oldVolt
  142. case 2: // 36
  143. voltage = "24" + oldVolt
  144. case 3: //48
  145. voltage = "30" + oldVolt
  146. case 4: //60
  147. voltage = "3C" + oldVolt
  148. case 5: //72
  149. voltage = "48" + oldVolt
  150. default:
  151. break
  152. }
  153. }else if self.maxVolt == "96" {
  154. // valtages = ["12V","24V","36V","48V","60V","72V","96V"]
  155. switch selectedVoltage {
  156. case 0: // 12
  157. voltage = "0C" + oldVolt
  158. case 1: //24
  159. voltage = "18" + oldVolt
  160. case 2: // 36
  161. voltage = "24" + oldVolt
  162. case 3: //48
  163. voltage = "30" + oldVolt
  164. case 4: //60
  165. voltage = "3C" + oldVolt
  166. case 5: //72
  167. voltage = "48" + oldVolt
  168. case 6: //96
  169. voltage = "60" + oldVolt
  170. default:
  171. break
  172. }
  173. }
  174. }else {
  175. if self.maxVolt == "12" {
  176. // valtages = ["12V","Auto".da_localizedStr()]
  177. switch selectedVoltage {
  178. case 0: // 12
  179. voltage = "0C" + oldVolt
  180. case 1: //自动识别
  181. voltage = "FF" + oldVolt
  182. default:
  183. break
  184. }
  185. }else if self.maxVolt == "24" {
  186. // valtages = ["12V","24V","Auto".da_localizedStr()]
  187. switch selectedVoltage {
  188. case 0: // 12
  189. voltage = "0C" + oldVolt
  190. case 1: //24
  191. voltage = "18" + oldVolt
  192. case 2: // 自动识别
  193. voltage = "FF" + oldVolt
  194. default:
  195. break
  196. }
  197. }else if self.maxVolt == "36" {
  198. // valtages = ["12V","24V","36V","Auto".da_localizedStr()]
  199. switch selectedVoltage {
  200. case 0: // 12
  201. voltage = "0C" + oldVolt
  202. case 1: //24
  203. voltage = "18" + oldVolt
  204. case 2: // 36
  205. voltage = "24" + oldVolt
  206. case 3: //自动识别
  207. voltage = "FF" + oldVolt
  208. default:
  209. break
  210. }
  211. }else if self.maxVolt == "48" {
  212. // valtages = ["12V","24V","36V","48V","Auto".da_localizedStr()]
  213. switch selectedVoltage {
  214. case 0: // 12
  215. voltage = "0C" + oldVolt
  216. case 1: //24
  217. voltage = "18" + oldVolt
  218. case 2: // 36
  219. voltage = "24" + oldVolt
  220. case 3: //48
  221. voltage = "30" + oldVolt
  222. case 4: //自动识别
  223. voltage = "FF" + oldVolt
  224. default:
  225. break
  226. }
  227. }else if self.maxVolt == "60" {
  228. // valtages = ["12V","24V","36V","48V","60V","Auto".da_localizedStr()]
  229. switch selectedVoltage {
  230. case 0: // 12
  231. voltage = "0C" + oldVolt
  232. case 1: //24
  233. voltage = "18" + oldVolt
  234. case 2: // 36
  235. voltage = "24" + oldVolt
  236. case 3: //48
  237. voltage = "30" + oldVolt
  238. case 4: //60
  239. voltage = "3C" + oldVolt
  240. case 5: //自动识别
  241. voltage = "FF" + oldVolt
  242. default:
  243. break
  244. }
  245. }else if self.maxVolt == "72" {
  246. // valtages = ["12V","24V","36V","48V","60V","72V","Auto".da_localizedStr()]
  247. switch selectedVoltage {
  248. case 0: // 12
  249. voltage = "0C" + oldVolt
  250. case 1: //24
  251. voltage = "18" + oldVolt
  252. case 2: // 36
  253. voltage = "24" + oldVolt
  254. case 3: //48
  255. voltage = "30" + oldVolt
  256. case 4: //60
  257. voltage = "3C" + oldVolt
  258. case 5: //72
  259. voltage = "48" + oldVolt
  260. case 6: //自动识别
  261. voltage = "FF" + oldVolt
  262. default:
  263. break
  264. }
  265. }else if self.maxVolt == "96" {
  266. switch selectedVoltage {
  267. case 0: // 12
  268. voltage = "0C" + oldVolt
  269. case 1: //24
  270. voltage = "18" + oldVolt
  271. case 2: // 36
  272. voltage = "24" + oldVolt
  273. case 3: //48
  274. voltage = "30" + oldVolt
  275. case 4: //60
  276. voltage = "3C" + oldVolt
  277. case 5: //72
  278. voltage = "48" + oldVolt
  279. case 6: //96
  280. voltage = "60" + oldVolt
  281. case 7: //自动识别
  282. voltage = "FF" + oldVolt
  283. default:
  284. break
  285. }
  286. }
  287. }
  288. let voltageCmd = "FF06E003" + voltage
  289. cmdArr?.append(voltageCmd)
  290. }
  291. if selectedBatteyType != -1{
  292. var batteryType = ""
  293. switch selectedBatteyType {
  294. case 0: // 自定义
  295. batteryType = "0000"
  296. case 1: // FLD
  297. batteryType = "0001"
  298. case 2: // SLD
  299. batteryType = "0002"
  300. case 3: // GEL
  301. batteryType = "0003"
  302. case 4: // LI
  303. batteryType = "0004"
  304. default:
  305. break
  306. }
  307. let voltageCmd = "FF06E004" + batteryType
  308. cmdArr?.append(voltageCmd)
  309. }
  310. if cmdArr?.count == 0 {
  311. g_toast("No_data_update".da_localizedStr())
  312. dismiss()
  313. }
  314. if self.cmdArr?.count == 1 {
  315. if self.cmdArr?.first?.hasPrefix("FF06E004") == true {
  316. if self.isAuto == true {
  317. let voltage1 = "18" + oldVolt
  318. let voltageCmd1 = "FF06E003" + oldVolt
  319. cmdArr?.append(voltageCmd1)
  320. }
  321. }
  322. }
  323. log.debug(" 发送的指令 --- \(self.cmdArr)")
  324. // if isDemo == false{
  325. BabyBluetoothSwift.shareInstance()?.writeData2222(cmdArr, completionHandler: { (result) in
  326. NotificationCenter.default.post(name: NSNotification.Name(kNotifionCationSetBatteryType), object: nil)
  327. if self.batteryDict?.isEmpty == false{
  328. NotificationCenter.default.post(name: NSNotification.Name(rawValue: KNotifitionToChangeBatteryType), object: nil, userInfo: self.batteryDict)
  329. }
  330. self.completehandle!(self.batStr)
  331. g_showHUD("Successful".da_localizedStr())
  332. })
  333. }else{
  334. g_showHUD("Current_demo_data".da_localizedStr())
  335. }
  336. dismiss()
  337. }
  338. @IBAction func cancelAction(_ sender: Any) {
  339. dismiss()
  340. }
  341. }
  342. extension SBTSetBatteryVCtr{
  343. func addGesture() {
  344. closeLabel.isUserInteractionEnabled = true
  345. let tap1 = UITapGestureRecognizer(target: self, action: #selector(valtageTap))
  346. let tap2 = UITapGestureRecognizer(target: self, action: #selector(batteryTap))
  347. let tap3 = UITapGestureRecognizer(target: self, action: #selector(closeTap))
  348. systemVoltageBox.addGestureRecognizer(tap1)
  349. batteryTapeBox.addGestureRecognizer(tap2)
  350. closeLabel.addGestureRecognizer(tap3)
  351. }
  352. @objc func valtageTap() {
  353. log.debug("点击电压")
  354. var valtages = [String]()
  355. if batteryType.text == "锂LI" || batteryType.text == "Lithium LI" {
  356. if self.maxVolt == "12" {
  357. valtages = ["12V"]
  358. }else if self.maxVolt == "24" {
  359. valtages = ["12V","24V"]
  360. }else if self.maxVolt == "36" {
  361. valtages = ["12V","24V","36V"]
  362. }else if self.maxVolt == "48" {
  363. valtages = ["12V","24V","36V","48V"]
  364. }else if self.maxVolt == "60" {
  365. valtages = ["12V","24V","36V","48V","60V"]
  366. }else if self.maxVolt == "72" {
  367. valtages = ["12V","24V","36V","48V","60V","72V"]
  368. }else if self.maxVolt == "96" {
  369. valtages = ["12V","24V","36V","48V","60V","72V","96V"]
  370. }
  371. }else {
  372. if self.maxVolt == "12" {
  373. valtages = ["12V","Auto".da_localizedStr()]
  374. }else if self.maxVolt == "24" {
  375. valtages = ["12V","24V","Auto".da_localizedStr()]
  376. }else if self.maxVolt == "36" {
  377. valtages = ["12V","24V","36V","Auto".da_localizedStr()]
  378. }else if self.maxVolt == "48" {
  379. valtages = ["12V","24V","36V","48V","Auto".da_localizedStr()]
  380. }else if self.maxVolt == "60" {
  381. valtages = ["12V","24V","36V","48V","60V","Auto".da_localizedStr()]
  382. }else if self.maxVolt == "72" {
  383. valtages = ["12V","24V","36V","48V","60V","72V","Auto".da_localizedStr()]
  384. }else if self.maxVolt == "96" {
  385. valtages = ["12V","24V","36V","48V","60V","72V","96V","Auto".da_localizedStr()]
  386. }
  387. }
  388. // let valtages = ["12V","24V","36V","48V","60V","72V","96V","自动识别"]
  389. let valtagePick = THScrollChooseView(question: valtages, withDefaultDesc:"12V")
  390. valtagePick!.show()
  391. valtagePick?.confirmBlock = {[weak self](selectedIndex) in
  392. log.debug(valtages[selectedIndex])
  393. self?.systemVoltage.text = valtages[selectedIndex]
  394. self?.systemVoltage.textColor = UIColor(hexString: "222222")
  395. self?.selectedVoltage = selectedIndex
  396. }
  397. }
  398. @objc func batteryTap() {
  399. log.debug("点击电池")
  400. let batterys = ["User-defined".da_localizedStr(),"Vented_lead_acid_battery_FLD".da_localizedStr(),"Lead_acid_battery_SLD".da_localizedStr(),"Gel_lead_acid_GEL".da_localizedStr(),"Lithium_iron_phosphate_battery_LI".da_localizedStr()]
  401. let batteryPick = THScrollChooseView(question: batterys, withDefaultDesc:"User-defined".da_localizedStr())
  402. batteryPick!.show()
  403. batteryPick?.confirmBlock = {[weak self](selectedIndex) in
  404. log.debug(batterys[selectedIndex])
  405. self?.batteryType.text = batterys[selectedIndex]
  406. self?.batStr = batterys[selectedIndex]
  407. self?.batteryType.textColor = UIColor(hexString: "222222")
  408. self?.selectedBatteyType = selectedIndex
  409. switch batterys[selectedIndex] {
  410. case "自定义":
  411. AppShare.batteryType = .USER
  412. case "开口铅酸FLD":
  413. AppShare.batteryType = .FLD
  414. case "密封铅酸SLD":
  415. AppShare.batteryType = .SLD
  416. case "胶体铅酸GEL":
  417. AppShare.batteryType = .GEL
  418. case "锂LI":
  419. AppShare.batteryType = .LI
  420. case "User-defined":
  421. AppShare.batteryType = .USER
  422. case "Vented lead acid battery FLD":
  423. AppShare.batteryType = .FLD
  424. case "Lead acid battery SLD":
  425. AppShare.batteryType = .SLD
  426. case "Gel_lead_acid_GEL":
  427. AppShare.batteryType = .GEL
  428. case "Lithium iron phosphate battery LI":
  429. AppShare.batteryType = .LI
  430. default:
  431. break
  432. }
  433. let dict = ["batteryType":"\(selectedIndex)"]
  434. log.debug(" batteryType =\(dict) ")
  435. self?.batteryDict = dict
  436. }
  437. }
  438. @objc func closeTap() {
  439. dismiss()
  440. }
  441. func createUI() {
  442. self.batterySettingLabel.text = "Battery_settings".da_localizedStr()
  443. self.systemVoltageLabel.text = "System_voltage".da_localizedStr()
  444. self.batteryTypeLabel.text = "Battery_type".da_localizedStr()
  445. self.closeLabel.text = "Retract".da_localizedStr()
  446. self.saveBtn.setTitle("Setup".da_localizedStr(), for: .normal)
  447. self.cancelBtn.setTitle("Cancel".da_localizedStr(), for: .normal)
  448. self.backViewColor = UIColor(white: 1, alpha: 0.84)
  449. let bgLayer1 = CAGradientLayer()
  450. bgLayer1.colors = [UIColor(red: 0.82, green: 0.82, blue: 0.82, alpha: 1).cgColor, UIColor(red: 0.74, green: 0.74, blue: 0.74, alpha: 1).cgColor]
  451. bgLayer1.locations = [0, 1]
  452. bgLayer1.frame = self.cancelBtn.bounds
  453. bgLayer1.startPoint = CGPoint(x: 0.09, y: 0.31)
  454. bgLayer1.endPoint = CGPoint(x: 0.83, y: 0.83)
  455. self.cancelBtn.layer.addSublayer(bgLayer1)
  456. // fillCode
  457. let bgLayer2 = CAGradientLayer()
  458. bgLayer2.colors = [UIColor(red: 1, green: 0.52, blue: 0.07, alpha: 1).cgColor, UIColor(red: 1, green: 0.67, blue: 0, alpha: 1).cgColor]
  459. bgLayer2.locations = [0, 1]
  460. bgLayer2.frame = self.saveBtn.bounds
  461. bgLayer2.startPoint = CGPoint(x: 1, y: 1)
  462. bgLayer2.endPoint = CGPoint(x: 0, y: 0)
  463. self.saveBtn.layer.addSublayer(bgLayer2)
  464. systemVoltage.text = self.voltageStr!
  465. batteryType.text = self.batteryTypeStr
  466. }
  467. }