1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485 |
- //
- // SBTHomeVCtr.swift
- // SolarBT
- //
- // Created by weclouds on 2019/1/23.
- // Copyright © 2019 weclouds. All rights reserved.
- //
- import UIKit
- import PKHUD
- import Localize_Swift
- struct BLEData {
- var perpheralName:String?
- var device_name :String?
- var device_type :String?
- var device_model:String?
- var online: Bool? // 是否在线
- var isOld:Bool?
- }
- extension Array where Element: Hashable {
-
- func removingDuplicates() -> [Element] {
- var addedDict = [Element: Bool]()
-
- return filter {
- addedDict.updateValue(true, forKey: $0) == nil
- }
- }
-
- mutating func removeDuplicates() {
- self = self.removingDuplicates()
- }
- }
- class SBTHomeVCtr: BaseViewController {
- lazy var leftMenuVC = SBTLeftMenuVCtr()
- var snnumber :String? = "0"
- var soft :String?
- var hard: String?
- var deviceTypeNumber : String?
- var disconectBlock : (()->Void)?
- var emptyView : SBTEmptyView?
- var isCurrentVC: Bool? = true //是否当前界面
-
- var isAddDevice:Bool? = false //判断是否是添加设备状态
-
- var isStart :Bool? = true //判断是否 刚开始进入首页
-
- var isAuto : Bool? = false //判断是否自动跳转~
-
- var peripheralInfo = [String:String]()
-
- var isOld : Bool? = true // 判断是否是jiu xie yi
-
- var dataDict : [String: Any] = [String: Any]()
- let key: DeviceDictKey = DeviceDictKey()
- var historyDataArr: [[String: String]] = [[String: String]]()
-
- static let peripheralPlist = "Peripheral.plist"
- static let deviceDetailPlist = "DeviceDetailPlist.plist"
- var baseInfoPlist :CLDeviceDataPlist? = CLDeviceDataPlist() //保存基本信息
- var detailInfoPlist : CLDeviceDataPlist? = CLDeviceDataPlist()// 详细信息
- var RootDict : [String:Any]? = [String:Any]()
- var tempArr :[BLEData]? = [BLEData]()
- lazy var device = DeviceData()
- var dataDictionary : [String : String]?
- var isOnline :Bool? {
- didSet{
-
- }
- }
-
- var isReconnect : Bool? = false {
- didSet{
-
- }
- }
- lazy var tableView: UITableView = {
- let tableView = UITableView(frame: CGRect(x: 0, y: 0, width: KSCREENWIDTH, height: KSCREENHEIGHT - KNavBarHeight), style: .plain)
- tableView.backgroundColor = UIColor(hexString: "F8F8F8")
- tableView.delegate = self
- tableView.dataSource = self
- tableView.separatorStyle = .none
- tableView.register(UINib(nibName: "SBTHomeCell", bundle: nil), forCellReuseIdentifier: "cell")
- return tableView
- }()
-
- var aPeripherals:[BLEData]?
-
- var baby1:BabyBluetooth?
-
- let deviceAlertVC = SBTAddDeviceVCtr()
-
- var editingIndexPath : IndexPath?
-
- var isConnectBtn :Bool?
-
- let header = SBTHomeTableViewHeader()
- }
- extension SBTHomeVCtr {
- func registGestureShowSide() {
-
- gy_registGestureShowSide { (direction) in
- if direction == .left {
- self.gy_showSide(configuration: { (config) in
- //config.animationType = .
- config.sideRelative = 0.5
- }, viewController: SBTLeftMenuVCtr())
- }
- }
- }
- func createUI() {
- //手势冲突
- //registGestureShowSide()
- setNavgationBar()
- let rightBtn = UIBarButtonItem.createBarbuttonItem(image: "添加", highImage: "添加", target: self, action: #selector(addDevice))
- self.navigationItem.rightBarButtonItem = rightBtn
-
- let leftBtn = UIBarButtonItem.createBarbuttonItem(image: "menu", highImage: "menu", target: self, action: #selector(goMenu))
- self.navigationItem.leftBarButtonItem = leftBtn
-
- self.view.addSubview(tableView)
- //设置空数据页面
- setupEmptyView()
-
- }
-
- //自定义空数据界面显示
- func setupEmptyView() {
- emptyView = Bundle.main.loadNibNamed("SBTEmptyView", owner: self, options: nil)?.last as! SBTEmptyView
- emptyView!.addButton.addTarget(self, action: #selector(addDevice), for: .touchUpInside)
- emptyView!.frame = view.bounds
- emptyView!.buttonStr = "Add_it_now".da_localizedStr()
- emptyView!.messageStr = "No_devices_are_currently_added".da_localizedStr()
- emptyView!.reloadView()
-
- //空数据界面显示
- let emptyV : HDEmptyView = HDEmptyView.emptyViewWithCustomView(customView: emptyView!) as! HDEmptyView
- tableView.ly_emptyView = emptyV
- tableView.ly_emptyView?.tapContentViewBlock = {
- print("点击界面空白处")
- }
- tableView.ly_showEmptyView()
-
- }
-
- @objc func updateEmptyViewLanguage() {
- self.emptyView!.buttonStr = "Add_it_now".da_localizedStr()
- self.emptyView!.messageStr = "No_devices_are_currently_added".da_localizedStr()
- self.emptyView!.reloadView()
- self.tableView.reloadData()
- }
- //设置按钮
- func configSwipeButtons() {
- //iOS 11 层级 UITableView -> UISwipActionPull
- if #available(iOS 11, *) {
- for subview in tableView.subviews{
- if subview.isKind(of: NSClassFromString("UISwipeActionPullView")!) && subview.subviews.count >= 2 {
- let deleteButton = subview.subviews[1] as!UIButton
- let connectButton = subview.subviews[0] as!UIButton
- configDelegateButton(deleteButton)
- configConnectButton(connectButton)
- }else if subview.isKind(of: NSClassFromString("UISwipeActionPullView")!) && subview.subviews.count == 1 {
-
- let connectButton = subview.subviews[0] as!UIButton
- configDisConnectButton(connectButton)
- }
- }
- }else{
- //iOS 8-10: 画UITableView->UITableViewCell->UITableVIewCellDeleteConfirmationView
- let tabCell = tableView.cellForRow(at: self.editingIndexPath!)
- for subview in (tabCell?.subviews)!{
- if subview.isKind(of: NSClassFromString("UITableViewCellDeleteConfirmationView")!) && subview.subviews.count >= 2{
- let deleteButton = subview.subviews[0] as!UIButton
- let connectButton = subview.subviews[1] as!UIButton
- configDelegateButton(deleteButton)
- configConnectButton(connectButton)
- }else if subview.isKind(of: NSClassFromString("UITableViewCellDeleteConfirmationView")!) && subview.subviews.count == 1 {
-
- let connectButton = subview.subviews[0] as!UIButton
-
- configDisConnectButton(connectButton)
- }
- }
- }
- }
- func configDelegateButton(_ deleteButton: UIButton) {
-
- deleteButton.setBackgroundImage(UIImage(named: "白色-删除"), for: .normal)
- deleteButton.setBackgroundImage(UIImage(named: "白色-删除"), for: .selected)
- deleteButton.backgroundColor = UIColor.white
- deleteButton.setTitle("Delete".da_localizedStr(), for: .normal)
- deleteButton.setTitleColor(UIColor(hexString: "#888888"), for: .normal)
- deleteButton.titleLabel?.font = UIFont(name: PingFangSC_Medium, size: 12)
- }
-
- func configConnectButton(_ connectButton: UIButton) {
- // fillCode
- connectButton.setBackgroundImage(UIImage(named: "绿色-重新连接"), for: .normal)
- connectButton.setBackgroundImage(UIImage(named: "绿色-重新连接"), for: .selected)
- connectButton.setTitle("Reconnect".da_localizedStr(), for: .normal)
- connectButton.setTitleColor(UIColor(hexString: "#FFFFFF"), for: .normal)
- connectButton.titleLabel?.font = UIFont(name: PingFangSC_Medium, size: 12)
- }
-
- func configDisConnectButton(_ connectButton: UIButton) {
- // fillCode
- connectButton.setBackgroundImage(UIImage(named: "橙色-断开"), for: .normal)
- connectButton.setBackgroundImage(UIImage(named: "橙色-断开"), for: .selected)
- connectButton.setTitle("Disconnect".da_localizedStr(), for: .normal)
- connectButton.setTitleColor(UIColor(hexString: "#FFFFFF"), for: .normal)
- connectButton.titleLabel?.font = UIFont(name: PingFangSC_Medium, size: 12)
- }
-
- }
- extension SBTHomeVCtr : UITableViewDelegate,UITableViewDataSource {
-
- func numberOfSections(in tableView: UITableView) -> Int {
- return 1
- }
-
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return aPeripherals!.count
- }
-
- func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
- return 0.01
- }
-
- func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
- return 86
- }
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! SBTHomeCell
- cell.backgroundColor = UIColor(hexString: "F8F8F8")
- cell.selectionStyle = .none
- cell.preservesSuperviewLayoutMargins = false
-
- let peripheral = self.aPeripherals![indexPath.row]
- cell.bleData = peripheral
- log.debug("aaaddd \(indexPath.row)---- \(peripheral.device_name)")
- return cell
- }
- func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
- if self.tableView.responds(to: #selector(setter: UITableViewCell.separatorInset)) {
- self.tableView.separatorInset = UIEdgeInsets(top: 0, left: 25, bottom: 0, right: 15)
- }
- //iOS 8 之后 // cell的间隔
- if self.tableView.responds(to: #selector(setter: UIView.layoutMargins)) {
- self.tableView.separatorInset = UIEdgeInsets(top: 0, left: 25, bottom: 0, right: 25)
- }
- }
- //iOS 11 将进入此方法 actions.performsFirstActionWithFullSwipe = NO 可以控制是否自动删除。
- func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
- let perpheral: BLEData = self.aPeripherals![indexPath.row]
- let devicename = perpheral.device_name
- let devicetype = perpheral.device_type
- let devicemodel = perpheral.device_model
- let perpheralName = perpheral.perpheralName
- let isOld = perpheral.isOld
- //拼接蓝牙名字
- let BLEName = perpheralName
-
- if perpheral.online!{
- let disconnectAction = UIContextualAction(style: .normal, title: "断开") { (action, sourceView, completionHandler) in
- completionHandler(true)
- log.debug("断开连接")
- //处理UI
- // self.aPeripherals?.remove(at: indexPath.row)
- let per2 = BLEData(perpheralName: perpheralName, device_name: devicename, device_type: devicetype, device_model: devicemodel, online: false,isOld: isOld)
- //修改元素
- self.aPeripherals![indexPath.row] = per2
- log.debug("断开连接 \(self.aPeripherals)")
- self.tableView.reloadData()
- BabyBluetoothSwift.shareInstance()?.cancelAllPeripheralsConnection()
- //重新扫描
- BabyBluetoothSwift.shareInstance()?.scanForPeripherals()
- }
- let actions = UISwipeActionsConfiguration(actions: [disconnectAction])
- return actions
- }else{
- let deleteAction = UIContextualAction(style: .normal, title: "删除") { (action, sourceView, completionHandler) in
- completionHandler(true)
- log.debug("删除")
- self.aPeripherals?.remove(at: indexPath.row)
- self.reloadTableView()
- self.isReconnect = false // 删除数据
- self.tableView.reloadData()
- let key = devicetype! + devicemodel!
- //删除缓存数据
- self.baseInfoPlist!.deleteData(SBTHomeVCtr.peripheralPlist, key: key)
- }
-
- let disconnectAction = UIContextualAction(style: .normal, title: "重新连接") { (action, sourceView, completionHandler) in
- completionHandler(true)
- // BabyBluetoothSwift.shareInstance()?.cancelAllPeripheralsConnection()
- HUD.flash(.progress)
- self.isReconnect = true
-
- if BabyBluetoothSwift.shareInstance()?.peripheralExits(atPeripheralName: BLEName) == true {
- //BabyBluetoothSwift.shareInstance()?.reconnectPeripheral(withName: BLEName)
- // BabyBluetoothSwift.shareInstance()?.connectPeripheral(withName: BLEName)
- self.aPeripherals?.remove(at: indexPath.row)
- let per2 = BLEData(perpheralName:perpheralName,device_name: devicename, device_type: devicetype, device_model: devicemodel, online: true,isOld: isOld)
- self.aPeripherals?.append(per2)
- //删除重复的
- self.tableView.reloadData()
-
- BabyBluetoothSwift.shareInstance()?.connectPeripheral(withName:perpheralName, connectCompletionHandler: { (periph, isSuccess) in
- if isSuccess == false{
- HUD.flash(.error, delay: 1)
- return
- }
- HUD.flash(.success, delay: 1)
- log.debug("连接成功 --\(periph?.name)")
- let per2 = BLEData(perpheralName:perpheralName,device_name: devicename, device_type: devicetype, device_model: devicemodel, online: true,isOld: isOld)
- //self.aPeripherals?.append(per2)
- self.aPeripherals![indexPath.row] = per2
- //删除重复的
- self.tableView.reloadRows(at: [indexPath], with: .fade)
- let bleKey = devicetype! + devicemodel!
- let alert = SBTReadLoadingVCtr(nibName: "SBTReadLoadingVCtr", bundle: nil)
- alert.deviceName = devicename
- alert.message = "Data_loading".da_localizedStr()
- alert.show()
- // alert.dismiss(7)
- self.readDeviceData(bleKey, devicename: devicename!, isOld: isOld!, peripheralName: perpheralName!, completehandle: {
- alert.dismiss()
- })
-
- })
-
-
-
-
- }else{
- }
- }
- let actions = UISwipeActionsConfiguration(actions: [deleteAction,disconnectAction])
- return actions
- }
-
- }
- ///判断 是否包含当前设备
- @objc func removeRow(_ indexPath : IndexPath) {
- self.aPeripherals?.remove(at: indexPath.row)
- self.tableView.reloadData()
- }
-
- @objc func rescan() {
- BabyBluetoothSwift.shareInstance()?.scanForPeripherals()
- }
- @objc func reconnect2(_ devicename:String) {
- Thread.sleep(forTimeInterval: 0.01)
- BabyBluetoothSwift.shareInstance()?.connectPeripheral(withName: devicename)
- log.debug("重新连接 \(aPeripherals)")
- }
- func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath)
- {
- log.debug("editingStyle")
- if editingStyle == UITableViewCell.EditingStyle.delete {
- }
- }
- //滑动删除
- func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
- return true
- }
- func tableView(_ tableView: UITableView, willBeginEditingRowAt indexPath: IndexPath) {
- self.editingIndexPath = indexPath
- view.setNeedsLayout() //出发 viewDidLayoutSubviews()
- }
- func tableView(_ tableView: UITableView, didEndEditingRowAt indexPath: IndexPath?) {
- log.debug("self.editingIndexPath -- \(self.editingIndexPath)")
- self.editingIndexPath = nil
- }
-
-
- func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
-
- if self.aPeripherals?.count == 0{ return }
-
- let model: BLEData = self.aPeripherals![indexPath.row]
- let peripheralName = model.perpheralName
- let devicename = model.device_name
- let devicetype = model.device_type
- let device_model = model.device_model
- let isOld = model.isOld
- AppShare.perihperalName = peripheralName
- if model.online == true {
- let belKey = devicetype! + device_model!
- let user = UserDefaults.standard
- //user.set(bleKey, forKey: "SavePeripheral")
- let periName = user.value(forKey: "SavePeripheral") as? String
- // if periName == peripheralName{
- // self.readPeripheraldata(belKey, deviceName: devicename!, isOld: isOld!, perihperalName: peripheralName!)
- // }else{
-
- let alert = SBTReadLoadingVCtr(nibName: "SBTReadLoadingVCtr", bundle: nil)
- alert.deviceName = " "
- alert.message = "Data_loading".da_localizedStr()
- alert.show()
- self.bleHasBeemDisconnectect {
- alert.dismiss()
- return
- }
- var isSussess = false
- self.readDeviceData(belKey, devicename: devicename!, isOld: isOld!, peripheralName: peripheralName!, completehandle: {
- alert.dismiss()
- isSussess = true
- })
-
- delay(15) {
- if isSussess == false{
- alert.dismiss()
- }
- }
- //}
-
- }else{
- if self.aPeripherals!.count > 1{
- for var mod in self.aPeripherals!{
- if mod.perpheralName != peripheralName && mod.online == true{
- BabyBluetoothSwift.shareInstance()?.cancelAllPeripheralsConnection()
- mod.online = false
- }
- }
- }
-
- if BabyBluetoothSwift.shareInstance()?.peripheralExits(atPeripheralName: peripheralName) == true {
- HUD.show(.progress)
- BabyBluetoothSwift.shareInstance()?.connectPeripheral(withName: peripheralName, connectCompletionHandler: { (periph, isSuccess) in
- if isSuccess == false {
- HUD.flash(.error, delay: 1)
- return
- }
-
- HUD.flash(.success, delay: 1)
- log.debug("连接成功 --\(periph?.name)")
- }, startWriteDataCallback: {
- self.readDeviceData(for: indexPath, peripheralName: peripheralName!, devicename: devicename!, devicetype: devicetype!, device_model: device_model!, isOld: isOld!)
- })
-
- }else{
- g_showHUD("unfoundDevice".da_localizedStr())
- }
-
- // }
- }
- }
-
-
- func readDeviceData(for indexPath:IndexPath, peripheralName:String,devicename:String,devicetype:String,device_model:String,isOld:Bool) {
- log.debug("")
- let per2 = BLEData(perpheralName:peripheralName,device_name: devicename, device_type: devicetype, device_model: device_model, online: true,isOld: isOld)
- //self.aPeripherals?.append(per2)
- self.aPeripherals![indexPath.row] = per2
- //删除重复的
- self.tableView.reloadRows(at: [indexPath], with: .fade)
- let bleKey = devicetype + device_model
- let alert = SBTReadLoadingVCtr(nibName: "SBTReadLoadingVCtr", bundle: nil)
- alert.deviceName = " "
- alert.message = "Data_loading".da_localizedStr()
- alert.show()
- self.bleHasBeemDisconnectect {
- alert.dismiss()
- return
- }
- // alert.dismiss(7)
- var isSussess = false
- self.readDeviceData(bleKey, devicename: devicename, isOld: isOld, peripheralName: peripheralName, completehandle: {
- let user = UserDefaults.standard
- user.set(peripheralName, forKey: "SavePeripheral")
- user.synchronize()
- alert.dismiss()
- isSussess = true
- })
- delay(15) {
- if isSussess == false{
- alert.dismiss()
- }
- }
- }
- func readDeviceData(_ bleKey:String,devicename:String,isOld:Bool,peripheralName:String,completehandle:@escaping (()->Void)) {
- if isOld == true{
- let arr = ["FF03000A0001","FF03000B000F","FF030100000A","FF0301200003","FF03F000000A","FF03F001000A","FF03F002000A","FF03F003000A","FF03F004000A","FF03F005000A","FF03F006000A","FF03F007000A","FF030115000B","FF03E0010001","FF03E003001E","FF03DF000001","FF03DF0D0001","FF03E0210001"]
-
- BabyBluetoothSwift.shareInstance()?.writeData2222(arr, completionHandler: { (result) in
- log.debug("返回数据 --- \(ReceiveData)")
- self.realdata_000A(result!["FF03000A0001"] as! String)
- self.real_000B000F(result!["FF03000B000F"] as! String)
- self.realdata_0100(result!["FF030100000A"] as! String)
- self.realdata_0120(result!["FF0301200003"] as! String)
- self.historyDataArr.removeAll()
- self.realdata_F000(result!["FF03F000000A"] as! String)
- self.realdata_F001(result!["FF03F001000A"] as! String)
- self.realdata_F002(result!["FF03F002000A"] as! String)
- self.realdata_F003(result!["FF03F003000A"] as! String)
- self.realdata_F004(result!["FF03F004000A"] as! String)
- self.realdata_F005(result!["FF03F005000A"] as! String);
- self.realdata_F006(result!["FF03F006000A"] as! String);
- self.realdata_F007(result!["FF03F007000A"] as! String);
- self.realdata_0115(result!["FF030115000B"] as! String)
- self.realdata_E001(result!["FF03E0010001"] as! String);
- self.realdata_E003(result!["FF03E003001E"] as! String);
- self.realdata_DF00(result!["FF03DF000001"] as! String);
- self.realdata_DF0D(result!["FF03DF0D0001"] as! String)
- self.realdata_E021(result!["FF03E0210001"] as! String)
- AppShare.dataDict = self.dataDict
- if AppShare.isRefresh == true {
- NotificationCenter.default.post(name: NSNotification.Name(kBabyBluetoothNotificationAllDataRecieve), object: nil)
- }
- //alert.dismiss()
- completehandle()
- self.readPeripheraldata(bleKey, deviceName: devicename, isOld: isOld, perihperalName: peripheralName)
-
-
- })
- }else{
- let arr = ["FF03000A0001","FF03000B000F","FF0300490020","FF0300FD000D","FF03F000000A","FF03F001000A","FF03F002000A","FF03F003000A","FF03F004000A","FF03F005000A","FF03F006000A","FF03F007000A","FF030115000B","FF03E0010001","FF03E003001E","FF03DF000001","FF03DF0D0001","FF03E0210001"]
-
- BabyBluetoothSwift.shareInstance()?.writeData2222(arr, completionHandler: { (result) in
- self.realdata_000A(result!["FF03000A0001"] as! String)
- self.real_000B000F(result!["FF03000B000F"] as! String)
- self.readdata_0049(result!["FF0300490020"] as! String)
- self.realdata_00FD(result!["FF0300FD000D"] as! String)
- self.historyDataArr.removeAll()
- self.realdata_F000(result!["FF03F000000A"] as! String)
- self.realdata_F001(result!["FF03F001000A"] as! String)
- self.realdata_F002(result!["FF03F002000A"] as! String)
- self.realdata_F003(result!["FF03F003000A"] as! String)
- self.realdata_F004(result!["FF03F004000A"] as! String)
- self.realdata_F005(result!["FF03F005000A"] as! String);
- self.realdata_F006(result!["FF03F006000A"] as! String);
- self.realdata_F007(result!["FF03F007000A"] as! String);
- self.realdata_0115(result!["FF030115000B"] as! String)
- self.realdata_E001(result!["FF03E0010001"] as! String);
- self.realdata_E003(result!["FF03E003001E"] as! String);
- self.realdata_DF00(result!["FF03DF000001"] as! String);
- self.realdata_DF0D(result!["FF03DF0D0001"] as! String)
- self.realdata_E021(result!["FF03E0210001"] as! String)
- AppShare.dataDict = self.dataDict
- if AppShare.isRefresh == true {
- NotificationCenter.default.post(name: NSNotification.Name(kBabyBluetoothNotificationAllDataRecieve), object: nil)
- }
- completehandle()
- //alert.dismiss()
- self.readPeripheraldata(bleKey, deviceName: devicename, isOld: isOld, perihperalName: peripheralName)
-
- })
- }
- }
- @objc func readPeripheral(_ paramArray:[Any]) {
- readPeripheraldata(paramArray.first! as! String, deviceName: paramArray[1] as! String, isOld: paramArray[2] as! Bool, perihperalName: paramArray.last! as!String)
- }
- @objc func readPeripheraldata(_ bleKey:String,deviceName:String,isOld:Bool,perihperalName:String) {
-
- log.debug("sjfkaefkoanmefkoam===\(dataDict)")
- AppShare.perihperalName = perihperalName
- //保存数据
- detailInfoPlist?.saveData111(SBTHomeVCtr.deviceDetailPlist, key: bleKey, deviceData: dataDict)
- let vc = SBTDeviceDetaiVCtr()
- vc.plist_node = bleKey
- vc.isOld = isOld
- let nvc = self.navigationController as! RootNavigationController
- nvc.pushViewController(vc)
-
- }
-
-
- }
- //Action 深圳市硕日新能源科技有限公司来了
- // <ff100049 004080 6df1 5733 5e02 7855 65e5 65b0 80fd 6e90 79d1 6280 6709 516c 53f8 4f60 4eec 597d c574>
- // <ff0320 6d f157335e 02785565 e565b080 fd6e9079 d1628067 09516c53 f84f604e ec000031 ff>
- extension SBTHomeVCtr{
- //点击添加设备
- @objc func addDevice() {
- self.isAuto = true
- log.debug("添加设备")
- //搜索设备
- BabyBluetoothSwift.shareInstance()?.scanForPeripherals()
- deviceAlertVC.show()
- deviceAlertVC.addDeviceLabel.text = "Add_device".da_localizedStr()
- if "Add_device".da_localizedStr() == "Add device" {
- deviceAlertVC.langua = "en"
- }else if "Add_device".da_localizedStr() == "添加设备"{
- deviceAlertVC.langua = "cn"
- }
- deviceAlertVC.reScanBtn.setTitle("Rescan".da_localizedStr(), for: .normal)
- deviceAlertVC.closeBtn.setTitle("Colse".da_localizedStr(), for: .normal)
- deviceAlertVC.completionCallback = {[weak self](peripheralname) in
- //清空数组
- self!.aPeripherals?.removeAll()
- log.debug("连接成功---\(peripheralname)")
- for mod in self!.aPeripherals! {
- let key = mod.device_type! + mod.device_model!
- //删除缓存数据
- self!.baseInfoPlist!.deleteData(SBTHomeVCtr.peripheralPlist, key: key)
- }
-
- self?.isAddDevice = true
- var model = BLEData()
- model.perpheralName = peripheralname;
- self!.tempArr?.append(model)
-
- /*检查读写状态,连接之后是否可以写*/
- // delay(3) {
- // }
- BLEConnectUtils.shared.checkWriteStatus {
- log.debug("开始读取数据")
- BabyBluetoothSwift.shareInstance()?.writeData("FF03000B000F", completionHandler: { (result) in
- if result == nil{
- log.debug("没有返回结果")
- }else{
- log.debug("返回结果为--- \(result)")
- self?.reloadDeviceUI_1(result!)
- }
- })
- }
- }
- }
-
- func reloadTableView(){
- // let header :SBTHomeTableViewHeader = self.tableView.headerView(forSection: 0)
- let total = aPeripherals?.count
- var device0 = 0
- var device1 = 0
- var device2 = 0
- for perpheral in aPeripherals! {
- // log.info("====== \(perpheral.device_type)")
- if perpheral.device_type == "0"{
- device0 += 1
- log.debug(device0)
-
- }else if perpheral.device_type == "3"{
- device1 += 1
- }else if perpheral.device_type == "5"{
- device2 += 1
- }
- }
- let deviceCount = ["\(total ?? 0)","\(device0)","\(device1)","\(device2)"]
- header.deviceCounts = deviceCount
- header.reloadDeviceCount()
- }
-
-
-
- @objc func goMenu() {
- log.debug("打开侧栏 -----")
- let conf = CWLateralSlideConfiguration.default()
- conf?.distance = 0.5
- self.cw_showDrawerViewController(leftMenuVC, animationType: .default, configuration: nil)
- }
-
-
-
- }
- extension SBTHomeVCtr{
- override func viewDidLoad() {
- super.viewDidLoad()
-
-
- checkTheBlueToothWasActivity()
- notificationRegister()
-
- baby1 = BabyBluetooth.share()
-
- self.sbtTitle = "Device_list".da_localizedStr()
- LocalizationManager.shareInstance().callback = {
- self.sbtTitle = "Device_list".da_localizedStr()
-
- }
- isConnectBtn = false
-
- self.aPeripherals = [BLEData]()
-
- createUI()
-
-
- self.view.backgroundColor = UIColor.white
- // Do any additional setup after loading the view.
- self.babyDelegate()
-
- //创建 plist文件
- baseInfoPlist!.createPlistFile(SBTHomeVCtr.peripheralPlist)
- detailInfoPlist?.createPlistFile(SBTHomeVCtr.deviceDetailPlist)
- }
- override func viewDidDisappear(_ animated: Bool) {
- super.viewDidDisappear(animated)
- self.isCurrentVC = false
- self.isStart = false
- BabyBluetoothSwift.shareInstance()?.cancelScan()
- notificationUnRegister()
- }
- override func viewDidLayoutSubviews() {
- super.viewDidLayoutSubviews()
-
-
- if self.editingIndexPath != nil {
- configSwipeButtons()
- }
-
-
- }
- override func viewDidAppear(_ animated: Bool) {
- super.viewDidAppear(animated)
- // getDevieData()
- BabyBluetoothSwift.shareInstance()?.centralBlock = { (central) in
-
- if central?.state == .poweredOn{
- log.debug("蓝牙打开")
- HUD.flash(.label("poweredOn".da_localizedStr()), delay: 1.0) { _ in
- print("License Obtained.")
- }
- }else if central?.state == .unsupported{
- log.debug("不支持蓝牙")
- g_showHUD("unsupported".da_localizedStr())
- }else if central?.state == .unknown{
- log.debug("未知状态")
- g_showHUD("unknown".da_localizedStr())
- }else if central?.state == .resetting{
- log.debug("蓝牙重启")
- g_showHUD("resetting".da_localizedStr())
- }else if central?.state == .unauthorized{
- log.debug("蓝牙未授权")
- g_showHUD("unauthorized".da_localizedStr())
- }else if central?.state == .poweredOff{
- log.debug("蓝牙已关闭")
- g_showHUD("poweredOff".da_localizedStr())
-
- if self.aPeripherals!.count > 0 {
- for i in 0..<(self.aPeripherals?.count)!{
- var model = self.aPeripherals![i]
- model.online = false
- self.aPeripherals![i] = model
- self.tableView.reloadData()
- }
- }
- }
- }
- }
-
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
-
- self.isCurrentVC = true
-
- BabyBluetoothSwift.shareInstance()?.scanForPeripherals()
-
- let dict = baseInfoPlist!.readAllPlistData(SBTHomeVCtr.peripheralPlist)
- let key = PeripheralINfo()
- // 遍历字典
- for (k , v) in dict {
- log.debug("\(k) --- \(v)")
- let dict1 : [String: String] = v as! [String : String]
- let devicename = dict1[key.DeviceName]
- let DeviceType = dict1[key.DeviceType]
- let PerpheralName = dict1[key.PerpheralName]
- let ProduceType = dict1[key.ProduceType]
- let isOldstr = dict1[key.IsOld]
- var isOld = false
- if isOldstr == "1"{
- isOld = true
- }
- let model = BLEData(perpheralName: PerpheralName, device_name: devicename, device_type: DeviceType, device_model: ProduceType, online: false,isOld: isOld)
- // if self.aPeripherals?.count == 0{ //刚开始进入这个页面 aPeripherals 为0
- if self.isStart == true{
- self.aPeripherals?.append(model)
- self.tableView.reloadData()
- }
- }
- log.debug("保存的设备数据\(dict)")
- }
- }
- extension SBTHomeVCtr{
-
- func notificationRegister() {
- NotificationCenter.default.addObserver(self, selector: #selector(changeLanguage), name: NSNotification.Name(KCHANGELANGUAGE), object: nil)
-
- NotificationCenter.default.addObserver(self, selector: #selector(notifitionTheBluetoothHasBeenDiscontect(notif:)), name: NSNotification.Name(kBabyBluetoothNotificationDeviceWasDisconect), object: nil)
-
- NotificationCenter.default.addObserver(self, selector: #selector(updateEmptyViewLanguage), name: NSNotification.Name("updateEmptyViewLanguage"), object: nil)
-
- }
-
- func notificationUnRegister() {
- // NotificationCenter.default.removeObserver(self)
- }
-
- func bleHasBeemDisconnectect(_ completeBlock : @escaping (()->Void)) {
- self.disconectBlock = completeBlock
- }
- @objc func notifitionTheBluetoothHasBeenDiscontect(notif:Notification) {
- if self.isCurrentVC == false {
- let dict = notif.userInfo as! [String: String]
- let peripheralname = dict["peripheral"]
- for (index,value) in (self.aPeripherals?.enumerated())! {
- log.debug("当前下标 -- \(index), 当前数组 -- \(value)")
- let perip : BLEData = value
- if peripheralname == perip.perpheralName{
- let model = BLEData(perpheralName: peripheralname, device_name: perip.device_name, device_type: perip.device_type, device_model: perip.device_model, online: false, isOld: perip.isOld)
- self.aPeripherals![index] = model
- self.tableView.reloadData()
- }
- }
- } else {
- let dict = notif.userInfo as! [String: String]
- let peripheralname = dict["peripheral"]
- for (index,value) in (self.aPeripherals?.enumerated())! {
- log.debug("当前下标 -- \(index), 当前数组 -- \(value)")
- let perip : BLEData = value
- if peripheralname == perip.perpheralName{
- let model = BLEData(perpheralName: peripheralname, device_name: perip.device_name, device_type: perip.device_type, device_model: perip.device_model, online: false, isOld: perip.isOld)
- self.aPeripherals![index] = model
- self.tableView.reloadData()
- }
- }
- if self.disconectBlock != nil{
- self.disconectBlock!()
- }
-
-
- }
- }
-
- @objc func changeLanguage() {
- log.debug("执行了 -- changeLanguage \(DAConfig.currentLanguage())")
- self.sbtTitle = "Device_list".da_localizedStr()
- }
- }
- extension SBTHomeVCtr{
- ///判断新旧协议
- func isOldProtocle(_ recieveData: String?) ->Bool {
-
- var numberArr = [String]()
- numberArr.removeAll()
- var temper2 = [String]()
- temper2.removeAll()
- for i in 0..<recieveData!.count {
- numberArr.append("0")
- let aStr = recieveData!.subString(start: i, length: 1)
- temper2.append(aStr)
- }
- if temper2 == numberArr {
- return true
- }else{
- return false
- }
- }
-
- func reloadDeviceUI_1(_ reciveData : String ) {
-
- // self.aPeripherals?.removeAll()
- let str0 = reciveData
- log.debug("处理真实数据 - \(str0)")
- //定义两个临时变量
- var device_type = ""
- var device_model = ""
- var isOldStr = ""
- //1400
- //额定放电电流
- let num00 = UUUtils.numberHexString(str0.subString(start: 7, length: 2))
- log.debug("额定放电电流:\(num00!)")
- //产品类型
- let num0 = UUUtils.numberHexString(str0.subString(start: 9, length: 2))
- log.debug("产品类型:\(num0)")
-
- switch num0?.intValue {
- case 0:
- device_type = "控制器"
- case 1:
- device_type = "控制器,路灯"
- case 3:
- device_type = "逆变器"
- case 4:
- device_type = "逆变一体机"
- case 5:
- device_type = "市电路灯"
-
- default:
- break
- }
- self.deviceTypeNumber = "\(num0!)"
- var num1 = UUUtils.string(fromHexString: str0.subString(start: 11, length: 32))
- num1 = num1?.trimmingCharacters(in: .whitespaces) //过滤开头结尾空白
- log.debug("产品型号\(num1!)")
- device_model = num1!
-
- //软件版本 00020306 00010005
- // let num2 = UUUtils.numberHexString(str0.subString(start: 11 + 32, length: 8))
- log.debug("------ \(str0.subString(start: 11 + 32, length: 8))")
- let a = UUUtils.numberHexString(str0.subString(start: 11 + 32, length: 2)) // 软件版本
- let b = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 2, length: 2))
- let c = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 2 + 2, length: 2))
- let d = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 2 + 2 + 2, length: 2))
- //硬件版本
- let m1 = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8, length: 2))
- let m2 = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8 + 2, length: 2))
- let m3 = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8 + 2 + 2, length: 2))
- let m4 = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8 + 2 + 2 + 2, length: 2))
- log.debug("硬件版本 : V\(m2!).\(m3!).\(m4!)")
-
- let m11 = Int(m1!)
-
- log.debug("硬件版本返回 --\(m11)")
- if m11 < 2{// 用来区分新旧协议,xx < 2 旧协议,>=2 新协议 tubfhu
- isOldStr = "1"
-
- }else {
- isOldStr = "0"
- }
- log.debug("硬件版本 新旧协议 --- \(str0.subString(start: 11 + 32 + 8, length: 8))")
-
- //产品序列号
- let snnum = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8 + 8, length: 8))
- log.debug("产品序列号 --- \(snnum!)")
- self.snnumber = "\(snnum!)"
-
- let key1 = PeripheralINfo()
- peripheralInfo[key1.IsOld] = isOldStr
- peripheralInfo[key1.DeviceType] = device_type
- peripheralInfo[key1.ProduceType] = device_model
-
- if isOldStr == "1" {
- //旧协议
- log.debug("当前为旧协议")
-
- let deviceName = ""
- let model = self.tempArr?.first
- let peripheralname = model?.perpheralName
- let key = PeripheralINfo()
- let belKey = device_type + device_model
- peripheralInfo[key.PerpheralName] = peripheralname
- peripheralInfo[key.DeviceName] = " "
- peripheralInfo[key.Online] = "0"
-
- log.debug("设备数据 -- \(peripheralInfo)")
- //self.baseInfoPlist?.removeAllData(SBTHomeVCtr.peripheralPlist)
- if peripheralname == nil {
- self.baseInfoPlist?.updatePlist(At: SBTHomeVCtr.peripheralPlist, key: belKey, deviceName: deviceName)
- }else{//这里需要更新设备名称
- self.baseInfoPlist!.saveData111(SBTHomeVCtr.peripheralPlist, key: belKey, deviceData: peripheralInfo)
- }
-
- self.soft = "V\(a!).\(b!).\(c!).\(d!)"
- self.hard = "V\(m2!).\(m3!).\(m4!)"
-
-
- let aIsOld = true
- // 获取 蓝牙名称 --
- let perpheralName_save = self.baseInfoPlist?.readPeripheral(from: SBTHomeVCtr.peripheralPlist, key: belKey)
- let deviceMo = BLEData(perpheralName: perpheralName_save, device_name: deviceName, device_type: device_type, device_model: device_model, online: true,isOld:aIsOld )
-
- //判断 是否存在已经存在 则 修改数组内容,否则 重新添加
- // if isAddDevice == true{
- let isContains = self.aPeripherals?.contains(where: { (item) -> Bool in
- return item.device_model == device_model && item.device_type == device_type
- })
-
- if isContains == true {
- //查找索引
- let index = self.aPeripherals?.firstIndex(where: { (item) -> Bool in
- return item.device_model == device_model && item.device_type == device_type
- })
- self.aPeripherals![index!] = deviceMo
- }else{
- self.aPeripherals?.append(deviceMo)
- }
- self.tableView.reloadData()
- if self.isAuto == true {
- //上传数据
- NetworkManager.shared.updateDeviceInfo("\(num0!)", number: self.snnumber!, name: belKey, model: device_model, hardwareVersion: "V\(m2!).\(m3!).\(m4!)", softwareVersion: "V\(a!).\(b!).\(c!).\(d!)", productionDate: nil, macAddress: nil, productionBatch: nil)
- delay(1) { //设备名称
- let model: BLEData = deviceMo
- let peripheralName = model.perpheralName
- let devicename = model.device_name
- let devicetype = model.device_type
- let device_model = model.device_model
- let isOld = model.isOld
- let belKey = devicetype! + device_model!
- let alert = SBTReadLoadingVCtr(nibName: "SBTReadLoadingVCtr", bundle: nil)
- alert.deviceName = devicename
- alert.message = "Data_loading".da_localizedStr()
- alert.show()
- var isSuccess = false
- // alert.dismiss(8)
- let arr = ["FF03000A0001","FF03000B000F","FF030100000A","FF0301200003","FF03F000000A","FF03F001000A","FF03F002000A","FF03F003000A","FF03F004000A","FF03F005000A","FF03F006000A","FF03F007000A","FF030115000B","FF03E0010001","FF03E003001E","FF03DF000001","FF03DF0D0001","FF03E0210001"]
- self.bleHasBeemDisconnectect {
- alert.dismiss()
- return
- }
- BabyBluetoothSwift.shareInstance()?.writeData2222(arr, completionHandler: { (result) in
- log.debug("返回数据 --- \(ReceiveData)")
- self.realdata_000A(result!["FF03000A0001"] as! String)
- self.real_000B000F(result!["FF03000B000F"] as! String)
- self.realdata_0100(result!["FF030100000A"] as! String)
- self.realdata_0120(result!["FF0301200003"] as! String)
- self.historyDataArr.removeAll()
- self.realdata_F000(result!["FF03F000000A"] as! String)
- self.realdata_F001(result!["FF03F001000A"] as! String)
- self.realdata_F002(result!["FF03F002000A"] as! String)
- self.realdata_F003(result!["FF03F003000A"] as! String)
- self.realdata_F004(result!["FF03F004000A"] as! String)
- self.realdata_F005(result!["FF03F005000A"] as! String);
- self.realdata_F006(result!["FF03F006000A"] as! String);
- self.realdata_F007(result!["FF03F007000A"] as! String);
- self.realdata_0115(result!["FF030115000B"] as! String)
- self.realdata_E001(result!["FF03E0010001"] as! String);
- self.realdata_E003(result!["FF03E003001E"] as! String);
- self.realdata_DF00(result!["FF03DF000001"] as! String);
- self.realdata_DF0D(result!["FF03DF0D0001"] as! String)
- self.realdata_E021(result!["FF03E0210001"] as! String)
- AppShare.dataDict = self.dataDict
- if AppShare.isRefresh == true {
- NotificationCenter.default.post(name: NSNotification.Name(kBabyBluetoothNotificationAllDataRecieve), object: nil)
- }
- alert.dismiss()
- isSuccess = true
- self.readPeripheraldata(belKey, deviceName: devicename!, isOld: isOld!, perihperalName: peripheralName!)
-
- let user = UserDefaults.standard
- user.set(peripheralName, forKey: "SavePeripheral")
- user.synchronize()
-
- //上传操作
- self.uploadAction()
-
- })
- delay(15, closure: {
- if isSuccess == false {
- HUD.flash(.error, delay: 1)
- return
- }
- })
- }
- self.isAuto = false
- }
-
- } else {
-
- delay(0.02) {
- //新协议
- BabyBluetoothSwift.shareInstance()?.writeData("FF0300490020", completionHandler: { (result) in
- log.debug("当前为新协议-----\(result)")
- self.reloadDeviceUI_2(result!)
- })
- }
- }
- }
-
-
- func uploadAction() {
- NetworkManager.shared.uploadlocation(self.snnumber!)
-
- let loadS = (self.dataDict[self.key.loadStatus] as! String == "Load_is_on".da_localizedStr()) ? "1" : "0"
- let batteryPowerStr = Double(self.dataDict[self.key.batteryVoltage] as! String)! * Double(self.dataDict[self.key.batteryTemp] as! String)!
- let batteryPower = String(format: "%.2f",batteryPowerStr )
- NetworkManager.shared.uploadtodayData(self.snnumber!,
- solarStatus: self.dataDict[self.key.solarStatus] as! String,
- solarVoltage: self.dataDict[self.key.solarVoltage] as! String,
- solarCurrent: self.dataDict[self.key.solarCurrent] as! String,
- solarPower: self.dataDict[self.key.solarPower] as! String,
- batteryStatus: self.dataDict[self.key.batteryStatus] as! String,
- batteryVoltage: self.dataDict[self.key.batteryVoltage] as! String,
- batteryCurrent: self.dataDict[self.key.batteryTemp] as! String,
- batteryTemp: self.dataDict[self.key.batteryTemp] as! String,
- batteryPower: batteryPower, loadStatus: loadS,
- loadVoltage: self.dataDict[self.key.loadVoltage] as! String,
- loadCurrent: self.dataDict[self.key.loadCurrent] as! String,
- loadPower: (self.dataDict[self.key.loadPower] as! String), deviceStatus: "0", alarmStr: self.dataDict[self.key.loadStatus] as! String, generationCapacity: "0", dischargeCapacity: "0", totalGenerationCapacity: self.dataDict[self.key.generatEnergyTotal] as! String, totalDischargeCapacity: self.dataDict[self.key.usedEnergyTotal] as! String, runTime: self.dataDict[self.key.runTime] as! String, overTimes: self.dataDict[self.key.overTimes] as! String, fullTimes: self.dataDict[self.key.fullTimes] as! String, chargingAmpereHour: self.dataDict[self.key.chargingAmpereHour] as! String, dischargeAmpereHour: self.dataDict[self.key.dischargeAmpereHour] as! String)
-
- var hisarr = [[String:String]]()
- let first = nowTime("yyyy-MM-dd")
- let second = getLastDay(first)
- let third = getLastDay(second)
- let foreth = getLastDay(third)
- let fifth = getLastDay(foreth)
- let sixth = getLastDay(fifth)
- let seventh = getLastDay(sixth)
- let eighth = getLastDay(seventh)
- let dayArr = [first,second,third,foreth,fifth,sixth,seventh,eighth]
- for i in 0..<self.historyDataArr.count{
- let item = self.historyDataArr[i]
- let dataList1 = [
- "batteryVoltageMin":item["his_batteryVoltageMin"], //蓄电池最低电压
- "batteryVoltageMax":item["his_batteryVoltageMax"],//蓄电池最高电压
- "chargingCurrentMax":item["his_chargingCurrentMax"] ,//充电最大电流
- "chargingPowerMax" : item["his_chargingPowerMax"],//充电最大功率
- "chargingAmpereHour" : item["his_chargingAmpereHour"],//充电安时数
- "dischargeCurrentmax" : item["his_dischargeCurrentmax"],//放电最大电流
- "dischargePowerMax" : item["his_dischargePowerMax"], //放电最大功率
- "dischargeAmpereHour" : item["his_dischargeAmpereHour"],//放电安时数
- "generationCapacity": item["his_generationCapacity"], //发电量
- "dischargeCapacity" : item["his_dischargeCapacity"],//放电量
- "updatetime" : dayArr[i]//故障状态
- ]
- hisarr.append(dataList1 as! [String : String])
- }
- NetworkManager.shared.uploadHistoryData(self.snnumber!, locationList: hisarr)
- }
- func reloadDeviceUI_2(_ reciveData : String ) {
-
- // self.aPeripherals?.removeAll()
- let str0 = reciveData
- log.debug("处理真实数据 - \(str0)")
-
- var deviceName = ""
-
- var num5 = UUUtils.string(fromHexString: str0.subString(start: 7 , length: str0.count - 12))
- log.debug("要解析的十六进制 \( str0.subString(start: 7 , length: str0.count - 12))")
- num5 = num5?.trimmingCharacters(in: .whitespaces) //过滤开头结尾空白
- if num5 == nil || num5 == "" {
- let ChineseString = UUUtils.deUnicodeString(fromHexString: str0.subString(start: 7 , length: str0.count - 12))
- log.debug("设备中文名称 \(ChineseString!)")
- deviceName = ChineseString!
- dataDict[key.productName] = deviceName
- }else{
- log.debug("设备名称 \(num5!)")
- deviceName = num5!
- dataDict[key.productName] = num5
- }
- let model = self.tempArr?.first
- let peripheralname = model?.perpheralName
- let key = PeripheralINfo()
-
- let device_type = peripheralInfo[key.DeviceType]
- let device_model = peripheralInfo[key.ProduceType]
- let isOldStr = peripheralInfo[key.IsOld]
- let belKey = device_type! + device_model!
- peripheralInfo[key.PerpheralName] = peripheralname
- if isOldStr == "1"{
- peripheralInfo[key.DeviceName] = " "
- }else
- {
- peripheralInfo[key.DeviceName] = deviceName
- }
-
- peripheralInfo[key.Online] = "0"
-
- log.debug("设备数据 -- \(peripheralInfo)")
- //先清空沙盒内容
- // self.baseInfoPlist!.deleteData(SBTHomeVCtr.peripheralPlist, key: key)
- // self.baseInfoPlist?.removeAllData(SBTHomeVCtr.peripheralPlist)
- if peripheralname == nil {
- self.baseInfoPlist?.updatePlist(At: SBTHomeVCtr.peripheralPlist, key: belKey, deviceName: deviceName)
- }else{//这里需要更新设备名称
- self.baseInfoPlist!.saveData111(SBTHomeVCtr.peripheralPlist, key: belKey, deviceData: peripheralInfo)
- }
- var aIsOld = false
- if isOldStr == "1" {
- aIsOld = true
- }
-
- //上传数据
- NetworkManager.shared.updateDeviceInfo(self.deviceTypeNumber ?? "0", number: self.snnumber!, name: belKey, model: device_model, hardwareVersion: self.hard ?? "V0.0.0", softwareVersion: self.soft ?? "V0.0.0.0", productionDate: nil, macAddress: nil, productionBatch: nil)
- // 获取 蓝牙名称 --
- let perpheralName_save = self.baseInfoPlist?.readPeripheral(from: SBTHomeVCtr.peripheralPlist, key: belKey)
- let deviceMo = BLEData(perpheralName: perpheralName_save, device_name: deviceName, device_type: device_type, device_model: device_model, online: true,isOld:aIsOld )
-
- //判断 是否存在已经存在 则 修改数组内容,否则 重新添加
- // if isAddDevice == true{
-
- let isContains = self.aPeripherals?.contains(where: { (item) -> Bool in
- return item.device_model == device_model && item.device_type == device_type
- })
- if isContains == true {
- //查找索引
- let index = self.aPeripherals?.firstIndex(where: { (item) -> Bool in
- return item.device_model == device_model && item.device_type == device_type
- })
- self.aPeripherals![index!] = deviceMo
- }else{
- self.aPeripherals?.append(deviceMo)
- }
- // isAddDevice = false
- // }
-
- self.tableView.reloadData()
- if self.isAuto == true {
- delay(1) { //设备名称
- let model: BLEData = deviceMo
- let peripheralName = model.perpheralName
- let devicename = model.device_name
- let devicetype = model.device_type
- let device_model = model.device_model
- let isOld = model.isOld
- let belKey = devicetype! + device_model!
- let alert = SBTReadLoadingVCtr(nibName: "SBTReadLoadingVCtr", bundle: nil)
- alert.deviceName = " "
- alert.message = "Data_loading".da_localizedStr()
- alert.show()
- self.bleHasBeemDisconnectect {
- alert.dismiss()
- return
- }
-
- let arr = ["FF03000A0001","FF03000B000F","FF0300490020","FF0300FD000D","FF03F000000A","FF03F001000A","FF03F002000A","FF03F003000A","FF03F004000A","FF03F005000A","FF03F006000A","FF03F007000A","FF030115000B","FF03E0010001","FF03E003001E","FF03DF000001","FF03DF0D0001","FF03E0210001"]
- var isSuccess = false
- BabyBluetoothSwift.shareInstance()?.writeData2222(arr, completionHandler: { (result) in
- self.realdata_000A(result!["FF03000A0001"] as! String)
- self.real_000B000F(result!["FF03000B000F"] as! String)
- self.readdata_0049(result!["FF0300490020"] as! String)
- self.realdata_00FD(result!["FF0300FD000D"] as! String)
- self.historyDataArr.removeAll()
- self.realdata_F000(result!["FF03F000000A"] as! String)
- self.realdata_F001(result!["FF03F001000A"] as! String)
- self.realdata_F002(result!["FF03F002000A"] as! String)
- self.realdata_F003(result!["FF03F003000A"] as! String)
- self.realdata_F004(result!["FF03F004000A"] as! String)
- self.realdata_F005(result!["FF03F005000A"] as! String);
- self.realdata_F006(result!["FF03F006000A"] as! String);
- self.realdata_F007(result!["FF03F007000A"] as! String);
- self.realdata_0115(result!["FF030115000B"] as! String)
- self.realdata_E001(result!["FF03E0010001"] as! String);
- self.realdata_E003(result!["FF03E003001E"] as! String);
- self.realdata_DF00(result!["FF03DF000001"] as! String);
- self.realdata_DF0D(result!["FF03DF0D0001"] as! String)
- self.realdata_E021(result!["FF03E0210001"] as! String)
- AppShare.dataDict = self.dataDict
- if AppShare.isRefresh == true {
- NotificationCenter.default.post(name: NSNotification.Name(kBabyBluetoothNotificationAllDataRecieve), object: nil)
- }
- isSuccess = true
- alert.dismiss()
- self.readPeripheraldata(belKey, deviceName: devicename!, isOld: isOld!, perihperalName: peripheralName!)
- let user = UserDefaults.standard
- user.set(peripheralName, forKey: "SavePeripheral")
- user.synchronize()
- //上传操作
- self.uploadAction()
- })
- delay(15, closure: {
- if isSuccess == false {
- HUD.flash(.error, delay: 1)
- return
- }
- })
- }
- self.isAuto = false
- }
- }
- func disConnectName(_ peripheralname:String) {
- //断开设备
- self.perform(#selector(__disConectName(_:)), with: peripheralname, afterDelay: 5)
- }
-
- @objc func __disConectName(_ peripheralName:String) {
- log.debug("__disConectName\(peripheralName)")
-
- let peripheral = baby1?.findConnectedPeripheral(peripheralName)
- baby1?.cancelPeripheralConnection(peripheral!)
- }
- }
- //MARK: BLE
- extension SBTHomeVCtr{
- func babyDelegate() {
- log.debug("执行了没 ")
- //设置通知状态改变的block
- baby1?.setBlockOnCentralManagerDidUpdateState({ (central) in
- log.debug("蓝牙状态 检测----- \(central?.state)")
-
- //blockOnCentralManagerDidUpdateState
- if central?.state == .poweredOn{
- log.debug("蓝牙打开")
- HUD.flash(.label("蓝牙已打开"), delay: 2.0) { _ in
- print("License Obtained.")
- }
- // g_showHUD("蓝牙已打开")
-
- }else if central?.state == .unsupported{
- log.debug("不支持蓝牙")
- g_showHUD("不支持蓝牙")
- }else if central?.state == .unknown{
- log.debug("未知状态")
- g_showHUD("未知状态")
- }else if central?.state == .resetting{
- log.debug("蓝牙重启")
- g_showHUD("蓝牙重启")
- }else if central?.state == .unauthorized{
- log.debug("蓝牙未授权")
- g_showHUD("蓝牙未授权")
- }else if central?.state == .poweredOff{
- log.debug("蓝牙已关闭")
- g_showHUD("蓝牙已关闭")
- }
- })
-
- baby1?.setBlockOnDiscoverToPeripherals({ (central, peripheral, advertisementData, RSSI) in
- log.debug("输出内容---\(central),\(peripheral), -\(advertisementData)- \(RSSI)")
- })
- baby1?.setBlockOnCancelScanBlock({ (central) in
- log.debug("停止扫描取消")
- })
-
- baby1?.setBlockOnCancelAllPeripheralsConnectionBlock({ (central) in
- log.debug("断开所有设备")
- })
-
- baby1?.setBlockOnDisconnect({ (central, peripheral, erro) in
- log.debug(" SBHOMEVCTR ---->断开了设备\(peripheral)")
- })
-
-
- }
- }
- extension SBTHomeVCtr{
-
- //获取设备数据
- func getDevieData(){
- BabyBluetoothSwift.shareInstance()?.testBlock2 = {[weak self](deviceData) in
-
- let a = deviceData?.value(forKey: ReceiveData) as! String
-
- let str0 = a.subString(start: 0, length: 6)
-
- log.debug("返回的数据 -- \(a)")
-
- if str0.subString(start: 1, length: 4) == "ff03" {
- let dataTagValue : String = deviceData?.value(forKey: DataTag) as! String
- if dataTagValue == "FF03000A0001"{
- self!.realdata_000A(a)
- }
- if dataTagValue == "FF03000B000F"{
- self?.real_000B000F(a)
- self!.reloadDeviceUI_1(a)
- }
-
- if dataTagValue == "FF03000B004E" {
- self!.realdata_000B(a)
- //
- }
- if dataTagValue == "FF0300FD000D" {
- self!.realdata_00FD(a)
- }
- //"FF030100000A","FF0301200003",
-
- if dataTagValue == "FF030100000A" {
- self!.realdata_0100(a)
- }
- if dataTagValue == "FF0301200003" {
- self!.realdata_0120(a)
- }
-
- if dataTagValue == "FF03F000000A" {
- self!.historyDataArr.removeAll()
- self!.realdata_F000(a)
- }
- if dataTagValue == "FF03F001000A" {
- self!.realdata_F001(a)
- }
- if dataTagValue == "FF03F002000A" {
- self!.realdata_F002(a)
- }
- if dataTagValue == "FF03F003000A" {
- self!.realdata_F003(a)
- }
- if dataTagValue == "FF03F004000A" {
- self!.realdata_F004(a);
- }
- if dataTagValue == "FF03F005000A" {
- self!.realdata_F005(a);
- }
- if dataTagValue == "FF03F006000A" {
- self!.realdata_F006(a);
- }
-
- if dataTagValue == "FF03F007000A" {
- self!.realdata_F007(a);
- }
- if dataTagValue == "FF030115000B"{
- self!.realdata_0115(a)
- }
- if dataTagValue == "FF03E0010001" {
- self!.realdata_E001(a);
- }
- if dataTagValue == "FF03E003001E" {
- self!.realdata_E003(a);
- }
- if dataTagValue == "FF03DF000001" {
- self!.realdata_DF00(a);
- }
- if dataTagValue == "FF03DF0D0001" {
- self!.realdata_DF0D(a)
- }
- if dataTagValue == "FF03E0210001"{
- self!.realdata_E021(a)
- }
- if dataTagValue == "FF0300490020"{
- self!.readdata_0049(a)
- self!.reloadDeviceUI_2(a)
- }
- }
- // log.debug("返回数据dataDict-- \(self!.dataDict)")
- AppShare.dataDict = self!.dataDict
-
- //kBabyBluetoothNotificationAllDataRecieve
- if AppShare.isRefresh == true {
- NotificationCenter.default.post(name: NSNotification.Name(kBabyBluetoothNotificationAllDataRecieve), object: nil)
- }
-
- }
- }
-
-
- //实时数据
- func realdata_000A(_ reciveData : String) {
- let str0 = reciveData
- log.debug("处理真实数据 - \(str0)")
- //系统支持的最高电压
- let num00 = UUUtils.numberHexString(str0.subString(start: 7 , length: 2))
- log.debug("额定充电电流: \(num00!)")
- dataDict[key.MaxVolt] = "\(num00!)"
- // paramDataDict[key.MaxVolt] = "\(num00!)"
-
-
- let num0 = UUUtils.numberHexString(str0.subString(start: 7 + 2, length: 2))
- log.debug("额定充电电流: \(String(format: "%.2f", num0!.doubleValue))")
- dataDict[key.IchageRage] = String(format: "%.2f", num0!.doubleValue)
- // paramDataDict[key.IchageRage] = "\(num0!)"
- }
-
- //读取名称
- func readdata_0049(_ reciveData : String){
- let str0 = reciveData
- log.debug("-------处理真实数据 - \(str0)")
-
- var num5 = UUUtils.string(fromHexString: str0.subString(start: 7 , length: str0.count - 12)) // 先转化为汉字 如果汉字无法解析出来,则用数字解析
- log.debug("=====要解析的十六进制 \( str0.subString(start: 7 , length: str0.count - 12))")
-
- // let isOld = isOldProtocle(num5!)
-
- num5 = num5?.trimmingCharacters(in: .whitespaces) //过滤开头结尾空白
- log.debug("======设备名称 \(num5)")
- if num5 == nil || num5 == ""{
- let ChineseString = UUUtils.deUnicodeString(fromHexString: str0.subString(start: 7 , length: str0.count - 12))
- log.debug("设备中文名称 \(ChineseString!)")
- dataDict[key.productName] = "\(ChineseString!)"
- }else{
- dataDict[key.productName] = num5
- }
-
- for i in 0..<self.aPeripherals!.count {
- var mo = self.aPeripherals![i]
- mo.device_name = dataDict[key.productName] as! String
- self.aPeripherals![i] = mo
- self.tableView.reloadData()
- }
-
- }
- func real_000B000F(_ reciveData : String) {
- let str0 = reciveData
- log.debug("处理真实数据 - \(str0)")
-
- //1400 <aaaaaaa>
- //额定放电电流
- let num00 = UUUtils.numberHexString(str0.subString(start: 7, length: 2))
- log.debug("额定放电电流:\(num00!)")
- dataDict[key.idischagRate] = "\(num00!)"
- //产品类型
- let num0 = UUUtils.numberHexString(str0.subString(start: 9, length: 2))
- log.debug("产品类型:\(num0)")
- var device_type = ""
- switch num0?.intValue {
- case 0:
- device_type = "Controller_household".da_localizedStr()
- case 1:
- device_type = "Controller_street_light".da_localizedStr()
- case 3:
- device_type = "Inverter".da_localizedStr()
- case 4:
- device_type = "All-in-one_inverter".da_localizedStr()
-
- default:
- break
- }
-
- dataDict[key.device_type] = device_type
- var num1 = UUUtils.string(fromHexString: str0.subString(start: 11, length: 32))
- num1 = num1?.trimmingCharacters(in: .whitespaces) //过滤开头结尾空白
- log.debug("产品型号\(num1)")
- dataDict[key.product_type] = num1!
- //软件版本
- // let num2 = UUUtils.numberHexString(str0.subString(start: 11 + 32, length: 8))
- let a = UUUtils.numberHexString(str0.subString(start: 11 + 32, length: 2)) // 软件版本
- let b = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 2, length: 2))
- let c = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 2 + 2, length: 2))
- let d = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 2 + 2 + 2, length: 2))
-
- if a == 0{// 旧协议最高位是0x00,是0x01是新版本(可访问1B-59),这样已可区分新旧协议
-
- }else if a == 1{
-
- }
- log.debug("软件版本 : V \(b!).\(c!).\(d!)")
- dataDict[key.softWareCersion] = "V\(b!).\(c!).\(d!)"
- //硬件版本
- let m1 = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8, length: 2))
- let m2 = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8 + 2, length: 2))
- let m3 = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8 + 2 + 2, length: 2))
- let m4 = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8 + 2 + 2 + 2, length: 2))
- log.debug("硬件版本 : V\(m2!).\(m3!).\(m4!)")
- // 序列号
- dataDict[key.hardWareCersion] = "V \(m2!).\(m3!).\(m4!)"
- // let num4 = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8 + 8 , length: 8))
- let num41 = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8 + 8 , length: 2))
- let num42 = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8 + 8 + 2 , length: 2))
- let num43 = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8 + 8 + 4 , length: 4)) //补足5位
-
- log.debug("\( String(format: "%05d", num43!.intValue))") // 年月(低十六位)
-
- dataDict[key.productSN] = "\(num41!)\(num42!)\( String(format: "%05d", num43!.intValue))"
-
- }
-
- func realdata_000B(_ reciveData : String) {
- let str0 = reciveData
- log.debug("处理真实数据 - \(str0)")
- //1400
- //额定放电电流
- let num00 = UUUtils.numberHexString(str0.subString(start: 7, length: 2))
- log.debug("额定放电电流:\(num00!)")
- dataDict[key.idischagRate] = "\(num00!)"
- //产品类型
- let num0 = UUUtils.numberHexString(str0.subString(start: 9, length: 2))
- log.debug("产品类型:\(num0)")
- var device_type = ""
- switch num0?.intValue {
- case 0:
- device_type = "Controller_household".da_localizedStr()
- case 1:
- device_type = "Controller_street_light".da_localizedStr()
- case 3:
- device_type = "Inverter".da_localizedStr()
- case 4:
- device_type = "All-in-one_inverter".da_localizedStr()
-
- default:
- break
- }
-
- dataDict[key.device_type] = device_type
- var num1 = UUUtils.string(fromHexString: str0.subString(start: 11, length: 32))
- num1 = num1?.trimmingCharacters(in: .whitespaces) //过滤开头结尾空白
- log.debug("产品型号\(num1)")
- dataDict[key.product_type] = num1!
- //软件版本
- // let num2 = UUUtils.numberHexString(str0.subString(start: 11 + 32, length: 8))
- let a = UUUtils.numberHexString(str0.subString(start: 11 + 32, length: 2))
- let b = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 2, length: 2))
- let c = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 2 + 2, length: 2))
- let d = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 2 + 2 + 2, length: 2))
- log.debug("软件版本 : V \(b!).\(c!).\(d!)")
- dataDict[key.softWareCersion] = "V\(b!).\(c!).\(d!)"
- //硬件版本
- let m1 = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8, length: 2))
- let m2 = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8 + 2, length: 2))
- let m3 = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8 + 2 + 2, length: 2))
- let m4 = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8 + 2 + 2 + 2, length: 2))
- log.debug("硬件版本 : V\(m2!).\(m3!).\(m4!)")
- // 序列号
- dataDict[key.hardWareCersion] = "V \(m2!).\(m3!).\(m4!)"
- // let num4 = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8 + 8 , length: 8))
- let num41 = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8 + 8 , length: 2))
- let num42 = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8 + 8 + 2 , length: 2))
- let num43 = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8 + 8 + 4 , length: 4)) //补足5位
-
- log.debug("\( String(format: "%05d", num43!.intValue))") // 年月(低十六位)
-
- dataDict[key.productSN] = "\(num41!)\(num42!)\( String(format: "%05d", num43!.intValue))"
-
- var num5 = UUUtils.string(fromHexString: str0.subString(start: 255 , length: 64))
- log.debug("要解析的十六进制 \( str0.subString(start: 255 , length: 64))")
- num5 = num5?.trimmingCharacters(in: .whitespaces) //过滤开头结尾空白
- log.debug("设备名称 \(num5)")
- //f16d33 575578 e565b 065fd8 0906ed179
- if num5 == nil || num5 == "" {
- let ChineseString = UUUtils.deUnicodeString(fromHexString: str0.subString(start: 255 , length: 64))
- // ff ff
- dataDict[key.productName] = ChineseString
- log.debug("设备中文名称 \(ChineseString!)")
- }else{
- dataDict[key.productName] = num5
- }
-
- let year = UUUtils.numberHexString(str0.subString(start: 75, length: 2))
- let mouth = UUUtils.numberHexString(str0.subString(start: 75 + 2, length: 2))
- let day = UUUtils.numberHexString(str0.subString(start: 75 + 2, length: 2))
- log.debug("生产日期 : \(year!)-\(mouth!)-\(day!)")
- }
- //温度处理
- func temp(with num:NSNumber)-> String{
- let numInt = num.intValue
- // let q1 = UUUtils.numberHexString(q)?.intValue
-
- let q2 = numInt & 128
- print("判断正负\(q2)")
- if q2 != 128 {
- log.debug("温度\(num.floatValue )")
- return "\(num.intValue )"
-
- }else{
-
- log.debug("温度: -\(numInt & 127 )")
- return "-\(numInt & 127 )"
- }
- }
- //1s刷新一次
-
- //实时数据
- func realdata_00FD(_ reciveData : String) {
- let str0 = reciveData
- log.debug("处理真实数据 - \(str0)")
-
- //太阳能板状态 0 000 0000 0000 0000 0000 0000 0000 0010
- // 31
-
- var num0A = UUUtils.getBinaryByHex(str0.subString(start: 11 , length: 8))
- let num0A_1 = num0A?.subString(start: 0, length: 16)
- let num0A_2 = num0A?.subString(start: 16, length: 16)
- let num0A_II = num0A_2! + num0A_1!
- num0A = num0A_II
- log.debug("太阳能板状态\(num0A!)")
-
- var solarStatusStr = ""
- let B30 = num0A?.subString(start: 1, length: 1)
- let B29 = num0A?.subString(start: 2, length: 1)
- let B28 = num0A?.subString(start: 3, length: 1)
- let B27 = num0A?.subString(start: 4, length: 1)
- let B26 = num0A?.subString(start: 5, length: 1)
- let B25 = num0A?.subString(start: 6, length: 1)
- let B24 = num0A?.subString(start: 7, length: 1)
- let B23 = num0A?.subString(start: 8, length: 1)
-
- //负载
- let B20 = num0A?.subString(start: 11, length: 1)
- let B19 = num0A?.subString(start: 12, length: 1)
- let B15 = num0A?.subString(start: 16, length: 1)
- //系统
- let B21 = num0A?.subString(start: 10, length: 1)
- let B14 = num0A?.subString(start: 17, length: 1)
- let B13 = num0A?.subString(start: 18, length: 1)
-
- //蓄电池状态
- let B22 = num0A?.subString(start: 9, length: 1)
- let B18 = num0A?.subString(start: 13, length: 1)
- let B17 = num0A?.subString(start: 14, length: 1)
- let B16 = num0A?.subString(start: 15, length: 1)
- let B12 = num0A?.subString(start: 19, length: 1)
- let B11 = num0A?.subString(start: 20, length: 1)
- let B10 = num0A?.subString(start: 21, length: 1)
-
- var batteryStatusStr = ""
- if B10 == "1" {
- batteryStatusStr = "BMS_overcharge_protection".da_localizedStr()
- }
- if B11 == "1" {
- batteryStatusStr = "Battery_low_temperature_protection".da_localizedStr()
- }
- if B12 == "1" {
- batteryStatusStr = "Battery_reverse_polarity".da_localizedStr()
- }
- if B16 == "1" {
- batteryStatusStr = "Battery_over_discharge".da_localizedStr()
- }
- if B17 == "1" {
- batteryStatusStr = "Battery_overvoltage".da_localizedStr()
- }
- if B18 == "1" {
- batteryStatusStr = "Undervoltage_warning".da_localizedStr()
- }
- if B22 == "1" {
- batteryStatusStr = "External_ambient_temperature_is_too_high".da_localizedStr()
- }
-
- if B10 == "0" && B11 == "0" && B12 == "0" && B16 == "0" && B17 == "0" && B18 == "0" && B22 == "0"{
- batteryStatusStr = "Normal".da_localizedStr()
- }
- dataDict[key.batteryStatus] = batteryStatusStr
- /// realDataDict[key.batteryStatus] = batteryStatusStr
- //太阳能状态
-
- if B23 == "1" {
- solarStatusStr = "PV_input_power_is_too_large".da_localizedStr()
- }
- if B24 == "1" {
- solarStatusStr = "Short_circuit_of_photovoltaic_input".da_localizedStr()
- }
- if B25 == "1" {
- solarStatusStr = "Overvoltage_at_the_photovoltaic_input".da_localizedStr()
- }
- if B26 == "1" {
- solarStatusStr = "Solar_panel_countercurrent".da_localizedStr()
- }
- if B27 == "1" {
- solarStatusStr = "Solar_panel_working_point_overpressure".da_localizedStr()
- }
- if B28 == "1" {
- solarStatusStr = "Solar_panel_reverse_connection".da_localizedStr()
- }
- dataDict[key.solarStatus] = solarStatusStr
- /// realDataDict[key.solarStatus] = solarStatusStr
- if B23 == "0" && B24 == "0" && B25 == "0" && B26 == "0" && B27 == "0" && B28 == "0" {
- dataDict[key.solarStatus] = batteryStatusStr
- /// realDataDict[key.solarStatus] = batteryStatusStr
- }
-
-
- let num0 = UUUtils.getBinaryByHex(str0.subString(start: 7, length: 4))
- log.debug("00FD \(num0!)")
-
- let low_8_bit = UUUtils.numberHexString(str0.subString(start: 9, length: 2))
-
- log.debug("low_8_bit - \(low_8_bit)")
- //1000000000000000
-
- var changeStatus = ""
- if low_8_bit == 0 {
- changeStatus = "Charging_is_not_turned_on".da_localizedStr()
- }else if low_8_bit == 1 {
- changeStatus = "Start_charging_mode".da_localizedStr()
- }else if low_8_bit == 2 {
- changeStatus = "MPPT_charge_mode".da_localizedStr()
- }else if low_8_bit == 3 {
- changeStatus = "Equalizing_charge_mode".da_localizedStr()
- }else if low_8_bit == 4 {
- changeStatus = "Floating_charge_mode".da_localizedStr()
- }else if low_8_bit == 5 {
- changeStatus = "Boost_charge_mode".da_localizedStr()
- }else if low_8_bit == 6 {
- changeStatus = "Current_limiting_(super_power)".da_localizedStr()
- }else if low_8_bit == 7 {
- changeStatus = "Fully_charged".da_localizedStr()
- }
-
- // realDataDict[key.solarStatus] = solarStatusStr
- if B23 == "0" && B24 == "0" && B25 == "0" && B26 == "0" && B27 == "0" && B28 == "0" {
- dataDict[key.solarStatus] = changeStatus
- // realDataDict[key.solarStatus] = batteryStatusStr
- }
- //0000 0000 0000 0000 从后往前数
- let loadStatus = "\(num0?.subString(start: 0, length: 1) ?? "")"
- var loadStatusStr = ""
-
- if B15 == "1" {
- loadStatusStr = "Open_load".da_localizedStr()
- }
- if B19 == "1" {
- loadStatusStr = "Load_short_circuit".da_localizedStr()
- }
- if B20 == "1" {
- loadStatusStr = "Excessive_load_power_or_overload".da_localizedStr()
- }
-
- if B15 == "0" && B19 == "0" && B20 == "0" {
- if loadStatus == "0" {
- loadStatusStr = "Load_is_off".da_localizedStr()
- }else if loadStatus == "1" {
- loadStatusStr = "Load_is_on".da_localizedStr()
- }
- }
-
-
-
- log.debug("负载状态 :\(num0?.subString(start: 7, length: 1))") // 低八位
- dataDict[key.loadStatus] = loadStatusStr
- // realDataDict[key.loadStatus] = loadStatusStr
- //蓄电池状态
- //太阳能电压 0107
- let num1 = UUUtils.numberHexString(str0.subString(start: 47, length: 4))
- print("太阳能电压 \(num1!.floatValue * 0.1)" )
- let solarV = String(format: "%.1f", num1!.floatValue * 0.1)
- dataDict[key.solarVoltage] = "\(solarV)"
- // realDataDict[key.solarVoltage] = "\(solarV)"
-
- // 太阳能电流 0108
- let num2 = UUUtils.numberHexString(str0.subString(start: 47 + 4, length: 4))
- print("太阳能电流 \(num2!.floatValue * 0.01)")
- dataDict[key.solarCurrent] = String(format: "%.2f", num2!.floatValue * 0.01)
- // realDataDict[key.solarCurrent] = String(format: "%.2f", num2!.floatValue * 0.01)
-
- // 太阳能功率 0109
- let num2_1 = UUUtils.numberHexString(str0.subString(start: 47 + 4 + 4, length: 4))
- print("太阳能功率 \(num2_1!)")
- dataDict[key.solarPower] = "\(num2_1!)"
- // realDataDict[key.solarPower] = "\(num2_1!)"
-
- //蓄电池 电压
- let num3 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 4, length: 4))
- print("蓄电池电压 \(num3!.floatValue * 0.1)")
- dataDict[key.batteryVoltage] = String(format: "%.1f", num3!.floatValue * 0.1)
- // realDataDict[key.batteryVoltage] = String(format: "%.1f", num3!.floatValue * 0.1)
-
- let num4 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 5, length: 4))
- print("蓄电池电流\(num4!.floatValue * 0.01)")
- dataDict[key.batteryCurrent] = String(format: "%.2f", num4!.floatValue * 0.01)
- // realDataDict[key.batteryCurrent] = String(format: "%.2f", num4!.floatValue * 0.01)
-
- let num5 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 6 + 2 , length: 2))
- print("温度 \(temp(with: num5!))")
- dataDict[key.batteryTemp] = "\(temp(with: num5!))"
- // realDataDict[key.batteryTemp] = "\(temp(with: num5!))"
-
- //负载
- let num6 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 7, length: 4))
- print("负载电压\(num6!.floatValue * 0.1)")
- dataDict[key.loadVoltage] = String(format: "%.1f", num6!.floatValue * 0.1)
- // realDataDict[key.loadVoltage] = String(format: "%.1f", num6!.floatValue * 0.1)
-
- let num7 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 8, length: 4))
- print("负载电流\(num7!.floatValue * 0.01)")
- dataDict[key.loadCurrent] = String(format: "%.2f", num7!.floatValue * 0.01)
- // realDataDict[key.loadCurrent] = String(format: "%.2f", num7!.floatValue * 0.01)
-
- let num8 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 9, length: 4))
- print("功率 \(num8! )")
- dataDict[key.loadPower] = "\(num8!)"
- // realDataDict[key.loadPower] = "\(num8!)"
-
-
- }
-
-
- func realdata_0100(_ reciveData : String) {
- let str0 = reciveData
- log.debug("处理真实数据 - \(str0)")
-
-
- //太阳能电压 0107
- let num1 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 7, length: 4))
- print("太阳能电压 \(num1!.floatValue * 0.1)" )
- let solarV = String(format: "%.1f", num1!.floatValue * 0.1)
- dataDict[key.solarVoltage] = "\(solarV)"
- // realDataDict[key.solarVoltage] = "\(solarV)"
- // 太阳能电流 0108
- let num2 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 8, length: 4))
- print("太阳能电流 \(num2!.floatValue * 0.01)")
- dataDict[key.solarCurrent] = String(format: "%.2f", num2!.floatValue * 0.01)
- //realDataDict[key.solarCurrent] = String(format: "%.2f", num2!.floatValue * 0.01)
- // 太阳能功率 0109
- let num2_1 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 9, length: 4))
- print("太阳能功率 \(num2_1!)")
- dataDict[key.solarPower] = "\(num2_1!)"
- // realDataDict[key.solarPower] = "\(num2_1!)"
- //蓄电池 电压 101
- let num3 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 1, length: 4))
- print("蓄电池电压 \(num3!.floatValue * 0.1)")
- dataDict[key.batteryVoltage] = String(format: "%.1f", num3!.floatValue * 0.1)
- // realDataDict[key.batteryVoltage] = String(format: "%.1f", num3!.floatValue * 0.1)
- //蓄电池电流 102
- let num4 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 2, length: 4))
- print("蓄电池电流\(num4!.floatValue * 0.01)")
- dataDict[key.batteryCurrent] = String(format: "%.2f", num4!.floatValue * 0.01)
- // realDataDict[key.batteryCurrent] = String(format: "%.2f", num4!.floatValue * 0.01)
-
- //蓄电池温度 0103
- let num5 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 3 , length: 2))
- print("温度 \(temp(with: num5!))")
- dataDict[key.batteryTemp] = "\(temp(with: num5!))"
- //realDataDict[key.batteryTemp] = "\(temp(with: num5!))"
- //负载电压 0104
- let num6 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 4, length: 4))
- print("负载电压\(num6!.floatValue * 0.1)")
- dataDict[key.loadVoltage] = String(format: "%.1f", num6!.floatValue * 0.1)
- // realDataDict[key.loadVoltage] = String(format: "%.1f", num6!.floatValue * 0.1)
- //负载电流 0105
- let num7 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 5, length: 4))
- print("负载电流\(num7!.floatValue * 0.01)")
- dataDict[key.loadCurrent] = String(format: "%.2f", num7!.floatValue * 0.01)
- //realDataDict[key.loadCurrent] = String(format: "%.2f", num7!.floatValue * 0.01)
-
- //负载功率 0106
- let num8 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 6, length: 4))
- print("功率 \(num8! )")
- // realDataDict[key.loadPower] = "\(num8!)"
-
- dataDict[key.loadPower] = "\(num8!)"
- }
-
- func realdata_0120(_ reciveData : String) {
- let str0 = reciveData
- log.debug("处理真实数据 - \(str0)")
-
-
- var num0A = UUUtils.getBinaryByHex(str0.subString(start: 11 , length: 8))
- let num0A_1 = num0A?.subString(start: 0, length: 16)
- let num0A_2 = num0A?.subString(start: 16, length: 16)
- let num0A_II = num0A_2! + num0A_1!
- num0A = num0A_II
- log.debug("太阳能板状态\(num0A!)")
-
- var solarStatusStr = ""
- let B30 = num0A?.subString(start: 1, length: 1)
- let B29 = num0A?.subString(start: 2, length: 1)
- let B28 = num0A?.subString(start: 3, length: 1)
- let B27 = num0A?.subString(start: 4, length: 1)
- let B26 = num0A?.subString(start: 5, length: 1)
- let B25 = num0A?.subString(start: 6, length: 1)
- let B24 = num0A?.subString(start: 7, length: 1)
- let B23 = num0A?.subString(start: 8, length: 1)
-
- //负载
- let B20 = num0A?.subString(start: 11, length: 1)
- let B19 = num0A?.subString(start: 12, length: 1)
- let B15 = num0A?.subString(start: 16, length: 1)
- //系统
- let B21 = num0A?.subString(start: 10, length: 1)
- let B14 = num0A?.subString(start: 17, length: 1)
- let B13 = num0A?.subString(start: 18, length: 1)
-
- //蓄电池状态
- let B22 = num0A?.subString(start: 9, length: 1)
- let B18 = num0A?.subString(start: 13, length: 1)
- let B17 = num0A?.subString(start: 14, length: 1)
- let B16 = num0A?.subString(start: 15, length: 1)
- let B12 = num0A?.subString(start: 19, length: 1)
- let B11 = num0A?.subString(start: 20, length: 1)
- let B10 = num0A?.subString(start: 21, length: 1)
-
- var batteryStatusStr = ""
- if B10 == "1" {
- batteryStatusStr = "BMS_overcharge_protection".da_localizedStr()
- }
- if B11 == "1" {
- batteryStatusStr = "Battery_low_temperature_protection,_stop_charging".da_localizedStr()
- }
- if B12 == "1" {
- batteryStatusStr = "Battery_reverse_polarity".da_localizedStr()
- }
- if B16 == "1" {
- batteryStatusStr = "Battery_over_discharge".da_localizedStr()
- }
- if B17 == "1" {
- batteryStatusStr = "Battery_overvoltage".da_localizedStr()
- }
- if B18 == "1" {
- batteryStatusStr = "Undervoltage_warning".da_localizedStr()
- }
- if B22 == "1" {
- batteryStatusStr = "External_ambient_temperature_is_too_high".da_localizedStr()
- }
-
- if B10 == "0" && B11 == "0" && B12 == "0" && B16 == "0" && B17 == "0" && B18 == "0" && B22 == "0"{
- batteryStatusStr = "Normal".da_localizedStr()
- }
- dataDict[key.batteryStatus] = batteryStatusStr
- // realDataDict[key.batteryStatus] = batteryStatusStr
- //太阳能状态
- if B23 == "1" {
- solarStatusStr = "PV_input_power_is_too_large".da_localizedStr()
- }
- if B24 == "1" {
- solarStatusStr = "Short_circuit_of_photovoltaic_input".da_localizedStr()
- }
- if B25 == "1" {
- solarStatusStr = "Overvoltage_at_the_photovoltaic_input".da_localizedStr()
- }
- if B26 == "1" {
- solarStatusStr = "Solar_panel_countercurrent".da_localizedStr()
- }
- if B27 == "1" {
- solarStatusStr = "Solar_panel_working_point_overpressure".da_localizedStr()
- }
- if B28 == "1" {
- solarStatusStr = "Solar_panel_reverse_connection".da_localizedStr()
- }
- dataDict[key.solarStatus] = solarStatusStr
-
-
- //0120
- let num0 = UUUtils.getBinaryByHex(str0.subString(start: 7, length: 4))
- log.debug("0120 --- \(num0!)")
- //0000 0000 0000 0000 从后往前数。 1000 0000 0000 0000
- let loadStatus = "\(num0?.subString(start: 0, length: 1) ?? "")"
- //di低八位
- let low_8_bit = UUUtils.numberHexString(str0.subString(start: 9, length: 2))
-
- log.debug("low_8_bit - \(low_8_bit)")
- //1000000000000000
-
- var changeStatus = ""
- if low_8_bit == 0 {
- changeStatus = "Charging_is_not_turned_on".da_localizedStr()
- }else if low_8_bit == 1 {
- changeStatus = "Start_charging_mode".da_localizedStr()
- }else if low_8_bit == 2 {
- changeStatus = "MPPT_charge_mode".da_localizedStr()
- }else if low_8_bit == 3 {
- changeStatus = "Equalizing_charge_mode".da_localizedStr()
- }else if low_8_bit == 4 {
- changeStatus = "Floating_charge_mode".da_localizedStr()
- }else if low_8_bit == 5 {
- changeStatus = "Boost_charge_mode".da_localizedStr()
- }else if low_8_bit == 6 {
- changeStatus = "Current_limiting_(super_power)".da_localizedStr()
- }else if low_8_bit == 7 {
- changeStatus = "Fully_charged".da_localizedStr()
- }
-
- // realDataDict[key.solarStatus] = solarStatusStr
- if B23 == "0" && B24 == "0" && B25 == "0" && B26 == "0" && B27 == "0" && B28 == "0" {
- dataDict[key.solarStatus] = changeStatus
-
- }
-
- var loadStatusStr = ""
- if B15 == "1" {
- loadStatusStr = "Open_load".da_localizedStr()
- }
- if B19 == "1" {
- loadStatusStr = "Load_short_circuit".da_localizedStr()
- }
- if B20 == "1" {
- loadStatusStr = "Excessive_load_power_or_overload".da_localizedStr()
- }
-
- if B15 == "0" && B19 == "0" && B20 == "0" {
- if loadStatus == "0" {
- loadStatusStr = "Load_is_off".da_localizedStr()
- }else if loadStatus == "1" {
- loadStatusStr = "Load_is_on".da_localizedStr()
- }
- }
-
- log.debug("负载状态 :\(num0?.subString(start: 7, length: 1))") // 低八位
- //充电状态 --- \(0120低八位)
- dataDict[key.loadStatus] = loadStatusStr
- // realDataDict[key.loadStatus] = loadStatusStr
-
- }
- //处理历史数据
- func hisData(_ reciveData:String) {
-
- let str0 = reciveData
- log.debug("处理真实数据 - \(str0)")
-
- var F000Dict = [String: String]()
-
- //蓄电池当天最低电压
- let num0 = UUUtils.numberHexString(str0.subString(start: 7, length: 4))
- log.debug("蓄电池当天最低电压: \( String(format: "%.1f", num0!.floatValue * 0.1))")
- F000Dict[key.his_batteryVoltageMin] = String(format: "%.1f", num0!.floatValue * 0.1)
-
- //蓄电池当天最高电压
- let num1 = UUUtils.numberHexString(str0.subString(start: 7 + 4, length: 4))
-
- log.debug("蓄电池当天最高电压\(String(format: "%.1f", num1!.floatValue * 0.1))")
- F000Dict[key.his_batteryVoltageMax] = String(format: "%.1f", num1!.floatValue * 0.1)
-
- let num2 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 2, length: 4))
- log.debug("当天充电最大电流\(String(format: "%.2f", num2!.floatValue * 0.01))")
- F000Dict[key.his_chargingCurrentMax] = String(format: "%.2f", num2!.floatValue * 0.01)
-
- let num3 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 3, length: 4))
- log.debug("当天放电最大电流\(String(format: "%.2f", num3!.floatValue * 0.01))")
- F000Dict[key.his_dischargeCurrentmax] = String(format: "%.2f", num3!.floatValue * 0.01)
-
- let num4 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 4, length: 4))
- log.debug("当天充电最大功率\(num4!)" )
- F000Dict[key.his_chargingPowerMax] = "\(num4!)"
- let num5 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 5 , length: 4))
- log.debug("当天放电最大功率\(num5!)")
- F000Dict[key.his_dischargePowerMax] = "\(num5!)"
- let num6 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 6, length: 4))
- log.debug("充电安时数 \(num6!)")
- F000Dict[key.his_chargingAmpereHour] = "\(num6!)"
- let num7 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 7 , length: 4))
- log.debug("放电安时数 \(num7!)")
- F000Dict[key.his_dischargeAmpereHour] = "\(num7!)"
- let num8 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 8, length: 4))
- log.debug("当天发电量 \(num8!)")
- F000Dict[key.his_generationCapacity] = "\(num8!)"
- let num9 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 9, length: 4))
- log.debug("充电量 \(num9!)")
- F000Dict[key.his_dischargeCapacity] = "\(num9!)"
-
- historyDataArr.append(F000Dict)
- }
-
- func realdata_F000(_ reciveData : String) {
- hisData(reciveData)
-
- }
-
- func realdata_F001(_ reciveData : String) {
- hisData(reciveData)
- }
-
- func realdata_F002(_ reciveData : String) {
- hisData(reciveData)
- }
-
- func realdata_F003(_ reciveData : String) {
- hisData(reciveData)
- }
-
- func realdata_F004(_ reciveData : String) {
- hisData(reciveData)
- }
-
-
- func realdata_F005(_ reciveData : String) {
- hisData(reciveData)
- }
-
- func realdata_F006(_ reciveData : String) {
- hisData(reciveData)
- //dataDict[key.historydatas] = historyDataArr
- }
- func realdata_F007(_ reciveData : String) {
- hisData(reciveData)
- dataDict[key.historydatas] = historyDataArr
- }
-
- func realdata_0115(_ reciveData :String) {
- let str0 = reciveData
- log.debug("处理真实数据 - \(str0)")
- //
- let num0 = UUUtils.numberHexString(str0.subString(start: 7 , length: 4))
- log.debug("总运行天数 - \(num0!)")
- dataDict[key.runTime] = ("\(num0!)")
-
- let num1 = UUUtils.numberHexString(str0.subString(start: 7 + 4 , length: 4))
- log.debug("蓄电池总过放次数 - \(num1!)")
- dataDict[key.overTimes] = "\(num1!)"
-
- let num2 = UUUtils.numberHexString(str0.subString(start: 7 + 8 , length: 4))
- log.debug("蓄电池总充满次数 - \(num2!)")
- dataDict[key.fullTimes] = "\(num2!)"
-
- let num3 = UUUtils.numberHexString(str0.subString(start: 7 + 12 , length: 8))
- log.debug("蓄电池总充电安时数 - \(num3!)")
- dataDict[key.chargingAmpereHour] = "\(num3!)"
-
- let num4 = UUUtils.numberHexString(str0.subString(start: 7 + 12 + 8 , length: 8))
- log.debug("蓄电池总放电安时数 - \(num4!)")
- dataDict[key.dischargeAmpereHour] = "\(num4!)"
-
- let num5 = UUUtils.numberHexString(str0.subString(start: 7 + 12 + 8 * 2 , length: 8))
- log.debug("累计发电量 - \(num5!)")
- dataDict[key.generatEnergyTotal] = "\(num5!)"
-
-
- let num6 = UUUtils.numberHexString(str0.subString(start: 7 + 12 + 8 * 3, length: 8))
- log.debug("累计用电量 - \(num6!)")
- dataDict[key.usedEnergyTotal] = "\(num6!)"
- }
-
- func realdata_E001(_ reciveData : String) {
- let str0 = reciveData
- log.debug("处理真实数据 - \(str0)")
- //e001
- // 判断是否自动识别
- let num0 = UUUtils.numberHexString(str0.subString(start: 7 , length: 4))
- log.debug(" 最大充电电流- \(String(format: "%.2f", num0!.floatValue * 0.01))")
- dataDict[key.MaxChargeCurrent] = "\(String(format: "%.2f", num0!.floatValue * 0.01))"
- }
-
- func realdata_E003(_ reciveData : String) {
- let str0 = reciveData
- log.debug("处理真实数据 - \(str0)")
-
- //e003
- let FF = str0.subString(start: 7, length: 2)
-
- if FF == "FF" || FF == "ff"{
- dataDict[key.autoRecognition] = "1"
- // 判断是否自动识别
- let num0 = UUUtils.numberHexString(str0.subString(start: 7 + 2 , length: 2))
- log.debug("系统电压设置 - \(num0!)")
- if num0 == 0 {
- dataDict[key.BatRateVolt] = "Auto".da_localizedStr()
- dataDict[key.autoRecognition] = "1"
- }else{
- dataDict[key.BatRateVolt] = "\(num0!)"
- }
-
-
- }else{
- dataDict[key.autoRecognition] = "0"
- // 判断是否自动识别
- let num0 = UUUtils.numberHexString(str0.subString(start: 7 , length: 2))
- log.debug("系统电压设置 - \(num0!)")
- if num0 == 0 {
- dataDict[key.BatRateVolt] = "Auto".da_localizedStr()
- dataDict[key.autoRecognition] = "1"
- }else{
- dataDict[key.BatRateVolt] = "\(num0!)"
- }
- }
- // // FF
- // let FF = str0.subString(start: 7 , length: 2)
- // if FF == "FF"{
- // dataDict[key.autoRecognition] = "1"
- // //paramDataDict[key.autoRecognition] = "1"
- // }else{
- // dataDict[key.autoRecognition] = "0"
- // // paramDataDict[key.autoRecognition] = "0"
- // }
- // 判断是否自动识别
- let num0 = UUUtils.numberHexString(str0.subString(start: 7 + 2 , length: 2))
- log.debug("系统电压设置 - \(num0!)")
- dataDict[key.BatRateVolt] = "\(num0!)"
- // paramDataDict[key.BatRateVolt] = "\(num0!)"
- //e004
- let num1 = UUUtils.numberHexString(str0.subString(start: 7 + 4 , length: 4))
-
- var batterytype = ""
- switch num1?.intValue {
- case 0:
- batterytype = "User-defined".da_localizedStr()
- case 1:
- batterytype = "Vented_lead_acid_battery_FLD".da_localizedStr()
- case 2:
- batterytype = "Lead_acid_battery_SLD".da_localizedStr()
- case 3:
- batterytype = "Gel_lead_acid_GEL".da_localizedStr()
- case 4:
- batterytype = "Lithium_iron_phosphate_battery_LI".da_localizedStr()
- case 5:
- batterytype = "三元锂电池".da_localizedStr()
- case 6:
- batterytype = "Lithium_iron_phosphate_battery_LI".da_localizedStr()
- case 7:
- batterytype = "超级电容".da_localizedStr()
- case 10:
- batterytype = "自定义(铅酸专用)".da_localizedStr()
- default: break
-
- }
- log.debug("电池类型: \(batterytype)")
- dataDict[key.BatTypeSet] = "\(batterytype)"
- //e005
- let num2 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 2 , length: 4))
- log.debug("超压电压: \(num2!.floatValue * 0.1)")
- dataDict[key.BatOverVolt] = "\(num2!.floatValue * 0.1)"
- //e006
- let num3 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 3, length: 4))
- //BatChgLimitVolt
- log.debug("充电限制电压:\(num3!.floatValue * 0.1)")
- dataDict[key.BatChgLimitVolt] = "\(num3!.floatValue * 0.1)"
- //e007
- let num4 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 4, length: 4))
- log.debug("均衡充电电压\(num4!.floatValue * 0.1)")
- dataDict[key.BatConstChgVolt] = String(format: "%.1f", num4!.floatValue * 0.1)
- //e008
- let num5 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 5, length: 4))
- log.debug("提升充电电压/过充返回电压\(num5!.floatValue * 0.1)")
- dataDict[key.BatImprovChgVolt] = "\(String(format: "%.1f", num5!.floatValue * 0.1))"
- //e009
- let num6 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 6, length: 4))
- log.debug("浮充充电电压/过充返回电压\(num6!.floatValue * 0.1)")
- dataDict[key.BatFloatChgVolt] = "\(num6!.floatValue * 0.1)"
- //e00a
- let num7 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 7, length: 4))
- log.debug("提升充电返回电压\(num7!.floatValue * 0.1)")
-
- dataDict[key.BatImprovChgBackVolt] = String(format: "%.1f", num7!.floatValue * 0.1)
- //e00b
- let num8 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 8, length: 4))
- log.debug("过放返回电压\(num8!.floatValue * 0.1)")
- dataDict[key.BatOverDischgBackVolt] = "\(num8!.floatValue * 0.1)"
- //paramDataDict[key.BatOverDischgBackVolt] = "\(num8!.floatValue * 0.1)"
- //e00c
- let num9 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 9, length: 4))
- log.debug("欠压警告电压\(num9!.floatValue * 0.1)")
- dataDict[key.BatUnderVolt] = "\(num9!.floatValue * 0.1)"
- //e00d
- let num10 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 10, length: 4))
- log.debug("过放电压\(num10!.floatValue * 0.1)")
- dataDict[key.BatOverDischgVolt] = "\(num10!.floatValue * 0.1)"
- //e00e
- let num11 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 11, length: 4))
- log.debug("放电限制电压\(num11!.floatValue * 0.1)")
- dataDict[key.BatDischgLimitVolt] = "\(num11!.floatValue * 0.1)"
- //e010
- let num13 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 13, length: 4))
- log.debug("过放延时时间\(num13!)")
- dataDict[key.BatOverDischgDelayTime] = "\(num13!)"
- //e011
- let num14 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 14, length: 4))
- log.debug("均衡充电时间\(num14!)")
- dataDict[key.BatConstChgTime] = "\(num14!)"
- //e012
- let num15 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 15, length: 4))
- log.debug("提升充电时间 \(num15!)")
- dataDict[key.BatImprovChgTime] = "\(num15!)"
- //e013
- let num16 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 16, length: 4))
- log.debug("均衡充电间隔\(num16!)")
- dataDict[key.BatConstChgGapTime] = "\(num16!)"
- // paramDataDict[key.BatConstChgGapTime] = "\(num16!)"
- //e014
- let num17 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 17, length: 4))
- log.debug("温度补偿系数\(num17!)")
- dataDict[key.CoeffTemperCompen] = "\(num17!)"
- // paramDataDict[key.CoeffTemperCompen] = "\(num17!)"
- //e015
- let num18 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 18, length: 4))
- log.debug("充电上限温度\(temp(with: num18!))")
- dataDict[key.ChgMaxTemper] = "\(temp(with: num18!))"
- // paramDataDict[key.ChgMaxTemper] = "\(temp(with: num18!))"
- //e016
- let num19 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 19, length: 4))
- log.debug("充电下限温度\(temp(with: num19!))")
- dataDict[key.ChgMinTemper] = "\(temp(with: num19!))"
- // paramDataDict[key.ChgMinTemper] = "\(temp(with: num19!))"
- //e017
- let num20 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 20, length: 4))
- log.debug("放电上限温度\(temp(with: num20!))")
- dataDict[key.DisChagMaxTemper] = "\(temp(with: num20!))"
- /// paramDataDict[key.DisChagMaxTemper] = "\(temp(with: num20!))"
- //e018
- let num21 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 21, length: 4))
- log.debug("放电下限温度\(temp(with: num21!))")
- dataDict[key.DisChaMinTemper] = "\(temp(with: num21!))"
- // paramDataDict[key.DisChaMinTemper] = "\(temp(with: num21!))"
- //e01d
- let num26 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 26, length: 4))
-
- var loadwordmode = ""
- switch num26?.intValue {
- case 0:
- loadwordmode = "Light_control".da_localizedStr()
- case 1:
- loadwordmode = "Light_control,_delay_1_hour".da_localizedStr()
- case 2:
- loadwordmode = "Light_control,_delay_2_hours".da_localizedStr()
- case 3:
- loadwordmode = "Light_control,_delay_3_hours".da_localizedStr()
- case 4:
- loadwordmode = "Light_control,_delay_4_hours".da_localizedStr()
- case 5:
- loadwordmode = "Light_control,_delay_5_hours".da_localizedStr()
- case 6:
- loadwordmode = "Light_control,_delay_6_hours".da_localizedStr()
- case 7:
- loadwordmode = "Light_control,_delay_7_hours".da_localizedStr()
- case 8:
- loadwordmode = "Light_control,_delay_8_hours".da_localizedStr()
- case 9:
- loadwordmode = "Light_control,_delay_9_hours".da_localizedStr()
- case 10:
- loadwordmode = "Light_control,_delay_10_hours".da_localizedStr()
- case 11:
- loadwordmode = "Light_control,_delay_11_hours".da_localizedStr()
- case 12:
- loadwordmode = "Light_control,_delay_12_hours".da_localizedStr()
- case 13:
- loadwordmode = "Light_control,_delay_13_hours".da_localizedStr()
- case 14:
- loadwordmode = "Light_control,_delay_14_hours".da_localizedStr()
- case 15:
- loadwordmode = "Manual_mode".da_localizedStr()
- case 16:
- loadwordmode = "Debug_mode".da_localizedStr()
- case 17:
- loadwordmode = "Normal-on_mode".da_localizedStr()
- case 18:
- loadwordmode = "Timed_mode".da_localizedStr()
-
- default:
- break
- }
- log.debug("直流负载工作模式\(loadwordmode)")
- dataDict[key.DcLoadMode] = "\(loadwordmode)"
- // paramDataDict[key.DcLoadMode] = "\(loadwordmode)"
- //e01e
- let num27 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 27, length: 4))
- log.debug("光控延时时间\(num27!)")
- dataDict[key.LightCtrlDelayTime] = "\(num27!)"
- // paramDataDict[key.LightCtrlDelayTime] = "\(num27!)"
- //d01f
- let num28 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 28, length: 4))
- log.debug("光控电压\(num28!)")
- dataDict[key.LightCtrlVolt] = "\(num28!)"
- /// paramDataDict[key.LightCtrlVolt] = "\(num28!)"
- }
-
- func realdata_DF00(_ reciveData : String) {
- let str0 = reciveData
- log.debug("处理真实数据 - \(str0)")
- // <ff030200009190>
- let num0 = UUUtils.numberHexString(str0.subString(start: 7, length: 4))
- log.debug("开关控制 —— \(num0)")
- var cmd = ""
- if num0?.intValue == 0 {
- cmd = "Off".da_localizedStr()
- }else if num0?.intValue == 1{
- cmd = "On".da_localizedStr()
- }
- dataDict[key.CmdPowerOnOff] = cmd
- /// paramDataDict[key.CmdPowerOnOff] = cmd
- }
-
- func realdata_DF0D(_ reciveData : String) {
- let str0 = reciveData
- log.debug("处理真实数据 - \(str0)")
- // <ff030200 009190>
- let num0 = UUUtils.numberHexString(str0.subString(start: 7, length: 4))
- log.debug("手动均衡 —— \(num0)")
- var canhandle = ""
- if num0?.intValue == 0 {
- canhandle = "Prohibited".da_localizedStr()
- }else if num0?.intValue == 1{
- canhandle = "Enable".da_localizedStr()
- }
- dataDict[key.BattEqualChgImmediate] = canhandle
- /// paramDataDict[key.BattEqualChgImmediate] = canhandle
-
- }
-
- func realdata_E021(_ reciveData : String) {
- let str0 = reciveData
- log.debug("处理真实数据 - \(str0)")
- // <ff030201009000> str0.subString(start: 7, length: 4)
- let num0 = UUUtils.getBinaryByHex(str0.subString(start: 7, length: 4))
- log.debug("E021 —— \(num0)")
- //RX
- let rx = num0?.subString(start: 2, length: 3)
-
- var rx_function = ""
- if rx == "000"{
- rx_function = "Normal_communication".da_localizedStr()
- }
- log.debug("Rx端口 \(rx_function)")
- dataDict[key.rx] = "\(rx_function)"
- // paramDataDict[key.rx] = "\(rx_function)"
- let tx = num0?.subString(start: 5, length: 3)
- log.debug("")
-
- var tx_function = ""
- if tx == "000" {
- tx_function = "Normal_communication".da_localizedStr()
- }
- log.debug("tx端口 \(tx_function)")
- dataDict[key.tx] = "\(tx_function)"
- }
-
- }
|