SBTSetChargeVCtr.swift 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. //
  2. // SBTSetChargeVCtr.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. import IQKeyboardManagerSwift
  11. /*
  12. 1、 显示原来的
  13. 2、 电池类型切换
  14. 充电器数据变化
  15. 读取回来 ?
  16. 还是设置进去
  17. isDemo?
  18. */
  19. class SBTSetChargeVCtr: SwiftPopup {
  20. var isDemo :Bool?
  21. var peripheralName :String?
  22. var isOld: Bool?
  23. var MaxVoltage :String?
  24. //放大倍数
  25. var Multiple : Float? // 默认为1倍
  26. //IchageRage
  27. var IchageRage : String? //额定充电电流
  28. //完成回调
  29. var completehandle:(()->Void)?
  30. var difeffrentUIArr : [Int]? = [Int]()
  31. var CellBatteryType :BatteryType? = .USER{
  32. didSet{
  33. if isOld == true {
  34. if let batteryType = self.CellBatteryType {
  35. switch batteryType {
  36. case .USER:
  37. self.difeffrentUIArr = [1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1]
  38. case .SLD:
  39. self.difeffrentUIArr = [1,1,1,1, 1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1]
  40. case .FLD:
  41. self.difeffrentUIArr = [1,1,0,0, 1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1]
  42. case .GEL:
  43. self.difeffrentUIArr = [1,1,1,1, 1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1]
  44. case .LI:
  45. self.difeffrentUIArr = [1,1,1,1, 1,1,0,1,1,1,1,1,1,1,1,0,1,0,1,1,1,1]
  46. }
  47. }
  48. }else{
  49. if let batteryType = self.CellBatteryType {
  50. switch batteryType {
  51. case .USER:
  52. self.difeffrentUIArr = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
  53. case .SLD:
  54. self.difeffrentUIArr = [0,0,0,0, 1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,0,0]
  55. case .FLD:
  56. self.difeffrentUIArr = [0,0,0,0, 1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,0,0]
  57. case .GEL:
  58. self.difeffrentUIArr = [0,0,0,0, 1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,0,0]
  59. case .LI:
  60. self.difeffrentUIArr = [0,0,0,0, 1,1,0,1,1,1,1,1,1,1,1,0,1,0,1,1,0,0]
  61. }
  62. }
  63. }
  64. }
  65. }
  66. var selectedIndexPath :IndexPath?{
  67. didSet{
  68. }
  69. }
  70. var dataSources:[String]? = [String](){
  71. didSet{
  72. }
  73. }
  74. var sb_SelectedArr : [Bool]? = [false,false,false,false,false,false,false,false,false,false,false,
  75. false,false,false,false,false,false,false,false,false,false,false]
  76. let cellid1 = "cellid1"
  77. let cellid2 = "cellid2"
  78. let cellid3 = "cellid3"
  79. var istemp : String?
  80. @IBOutlet weak var closeLabel: UILabel!
  81. @IBOutlet weak var chageLabel: UILabel!
  82. @IBOutlet weak var saveBtn: UIButton!
  83. @IBOutlet weak var cancelBtn: UIButton!
  84. var collectionView:UICollectionView?
  85. @IBOutlet weak var contentView: UIView!
  86. var cmdArr : [String]? = [String]()
  87. var canEdit : Bool = true
  88. var itemTitles = [String]()
  89. override func viewDidLoad() {
  90. super.viewDidLoad()
  91. let def = UserDefaults.standard
  92. istemp = def.value(forKey: TEMPISCENTIGRADE) as? String
  93. var Upper_limit_of_charge_temperature = "Upper_limit_of_charge_temperature".da_localizedStr() + "(℃)"
  94. var Lower_limit_of_charge_temperature = "Lower_limit_of_charge_temperature".da_localizedStr() + "(℃)"
  95. var Upper_limit_of_discharge_temperature = "Upper_limit_of_discharge_temperature".da_localizedStr() + "(℃)"
  96. var Lower_limit_of_discharge_temperature = "Lower_limit_of_discharge_temperature".da_localizedStr() + "(℃)"
  97. if istemp == "0"{
  98. Upper_limit_of_charge_temperature = "Upper_limit_of_charge_temperature".da_localizedStr() + "(℉)"
  99. Lower_limit_of_charge_temperature = "Lower_limit_of_charge_temperature".da_localizedStr() + "(℉)"
  100. Upper_limit_of_discharge_temperature = "Upper_limit_of_discharge_temperature".da_localizedStr() + "(℉)"
  101. Lower_limit_of_discharge_temperature = "Lower_limit_of_discharge_temperature".da_localizedStr() + "(℉)"
  102. }else if istemp == "1"{
  103. Upper_limit_of_charge_temperature = "Upper_limit_of_charge_temperature".da_localizedStr() + "(℃)"
  104. Lower_limit_of_charge_temperature = "Lower_limit_of_charge_temperature".da_localizedStr() + "(℃)"
  105. Upper_limit_of_discharge_temperature = "Upper_limit_of_discharge_temperature".da_localizedStr() + "(℃)"
  106. Lower_limit_of_discharge_temperature = "Lower_limit_of_discharge_temperature".da_localizedStr() + "(℃)"
  107. }
  108. itemTitles = ["Maximum_charge_current(A)".da_localizedStr(),
  109. "Charge_on/off".da_localizedStr(),
  110. Upper_limit_of_charge_temperature,
  111. Lower_limit_of_charge_temperature,
  112. "Overvoltage".da_localizedStr(),
  113. "Limited_charge_voltage".da_localizedStr(),
  114. "Boost_charge_voltage".da_localizedStr(),
  115. "Floating_charge_voltage".da_localizedStr(),
  116. "Boost_charge_return_voltage".da_localizedStr(),
  117. "Boost_charge_time".da_localizedStr(),
  118. "Temperature_compensation_coefficient".da_localizedStr(),
  119. "Equalizing_charge_voltage".da_localizedStr(),
  120. "Equalizing_charge_time".da_localizedStr(),
  121. "Equalizing_charge_interval".da_localizedStr(),
  122. "Manual_equalizing".da_localizedStr(),
  123. "Over_discharge_return_voltage".da_localizedStr(),
  124. "Undervoltage_warning_voltage".da_localizedStr(),
  125. "Over_discharge_voltage".da_localizedStr(),
  126. "Discharge_cutoff_voltage".da_localizedStr(),
  127. "Over_discharge_delay_time".da_localizedStr(),
  128. Upper_limit_of_discharge_temperature,
  129. Lower_limit_of_discharge_temperature]
  130. createUI()
  131. }
  132. override func viewWillAppear(_ animated: Bool) {
  133. super.viewWillAppear(animated)
  134. NotificationCenter.default.addObserver(self, selector: #selector(notifionCationSetBatteryType), name: NSNotification.Name(rawValue: kNotifionCationSetBatteryType), object: nil)
  135. }
  136. override func viewDidDisappear(_ animated: Bool) {
  137. super.viewDidDisappear(animated)
  138. NotificationCenter.default.removeObserver(self)
  139. }
  140. override func viewDidLayoutSubviews() {
  141. super .viewDidLayoutSubviews()
  142. if let indexPath = self.selectedIndexPath {
  143. // log.debug("当前的IndexPath、\(indexPath)")
  144. self.collectionView?.scrollToItem(at: indexPath, at: .centeredVertically, animated: true)
  145. }
  146. }
  147. override func viewDidAppear(_ animated: Bool) {
  148. super .viewDidAppear(animated)
  149. if let indexPath = self.selectedIndexPath {
  150. // log.debug("当前的IndexPath、\(indexPath)")
  151. if indexPath.row == 1 {
  152. let cell = self.collectionView?.cellForItem(at: indexPath) as! SBTParamSetSwitch_Item
  153. cell.sb_selected = true
  154. }else if indexPath.row == 14{
  155. let cell = self.collectionView?.cellForItem(at: indexPath) as! SBTParamSet_balance_Item
  156. cell.sb_becameFirstResonder()
  157. cell.sb_selected = true
  158. }else{
  159. let cell = self.collectionView?.cellForItem(at: indexPath) as! SBTParamSet_Item
  160. cell.sb_selected = true
  161. cell.subTitleTextfield.becomeFirstResponder()
  162. }
  163. }
  164. }
  165. @IBAction func saveAction(_ sender: Any) {
  166. if self.cmdArr?.count != 0 {
  167. // setChargerParam(cmdArr!)
  168. if isDemo == false{
  169. BabyBluetoothSwift.shareInstance()?.writeData2222(cmdArr, completionHandler: { (result) in
  170. self.completehandle!()
  171. g_showHUD("Successful".da_localizedStr())
  172. })
  173. }else{
  174. g_showHUD("Current_demo_data".da_localizedStr())
  175. }
  176. }
  177. log.debug(cmdArr!)
  178. dismiss()
  179. }
  180. @IBAction func cancelAction(_ sender: Any) {
  181. dismiss()
  182. }
  183. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  184. dismiss()
  185. }
  186. }
  187. extension SBTSetChargeVCtr{
  188. //设置区别颜色
  189. @objc func notifionCationSetBatteryType() {
  190. let batteryType = AppShare.batteryType
  191. self.CellBatteryType = batteryType
  192. self.collectionView?.reloadData()
  193. }
  194. func createUI() {
  195. self.chageLabel.text = "Charger_settings".da_localizedStr()
  196. self.closeLabel.text = "Retract".da_localizedStr()
  197. self.saveBtn.setTitle("Setup".da_localizedStr(), for: .normal)
  198. self.cancelBtn.setTitle("Cancel".da_localizedStr(), for: .normal)
  199. self.backViewColor = UIColor(white: 1, alpha: 0.84)
  200. let bgLayer1 = CAGradientLayer()
  201. 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]
  202. bgLayer1.locations = [0, 1]
  203. bgLayer1.frame = self.cancelBtn.bounds
  204. bgLayer1.startPoint = CGPoint(x: 0.09, y: 0.31)
  205. bgLayer1.endPoint = CGPoint(x: 0.83, y: 0.83)
  206. self.cancelBtn.layer.addSublayer(bgLayer1)
  207. // fillCode
  208. let bgLayer2 = CAGradientLayer()
  209. 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]
  210. bgLayer2.locations = [0, 1]
  211. bgLayer2.frame = self.saveBtn.bounds
  212. bgLayer2.startPoint = CGPoint(x: 1, y: 1)
  213. bgLayer2.endPoint = CGPoint(x: 0, y: 0)
  214. self.saveBtn.layer.addSublayer(bgLayer2)
  215. let layout = UICollectionViewFlowLayout()
  216. let itemWidth = (KSCREENWIDTH - 25 * 3 ) / 2
  217. layout.itemSize = CGSize(width:itemWidth , height: 89)
  218. layout.minimumLineSpacing = 12.5 //列间距
  219. layout.minimumInteritemSpacing = 10 //行间距
  220. layout.scrollDirection = .vertical
  221. collectionView = UICollectionView(frame: CGRect(x: 25, y: 0, width: KSCREENWIDTH - 50, height: 110 * 4 ), collectionViewLayout: layout)
  222. collectionView?.backgroundColor = UIColor.white
  223. collectionView?.showsVerticalScrollIndicator = false
  224. collectionView?.showsHorizontalScrollIndicator = false
  225. // collectionView?.isScrollEnabled = false
  226. collectionView?.dataSource = self
  227. collectionView?.delegate = self
  228. collectionView?.bounces = false
  229. collectionView?.register(SBTParamSet_Item.self, forCellWithReuseIdentifier: cellid1)
  230. collectionView?.register(SBTParamSetSwitch_Item.self, forCellWithReuseIdentifier: cellid2)
  231. collectionView?.register(SBTParamSet_balance_Item.self, forCellWithReuseIdentifier: cellid3)
  232. for i in 0..<22 {
  233. if i != 1 {
  234. let identifier = "cellid_" + "\(i)"
  235. collectionView?.register(SBTParamSet_Item.self, forCellWithReuseIdentifier: identifier)
  236. }
  237. }
  238. self.contentView.addSubview(collectionView!)
  239. }
  240. }
  241. extension SBTSetChargeVCtr :UICollectionViewDelegate,UICollectionViewDataSource{
  242. // 判断输入的字符串是否为数字,不含其它字符
  243. func isPurnInt(string:String) -> Bool {
  244. let scan : Scanner = Scanner(string: string)
  245. var val : Int = 0
  246. return scan.scanInt(&val) && scan.isAtEnd
  247. }
  248. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  249. return self.dataSources!.count
  250. }
  251. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  252. // log.debug(difeffrentUIArr)
  253. if indexPath.row == 1 {
  254. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellid2, for: indexPath) as! SBTParamSetSwitch_Item
  255. cell.differenceUI = self.difeffrentUIArr![indexPath.row]
  256. cell.canEdit = canEdit
  257. cell.title = itemTitles[indexPath.row]
  258. cell.sb_selected = sb_SelectedArr![indexPath.row]
  259. let cmd = dataSources![indexPath.row]
  260. if cmd == "开" || cmd == "On"{
  261. cell.isOn = true
  262. }else if cmd == "关" || cmd == "Off"{
  263. cell.isOn = false
  264. }
  265. cell.backgroundColor = UIColor.white
  266. cell.switchCallback = {(isOn) in
  267. var switchStr = ""
  268. if isOn == true{
  269. switchStr = "0001"
  270. }else{
  271. switchStr = "0000"
  272. }
  273. let switchCMD = "FF06DF00" + switchStr
  274. self.cmdArr?.append(switchCMD)
  275. }
  276. return cell
  277. }else if indexPath.row == 14{
  278. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellid3, for: indexPath) as! SBTParamSet_balance_Item
  279. cell.differenceUI = self.difeffrentUIArr![14]
  280. cell.title = itemTitles[indexPath.row]
  281. cell.sb_selected = sb_SelectedArr![indexPath.row]
  282. cell.canEdit = canEdit
  283. cell.subTitle = dataSources![indexPath.row]
  284. cell.backgroundColor = UIColor.white
  285. cell.balanceTypeCallback = {(balanceType) in
  286. //FF06E00B0087;
  287. if balanceType != -1{
  288. var balance = ""
  289. if balanceType == 0{
  290. balance = "0000"
  291. }else if balanceType == 1{
  292. balance = "0001"
  293. }
  294. let balanceCMD = "FF06DF0D" + balance
  295. self.cmdArr?.append(balanceCMD)
  296. }
  297. }
  298. return cell
  299. }
  300. else{
  301. let identifier = "cellid_" + "\(indexPath.row)"
  302. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath) as! SBTParamSet_Item
  303. cell.title = itemTitles[indexPath.row]
  304. cell.differenceUI = self.difeffrentUIArr![indexPath.row]
  305. cell.sb_selected = sb_SelectedArr![indexPath.row]
  306. cell.subTitle = dataSources![indexPath.row];
  307. cell.canEdit = canEdit
  308. cell.backgroundColor = UIColor.white
  309. if indexPath.row == 0 { // 最大充电电流
  310. cell.itemKeyboardType = .decimalPad
  311. }else if indexPath.row == 2{
  312. cell.itemKeyboardType = .numberPad
  313. }else if indexPath.row == 3{
  314. cell.itemKeyboardType = .numbersAndPunctuation
  315. }else if indexPath.row == 4{ // 超压电压
  316. cell.itemKeyboardType = .decimalPad
  317. }else if indexPath.row == 5{ //充电限制电压
  318. cell.itemKeyboardType = .decimalPad
  319. }else if indexPath.row == 6{ // 提升充电电压
  320. cell.itemKeyboardType = .decimalPad
  321. // cell.ChangeCharactersCallback = { (value) in
  322. // log.debug("返回的数据 --- \(value)")
  323. //
  324. // }
  325. }else if indexPath.row == 7{ // 浮充充电电压
  326. cell.itemKeyboardType = .decimalPad
  327. }else if indexPath.row == 8{ // 提升返回电压
  328. cell.itemKeyboardType = .decimalPad
  329. }else if indexPath.row == 9{ // 提升充电时间
  330. cell.itemKeyboardType = .numberPad
  331. }else if indexPath.row == 10{ // 温度补偿系数
  332. cell.itemKeyboardType = .numberPad
  333. }else if indexPath.row == 11{ // 均衡充电电压
  334. cell.itemKeyboardType = .decimalPad
  335. }else if indexPath.row == 12{ // 均衡充电时间
  336. cell.itemKeyboardType = .numberPad
  337. }
  338. else if indexPath.row == 13{ // 均衡充电间隔
  339. cell.itemKeyboardType = .numberPad
  340. }else if indexPath.row == 15{ // 过放返回电压
  341. cell.itemKeyboardType = .decimalPad
  342. }else if indexPath.row == 16{ // 欠压警告电压
  343. cell.itemKeyboardType = .decimalPad
  344. }else if indexPath.row == 17{ //过放电压
  345. cell.itemKeyboardType = .decimalPad
  346. }else if indexPath.row == 18{ //放电限制电压
  347. cell.itemKeyboardType = .decimalPad
  348. }else if indexPath.row == 19{ //过放延时时间
  349. cell.itemKeyboardType = .numberPad
  350. }else if indexPath.row == 20{ //放电上限温度
  351. cell.itemKeyboardType = .numberPad
  352. }else if indexPath.row == 21{ //放电下限温度
  353. cell.itemKeyboardType = .numbersAndPunctuation
  354. }
  355. cell.resultCallback = {(result,textfield) in
  356. // self.cmdArr?.removeAll()
  357. if indexPath.row == 0 {// 最大充电电流 0-255
  358. let result_Double = Double(result)
  359. let maxstr = String(format: "%.2f", Double(self.IchageRage!) as! CVarArg)
  360. let max = Double(maxstr)! * 100.0
  361. if result_Double! < 0 || result_Double! > max {
  362. self.g_toast("Maximum_current_range".da_localizedStr() + "\(max)A")
  363. textfield.text = ""
  364. return
  365. }else{
  366. //let MaxVoltInt = Int(self.MaxVoltage!)
  367. let result_int = Int(result_Double! * 100) //最后要转换为整形
  368. let cmd = String(format: "FF06E001%04X" ,result_int) // 转化为十六进制
  369. self.cmdArr?.append(cmd)
  370. }
  371. }else if indexPath.row == 2{ // 充电上限温度
  372. var result_Int = Int(result)
  373. let min = 30
  374. let max = 120
  375. var msg = "Charge_upper_temperature_range".da_localizedStr()
  376. if self.istemp == "0" {//摄氏度转华氏
  377. let result2 = result.fahrenheitToCentigrade_int()
  378. result_Int = Int(result2)
  379. msg = "Charge_upper_temperature_range_F".da_localizedStr()
  380. }
  381. if result_Int! < min || result_Int! > max {
  382. self.g_toast(msg)
  383. textfield.text = ""
  384. return
  385. }else{
  386. let cmd = String(format: "FF06E015%04X", result_Int!) // 转化为十六进制
  387. self.cmdArr?.append(cmd)
  388. }
  389. }else if indexPath.row == 3{ // 充电下限温度
  390. if self.isPurnInt(string: result) == false{
  391. self.g_toast("Please_enter_the_correct_number".da_localizedStr())
  392. return
  393. }
  394. if result.contains(".") == true{
  395. self.g_toast("Cannot_enter_decimals".da_localizedStr())
  396. return
  397. }
  398. var result_Int = Int(result)
  399. var min = -40
  400. var max = 20
  401. var zero = 0
  402. var msg = "Lower_charging_temperature_range".da_localizedStr()
  403. if self.istemp == "0" {//华氏度转摄氏度
  404. msg = "Lower_charging_temperature_range_F".da_localizedStr()
  405. let result2 = result.fahrenheitToCentigrade_int()
  406. result_Int = Int(result2)
  407. }
  408. if result_Int! < min || result_Int! > max {
  409. self.g_toast(msg)
  410. textfield.text = ""
  411. return
  412. }else{
  413. var cmd = ""
  414. if result_Int! >= min && result_Int! < zero {
  415. //判断负数
  416. let res = "\(result_Int!)"
  417. let resutA = res.subString(start: 1)
  418. let resubB = Int(resutA)
  419. let resubC = resubB! | 128
  420. cmd = String(format: "FF06E016%04X", resubC) // 转化为十六进制
  421. }else{
  422. cmd = String(format: "FF06E016%04X", result_Int!) // 转化为十六进制
  423. }
  424. self.cmdArr?.append(cmd)
  425. }
  426. }else if indexPath.row == 4{ //超压电压 *10
  427. let result_float = Double(result)
  428. if result_float! < 7.0 * Double(self.Multiple!) || result_float! > 17.0 * Double(self.Multiple!) {
  429. self.g_toast( "Voltage_range".da_localizedStr() + "\(7.0 * Double(self.Multiple!) )V~\(17.0 * Double(self.Multiple!))V")
  430. textfield.text = ""
  431. return
  432. }else{
  433. let reslutData = result_float! * 10 / Double(self.Multiple!)
  434. let reslutDataInt = Int(reslutData)
  435. log.debug(reslutData)
  436. let cmd = String(format: "FF06E005%04X", reslutDataInt) // 转化为十六进制
  437. log.debug("指令 -- \(cmd)")
  438. self.cmdArr?.append(cmd)
  439. }
  440. }else if indexPath.row == 5{ //充电限制电压 *10
  441. let result_float = Double(result)
  442. if result_float! < 7.0 * Double(self.Multiple!) || result_float! > 17.0 * Double(self.Multiple!) {
  443. self.g_toast("Voltage_range".da_localizedStr() + "\(7.0 * Double(self.Multiple!) )V~\(17.0 * Double(self.Multiple!))V")
  444. textfield.text = ""
  445. return
  446. }else{
  447. let reslutData = result_float! * 10 / Double(self.Multiple!)
  448. let reslutDataInt = Int(reslutData)
  449. let cmd = String(format: "FF06E006%04X", reslutDataInt) // 转化为十六进制
  450. self.cmdArr?.append(cmd)
  451. }
  452. }else if indexPath.row == 6{ // 提升充电电压 *10
  453. let result_float = Double(result)
  454. if result_float! < 7.0 * Double(self.Multiple!) || result_float! > 17.0 * Double(self.Multiple!) {
  455. self.g_toast("Voltage_range".da_localizedStr() + "\(7.0 * Double(self.Multiple!) )V~\(17.0 * Double(self.Multiple!))V")
  456. textfield.text = ""
  457. return
  458. }else{
  459. if self.CellBatteryType == BatteryType.LI {
  460. //修改界面的值
  461. let importV = Float(result)
  462. self.dataSources![4] = String(format: "%.1f", importV! + 2.0) //超压电压
  463. self.dataSources![5] = String(format: "%.1f", importV! + 1.1) //充电限制电压
  464. self.dataSources![6] = String(format: "%.1f", importV! )
  465. self.dataSources![7] = String(format: "%.1f", importV! ) // 浮充充电电压
  466. self.dataSources![8] = String(format: "%.1f", importV! - 1.2) //提升返回电压
  467. self.dataSources![11] = String(format: "%.1f", importV!) //均衡充电电压
  468. self.collectionView?.reloadData()
  469. //添加数据 4
  470. let reslutData4 = ( result_float! + 2.0 ) * 10 / Double(self.Multiple!)
  471. let reslutDataInt4 = Int(reslutData4)
  472. log.debug(reslutData4)
  473. let cmd4 = String(format: "FF06E005%04X", reslutDataInt4) // 转化为十六进制
  474. log.debug("指令 -- \(cmd4)")
  475. self.cmdArr?.append(cmd4)
  476. //5
  477. let reslutData5 = (result_float! + 1.1) * 10 / Double(self.Multiple!)
  478. let reslutDataInt5 = Int(reslutData5)
  479. let cmd5 = String(format: "FF06E006%04X", reslutDataInt5) // 转化为十六进制
  480. self.cmdArr?.append(cmd5)
  481. //
  482. // //7
  483. //
  484. let reslutData7 = result_float! * 10 / Double(self.Multiple!)
  485. let reslutDataInt7 = Int(reslutData7)
  486. let cmd7 = String(format: "FF06E009%04X", reslutDataInt7) // 转化为十六进制
  487. self.cmdArr?.append(cmd7)
  488. //8
  489. let reslutData8 = (result_float! - 1.2) * 10 / Double(self.Multiple!)
  490. let reslutDataInt8 = Int(reslutData8)
  491. let cmd8 = String(format: "FF06E00A%04X", reslutDataInt8) // 转化为十六进制
  492. self.cmdArr?.append(cmd8)
  493. //11
  494. let reslutData11 = result_float! * 10 / Double(self.Multiple!)
  495. let reslutDataInt11 = Int(reslutData11)
  496. let cmd11 = String(format: "FF06E007%04X", reslutDataInt11) // 转化为十六进制
  497. self.cmdArr?.append(cmd11)
  498. }
  499. //6
  500. let reslutData = result_float! * 10 / Double(self.Multiple!)
  501. let reslutDataInt = Int(reslutData)
  502. let cmd = String(format: "FF06E008%04X", reslutDataInt) // 转化为十六进制
  503. self.cmdArr?.append(cmd)
  504. }
  505. }else if indexPath.row == 7{ // 浮充充电电压 *10
  506. let result_float = Double(result)
  507. if result_float! < 7.0 * Double(self.Multiple!) || result_float! > 17.0 * Double(self.Multiple!) {
  508. self.g_toast("Voltage_range".da_localizedStr() + "\(7.0 * Double(self.Multiple!) )V~\(17.0 * Double(self.Multiple!))V")
  509. textfield.text = ""
  510. return
  511. }else{
  512. let reslutData = result_float! * 10 / Double(self.Multiple!)
  513. let reslutDataInt = Int(reslutData)
  514. let cmd = String(format: "FF06E009%04X", reslutDataInt) // 转化为十六进制
  515. self.cmdArr?.append(cmd)
  516. }
  517. }else if indexPath.row == 8{ // 提升充电返回电压 *10
  518. let result_float = Double(result)
  519. if result_float! < 7.0 * Double(self.Multiple!) || result_float! > 17.0 * Double(self.Multiple!) {
  520. self.g_toast("Voltage_range".da_localizedStr() + "\(7.0 * Double(self.Multiple!) )V~\(17.0 * Double(self.Multiple!))V")
  521. textfield.text = ""
  522. return
  523. }else{
  524. let reslutData = result_float! * 10 / Double(self.Multiple!)
  525. let reslutDataInt = Int(reslutData)
  526. let cmd = String(format: "FF06E00A%04X", reslutDataInt) // 转化为十六进制
  527. self.cmdArr?.append(cmd)
  528. }
  529. }else if indexPath.row == 9{ // 提升充电时间
  530. let result_Int = Int(result)
  531. if result_Int! < 10 || result_Int! > 300 {
  532. self.g_toast("Increase_charging_time_range")
  533. textfield.text = ""
  534. return
  535. }else{
  536. let cmd = String(format: "FF06E012%04X", result_Int!) // 转化为十六进制
  537. self.cmdArr?.append(cmd)
  538. }
  539. }else if indexPath.row == 10{ // 温度补偿系数
  540. // if result.contains(".") == true{
  541. // self.g_toast("不能输入小数")
  542. // return
  543. // }
  544. let result_Int = Int(result)
  545. if result_Int! > 5 {
  546. self.g_toast("Temperature_compensation_coefficient_range")
  547. textfield.text = ""
  548. return
  549. }else{
  550. // let res = result.subString(start: 1)//去掉负号
  551. if result_Int! == 0 {
  552. textfield.text = "0"
  553. let cmd = String(format: "FF06E014%04X", result_Int!) // 转化为十六进制
  554. self.cmdArr?.append(cmd)
  555. }else if result_Int! > 0 && result_Int! <= 5{
  556. textfield.text = "-" + result
  557. let cmd = String(format: "FF06E014%04X", result_Int!) // 转化为十六进制
  558. self.cmdArr?.append(cmd)
  559. }else{
  560. textfield.text = ""
  561. return
  562. }
  563. }
  564. }else if indexPath.row == 11{ // 均衡充电电压 *10
  565. let result_float = Double(result)
  566. if result_float! < 7.0 * Double(self.Multiple!) || result_float! > 17.0 * Double(self.Multiple!) {
  567. self.g_toast("Voltage_range".da_localizedStr() + "\(7.0 * Double(self.Multiple!) )V~\(17.0 * Double(self.Multiple!))V")
  568. textfield.text = ""
  569. return
  570. }else{
  571. let reslutData = result_float! * 10 / Double(self.Multiple!)
  572. let reslutDataInt = Int(reslutData)
  573. let cmd = String(format: "FF06E007%04X", reslutDataInt) // 转化为十六进制
  574. self.cmdArr?.append(cmd)
  575. }
  576. }else if indexPath.row == 12{ // 均衡充电时间
  577. let result_Int = Int(result)
  578. if result_Int! < 0 || result_Int! > 300 {
  579. self.g_toast("Balanced_charging_time_range".da_localizedStr())
  580. textfield.text = ""
  581. return
  582. }else{
  583. let cmd = String(format: "FF06E011%04X", result_Int!) // 转化为十六进制
  584. self.cmdArr?.append(cmd)
  585. }
  586. }else if indexPath.row == 13{ // 自动均衡
  587. let result_Int = Int(result)
  588. if result_Int! < 0 || result_Int! > 255 {
  589. self.g_toast("Automatic_equalization_range".da_localizedStr())
  590. textfield.text = ""
  591. return
  592. }else{
  593. let cmd = String(format: "FF06E013%04X", result_Int!) // 转化为十六进制
  594. self.cmdArr?.append(cmd)
  595. }
  596. }else if indexPath.row == 15{ // 过放返回电压 *10
  597. let result_float = Double(result)
  598. if result_float! < 7.0 * Double(self.Multiple!) || result_float! > 17.0 * Double(self.Multiple!) {
  599. self.g_toast("Voltage_range".da_localizedStr() + "\(7.0 * Double(self.Multiple!) )V~\(17.0 * Double(self.Multiple!))V")
  600. textfield.text = ""
  601. return
  602. }else{
  603. let reslutData = result_float! * 10 / Double(self.Multiple!)
  604. let reslutDataInt = Int(reslutData)
  605. let cmd = String(format: "FF06E00B%04X", reslutDataInt) // 转化为十六进制
  606. self.cmdArr?.append(cmd)
  607. }
  608. }else if indexPath.row == 16{ // 欠压警告电压 *10
  609. let result_float = Double(result)
  610. if result_float! < 7.0 * Double(self.Multiple!) || result_float! > 17.0 * Double(self.Multiple!) {
  611. self.g_toast("Voltage_range".da_localizedStr() + "\(7.0 * Double(self.Multiple!) )V~\(17.0 * Double(self.Multiple!))V")
  612. textfield.text = ""
  613. return
  614. }else{
  615. let reslutData = result_float! * 10 / Double(self.Multiple!)
  616. let reslutDataInt = Int(reslutData)
  617. let cmd = String(format: "FF06E00C%04X", reslutDataInt) // 转化为十六进制
  618. log.debug(cmd)
  619. self.cmdArr?.append(cmd)
  620. }
  621. }else if indexPath.row == 17{ // 过放电压 *10
  622. let result_float = Double(result)
  623. if result_float! < 7.0 * Double(self.Multiple!) || result_float! > 17.0 * Double(self.Multiple!) {
  624. self.g_toast("Voltage_range".da_localizedStr() + "\(7.0 * Double(self.Multiple!) )V~\(17.0 * Double(self.Multiple!))V")
  625. textfield.text = ""
  626. return
  627. }else{
  628. if self.CellBatteryType == BatteryType.LI {
  629. //修改界面的值
  630. let overV = Float(result)
  631. self.dataSources![16] = String(format: "%.1f", overV! + 1.0) //欠压警告电压
  632. self.dataSources![17] = String(format: "%.1f", overV! ) //过放电压
  633. self.dataSources![18] = String(format: "%.1f", overV! - 0.5)
  634. //放点截止电压
  635. self.collectionView?.reloadData()
  636. //16
  637. let reslutData16 = (result_float! + 1.0) * 10 / Double(self.Multiple!)
  638. let reslutDataInt16 = Int(reslutData16)
  639. let cmd16 = String(format: "FF06E00C%04X", reslutDataInt16) // 转化为十六进制
  640. log.debug(cmd16)
  641. self.cmdArr?.append(cmd16)
  642. //18
  643. let reslutData18 = (result_float! - 0.5) * 10 / Double(self.Multiple!)
  644. let reslutDataInt18 = Int(reslutData18)
  645. let cmd18 = String(format: "FF06E00E%04X", reslutDataInt18) // 转化为十六进制
  646. self.cmdArr?.append(cmd18)
  647. }
  648. //18
  649. let reslutData = result_float! * 10 / Double(self.Multiple!)
  650. let reslutDataInt = Int(reslutData)
  651. let cmd = String(format: "FF06E00D%04X", reslutDataInt) // 转化为十六进制
  652. self.cmdArr?.append(cmd)
  653. }
  654. }else if indexPath.row == 18{ // 放电限制电压 *10
  655. let result_float = Double(result)
  656. if result_float! < 7.0 * Double(self.Multiple!) || result_float! > 17.0 * Double(self.Multiple!) {
  657. self.g_toast("Voltage_range".da_localizedStr() + "\(7.0 * Double(self.Multiple!) )V~\(17.0 * Double(self.Multiple!))V")
  658. textfield.text = ""
  659. return
  660. }else{
  661. let reslutData = result_float! * 10 / Double(self.Multiple!)
  662. let reslutDataInt = Int(reslutData)
  663. let cmd = String(format: "FF06E00E%04X", reslutDataInt) // 转化为十六进制
  664. self.cmdArr?.append(cmd)
  665. }
  666. }else if indexPath.row == 19{ // 过放延时d时间
  667. let result_Int = Int(result)
  668. if result_Int! < 0 || result_Int! > 120 {
  669. self.g_toast("discharge_delay_time_range".da_localizedStr())
  670. textfield.text = ""
  671. return
  672. }else{
  673. let cmd = String(format: "FF06E010%04X", result_Int!) // 转化为十六进制
  674. self.cmdArr?.append(cmd)
  675. }
  676. }else if indexPath.row == 20{ //放电上限温度
  677. // let result_Int = Int(result)
  678. var result_Int = Int(result)
  679. let min = 30
  680. let max = 120
  681. var msg = "Upper_discharge_temperature_range".da_localizedStr()
  682. if self.istemp == "0" {//华氏度转摄氏度
  683. msg = "Upper_discharge_temperature_range_F".da_localizedStr()
  684. let result2 = result.fahrenheitToCentigrade_int()
  685. result_Int = Int(result2)
  686. }
  687. if result_Int! <= min || result_Int! >= max {
  688. self.g_toast(msg)
  689. textfield.text = ""
  690. return
  691. }else{
  692. let cmd = String(format: "FF06E017%04X", result_Int!) // 转化为十六进制
  693. self.cmdArr?.append(cmd)
  694. }
  695. }else if indexPath.row == 21{ //放电下限温度,
  696. if self.isPurnInt(string: result) == false{
  697. self.g_toast("Please_enter_the_correct_number".da_localizedStr())
  698. return
  699. }
  700. if result.contains(".") == true{
  701. self.g_toast("Cannot_enter_decimals".da_localizedStr())
  702. return
  703. }
  704. var result_Int = Int(result)
  705. let min = -40
  706. let max = 20
  707. let zero = 0
  708. var msg = "Lower_discharge_temperature_range".da_localizedStr()
  709. if self.istemp == "0" {//摄氏度
  710. msg = "Lower_discharge_temperature_range_F".da_localizedStr()
  711. let result2 = result.fahrenheitToCentigrade_int()
  712. result_Int = Int(result2)
  713. }
  714. if result_Int! < min || result_Int! > max {
  715. self.g_toast(msg)
  716. textfield.text = ""
  717. return
  718. }else{
  719. var cmd = ""
  720. if result_Int! >= min && result_Int! < zero {
  721. //判断负数
  722. let res = "\(result_Int!)"
  723. let resutA = res.subString(start: 1)
  724. let resubB = Int(resutA)
  725. let resubC = resubB! | 128
  726. cmd = String(format: "FF06E018%04X", resubC) // 转化为十六进制
  727. }else{ // 0~20
  728. cmd = String(format: "FF06E018%04X", result_Int!) // 转化为十六进制
  729. }
  730. self.cmdArr?.append(cmd)
  731. }
  732. }
  733. }
  734. return cell
  735. }
  736. }
  737. func collectionView(_ collectionView: UICollectionView, didSelectItemAt
  738. indexPath: IndexPath) {
  739. // for i in 0..<22 {
  740. // if i == indexPath.row{
  741. // // sb_SelectedArr![i] = true
  742. // if indexPath.row == 1 {
  743. //
  744. // }else if indexPath.row == 14{
  745. //
  746. //
  747. // }else{
  748. // let cell = self.collectionView?.cellForItem(at: indexPath) as! SBTParamSet_Item
  749. //
  750. // cell.sb_selected = true
  751. // }
  752. // self.collectionView?.reloadData()
  753. //
  754. //
  755. // }else{
  756. // // sb_SelectedArr![i] = false
  757. // if indexPath.row == 1 {
  758. //
  759. // }else if indexPath.row == 14{
  760. //
  761. //
  762. // }else{
  763. // let cell = self.collectionView?.cellForItem(at: indexPath) as! SBTParamSet_Item
  764. //
  765. // cell.sb_selected = false
  766. // }
  767. // self.collectionView?.reloadData()
  768. // }
  769. // }
  770. //
  771. //
  772. // log.debug("当前的IndexPath、\(indexPath)")
  773. //
  774. }
  775. //数据范围
  776. func juDageDataRange(_ indexPath :IndexPath ,result :String) {
  777. }
  778. }