// // SBTSetBatteryVCtr.swift // SolarBT // // Created by weclouds on 2019/3/20. // Copyright © 2019 weclouds. All rights reserved. // import UIKit import SwiftPopup class SBTSetBatteryVCtr: SwiftPopup { var batRateVolt : String? var isAuto : Bool? = false var isDemo :Bool? var peripheralName :String? var batStr :String? var batteryDict : [String:String]? = [String:String]() var selectedIndex : Int? //完成回调 var completehandle:((String?)->Void)? var maxVolt :String? var voltageStr : String?{ didSet{ } } var batteryTypeStr :String?{ didSet{ } } var selectedVoltage : Int? = -1 var selectedBatteyType: Int? = -1 var cmdArr : [String]? = [String]() @IBOutlet weak var batterySettingLabel: UILabel! @IBOutlet weak var batteryTypeLabel: UILabel! @IBOutlet weak var systemVoltageLabel: UILabel! @IBOutlet weak var saveBtn: UIButton! @IBOutlet weak var cancelBtn: UIButton! @IBOutlet weak var systemVoltageBox: UIView! @IBOutlet weak var batteryTapeBox: UIView! @IBOutlet weak var closeLabel: UILabel! @IBOutlet weak var batteryType: UILabel! @IBOutlet weak var systemVoltage: UILabel! override func viewDidLoad() { super.viewDidLoad() createUI() addGesture() // Do any additional setup after loading the view. } override func viewDidAppear(_ animated: Bool) { super .viewDidAppear(animated) if self.selectedIndex == 0 { valtageTap() }else if selectedIndex == 1{ batteryTap() } } //00 override func touchesBegan(_ touches: Set, with event: UIEvent?) { // dismiss() } @IBAction func saveAction(_ sender: Any) { //~/Library/Logs/DiagnosticReports and has a prefix 'lldb-rpc-server if self.isDemo == false { // cmdArr?.removeAll() var oldVolt = "00" if self.batRateVolt == "Auto" || self.batRateVolt == "自动识别"{ oldVolt = "00" }else{ oldVolt = String(format: "%02X", Int(self.batRateVolt!)!) } //判断输入 if selectedVoltage != -1 { // // let key = DeviceDictKey() var voltage = "" // 设置高八位 if batteryType.text == "锂LI" || batteryType.text == "Lithium LI" { if self.maxVolt == "12" { // valtages = ["12V"] switch selectedVoltage { case 0: // 12 voltage = "0C" + oldVolt default: break } }else if self.maxVolt == "24" { // valtages = ["12V","24V"] switch selectedVoltage { case 0: // 12 voltage = "0C" + oldVolt case 1: //24 voltage = "18" + oldVolt default: break } }else if self.maxVolt == "36" { //valtages = ["12V","24V","36V"] switch selectedVoltage { case 0: // 12 voltage = "0C" + oldVolt case 1: //24 voltage = "18" + oldVolt case 2: // 36 voltage = "24" + oldVolt default: break } }else if self.maxVolt == "48" { // valtages = ["12V","24V","36V","48V"] switch selectedVoltage { case 0: // 12 voltage = "0C" + oldVolt case 1: //24 voltage = "18" + oldVolt case 2: // 36 voltage = "24" + oldVolt case 3: //48 voltage = "30" + oldVolt default: break } }else if self.maxVolt == "60" { //valtages = ["12V","24V","36V","48V","60V"] switch selectedVoltage { case 0: // 12 voltage = "0C" + oldVolt case 1: //24 voltage = "18" + oldVolt case 2: // 36 voltage = "24" + oldVolt case 3: //48 voltage = "30" + oldVolt case 4: //60 voltage = "3C" + oldVolt default: break } }else if self.maxVolt == "72" { // valtages = ["12V","24V","36V","48V","60V","72V"] switch selectedVoltage { case 0: // 12 voltage = "0C" + oldVolt case 1: //24 voltage = "18" + oldVolt case 2: // 36 voltage = "24" + oldVolt case 3: //48 voltage = "30" + oldVolt case 4: //60 voltage = "3C" + oldVolt case 5: //72 voltage = "48" + oldVolt default: break } }else if self.maxVolt == "96" { // valtages = ["12V","24V","36V","48V","60V","72V","96V"] switch selectedVoltage { case 0: // 12 voltage = "0C" + oldVolt case 1: //24 voltage = "18" + oldVolt case 2: // 36 voltage = "24" + oldVolt case 3: //48 voltage = "30" + oldVolt case 4: //60 voltage = "3C" + oldVolt case 5: //72 voltage = "48" + oldVolt case 6: //96 voltage = "60" + oldVolt default: break } } }else { if self.maxVolt == "12" { // valtages = ["12V","Auto".da_localizedStr()] switch selectedVoltage { case 0: // 12 voltage = "0C" + oldVolt case 1: //自动识别 voltage = "FF" + oldVolt default: break } }else if self.maxVolt == "24" { // valtages = ["12V","24V","Auto".da_localizedStr()] switch selectedVoltage { case 0: // 12 voltage = "0C" + oldVolt case 1: //24 voltage = "18" + oldVolt case 2: // 自动识别 voltage = "FF" + oldVolt default: break } }else if self.maxVolt == "36" { // valtages = ["12V","24V","36V","Auto".da_localizedStr()] switch selectedVoltage { case 0: // 12 voltage = "0C" + oldVolt case 1: //24 voltage = "18" + oldVolt case 2: // 36 voltage = "24" + oldVolt case 3: //自动识别 voltage = "FF" + oldVolt default: break } }else if self.maxVolt == "48" { // valtages = ["12V","24V","36V","48V","Auto".da_localizedStr()] switch selectedVoltage { case 0: // 12 voltage = "0C" + oldVolt case 1: //24 voltage = "18" + oldVolt case 2: // 36 voltage = "24" + oldVolt case 3: //48 voltage = "30" + oldVolt case 4: //自动识别 voltage = "FF" + oldVolt default: break } }else if self.maxVolt == "60" { // valtages = ["12V","24V","36V","48V","60V","Auto".da_localizedStr()] switch selectedVoltage { case 0: // 12 voltage = "0C" + oldVolt case 1: //24 voltage = "18" + oldVolt case 2: // 36 voltage = "24" + oldVolt case 3: //48 voltage = "30" + oldVolt case 4: //60 voltage = "3C" + oldVolt case 5: //自动识别 voltage = "FF" + oldVolt default: break } }else if self.maxVolt == "72" { // valtages = ["12V","24V","36V","48V","60V","72V","Auto".da_localizedStr()] switch selectedVoltage { case 0: // 12 voltage = "0C" + oldVolt case 1: //24 voltage = "18" + oldVolt case 2: // 36 voltage = "24" + oldVolt case 3: //48 voltage = "30" + oldVolt case 4: //60 voltage = "3C" + oldVolt case 5: //72 voltage = "48" + oldVolt case 6: //自动识别 voltage = "FF" + oldVolt default: break } }else if self.maxVolt == "96" { switch selectedVoltage { case 0: // 12 voltage = "0C" + oldVolt case 1: //24 voltage = "18" + oldVolt case 2: // 36 voltage = "24" + oldVolt case 3: //48 voltage = "30" + oldVolt case 4: //60 voltage = "3C" + oldVolt case 5: //72 voltage = "48" + oldVolt case 6: //96 voltage = "60" + oldVolt case 7: //自动识别 voltage = "FF" + oldVolt default: break } } } let voltageCmd = "FF06E003" + voltage cmdArr?.append(voltageCmd) } if selectedBatteyType != -1{ var batteryType = "" switch selectedBatteyType { case 0: // 自定义 batteryType = "0000" case 1: // FLD batteryType = "0001" case 2: // SLD batteryType = "0002" case 3: // GEL batteryType = "0003" case 4: // LI batteryType = "0004" default: break } let voltageCmd = "FF06E004" + batteryType cmdArr?.append(voltageCmd) } if cmdArr?.count == 0 { g_toast("No_data_update".da_localizedStr()) dismiss() } if self.cmdArr?.count == 1 { if self.cmdArr?.first?.hasPrefix("FF06E004") == true { if self.isAuto == true { let voltage1 = "18" + oldVolt let voltageCmd1 = "FF06E003" + oldVolt cmdArr?.append(voltageCmd1) } } } log.debug(" 发送的指令 --- \(self.cmdArr)") // if isDemo == false{ BabyBluetoothSwift.shareInstance()?.writeData2222(cmdArr, completionHandler: { (result) in NotificationCenter.default.post(name: NSNotification.Name(kNotifionCationSetBatteryType), object: nil) if self.batteryDict?.isEmpty == false{ NotificationCenter.default.post(name: NSNotification.Name(rawValue: KNotifitionToChangeBatteryType), object: nil, userInfo: self.batteryDict) } self.completehandle!(self.batStr) g_showHUD("Successful".da_localizedStr()) }) }else{ g_showHUD("Current_demo_data".da_localizedStr()) } dismiss() } @IBAction func cancelAction(_ sender: Any) { dismiss() } } extension SBTSetBatteryVCtr{ func addGesture() { closeLabel.isUserInteractionEnabled = true let tap1 = UITapGestureRecognizer(target: self, action: #selector(valtageTap)) let tap2 = UITapGestureRecognizer(target: self, action: #selector(batteryTap)) let tap3 = UITapGestureRecognizer(target: self, action: #selector(closeTap)) systemVoltageBox.addGestureRecognizer(tap1) batteryTapeBox.addGestureRecognizer(tap2) closeLabel.addGestureRecognizer(tap3) } @objc func valtageTap() { log.debug("点击电压") var valtages = [String]() if batteryType.text == "锂LI" || batteryType.text == "Lithium LI" { if self.maxVolt == "12" { valtages = ["12V"] }else if self.maxVolt == "24" { valtages = ["12V","24V"] }else if self.maxVolt == "36" { valtages = ["12V","24V","36V"] }else if self.maxVolt == "48" { valtages = ["12V","24V","36V","48V"] }else if self.maxVolt == "60" { valtages = ["12V","24V","36V","48V","60V"] }else if self.maxVolt == "72" { valtages = ["12V","24V","36V","48V","60V","72V"] }else if self.maxVolt == "96" { valtages = ["12V","24V","36V","48V","60V","72V","96V"] } }else { if self.maxVolt == "12" { valtages = ["12V","Auto".da_localizedStr()] }else if self.maxVolt == "24" { valtages = ["12V","24V","Auto".da_localizedStr()] }else if self.maxVolt == "36" { valtages = ["12V","24V","36V","Auto".da_localizedStr()] }else if self.maxVolt == "48" { valtages = ["12V","24V","36V","48V","Auto".da_localizedStr()] }else if self.maxVolt == "60" { valtages = ["12V","24V","36V","48V","60V","Auto".da_localizedStr()] }else if self.maxVolt == "72" { valtages = ["12V","24V","36V","48V","60V","72V","Auto".da_localizedStr()] }else if self.maxVolt == "96" { valtages = ["12V","24V","36V","48V","60V","72V","96V","Auto".da_localizedStr()] } } // let valtages = ["12V","24V","36V","48V","60V","72V","96V","自动识别"] let valtagePick = THScrollChooseView(question: valtages, withDefaultDesc:"12V") valtagePick!.show() valtagePick?.confirmBlock = {[weak self](selectedIndex) in log.debug(valtages[selectedIndex]) self?.systemVoltage.text = valtages[selectedIndex] self?.systemVoltage.textColor = UIColor(hexString: "222222") self?.selectedVoltage = selectedIndex } } @objc func batteryTap() { log.debug("点击电池") 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()] let batteryPick = THScrollChooseView(question: batterys, withDefaultDesc:"User-defined".da_localizedStr()) batteryPick!.show() batteryPick?.confirmBlock = {[weak self](selectedIndex) in log.debug(batterys[selectedIndex]) self?.batteryType.text = batterys[selectedIndex] self?.batStr = batterys[selectedIndex] self?.batteryType.textColor = UIColor(hexString: "222222") self?.selectedBatteyType = selectedIndex switch batterys[selectedIndex] { case "自定义": AppShare.batteryType = .USER case "开口铅酸FLD": AppShare.batteryType = .FLD case "密封铅酸SLD": AppShare.batteryType = .SLD case "胶体铅酸GEL": AppShare.batteryType = .GEL case "锂LI": AppShare.batteryType = .LI case "User-defined": AppShare.batteryType = .USER case "Vented lead acid battery FLD": AppShare.batteryType = .FLD case "Lead acid battery SLD": AppShare.batteryType = .SLD case "Gel_lead_acid_GEL": AppShare.batteryType = .GEL case "Lithium iron phosphate battery LI": AppShare.batteryType = .LI default: break } let dict = ["batteryType":"\(selectedIndex)"] log.debug(" batteryType =\(dict) ") self?.batteryDict = dict } } @objc func closeTap() { dismiss() } func createUI() { self.batterySettingLabel.text = "Battery_settings".da_localizedStr() self.systemVoltageLabel.text = "System_voltage".da_localizedStr() self.batteryTypeLabel.text = "Battery_type".da_localizedStr() self.closeLabel.text = "Retract".da_localizedStr() self.saveBtn.setTitle("Setup".da_localizedStr(), for: .normal) self.cancelBtn.setTitle("Cancel".da_localizedStr(), for: .normal) self.backViewColor = UIColor(white: 1, alpha: 0.84) let bgLayer1 = CAGradientLayer() 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] bgLayer1.locations = [0, 1] bgLayer1.frame = self.cancelBtn.bounds bgLayer1.startPoint = CGPoint(x: 0.09, y: 0.31) bgLayer1.endPoint = CGPoint(x: 0.83, y: 0.83) self.cancelBtn.layer.addSublayer(bgLayer1) // fillCode let bgLayer2 = CAGradientLayer() 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] bgLayer2.locations = [0, 1] bgLayer2.frame = self.saveBtn.bounds bgLayer2.startPoint = CGPoint(x: 1, y: 1) bgLayer2.endPoint = CGPoint(x: 0, y: 0) self.saveBtn.layer.addSublayer(bgLayer2) systemVoltage.text = self.voltageStr! batteryType.text = self.batteryTypeStr } }