SBTHomeVCtr.swift 110 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485
  1. //
  2. // SBTHomeVCtr.swift
  3. // SolarBT
  4. //
  5. // Created by weclouds on 2019/1/23.
  6. // Copyright © 2019 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. import PKHUD
  10. import Localize_Swift
  11. struct BLEData {
  12. var perpheralName:String?
  13. var device_name :String?
  14. var device_type :String?
  15. var device_model:String?
  16. var online: Bool? // 是否在线
  17. var isOld:Bool?
  18. }
  19. extension Array where Element: Hashable {
  20. func removingDuplicates() -> [Element] {
  21. var addedDict = [Element: Bool]()
  22. return filter {
  23. addedDict.updateValue(true, forKey: $0) == nil
  24. }
  25. }
  26. mutating func removeDuplicates() {
  27. self = self.removingDuplicates()
  28. }
  29. }
  30. class SBTHomeVCtr: BaseViewController {
  31. lazy var leftMenuVC = SBTLeftMenuVCtr()
  32. var snnumber :String? = "0"
  33. var soft :String?
  34. var hard: String?
  35. var deviceTypeNumber : String?
  36. var disconectBlock : (()->Void)?
  37. var emptyView : SBTEmptyView?
  38. var isCurrentVC: Bool? = true //是否当前界面
  39. var isAddDevice:Bool? = false //判断是否是添加设备状态
  40. var isStart :Bool? = true //判断是否 刚开始进入首页
  41. var isAuto : Bool? = false //判断是否自动跳转~
  42. var peripheralInfo = [String:String]()
  43. var isOld : Bool? = true // 判断是否是jiu xie yi
  44. var dataDict : [String: Any] = [String: Any]()
  45. let key: DeviceDictKey = DeviceDictKey()
  46. var historyDataArr: [[String: String]] = [[String: String]]()
  47. static let peripheralPlist = "Peripheral.plist"
  48. static let deviceDetailPlist = "DeviceDetailPlist.plist"
  49. var baseInfoPlist :CLDeviceDataPlist? = CLDeviceDataPlist() //保存基本信息
  50. var detailInfoPlist : CLDeviceDataPlist? = CLDeviceDataPlist()// 详细信息
  51. var RootDict : [String:Any]? = [String:Any]()
  52. var tempArr :[BLEData]? = [BLEData]()
  53. lazy var device = DeviceData()
  54. var dataDictionary : [String : String]?
  55. var isOnline :Bool? {
  56. didSet{
  57. }
  58. }
  59. var isReconnect : Bool? = false {
  60. didSet{
  61. }
  62. }
  63. lazy var tableView: UITableView = {
  64. let tableView = UITableView(frame: CGRect(x: 0, y: 0, width: KSCREENWIDTH, height: KSCREENHEIGHT - KNavBarHeight), style: .plain)
  65. tableView.backgroundColor = UIColor(hexString: "F8F8F8")
  66. tableView.delegate = self
  67. tableView.dataSource = self
  68. tableView.separatorStyle = .none
  69. tableView.register(UINib(nibName: "SBTHomeCell", bundle: nil), forCellReuseIdentifier: "cell")
  70. return tableView
  71. }()
  72. var aPeripherals:[BLEData]?
  73. var baby1:BabyBluetooth?
  74. let deviceAlertVC = SBTAddDeviceVCtr()
  75. var editingIndexPath : IndexPath?
  76. var isConnectBtn :Bool?
  77. let header = SBTHomeTableViewHeader()
  78. }
  79. extension SBTHomeVCtr {
  80. func registGestureShowSide() {
  81. gy_registGestureShowSide { (direction) in
  82. if direction == .left {
  83. self.gy_showSide(configuration: { (config) in
  84. //config.animationType = .
  85. config.sideRelative = 0.5
  86. }, viewController: SBTLeftMenuVCtr())
  87. }
  88. }
  89. }
  90. func createUI() {
  91. //手势冲突
  92. //registGestureShowSide()
  93. setNavgationBar()
  94. let rightBtn = UIBarButtonItem.createBarbuttonItem(image: "添加", highImage: "添加", target: self, action: #selector(addDevice))
  95. self.navigationItem.rightBarButtonItem = rightBtn
  96. let leftBtn = UIBarButtonItem.createBarbuttonItem(image: "menu", highImage: "menu", target: self, action: #selector(goMenu))
  97. self.navigationItem.leftBarButtonItem = leftBtn
  98. self.view.addSubview(tableView)
  99. //设置空数据页面
  100. setupEmptyView()
  101. }
  102. //自定义空数据界面显示
  103. func setupEmptyView() {
  104. emptyView = Bundle.main.loadNibNamed("SBTEmptyView", owner: self, options: nil)?.last as! SBTEmptyView
  105. emptyView!.addButton.addTarget(self, action: #selector(addDevice), for: .touchUpInside)
  106. emptyView!.frame = view.bounds
  107. emptyView!.buttonStr = "Add_it_now".da_localizedStr()
  108. emptyView!.messageStr = "No_devices_are_currently_added".da_localizedStr()
  109. emptyView!.reloadView()
  110. //空数据界面显示
  111. let emptyV : HDEmptyView = HDEmptyView.emptyViewWithCustomView(customView: emptyView!) as! HDEmptyView
  112. tableView.ly_emptyView = emptyV
  113. tableView.ly_emptyView?.tapContentViewBlock = {
  114. print("点击界面空白处")
  115. }
  116. tableView.ly_showEmptyView()
  117. }
  118. @objc func updateEmptyViewLanguage() {
  119. self.emptyView!.buttonStr = "Add_it_now".da_localizedStr()
  120. self.emptyView!.messageStr = "No_devices_are_currently_added".da_localizedStr()
  121. self.emptyView!.reloadView()
  122. self.tableView.reloadData()
  123. }
  124. //设置按钮
  125. func configSwipeButtons() {
  126. //iOS 11 层级 UITableView -> UISwipActionPull
  127. if #available(iOS 11, *) {
  128. for subview in tableView.subviews{
  129. if subview.isKind(of: NSClassFromString("UISwipeActionPullView")!) && subview.subviews.count >= 2 {
  130. let deleteButton = subview.subviews[1] as!UIButton
  131. let connectButton = subview.subviews[0] as!UIButton
  132. configDelegateButton(deleteButton)
  133. configConnectButton(connectButton)
  134. }else if subview.isKind(of: NSClassFromString("UISwipeActionPullView")!) && subview.subviews.count == 1 {
  135. let connectButton = subview.subviews[0] as!UIButton
  136. configDisConnectButton(connectButton)
  137. }
  138. }
  139. }else{
  140. //iOS 8-10: 画UITableView->UITableViewCell->UITableVIewCellDeleteConfirmationView
  141. let tabCell = tableView.cellForRow(at: self.editingIndexPath!)
  142. for subview in (tabCell?.subviews)!{
  143. if subview.isKind(of: NSClassFromString("UITableViewCellDeleteConfirmationView")!) && subview.subviews.count >= 2{
  144. let deleteButton = subview.subviews[0] as!UIButton
  145. let connectButton = subview.subviews[1] as!UIButton
  146. configDelegateButton(deleteButton)
  147. configConnectButton(connectButton)
  148. }else if subview.isKind(of: NSClassFromString("UITableViewCellDeleteConfirmationView")!) && subview.subviews.count == 1 {
  149. let connectButton = subview.subviews[0] as!UIButton
  150. configDisConnectButton(connectButton)
  151. }
  152. }
  153. }
  154. }
  155. func configDelegateButton(_ deleteButton: UIButton) {
  156. deleteButton.setBackgroundImage(UIImage(named: "白色-删除"), for: .normal)
  157. deleteButton.setBackgroundImage(UIImage(named: "白色-删除"), for: .selected)
  158. deleteButton.backgroundColor = UIColor.white
  159. deleteButton.setTitle("Delete".da_localizedStr(), for: .normal)
  160. deleteButton.setTitleColor(UIColor(hexString: "#888888"), for: .normal)
  161. deleteButton.titleLabel?.font = UIFont(name: PingFangSC_Medium, size: 12)
  162. }
  163. func configConnectButton(_ connectButton: UIButton) {
  164. // fillCode
  165. connectButton.setBackgroundImage(UIImage(named: "绿色-重新连接"), for: .normal)
  166. connectButton.setBackgroundImage(UIImage(named: "绿色-重新连接"), for: .selected)
  167. connectButton.setTitle("Reconnect".da_localizedStr(), for: .normal)
  168. connectButton.setTitleColor(UIColor(hexString: "#FFFFFF"), for: .normal)
  169. connectButton.titleLabel?.font = UIFont(name: PingFangSC_Medium, size: 12)
  170. }
  171. func configDisConnectButton(_ connectButton: UIButton) {
  172. // fillCode
  173. connectButton.setBackgroundImage(UIImage(named: "橙色-断开"), for: .normal)
  174. connectButton.setBackgroundImage(UIImage(named: "橙色-断开"), for: .selected)
  175. connectButton.setTitle("Disconnect".da_localizedStr(), for: .normal)
  176. connectButton.setTitleColor(UIColor(hexString: "#FFFFFF"), for: .normal)
  177. connectButton.titleLabel?.font = UIFont(name: PingFangSC_Medium, size: 12)
  178. }
  179. }
  180. extension SBTHomeVCtr : UITableViewDelegate,UITableViewDataSource {
  181. func numberOfSections(in tableView: UITableView) -> Int {
  182. return 1
  183. }
  184. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  185. return aPeripherals!.count
  186. }
  187. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  188. return 0.01
  189. }
  190. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  191. return 86
  192. }
  193. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  194. let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! SBTHomeCell
  195. cell.backgroundColor = UIColor(hexString: "F8F8F8")
  196. cell.selectionStyle = .none
  197. cell.preservesSuperviewLayoutMargins = false
  198. let peripheral = self.aPeripherals![indexPath.row]
  199. cell.bleData = peripheral
  200. log.debug("aaaddd \(indexPath.row)---- \(peripheral.device_name)")
  201. return cell
  202. }
  203. func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
  204. if self.tableView.responds(to: #selector(setter: UITableViewCell.separatorInset)) {
  205. self.tableView.separatorInset = UIEdgeInsets(top: 0, left: 25, bottom: 0, right: 15)
  206. }
  207. //iOS 8 之后 // cell的间隔
  208. if self.tableView.responds(to: #selector(setter: UIView.layoutMargins)) {
  209. self.tableView.separatorInset = UIEdgeInsets(top: 0, left: 25, bottom: 0, right: 25)
  210. }
  211. }
  212. //iOS 11 将进入此方法 actions.performsFirstActionWithFullSwipe = NO 可以控制是否自动删除。
  213. func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
  214. let perpheral: BLEData = self.aPeripherals![indexPath.row]
  215. let devicename = perpheral.device_name
  216. let devicetype = perpheral.device_type
  217. let devicemodel = perpheral.device_model
  218. let perpheralName = perpheral.perpheralName
  219. let isOld = perpheral.isOld
  220. //拼接蓝牙名字
  221. let BLEName = perpheralName
  222. if perpheral.online!{
  223. let disconnectAction = UIContextualAction(style: .normal, title: "断开") { (action, sourceView, completionHandler) in
  224. completionHandler(true)
  225. log.debug("断开连接")
  226. //处理UI
  227. // self.aPeripherals?.remove(at: indexPath.row)
  228. let per2 = BLEData(perpheralName: perpheralName, device_name: devicename, device_type: devicetype, device_model: devicemodel, online: false,isOld: isOld)
  229. //修改元素
  230. self.aPeripherals![indexPath.row] = per2
  231. log.debug("断开连接 \(self.aPeripherals)")
  232. self.tableView.reloadData()
  233. BabyBluetoothSwift.shareInstance()?.cancelAllPeripheralsConnection()
  234. //重新扫描
  235. BabyBluetoothSwift.shareInstance()?.scanForPeripherals()
  236. }
  237. let actions = UISwipeActionsConfiguration(actions: [disconnectAction])
  238. return actions
  239. }else{
  240. let deleteAction = UIContextualAction(style: .normal, title: "删除") { (action, sourceView, completionHandler) in
  241. completionHandler(true)
  242. log.debug("删除")
  243. self.aPeripherals?.remove(at: indexPath.row)
  244. self.reloadTableView()
  245. self.isReconnect = false // 删除数据
  246. self.tableView.reloadData()
  247. let key = devicetype! + devicemodel!
  248. //删除缓存数据
  249. self.baseInfoPlist!.deleteData(SBTHomeVCtr.peripheralPlist, key: key)
  250. }
  251. let disconnectAction = UIContextualAction(style: .normal, title: "重新连接") { (action, sourceView, completionHandler) in
  252. completionHandler(true)
  253. // BabyBluetoothSwift.shareInstance()?.cancelAllPeripheralsConnection()
  254. HUD.flash(.progress)
  255. self.isReconnect = true
  256. if BabyBluetoothSwift.shareInstance()?.peripheralExits(atPeripheralName: BLEName) == true {
  257. //BabyBluetoothSwift.shareInstance()?.reconnectPeripheral(withName: BLEName)
  258. // BabyBluetoothSwift.shareInstance()?.connectPeripheral(withName: BLEName)
  259. self.aPeripherals?.remove(at: indexPath.row)
  260. let per2 = BLEData(perpheralName:perpheralName,device_name: devicename, device_type: devicetype, device_model: devicemodel, online: true,isOld: isOld)
  261. self.aPeripherals?.append(per2)
  262. //删除重复的
  263. self.tableView.reloadData()
  264. BabyBluetoothSwift.shareInstance()?.connectPeripheral(withName:perpheralName, connectCompletionHandler: { (periph, isSuccess) in
  265. if isSuccess == false{
  266. HUD.flash(.error, delay: 1)
  267. return
  268. }
  269. HUD.flash(.success, delay: 1)
  270. log.debug("连接成功 --\(periph?.name)")
  271. let per2 = BLEData(perpheralName:perpheralName,device_name: devicename, device_type: devicetype, device_model: devicemodel, online: true,isOld: isOld)
  272. //self.aPeripherals?.append(per2)
  273. self.aPeripherals![indexPath.row] = per2
  274. //删除重复的
  275. self.tableView.reloadRows(at: [indexPath], with: .fade)
  276. let bleKey = devicetype! + devicemodel!
  277. let alert = SBTReadLoadingVCtr(nibName: "SBTReadLoadingVCtr", bundle: nil)
  278. alert.deviceName = devicename
  279. alert.message = "Data_loading".da_localizedStr()
  280. alert.show()
  281. // alert.dismiss(7)
  282. self.readDeviceData(bleKey, devicename: devicename!, isOld: isOld!, peripheralName: perpheralName!, completehandle: {
  283. alert.dismiss()
  284. })
  285. })
  286. }else{
  287. }
  288. }
  289. let actions = UISwipeActionsConfiguration(actions: [deleteAction,disconnectAction])
  290. return actions
  291. }
  292. }
  293. ///判断 是否包含当前设备
  294. @objc func removeRow(_ indexPath : IndexPath) {
  295. self.aPeripherals?.remove(at: indexPath.row)
  296. self.tableView.reloadData()
  297. }
  298. @objc func rescan() {
  299. BabyBluetoothSwift.shareInstance()?.scanForPeripherals()
  300. }
  301. @objc func reconnect2(_ devicename:String) {
  302. Thread.sleep(forTimeInterval: 0.01)
  303. BabyBluetoothSwift.shareInstance()?.connectPeripheral(withName: devicename)
  304. log.debug("重新连接 \(aPeripherals)")
  305. }
  306. func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath)
  307. {
  308. log.debug("editingStyle")
  309. if editingStyle == UITableViewCell.EditingStyle.delete {
  310. }
  311. }
  312. //滑动删除
  313. func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
  314. return true
  315. }
  316. func tableView(_ tableView: UITableView, willBeginEditingRowAt indexPath: IndexPath) {
  317. self.editingIndexPath = indexPath
  318. view.setNeedsLayout() //出发 viewDidLayoutSubviews()
  319. }
  320. func tableView(_ tableView: UITableView, didEndEditingRowAt indexPath: IndexPath?) {
  321. log.debug("self.editingIndexPath -- \(self.editingIndexPath)")
  322. self.editingIndexPath = nil
  323. }
  324. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  325. if self.aPeripherals?.count == 0{ return }
  326. let model: BLEData = self.aPeripherals![indexPath.row]
  327. let peripheralName = model.perpheralName
  328. let devicename = model.device_name
  329. let devicetype = model.device_type
  330. let device_model = model.device_model
  331. let isOld = model.isOld
  332. AppShare.perihperalName = peripheralName
  333. if model.online == true {
  334. let belKey = devicetype! + device_model!
  335. let user = UserDefaults.standard
  336. //user.set(bleKey, forKey: "SavePeripheral")
  337. let periName = user.value(forKey: "SavePeripheral") as? String
  338. // if periName == peripheralName{
  339. // self.readPeripheraldata(belKey, deviceName: devicename!, isOld: isOld!, perihperalName: peripheralName!)
  340. // }else{
  341. let alert = SBTReadLoadingVCtr(nibName: "SBTReadLoadingVCtr", bundle: nil)
  342. alert.deviceName = " "
  343. alert.message = "Data_loading".da_localizedStr()
  344. alert.show()
  345. self.bleHasBeemDisconnectect {
  346. alert.dismiss()
  347. return
  348. }
  349. var isSussess = false
  350. self.readDeviceData(belKey, devicename: devicename!, isOld: isOld!, peripheralName: peripheralName!, completehandle: {
  351. alert.dismiss()
  352. isSussess = true
  353. })
  354. delay(15) {
  355. if isSussess == false{
  356. alert.dismiss()
  357. }
  358. }
  359. //}
  360. }else{
  361. if self.aPeripherals!.count > 1{
  362. for var mod in self.aPeripherals!{
  363. if mod.perpheralName != peripheralName && mod.online == true{
  364. BabyBluetoothSwift.shareInstance()?.cancelAllPeripheralsConnection()
  365. mod.online = false
  366. }
  367. }
  368. }
  369. if BabyBluetoothSwift.shareInstance()?.peripheralExits(atPeripheralName: peripheralName) == true {
  370. HUD.show(.progress)
  371. BabyBluetoothSwift.shareInstance()?.connectPeripheral(withName: peripheralName, connectCompletionHandler: { (periph, isSuccess) in
  372. if isSuccess == false {
  373. HUD.flash(.error, delay: 1)
  374. return
  375. }
  376. HUD.flash(.success, delay: 1)
  377. log.debug("连接成功 --\(periph?.name)")
  378. }, startWriteDataCallback: {
  379. self.readDeviceData(for: indexPath, peripheralName: peripheralName!, devicename: devicename!, devicetype: devicetype!, device_model: device_model!, isOld: isOld!)
  380. })
  381. }else{
  382. g_showHUD("unfoundDevice".da_localizedStr())
  383. }
  384. // }
  385. }
  386. }
  387. func readDeviceData(for indexPath:IndexPath, peripheralName:String,devicename:String,devicetype:String,device_model:String,isOld:Bool) {
  388. log.debug("")
  389. let per2 = BLEData(perpheralName:peripheralName,device_name: devicename, device_type: devicetype, device_model: device_model, online: true,isOld: isOld)
  390. //self.aPeripherals?.append(per2)
  391. self.aPeripherals![indexPath.row] = per2
  392. //删除重复的
  393. self.tableView.reloadRows(at: [indexPath], with: .fade)
  394. let bleKey = devicetype + device_model
  395. let alert = SBTReadLoadingVCtr(nibName: "SBTReadLoadingVCtr", bundle: nil)
  396. alert.deviceName = " "
  397. alert.message = "Data_loading".da_localizedStr()
  398. alert.show()
  399. self.bleHasBeemDisconnectect {
  400. alert.dismiss()
  401. return
  402. }
  403. // alert.dismiss(7)
  404. var isSussess = false
  405. self.readDeviceData(bleKey, devicename: devicename, isOld: isOld, peripheralName: peripheralName, completehandle: {
  406. let user = UserDefaults.standard
  407. user.set(peripheralName, forKey: "SavePeripheral")
  408. user.synchronize()
  409. alert.dismiss()
  410. isSussess = true
  411. })
  412. delay(15) {
  413. if isSussess == false{
  414. alert.dismiss()
  415. }
  416. }
  417. }
  418. func readDeviceData(_ bleKey:String,devicename:String,isOld:Bool,peripheralName:String,completehandle:@escaping (()->Void)) {
  419. if isOld == true{
  420. let arr = ["FF03000A0001","FF03000B000F","FF030100000A","FF0301200003","FF03F000000A","FF03F001000A","FF03F002000A","FF03F003000A","FF03F004000A","FF03F005000A","FF03F006000A","FF03F007000A","FF030115000B","FF03E0010001","FF03E003001E","FF03DF000001","FF03DF0D0001","FF03E0210001"]
  421. BabyBluetoothSwift.shareInstance()?.writeData2222(arr, completionHandler: { (result) in
  422. log.debug("返回数据 --- \(ReceiveData)")
  423. self.realdata_000A(result!["FF03000A0001"] as! String)
  424. self.real_000B000F(result!["FF03000B000F"] as! String)
  425. self.realdata_0100(result!["FF030100000A"] as! String)
  426. self.realdata_0120(result!["FF0301200003"] as! String)
  427. self.historyDataArr.removeAll()
  428. self.realdata_F000(result!["FF03F000000A"] as! String)
  429. self.realdata_F001(result!["FF03F001000A"] as! String)
  430. self.realdata_F002(result!["FF03F002000A"] as! String)
  431. self.realdata_F003(result!["FF03F003000A"] as! String)
  432. self.realdata_F004(result!["FF03F004000A"] as! String)
  433. self.realdata_F005(result!["FF03F005000A"] as! String);
  434. self.realdata_F006(result!["FF03F006000A"] as! String);
  435. self.realdata_F007(result!["FF03F007000A"] as! String);
  436. self.realdata_0115(result!["FF030115000B"] as! String)
  437. self.realdata_E001(result!["FF03E0010001"] as! String);
  438. self.realdata_E003(result!["FF03E003001E"] as! String);
  439. self.realdata_DF00(result!["FF03DF000001"] as! String);
  440. self.realdata_DF0D(result!["FF03DF0D0001"] as! String)
  441. self.realdata_E021(result!["FF03E0210001"] as! String)
  442. AppShare.dataDict = self.dataDict
  443. if AppShare.isRefresh == true {
  444. NotificationCenter.default.post(name: NSNotification.Name(kBabyBluetoothNotificationAllDataRecieve), object: nil)
  445. }
  446. //alert.dismiss()
  447. completehandle()
  448. self.readPeripheraldata(bleKey, deviceName: devicename, isOld: isOld, perihperalName: peripheralName)
  449. })
  450. }else{
  451. let arr = ["FF03000A0001","FF03000B000F","FF0300490020","FF0300FD000D","FF03F000000A","FF03F001000A","FF03F002000A","FF03F003000A","FF03F004000A","FF03F005000A","FF03F006000A","FF03F007000A","FF030115000B","FF03E0010001","FF03E003001E","FF03DF000001","FF03DF0D0001","FF03E0210001"]
  452. BabyBluetoothSwift.shareInstance()?.writeData2222(arr, completionHandler: { (result) in
  453. self.realdata_000A(result!["FF03000A0001"] as! String)
  454. self.real_000B000F(result!["FF03000B000F"] as! String)
  455. self.readdata_0049(result!["FF0300490020"] as! String)
  456. self.realdata_00FD(result!["FF0300FD000D"] as! String)
  457. self.historyDataArr.removeAll()
  458. self.realdata_F000(result!["FF03F000000A"] as! String)
  459. self.realdata_F001(result!["FF03F001000A"] as! String)
  460. self.realdata_F002(result!["FF03F002000A"] as! String)
  461. self.realdata_F003(result!["FF03F003000A"] as! String)
  462. self.realdata_F004(result!["FF03F004000A"] as! String)
  463. self.realdata_F005(result!["FF03F005000A"] as! String);
  464. self.realdata_F006(result!["FF03F006000A"] as! String);
  465. self.realdata_F007(result!["FF03F007000A"] as! String);
  466. self.realdata_0115(result!["FF030115000B"] as! String)
  467. self.realdata_E001(result!["FF03E0010001"] as! String);
  468. self.realdata_E003(result!["FF03E003001E"] as! String);
  469. self.realdata_DF00(result!["FF03DF000001"] as! String);
  470. self.realdata_DF0D(result!["FF03DF0D0001"] as! String)
  471. self.realdata_E021(result!["FF03E0210001"] as! String)
  472. AppShare.dataDict = self.dataDict
  473. if AppShare.isRefresh == true {
  474. NotificationCenter.default.post(name: NSNotification.Name(kBabyBluetoothNotificationAllDataRecieve), object: nil)
  475. }
  476. completehandle()
  477. //alert.dismiss()
  478. self.readPeripheraldata(bleKey, deviceName: devicename, isOld: isOld, perihperalName: peripheralName)
  479. })
  480. }
  481. }
  482. @objc func readPeripheral(_ paramArray:[Any]) {
  483. readPeripheraldata(paramArray.first! as! String, deviceName: paramArray[1] as! String, isOld: paramArray[2] as! Bool, perihperalName: paramArray.last! as!String)
  484. }
  485. @objc func readPeripheraldata(_ bleKey:String,deviceName:String,isOld:Bool,perihperalName:String) {
  486. log.debug("sjfkaefkoanmefkoam===\(dataDict)")
  487. AppShare.perihperalName = perihperalName
  488. //保存数据
  489. detailInfoPlist?.saveData111(SBTHomeVCtr.deviceDetailPlist, key: bleKey, deviceData: dataDict)
  490. let vc = SBTDeviceDetaiVCtr()
  491. vc.plist_node = bleKey
  492. vc.isOld = isOld
  493. let nvc = self.navigationController as! RootNavigationController
  494. nvc.pushViewController(vc)
  495. }
  496. }
  497. //Action 深圳市硕日新能源科技有限公司来了
  498. // <ff100049 004080 6df1 5733 5e02 7855 65e5 65b0 80fd 6e90 79d1 6280 6709 516c 53f8 4f60 4eec 597d c574>
  499. // <ff0320 6d f157335e 02785565 e565b080 fd6e9079 d1628067 09516c53 f84f604e ec000031 ff>
  500. extension SBTHomeVCtr{
  501. //点击添加设备
  502. @objc func addDevice() {
  503. self.isAuto = true
  504. log.debug("添加设备")
  505. //搜索设备
  506. BabyBluetoothSwift.shareInstance()?.scanForPeripherals()
  507. deviceAlertVC.show()
  508. deviceAlertVC.addDeviceLabel.text = "Add_device".da_localizedStr()
  509. if "Add_device".da_localizedStr() == "Add device" {
  510. deviceAlertVC.langua = "en"
  511. }else if "Add_device".da_localizedStr() == "添加设备"{
  512. deviceAlertVC.langua = "cn"
  513. }
  514. deviceAlertVC.reScanBtn.setTitle("Rescan".da_localizedStr(), for: .normal)
  515. deviceAlertVC.closeBtn.setTitle("Colse".da_localizedStr(), for: .normal)
  516. deviceAlertVC.completionCallback = {[weak self](peripheralname) in
  517. //清空数组
  518. self!.aPeripherals?.removeAll()
  519. log.debug("连接成功---\(peripheralname)")
  520. for mod in self!.aPeripherals! {
  521. let key = mod.device_type! + mod.device_model!
  522. //删除缓存数据
  523. self!.baseInfoPlist!.deleteData(SBTHomeVCtr.peripheralPlist, key: key)
  524. }
  525. self?.isAddDevice = true
  526. var model = BLEData()
  527. model.perpheralName = peripheralname;
  528. self!.tempArr?.append(model)
  529. /*检查读写状态,连接之后是否可以写*/
  530. // delay(3) {
  531. // }
  532. BLEConnectUtils.shared.checkWriteStatus {
  533. log.debug("开始读取数据")
  534. BabyBluetoothSwift.shareInstance()?.writeData("FF03000B000F", completionHandler: { (result) in
  535. if result == nil{
  536. log.debug("没有返回结果")
  537. }else{
  538. log.debug("返回结果为--- \(result)")
  539. self?.reloadDeviceUI_1(result!)
  540. }
  541. })
  542. }
  543. }
  544. }
  545. func reloadTableView(){
  546. // let header :SBTHomeTableViewHeader = self.tableView.headerView(forSection: 0)
  547. let total = aPeripherals?.count
  548. var device0 = 0
  549. var device1 = 0
  550. var device2 = 0
  551. for perpheral in aPeripherals! {
  552. // log.info("====== \(perpheral.device_type)")
  553. if perpheral.device_type == "0"{
  554. device0 += 1
  555. log.debug(device0)
  556. }else if perpheral.device_type == "3"{
  557. device1 += 1
  558. }else if perpheral.device_type == "5"{
  559. device2 += 1
  560. }
  561. }
  562. let deviceCount = ["\(total ?? 0)","\(device0)","\(device1)","\(device2)"]
  563. header.deviceCounts = deviceCount
  564. header.reloadDeviceCount()
  565. }
  566. @objc func goMenu() {
  567. log.debug("打开侧栏 -----")
  568. let conf = CWLateralSlideConfiguration.default()
  569. conf?.distance = 0.5
  570. self.cw_showDrawerViewController(leftMenuVC, animationType: .default, configuration: nil)
  571. }
  572. }
  573. extension SBTHomeVCtr{
  574. override func viewDidLoad() {
  575. super.viewDidLoad()
  576. checkTheBlueToothWasActivity()
  577. notificationRegister()
  578. baby1 = BabyBluetooth.share()
  579. self.sbtTitle = "Device_list".da_localizedStr()
  580. LocalizationManager.shareInstance().callback = {
  581. self.sbtTitle = "Device_list".da_localizedStr()
  582. }
  583. isConnectBtn = false
  584. self.aPeripherals = [BLEData]()
  585. createUI()
  586. self.view.backgroundColor = UIColor.white
  587. // Do any additional setup after loading the view.
  588. self.babyDelegate()
  589. //创建 plist文件
  590. baseInfoPlist!.createPlistFile(SBTHomeVCtr.peripheralPlist)
  591. detailInfoPlist?.createPlistFile(SBTHomeVCtr.deviceDetailPlist)
  592. }
  593. override func viewDidDisappear(_ animated: Bool) {
  594. super.viewDidDisappear(animated)
  595. self.isCurrentVC = false
  596. self.isStart = false
  597. BabyBluetoothSwift.shareInstance()?.cancelScan()
  598. notificationUnRegister()
  599. }
  600. override func viewDidLayoutSubviews() {
  601. super.viewDidLayoutSubviews()
  602. if self.editingIndexPath != nil {
  603. configSwipeButtons()
  604. }
  605. }
  606. override func viewDidAppear(_ animated: Bool) {
  607. super.viewDidAppear(animated)
  608. // getDevieData()
  609. BabyBluetoothSwift.shareInstance()?.centralBlock = { (central) in
  610. if central?.state == .poweredOn{
  611. log.debug("蓝牙打开")
  612. HUD.flash(.label("poweredOn".da_localizedStr()), delay: 1.0) { _ in
  613. print("License Obtained.")
  614. }
  615. }else if central?.state == .unsupported{
  616. log.debug("不支持蓝牙")
  617. g_showHUD("unsupported".da_localizedStr())
  618. }else if central?.state == .unknown{
  619. log.debug("未知状态")
  620. g_showHUD("unknown".da_localizedStr())
  621. }else if central?.state == .resetting{
  622. log.debug("蓝牙重启")
  623. g_showHUD("resetting".da_localizedStr())
  624. }else if central?.state == .unauthorized{
  625. log.debug("蓝牙未授权")
  626. g_showHUD("unauthorized".da_localizedStr())
  627. }else if central?.state == .poweredOff{
  628. log.debug("蓝牙已关闭")
  629. g_showHUD("poweredOff".da_localizedStr())
  630. if self.aPeripherals!.count > 0 {
  631. for i in 0..<(self.aPeripherals?.count)!{
  632. var model = self.aPeripherals![i]
  633. model.online = false
  634. self.aPeripherals![i] = model
  635. self.tableView.reloadData()
  636. }
  637. }
  638. }
  639. }
  640. }
  641. override func viewWillAppear(_ animated: Bool) {
  642. super.viewWillAppear(animated)
  643. self.isCurrentVC = true
  644. BabyBluetoothSwift.shareInstance()?.scanForPeripherals()
  645. let dict = baseInfoPlist!.readAllPlistData(SBTHomeVCtr.peripheralPlist)
  646. let key = PeripheralINfo()
  647. // 遍历字典
  648. for (k , v) in dict {
  649. log.debug("\(k) --- \(v)")
  650. let dict1 : [String: String] = v as! [String : String]
  651. let devicename = dict1[key.DeviceName]
  652. let DeviceType = dict1[key.DeviceType]
  653. let PerpheralName = dict1[key.PerpheralName]
  654. let ProduceType = dict1[key.ProduceType]
  655. let isOldstr = dict1[key.IsOld]
  656. var isOld = false
  657. if isOldstr == "1"{
  658. isOld = true
  659. }
  660. let model = BLEData(perpheralName: PerpheralName, device_name: devicename, device_type: DeviceType, device_model: ProduceType, online: false,isOld: isOld)
  661. // if self.aPeripherals?.count == 0{ //刚开始进入这个页面 aPeripherals 为0
  662. if self.isStart == true{
  663. self.aPeripherals?.append(model)
  664. self.tableView.reloadData()
  665. }
  666. }
  667. log.debug("保存的设备数据\(dict)")
  668. }
  669. }
  670. extension SBTHomeVCtr{
  671. func notificationRegister() {
  672. NotificationCenter.default.addObserver(self, selector: #selector(changeLanguage), name: NSNotification.Name(KCHANGELANGUAGE), object: nil)
  673. NotificationCenter.default.addObserver(self, selector: #selector(notifitionTheBluetoothHasBeenDiscontect(notif:)), name: NSNotification.Name(kBabyBluetoothNotificationDeviceWasDisconect), object: nil)
  674. NotificationCenter.default.addObserver(self, selector: #selector(updateEmptyViewLanguage), name: NSNotification.Name("updateEmptyViewLanguage"), object: nil)
  675. }
  676. func notificationUnRegister() {
  677. // NotificationCenter.default.removeObserver(self)
  678. }
  679. func bleHasBeemDisconnectect(_ completeBlock : @escaping (()->Void)) {
  680. self.disconectBlock = completeBlock
  681. }
  682. @objc func notifitionTheBluetoothHasBeenDiscontect(notif:Notification) {
  683. if self.isCurrentVC == false {
  684. let dict = notif.userInfo as! [String: String]
  685. let peripheralname = dict["peripheral"]
  686. for (index,value) in (self.aPeripherals?.enumerated())! {
  687. log.debug("当前下标 -- \(index), 当前数组 -- \(value)")
  688. let perip : BLEData = value
  689. if peripheralname == perip.perpheralName{
  690. 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)
  691. self.aPeripherals![index] = model
  692. self.tableView.reloadData()
  693. }
  694. }
  695. } else {
  696. let dict = notif.userInfo as! [String: String]
  697. let peripheralname = dict["peripheral"]
  698. for (index,value) in (self.aPeripherals?.enumerated())! {
  699. log.debug("当前下标 -- \(index), 当前数组 -- \(value)")
  700. let perip : BLEData = value
  701. if peripheralname == perip.perpheralName{
  702. 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)
  703. self.aPeripherals![index] = model
  704. self.tableView.reloadData()
  705. }
  706. }
  707. if self.disconectBlock != nil{
  708. self.disconectBlock!()
  709. }
  710. }
  711. }
  712. @objc func changeLanguage() {
  713. log.debug("执行了 -- changeLanguage \(DAConfig.currentLanguage())")
  714. self.sbtTitle = "Device_list".da_localizedStr()
  715. }
  716. }
  717. extension SBTHomeVCtr{
  718. ///判断新旧协议
  719. func isOldProtocle(_ recieveData: String?) ->Bool {
  720. var numberArr = [String]()
  721. numberArr.removeAll()
  722. var temper2 = [String]()
  723. temper2.removeAll()
  724. for i in 0..<recieveData!.count {
  725. numberArr.append("0")
  726. let aStr = recieveData!.subString(start: i, length: 1)
  727. temper2.append(aStr)
  728. }
  729. if temper2 == numberArr {
  730. return true
  731. }else{
  732. return false
  733. }
  734. }
  735. func reloadDeviceUI_1(_ reciveData : String ) {
  736. // self.aPeripherals?.removeAll()
  737. let str0 = reciveData
  738. log.debug("处理真实数据 - \(str0)")
  739. //定义两个临时变量
  740. var device_type = ""
  741. var device_model = ""
  742. var isOldStr = ""
  743. //1400
  744. //额定放电电流
  745. let num00 = UUUtils.numberHexString(str0.subString(start: 7, length: 2))
  746. log.debug("额定放电电流:\(num00!)")
  747. //产品类型
  748. let num0 = UUUtils.numberHexString(str0.subString(start: 9, length: 2))
  749. log.debug("产品类型:\(num0)")
  750. switch num0?.intValue {
  751. case 0:
  752. device_type = "控制器"
  753. case 1:
  754. device_type = "控制器,路灯"
  755. case 3:
  756. device_type = "逆变器"
  757. case 4:
  758. device_type = "逆变一体机"
  759. case 5:
  760. device_type = "市电路灯"
  761. default:
  762. break
  763. }
  764. self.deviceTypeNumber = "\(num0!)"
  765. var num1 = UUUtils.string(fromHexString: str0.subString(start: 11, length: 32))
  766. num1 = num1?.trimmingCharacters(in: .whitespaces) //过滤开头结尾空白
  767. log.debug("产品型号\(num1!)")
  768. device_model = num1!
  769. //软件版本 00020306 00010005
  770. // let num2 = UUUtils.numberHexString(str0.subString(start: 11 + 32, length: 8))
  771. log.debug("------ \(str0.subString(start: 11 + 32, length: 8))")
  772. let a = UUUtils.numberHexString(str0.subString(start: 11 + 32, length: 2)) // 软件版本
  773. let b = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 2, length: 2))
  774. let c = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 2 + 2, length: 2))
  775. let d = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 2 + 2 + 2, length: 2))
  776. //硬件版本
  777. let m1 = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8, length: 2))
  778. let m2 = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8 + 2, length: 2))
  779. let m3 = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8 + 2 + 2, length: 2))
  780. let m4 = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8 + 2 + 2 + 2, length: 2))
  781. log.debug("硬件版本 : V\(m2!).\(m3!).\(m4!)")
  782. let m11 = Int(m1!)
  783. log.debug("硬件版本返回 --\(m11)")
  784. if m11 < 2{// 用来区分新旧协议,xx < 2 旧协议,>=2 新协议 tubfhu
  785. isOldStr = "1"
  786. }else {
  787. isOldStr = "0"
  788. }
  789. log.debug("硬件版本 新旧协议 --- \(str0.subString(start: 11 + 32 + 8, length: 8))")
  790. //产品序列号
  791. let snnum = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8 + 8, length: 8))
  792. log.debug("产品序列号 --- \(snnum!)")
  793. self.snnumber = "\(snnum!)"
  794. let key1 = PeripheralINfo()
  795. peripheralInfo[key1.IsOld] = isOldStr
  796. peripheralInfo[key1.DeviceType] = device_type
  797. peripheralInfo[key1.ProduceType] = device_model
  798. if isOldStr == "1" {
  799. //旧协议
  800. log.debug("当前为旧协议")
  801. let deviceName = ""
  802. let model = self.tempArr?.first
  803. let peripheralname = model?.perpheralName
  804. let key = PeripheralINfo()
  805. let belKey = device_type + device_model
  806. peripheralInfo[key.PerpheralName] = peripheralname
  807. peripheralInfo[key.DeviceName] = " "
  808. peripheralInfo[key.Online] = "0"
  809. log.debug("设备数据 -- \(peripheralInfo)")
  810. //self.baseInfoPlist?.removeAllData(SBTHomeVCtr.peripheralPlist)
  811. if peripheralname == nil {
  812. self.baseInfoPlist?.updatePlist(At: SBTHomeVCtr.peripheralPlist, key: belKey, deviceName: deviceName)
  813. }else{//这里需要更新设备名称
  814. self.baseInfoPlist!.saveData111(SBTHomeVCtr.peripheralPlist, key: belKey, deviceData: peripheralInfo)
  815. }
  816. self.soft = "V\(a!).\(b!).\(c!).\(d!)"
  817. self.hard = "V\(m2!).\(m3!).\(m4!)"
  818. let aIsOld = true
  819. // 获取 蓝牙名称 --
  820. let perpheralName_save = self.baseInfoPlist?.readPeripheral(from: SBTHomeVCtr.peripheralPlist, key: belKey)
  821. let deviceMo = BLEData(perpheralName: perpheralName_save, device_name: deviceName, device_type: device_type, device_model: device_model, online: true,isOld:aIsOld )
  822. //判断 是否存在已经存在 则 修改数组内容,否则 重新添加
  823. // if isAddDevice == true{
  824. let isContains = self.aPeripherals?.contains(where: { (item) -> Bool in
  825. return item.device_model == device_model && item.device_type == device_type
  826. })
  827. if isContains == true {
  828. //查找索引
  829. let index = self.aPeripherals?.firstIndex(where: { (item) -> Bool in
  830. return item.device_model == device_model && item.device_type == device_type
  831. })
  832. self.aPeripherals![index!] = deviceMo
  833. }else{
  834. self.aPeripherals?.append(deviceMo)
  835. }
  836. self.tableView.reloadData()
  837. if self.isAuto == true {
  838. //上传数据
  839. 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)
  840. delay(1) { //设备名称
  841. let model: BLEData = deviceMo
  842. let peripheralName = model.perpheralName
  843. let devicename = model.device_name
  844. let devicetype = model.device_type
  845. let device_model = model.device_model
  846. let isOld = model.isOld
  847. let belKey = devicetype! + device_model!
  848. let alert = SBTReadLoadingVCtr(nibName: "SBTReadLoadingVCtr", bundle: nil)
  849. alert.deviceName = devicename
  850. alert.message = "Data_loading".da_localizedStr()
  851. alert.show()
  852. var isSuccess = false
  853. // alert.dismiss(8)
  854. let arr = ["FF03000A0001","FF03000B000F","FF030100000A","FF0301200003","FF03F000000A","FF03F001000A","FF03F002000A","FF03F003000A","FF03F004000A","FF03F005000A","FF03F006000A","FF03F007000A","FF030115000B","FF03E0010001","FF03E003001E","FF03DF000001","FF03DF0D0001","FF03E0210001"]
  855. self.bleHasBeemDisconnectect {
  856. alert.dismiss()
  857. return
  858. }
  859. BabyBluetoothSwift.shareInstance()?.writeData2222(arr, completionHandler: { (result) in
  860. log.debug("返回数据 --- \(ReceiveData)")
  861. self.realdata_000A(result!["FF03000A0001"] as! String)
  862. self.real_000B000F(result!["FF03000B000F"] as! String)
  863. self.realdata_0100(result!["FF030100000A"] as! String)
  864. self.realdata_0120(result!["FF0301200003"] as! String)
  865. self.historyDataArr.removeAll()
  866. self.realdata_F000(result!["FF03F000000A"] as! String)
  867. self.realdata_F001(result!["FF03F001000A"] as! String)
  868. self.realdata_F002(result!["FF03F002000A"] as! String)
  869. self.realdata_F003(result!["FF03F003000A"] as! String)
  870. self.realdata_F004(result!["FF03F004000A"] as! String)
  871. self.realdata_F005(result!["FF03F005000A"] as! String);
  872. self.realdata_F006(result!["FF03F006000A"] as! String);
  873. self.realdata_F007(result!["FF03F007000A"] as! String);
  874. self.realdata_0115(result!["FF030115000B"] as! String)
  875. self.realdata_E001(result!["FF03E0010001"] as! String);
  876. self.realdata_E003(result!["FF03E003001E"] as! String);
  877. self.realdata_DF00(result!["FF03DF000001"] as! String);
  878. self.realdata_DF0D(result!["FF03DF0D0001"] as! String)
  879. self.realdata_E021(result!["FF03E0210001"] as! String)
  880. AppShare.dataDict = self.dataDict
  881. if AppShare.isRefresh == true {
  882. NotificationCenter.default.post(name: NSNotification.Name(kBabyBluetoothNotificationAllDataRecieve), object: nil)
  883. }
  884. alert.dismiss()
  885. isSuccess = true
  886. self.readPeripheraldata(belKey, deviceName: devicename!, isOld: isOld!, perihperalName: peripheralName!)
  887. let user = UserDefaults.standard
  888. user.set(peripheralName, forKey: "SavePeripheral")
  889. user.synchronize()
  890. //上传操作
  891. self.uploadAction()
  892. })
  893. delay(15, closure: {
  894. if isSuccess == false {
  895. HUD.flash(.error, delay: 1)
  896. return
  897. }
  898. })
  899. }
  900. self.isAuto = false
  901. }
  902. } else {
  903. delay(0.02) {
  904. //新协议
  905. BabyBluetoothSwift.shareInstance()?.writeData("FF0300490020", completionHandler: { (result) in
  906. log.debug("当前为新协议-----\(result)")
  907. self.reloadDeviceUI_2(result!)
  908. })
  909. }
  910. }
  911. }
  912. func uploadAction() {
  913. NetworkManager.shared.uploadlocation(self.snnumber!)
  914. let loadS = (self.dataDict[self.key.loadStatus] as! String == "Load_is_on".da_localizedStr()) ? "1" : "0"
  915. let batteryPowerStr = Double(self.dataDict[self.key.batteryVoltage] as! String)! * Double(self.dataDict[self.key.batteryTemp] as! String)!
  916. let batteryPower = String(format: "%.2f",batteryPowerStr )
  917. NetworkManager.shared.uploadtodayData(self.snnumber!,
  918. solarStatus: self.dataDict[self.key.solarStatus] as! String,
  919. solarVoltage: self.dataDict[self.key.solarVoltage] as! String,
  920. solarCurrent: self.dataDict[self.key.solarCurrent] as! String,
  921. solarPower: self.dataDict[self.key.solarPower] as! String,
  922. batteryStatus: self.dataDict[self.key.batteryStatus] as! String,
  923. batteryVoltage: self.dataDict[self.key.batteryVoltage] as! String,
  924. batteryCurrent: self.dataDict[self.key.batteryTemp] as! String,
  925. batteryTemp: self.dataDict[self.key.batteryTemp] as! String,
  926. batteryPower: batteryPower, loadStatus: loadS,
  927. loadVoltage: self.dataDict[self.key.loadVoltage] as! String,
  928. loadCurrent: self.dataDict[self.key.loadCurrent] as! String,
  929. 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)
  930. var hisarr = [[String:String]]()
  931. let first = nowTime("yyyy-MM-dd")
  932. let second = getLastDay(first)
  933. let third = getLastDay(second)
  934. let foreth = getLastDay(third)
  935. let fifth = getLastDay(foreth)
  936. let sixth = getLastDay(fifth)
  937. let seventh = getLastDay(sixth)
  938. let eighth = getLastDay(seventh)
  939. let dayArr = [first,second,third,foreth,fifth,sixth,seventh,eighth]
  940. for i in 0..<self.historyDataArr.count{
  941. let item = self.historyDataArr[i]
  942. let dataList1 = [
  943. "batteryVoltageMin":item["his_batteryVoltageMin"], //蓄电池最低电压
  944. "batteryVoltageMax":item["his_batteryVoltageMax"],//蓄电池最高电压
  945. "chargingCurrentMax":item["his_chargingCurrentMax"] ,//充电最大电流
  946. "chargingPowerMax" : item["his_chargingPowerMax"],//充电最大功率
  947. "chargingAmpereHour" : item["his_chargingAmpereHour"],//充电安时数
  948. "dischargeCurrentmax" : item["his_dischargeCurrentmax"],//放电最大电流
  949. "dischargePowerMax" : item["his_dischargePowerMax"], //放电最大功率
  950. "dischargeAmpereHour" : item["his_dischargeAmpereHour"],//放电安时数
  951. "generationCapacity": item["his_generationCapacity"], //发电量
  952. "dischargeCapacity" : item["his_dischargeCapacity"],//放电量
  953. "updatetime" : dayArr[i]//故障状态
  954. ]
  955. hisarr.append(dataList1 as! [String : String])
  956. }
  957. NetworkManager.shared.uploadHistoryData(self.snnumber!, locationList: hisarr)
  958. }
  959. func reloadDeviceUI_2(_ reciveData : String ) {
  960. // self.aPeripherals?.removeAll()
  961. let str0 = reciveData
  962. log.debug("处理真实数据 - \(str0)")
  963. var deviceName = ""
  964. var num5 = UUUtils.string(fromHexString: str0.subString(start: 7 , length: str0.count - 12))
  965. log.debug("要解析的十六进制 \( str0.subString(start: 7 , length: str0.count - 12))")
  966. num5 = num5?.trimmingCharacters(in: .whitespaces) //过滤开头结尾空白
  967. if num5 == nil || num5 == "" {
  968. let ChineseString = UUUtils.deUnicodeString(fromHexString: str0.subString(start: 7 , length: str0.count - 12))
  969. log.debug("设备中文名称 \(ChineseString!)")
  970. deviceName = ChineseString!
  971. dataDict[key.productName] = deviceName
  972. }else{
  973. log.debug("设备名称 \(num5!)")
  974. deviceName = num5!
  975. dataDict[key.productName] = num5
  976. }
  977. let model = self.tempArr?.first
  978. let peripheralname = model?.perpheralName
  979. let key = PeripheralINfo()
  980. let device_type = peripheralInfo[key.DeviceType]
  981. let device_model = peripheralInfo[key.ProduceType]
  982. let isOldStr = peripheralInfo[key.IsOld]
  983. let belKey = device_type! + device_model!
  984. peripheralInfo[key.PerpheralName] = peripheralname
  985. if isOldStr == "1"{
  986. peripheralInfo[key.DeviceName] = " "
  987. }else
  988. {
  989. peripheralInfo[key.DeviceName] = deviceName
  990. }
  991. peripheralInfo[key.Online] = "0"
  992. log.debug("设备数据 -- \(peripheralInfo)")
  993. //先清空沙盒内容
  994. // self.baseInfoPlist!.deleteData(SBTHomeVCtr.peripheralPlist, key: key)
  995. // self.baseInfoPlist?.removeAllData(SBTHomeVCtr.peripheralPlist)
  996. if peripheralname == nil {
  997. self.baseInfoPlist?.updatePlist(At: SBTHomeVCtr.peripheralPlist, key: belKey, deviceName: deviceName)
  998. }else{//这里需要更新设备名称
  999. self.baseInfoPlist!.saveData111(SBTHomeVCtr.peripheralPlist, key: belKey, deviceData: peripheralInfo)
  1000. }
  1001. var aIsOld = false
  1002. if isOldStr == "1" {
  1003. aIsOld = true
  1004. }
  1005. //上传数据
  1006. 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)
  1007. // 获取 蓝牙名称 --
  1008. let perpheralName_save = self.baseInfoPlist?.readPeripheral(from: SBTHomeVCtr.peripheralPlist, key: belKey)
  1009. let deviceMo = BLEData(perpheralName: perpheralName_save, device_name: deviceName, device_type: device_type, device_model: device_model, online: true,isOld:aIsOld )
  1010. //判断 是否存在已经存在 则 修改数组内容,否则 重新添加
  1011. // if isAddDevice == true{
  1012. let isContains = self.aPeripherals?.contains(where: { (item) -> Bool in
  1013. return item.device_model == device_model && item.device_type == device_type
  1014. })
  1015. if isContains == true {
  1016. //查找索引
  1017. let index = self.aPeripherals?.firstIndex(where: { (item) -> Bool in
  1018. return item.device_model == device_model && item.device_type == device_type
  1019. })
  1020. self.aPeripherals![index!] = deviceMo
  1021. }else{
  1022. self.aPeripherals?.append(deviceMo)
  1023. }
  1024. // isAddDevice = false
  1025. // }
  1026. self.tableView.reloadData()
  1027. if self.isAuto == true {
  1028. delay(1) { //设备名称
  1029. let model: BLEData = deviceMo
  1030. let peripheralName = model.perpheralName
  1031. let devicename = model.device_name
  1032. let devicetype = model.device_type
  1033. let device_model = model.device_model
  1034. let isOld = model.isOld
  1035. let belKey = devicetype! + device_model!
  1036. let alert = SBTReadLoadingVCtr(nibName: "SBTReadLoadingVCtr", bundle: nil)
  1037. alert.deviceName = " "
  1038. alert.message = "Data_loading".da_localizedStr()
  1039. alert.show()
  1040. self.bleHasBeemDisconnectect {
  1041. alert.dismiss()
  1042. return
  1043. }
  1044. let arr = ["FF03000A0001","FF03000B000F","FF0300490020","FF0300FD000D","FF03F000000A","FF03F001000A","FF03F002000A","FF03F003000A","FF03F004000A","FF03F005000A","FF03F006000A","FF03F007000A","FF030115000B","FF03E0010001","FF03E003001E","FF03DF000001","FF03DF0D0001","FF03E0210001"]
  1045. var isSuccess = false
  1046. BabyBluetoothSwift.shareInstance()?.writeData2222(arr, completionHandler: { (result) in
  1047. self.realdata_000A(result!["FF03000A0001"] as! String)
  1048. self.real_000B000F(result!["FF03000B000F"] as! String)
  1049. self.readdata_0049(result!["FF0300490020"] as! String)
  1050. self.realdata_00FD(result!["FF0300FD000D"] as! String)
  1051. self.historyDataArr.removeAll()
  1052. self.realdata_F000(result!["FF03F000000A"] as! String)
  1053. self.realdata_F001(result!["FF03F001000A"] as! String)
  1054. self.realdata_F002(result!["FF03F002000A"] as! String)
  1055. self.realdata_F003(result!["FF03F003000A"] as! String)
  1056. self.realdata_F004(result!["FF03F004000A"] as! String)
  1057. self.realdata_F005(result!["FF03F005000A"] as! String);
  1058. self.realdata_F006(result!["FF03F006000A"] as! String);
  1059. self.realdata_F007(result!["FF03F007000A"] as! String);
  1060. self.realdata_0115(result!["FF030115000B"] as! String)
  1061. self.realdata_E001(result!["FF03E0010001"] as! String);
  1062. self.realdata_E003(result!["FF03E003001E"] as! String);
  1063. self.realdata_DF00(result!["FF03DF000001"] as! String);
  1064. self.realdata_DF0D(result!["FF03DF0D0001"] as! String)
  1065. self.realdata_E021(result!["FF03E0210001"] as! String)
  1066. AppShare.dataDict = self.dataDict
  1067. if AppShare.isRefresh == true {
  1068. NotificationCenter.default.post(name: NSNotification.Name(kBabyBluetoothNotificationAllDataRecieve), object: nil)
  1069. }
  1070. isSuccess = true
  1071. alert.dismiss()
  1072. self.readPeripheraldata(belKey, deviceName: devicename!, isOld: isOld!, perihperalName: peripheralName!)
  1073. let user = UserDefaults.standard
  1074. user.set(peripheralName, forKey: "SavePeripheral")
  1075. user.synchronize()
  1076. //上传操作
  1077. self.uploadAction()
  1078. })
  1079. delay(15, closure: {
  1080. if isSuccess == false {
  1081. HUD.flash(.error, delay: 1)
  1082. return
  1083. }
  1084. })
  1085. }
  1086. self.isAuto = false
  1087. }
  1088. }
  1089. func disConnectName(_ peripheralname:String) {
  1090. //断开设备
  1091. self.perform(#selector(__disConectName(_:)), with: peripheralname, afterDelay: 5)
  1092. }
  1093. @objc func __disConectName(_ peripheralName:String) {
  1094. log.debug("__disConectName\(peripheralName)")
  1095. let peripheral = baby1?.findConnectedPeripheral(peripheralName)
  1096. baby1?.cancelPeripheralConnection(peripheral!)
  1097. }
  1098. }
  1099. //MARK: BLE
  1100. extension SBTHomeVCtr{
  1101. func babyDelegate() {
  1102. log.debug("执行了没 ")
  1103. //设置通知状态改变的block
  1104. baby1?.setBlockOnCentralManagerDidUpdateState({ (central) in
  1105. log.debug("蓝牙状态 检测----- \(central?.state)")
  1106. //blockOnCentralManagerDidUpdateState
  1107. if central?.state == .poweredOn{
  1108. log.debug("蓝牙打开")
  1109. HUD.flash(.label("蓝牙已打开"), delay: 2.0) { _ in
  1110. print("License Obtained.")
  1111. }
  1112. // g_showHUD("蓝牙已打开")
  1113. }else if central?.state == .unsupported{
  1114. log.debug("不支持蓝牙")
  1115. g_showHUD("不支持蓝牙")
  1116. }else if central?.state == .unknown{
  1117. log.debug("未知状态")
  1118. g_showHUD("未知状态")
  1119. }else if central?.state == .resetting{
  1120. log.debug("蓝牙重启")
  1121. g_showHUD("蓝牙重启")
  1122. }else if central?.state == .unauthorized{
  1123. log.debug("蓝牙未授权")
  1124. g_showHUD("蓝牙未授权")
  1125. }else if central?.state == .poweredOff{
  1126. log.debug("蓝牙已关闭")
  1127. g_showHUD("蓝牙已关闭")
  1128. }
  1129. })
  1130. baby1?.setBlockOnDiscoverToPeripherals({ (central, peripheral, advertisementData, RSSI) in
  1131. log.debug("输出内容---\(central),\(peripheral), -\(advertisementData)- \(RSSI)")
  1132. })
  1133. baby1?.setBlockOnCancelScanBlock({ (central) in
  1134. log.debug("停止扫描取消")
  1135. })
  1136. baby1?.setBlockOnCancelAllPeripheralsConnectionBlock({ (central) in
  1137. log.debug("断开所有设备")
  1138. })
  1139. baby1?.setBlockOnDisconnect({ (central, peripheral, erro) in
  1140. log.debug(" SBHOMEVCTR ---->断开了设备\(peripheral)")
  1141. })
  1142. }
  1143. }
  1144. extension SBTHomeVCtr{
  1145. //获取设备数据
  1146. func getDevieData(){
  1147. BabyBluetoothSwift.shareInstance()?.testBlock2 = {[weak self](deviceData) in
  1148. let a = deviceData?.value(forKey: ReceiveData) as! String
  1149. let str0 = a.subString(start: 0, length: 6)
  1150. log.debug("返回的数据 -- \(a)")
  1151. if str0.subString(start: 1, length: 4) == "ff03" {
  1152. let dataTagValue : String = deviceData?.value(forKey: DataTag) as! String
  1153. if dataTagValue == "FF03000A0001"{
  1154. self!.realdata_000A(a)
  1155. }
  1156. if dataTagValue == "FF03000B000F"{
  1157. self?.real_000B000F(a)
  1158. self!.reloadDeviceUI_1(a)
  1159. }
  1160. if dataTagValue == "FF03000B004E" {
  1161. self!.realdata_000B(a)
  1162. //
  1163. }
  1164. if dataTagValue == "FF0300FD000D" {
  1165. self!.realdata_00FD(a)
  1166. }
  1167. //"FF030100000A","FF0301200003",
  1168. if dataTagValue == "FF030100000A" {
  1169. self!.realdata_0100(a)
  1170. }
  1171. if dataTagValue == "FF0301200003" {
  1172. self!.realdata_0120(a)
  1173. }
  1174. if dataTagValue == "FF03F000000A" {
  1175. self!.historyDataArr.removeAll()
  1176. self!.realdata_F000(a)
  1177. }
  1178. if dataTagValue == "FF03F001000A" {
  1179. self!.realdata_F001(a)
  1180. }
  1181. if dataTagValue == "FF03F002000A" {
  1182. self!.realdata_F002(a)
  1183. }
  1184. if dataTagValue == "FF03F003000A" {
  1185. self!.realdata_F003(a)
  1186. }
  1187. if dataTagValue == "FF03F004000A" {
  1188. self!.realdata_F004(a);
  1189. }
  1190. if dataTagValue == "FF03F005000A" {
  1191. self!.realdata_F005(a);
  1192. }
  1193. if dataTagValue == "FF03F006000A" {
  1194. self!.realdata_F006(a);
  1195. }
  1196. if dataTagValue == "FF03F007000A" {
  1197. self!.realdata_F007(a);
  1198. }
  1199. if dataTagValue == "FF030115000B"{
  1200. self!.realdata_0115(a)
  1201. }
  1202. if dataTagValue == "FF03E0010001" {
  1203. self!.realdata_E001(a);
  1204. }
  1205. if dataTagValue == "FF03E003001E" {
  1206. self!.realdata_E003(a);
  1207. }
  1208. if dataTagValue == "FF03DF000001" {
  1209. self!.realdata_DF00(a);
  1210. }
  1211. if dataTagValue == "FF03DF0D0001" {
  1212. self!.realdata_DF0D(a)
  1213. }
  1214. if dataTagValue == "FF03E0210001"{
  1215. self!.realdata_E021(a)
  1216. }
  1217. if dataTagValue == "FF0300490020"{
  1218. self!.readdata_0049(a)
  1219. self!.reloadDeviceUI_2(a)
  1220. }
  1221. }
  1222. // log.debug("返回数据dataDict-- \(self!.dataDict)")
  1223. AppShare.dataDict = self!.dataDict
  1224. //kBabyBluetoothNotificationAllDataRecieve
  1225. if AppShare.isRefresh == true {
  1226. NotificationCenter.default.post(name: NSNotification.Name(kBabyBluetoothNotificationAllDataRecieve), object: nil)
  1227. }
  1228. }
  1229. }
  1230. //实时数据
  1231. func realdata_000A(_ reciveData : String) {
  1232. let str0 = reciveData
  1233. log.debug("处理真实数据 - \(str0)")
  1234. //系统支持的最高电压
  1235. let num00 = UUUtils.numberHexString(str0.subString(start: 7 , length: 2))
  1236. log.debug("额定充电电流: \(num00!)")
  1237. dataDict[key.MaxVolt] = "\(num00!)"
  1238. // paramDataDict[key.MaxVolt] = "\(num00!)"
  1239. let num0 = UUUtils.numberHexString(str0.subString(start: 7 + 2, length: 2))
  1240. log.debug("额定充电电流: \(String(format: "%.2f", num0!.doubleValue))")
  1241. dataDict[key.IchageRage] = String(format: "%.2f", num0!.doubleValue)
  1242. // paramDataDict[key.IchageRage] = "\(num0!)"
  1243. }
  1244. //读取名称
  1245. func readdata_0049(_ reciveData : String){
  1246. let str0 = reciveData
  1247. log.debug("-------处理真实数据 - \(str0)")
  1248. var num5 = UUUtils.string(fromHexString: str0.subString(start: 7 , length: str0.count - 12)) // 先转化为汉字 如果汉字无法解析出来,则用数字解析
  1249. log.debug("=====要解析的十六进制 \( str0.subString(start: 7 , length: str0.count - 12))")
  1250. // let isOld = isOldProtocle(num5!)
  1251. num5 = num5?.trimmingCharacters(in: .whitespaces) //过滤开头结尾空白
  1252. log.debug("======设备名称 \(num5)")
  1253. if num5 == nil || num5 == ""{
  1254. let ChineseString = UUUtils.deUnicodeString(fromHexString: str0.subString(start: 7 , length: str0.count - 12))
  1255. log.debug("设备中文名称 \(ChineseString!)")
  1256. dataDict[key.productName] = "\(ChineseString!)"
  1257. }else{
  1258. dataDict[key.productName] = num5
  1259. }
  1260. for i in 0..<self.aPeripherals!.count {
  1261. var mo = self.aPeripherals![i]
  1262. mo.device_name = dataDict[key.productName] as! String
  1263. self.aPeripherals![i] = mo
  1264. self.tableView.reloadData()
  1265. }
  1266. }
  1267. func real_000B000F(_ reciveData : String) {
  1268. let str0 = reciveData
  1269. log.debug("处理真实数据 - \(str0)")
  1270. //1400 <aaaaaaa>
  1271. //额定放电电流
  1272. let num00 = UUUtils.numberHexString(str0.subString(start: 7, length: 2))
  1273. log.debug("额定放电电流:\(num00!)")
  1274. dataDict[key.idischagRate] = "\(num00!)"
  1275. //产品类型
  1276. let num0 = UUUtils.numberHexString(str0.subString(start: 9, length: 2))
  1277. log.debug("产品类型:\(num0)")
  1278. var device_type = ""
  1279. switch num0?.intValue {
  1280. case 0:
  1281. device_type = "Controller_household".da_localizedStr()
  1282. case 1:
  1283. device_type = "Controller_street_light".da_localizedStr()
  1284. case 3:
  1285. device_type = "Inverter".da_localizedStr()
  1286. case 4:
  1287. device_type = "All-in-one_inverter".da_localizedStr()
  1288. default:
  1289. break
  1290. }
  1291. dataDict[key.device_type] = device_type
  1292. var num1 = UUUtils.string(fromHexString: str0.subString(start: 11, length: 32))
  1293. num1 = num1?.trimmingCharacters(in: .whitespaces) //过滤开头结尾空白
  1294. log.debug("产品型号\(num1)")
  1295. dataDict[key.product_type] = num1!
  1296. //软件版本
  1297. // let num2 = UUUtils.numberHexString(str0.subString(start: 11 + 32, length: 8))
  1298. let a = UUUtils.numberHexString(str0.subString(start: 11 + 32, length: 2)) // 软件版本
  1299. let b = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 2, length: 2))
  1300. let c = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 2 + 2, length: 2))
  1301. let d = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 2 + 2 + 2, length: 2))
  1302. if a == 0{// 旧协议最高位是0x00,是0x01是新版本(可访问1B-59),这样已可区分新旧协议
  1303. }else if a == 1{
  1304. }
  1305. log.debug("软件版本 : V \(b!).\(c!).\(d!)")
  1306. dataDict[key.softWareCersion] = "V\(b!).\(c!).\(d!)"
  1307. //硬件版本
  1308. let m1 = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8, length: 2))
  1309. let m2 = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8 + 2, length: 2))
  1310. let m3 = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8 + 2 + 2, length: 2))
  1311. let m4 = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8 + 2 + 2 + 2, length: 2))
  1312. log.debug("硬件版本 : V\(m2!).\(m3!).\(m4!)")
  1313. // 序列号
  1314. dataDict[key.hardWareCersion] = "V \(m2!).\(m3!).\(m4!)"
  1315. // let num4 = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8 + 8 , length: 8))
  1316. let num41 = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8 + 8 , length: 2))
  1317. let num42 = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8 + 8 + 2 , length: 2))
  1318. let num43 = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8 + 8 + 4 , length: 4)) //补足5位
  1319. log.debug("\( String(format: "%05d", num43!.intValue))") // 年月(低十六位)
  1320. dataDict[key.productSN] = "\(num41!)\(num42!)\( String(format: "%05d", num43!.intValue))"
  1321. }
  1322. func realdata_000B(_ reciveData : String) {
  1323. let str0 = reciveData
  1324. log.debug("处理真实数据 - \(str0)")
  1325. //1400
  1326. //额定放电电流
  1327. let num00 = UUUtils.numberHexString(str0.subString(start: 7, length: 2))
  1328. log.debug("额定放电电流:\(num00!)")
  1329. dataDict[key.idischagRate] = "\(num00!)"
  1330. //产品类型
  1331. let num0 = UUUtils.numberHexString(str0.subString(start: 9, length: 2))
  1332. log.debug("产品类型:\(num0)")
  1333. var device_type = ""
  1334. switch num0?.intValue {
  1335. case 0:
  1336. device_type = "Controller_household".da_localizedStr()
  1337. case 1:
  1338. device_type = "Controller_street_light".da_localizedStr()
  1339. case 3:
  1340. device_type = "Inverter".da_localizedStr()
  1341. case 4:
  1342. device_type = "All-in-one_inverter".da_localizedStr()
  1343. default:
  1344. break
  1345. }
  1346. dataDict[key.device_type] = device_type
  1347. var num1 = UUUtils.string(fromHexString: str0.subString(start: 11, length: 32))
  1348. num1 = num1?.trimmingCharacters(in: .whitespaces) //过滤开头结尾空白
  1349. log.debug("产品型号\(num1)")
  1350. dataDict[key.product_type] = num1!
  1351. //软件版本
  1352. // let num2 = UUUtils.numberHexString(str0.subString(start: 11 + 32, length: 8))
  1353. let a = UUUtils.numberHexString(str0.subString(start: 11 + 32, length: 2))
  1354. let b = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 2, length: 2))
  1355. let c = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 2 + 2, length: 2))
  1356. let d = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 2 + 2 + 2, length: 2))
  1357. log.debug("软件版本 : V \(b!).\(c!).\(d!)")
  1358. dataDict[key.softWareCersion] = "V\(b!).\(c!).\(d!)"
  1359. //硬件版本
  1360. let m1 = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8, length: 2))
  1361. let m2 = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8 + 2, length: 2))
  1362. let m3 = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8 + 2 + 2, length: 2))
  1363. let m4 = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8 + 2 + 2 + 2, length: 2))
  1364. log.debug("硬件版本 : V\(m2!).\(m3!).\(m4!)")
  1365. // 序列号
  1366. dataDict[key.hardWareCersion] = "V \(m2!).\(m3!).\(m4!)"
  1367. // let num4 = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8 + 8 , length: 8))
  1368. let num41 = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8 + 8 , length: 2))
  1369. let num42 = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8 + 8 + 2 , length: 2))
  1370. let num43 = UUUtils.numberHexString(str0.subString(start: 11 + 32 + 8 + 8 + 4 , length: 4)) //补足5位
  1371. log.debug("\( String(format: "%05d", num43!.intValue))") // 年月(低十六位)
  1372. dataDict[key.productSN] = "\(num41!)\(num42!)\( String(format: "%05d", num43!.intValue))"
  1373. var num5 = UUUtils.string(fromHexString: str0.subString(start: 255 , length: 64))
  1374. log.debug("要解析的十六进制 \( str0.subString(start: 255 , length: 64))")
  1375. num5 = num5?.trimmingCharacters(in: .whitespaces) //过滤开头结尾空白
  1376. log.debug("设备名称 \(num5)")
  1377. //f16d33 575578 e565b 065fd8 0906ed179
  1378. if num5 == nil || num5 == "" {
  1379. let ChineseString = UUUtils.deUnicodeString(fromHexString: str0.subString(start: 255 , length: 64))
  1380. // ff ff
  1381. dataDict[key.productName] = ChineseString
  1382. log.debug("设备中文名称 \(ChineseString!)")
  1383. }else{
  1384. dataDict[key.productName] = num5
  1385. }
  1386. let year = UUUtils.numberHexString(str0.subString(start: 75, length: 2))
  1387. let mouth = UUUtils.numberHexString(str0.subString(start: 75 + 2, length: 2))
  1388. let day = UUUtils.numberHexString(str0.subString(start: 75 + 2, length: 2))
  1389. log.debug("生产日期 : \(year!)-\(mouth!)-\(day!)")
  1390. }
  1391. //温度处理
  1392. func temp(with num:NSNumber)-> String{
  1393. let numInt = num.intValue
  1394. // let q1 = UUUtils.numberHexString(q)?.intValue
  1395. let q2 = numInt & 128
  1396. print("判断正负\(q2)")
  1397. if q2 != 128 {
  1398. log.debug("温度\(num.floatValue )")
  1399. return "\(num.intValue )"
  1400. }else{
  1401. log.debug("温度: -\(numInt & 127 )")
  1402. return "-\(numInt & 127 )"
  1403. }
  1404. }
  1405. //1s刷新一次
  1406. //实时数据
  1407. func realdata_00FD(_ reciveData : String) {
  1408. let str0 = reciveData
  1409. log.debug("处理真实数据 - \(str0)")
  1410. //太阳能板状态 0 000 0000 0000 0000 0000 0000 0000 0010
  1411. // 31
  1412. var num0A = UUUtils.getBinaryByHex(str0.subString(start: 11 , length: 8))
  1413. let num0A_1 = num0A?.subString(start: 0, length: 16)
  1414. let num0A_2 = num0A?.subString(start: 16, length: 16)
  1415. let num0A_II = num0A_2! + num0A_1!
  1416. num0A = num0A_II
  1417. log.debug("太阳能板状态\(num0A!)")
  1418. var solarStatusStr = ""
  1419. let B30 = num0A?.subString(start: 1, length: 1)
  1420. let B29 = num0A?.subString(start: 2, length: 1)
  1421. let B28 = num0A?.subString(start: 3, length: 1)
  1422. let B27 = num0A?.subString(start: 4, length: 1)
  1423. let B26 = num0A?.subString(start: 5, length: 1)
  1424. let B25 = num0A?.subString(start: 6, length: 1)
  1425. let B24 = num0A?.subString(start: 7, length: 1)
  1426. let B23 = num0A?.subString(start: 8, length: 1)
  1427. //负载
  1428. let B20 = num0A?.subString(start: 11, length: 1)
  1429. let B19 = num0A?.subString(start: 12, length: 1)
  1430. let B15 = num0A?.subString(start: 16, length: 1)
  1431. //系统
  1432. let B21 = num0A?.subString(start: 10, length: 1)
  1433. let B14 = num0A?.subString(start: 17, length: 1)
  1434. let B13 = num0A?.subString(start: 18, length: 1)
  1435. //蓄电池状态
  1436. let B22 = num0A?.subString(start: 9, length: 1)
  1437. let B18 = num0A?.subString(start: 13, length: 1)
  1438. let B17 = num0A?.subString(start: 14, length: 1)
  1439. let B16 = num0A?.subString(start: 15, length: 1)
  1440. let B12 = num0A?.subString(start: 19, length: 1)
  1441. let B11 = num0A?.subString(start: 20, length: 1)
  1442. let B10 = num0A?.subString(start: 21, length: 1)
  1443. var batteryStatusStr = ""
  1444. if B10 == "1" {
  1445. batteryStatusStr = "BMS_overcharge_protection".da_localizedStr()
  1446. }
  1447. if B11 == "1" {
  1448. batteryStatusStr = "Battery_low_temperature_protection".da_localizedStr()
  1449. }
  1450. if B12 == "1" {
  1451. batteryStatusStr = "Battery_reverse_polarity".da_localizedStr()
  1452. }
  1453. if B16 == "1" {
  1454. batteryStatusStr = "Battery_over_discharge".da_localizedStr()
  1455. }
  1456. if B17 == "1" {
  1457. batteryStatusStr = "Battery_overvoltage".da_localizedStr()
  1458. }
  1459. if B18 == "1" {
  1460. batteryStatusStr = "Undervoltage_warning".da_localizedStr()
  1461. }
  1462. if B22 == "1" {
  1463. batteryStatusStr = "External_ambient_temperature_is_too_high".da_localizedStr()
  1464. }
  1465. if B10 == "0" && B11 == "0" && B12 == "0" && B16 == "0" && B17 == "0" && B18 == "0" && B22 == "0"{
  1466. batteryStatusStr = "Normal".da_localizedStr()
  1467. }
  1468. dataDict[key.batteryStatus] = batteryStatusStr
  1469. /// realDataDict[key.batteryStatus] = batteryStatusStr
  1470. //太阳能状态
  1471. if B23 == "1" {
  1472. solarStatusStr = "PV_input_power_is_too_large".da_localizedStr()
  1473. }
  1474. if B24 == "1" {
  1475. solarStatusStr = "Short_circuit_of_photovoltaic_input".da_localizedStr()
  1476. }
  1477. if B25 == "1" {
  1478. solarStatusStr = "Overvoltage_at_the_photovoltaic_input".da_localizedStr()
  1479. }
  1480. if B26 == "1" {
  1481. solarStatusStr = "Solar_panel_countercurrent".da_localizedStr()
  1482. }
  1483. if B27 == "1" {
  1484. solarStatusStr = "Solar_panel_working_point_overpressure".da_localizedStr()
  1485. }
  1486. if B28 == "1" {
  1487. solarStatusStr = "Solar_panel_reverse_connection".da_localizedStr()
  1488. }
  1489. dataDict[key.solarStatus] = solarStatusStr
  1490. /// realDataDict[key.solarStatus] = solarStatusStr
  1491. if B23 == "0" && B24 == "0" && B25 == "0" && B26 == "0" && B27 == "0" && B28 == "0" {
  1492. dataDict[key.solarStatus] = batteryStatusStr
  1493. /// realDataDict[key.solarStatus] = batteryStatusStr
  1494. }
  1495. let num0 = UUUtils.getBinaryByHex(str0.subString(start: 7, length: 4))
  1496. log.debug("00FD \(num0!)")
  1497. let low_8_bit = UUUtils.numberHexString(str0.subString(start: 9, length: 2))
  1498. log.debug("low_8_bit - \(low_8_bit)")
  1499. //1000000000000000
  1500. var changeStatus = ""
  1501. if low_8_bit == 0 {
  1502. changeStatus = "Charging_is_not_turned_on".da_localizedStr()
  1503. }else if low_8_bit == 1 {
  1504. changeStatus = "Start_charging_mode".da_localizedStr()
  1505. }else if low_8_bit == 2 {
  1506. changeStatus = "MPPT_charge_mode".da_localizedStr()
  1507. }else if low_8_bit == 3 {
  1508. changeStatus = "Equalizing_charge_mode".da_localizedStr()
  1509. }else if low_8_bit == 4 {
  1510. changeStatus = "Floating_charge_mode".da_localizedStr()
  1511. }else if low_8_bit == 5 {
  1512. changeStatus = "Boost_charge_mode".da_localizedStr()
  1513. }else if low_8_bit == 6 {
  1514. changeStatus = "Current_limiting_(super_power)".da_localizedStr()
  1515. }else if low_8_bit == 7 {
  1516. changeStatus = "Fully_charged".da_localizedStr()
  1517. }
  1518. // realDataDict[key.solarStatus] = solarStatusStr
  1519. if B23 == "0" && B24 == "0" && B25 == "0" && B26 == "0" && B27 == "0" && B28 == "0" {
  1520. dataDict[key.solarStatus] = changeStatus
  1521. // realDataDict[key.solarStatus] = batteryStatusStr
  1522. }
  1523. //0000 0000 0000 0000 从后往前数
  1524. let loadStatus = "\(num0?.subString(start: 0, length: 1) ?? "")"
  1525. var loadStatusStr = ""
  1526. if B15 == "1" {
  1527. loadStatusStr = "Open_load".da_localizedStr()
  1528. }
  1529. if B19 == "1" {
  1530. loadStatusStr = "Load_short_circuit".da_localizedStr()
  1531. }
  1532. if B20 == "1" {
  1533. loadStatusStr = "Excessive_load_power_or_overload".da_localizedStr()
  1534. }
  1535. if B15 == "0" && B19 == "0" && B20 == "0" {
  1536. if loadStatus == "0" {
  1537. loadStatusStr = "Load_is_off".da_localizedStr()
  1538. }else if loadStatus == "1" {
  1539. loadStatusStr = "Load_is_on".da_localizedStr()
  1540. }
  1541. }
  1542. log.debug("负载状态 :\(num0?.subString(start: 7, length: 1))") // 低八位
  1543. dataDict[key.loadStatus] = loadStatusStr
  1544. // realDataDict[key.loadStatus] = loadStatusStr
  1545. //蓄电池状态
  1546. //太阳能电压 0107
  1547. let num1 = UUUtils.numberHexString(str0.subString(start: 47, length: 4))
  1548. print("太阳能电压 \(num1!.floatValue * 0.1)" )
  1549. let solarV = String(format: "%.1f", num1!.floatValue * 0.1)
  1550. dataDict[key.solarVoltage] = "\(solarV)"
  1551. // realDataDict[key.solarVoltage] = "\(solarV)"
  1552. // 太阳能电流 0108
  1553. let num2 = UUUtils.numberHexString(str0.subString(start: 47 + 4, length: 4))
  1554. print("太阳能电流 \(num2!.floatValue * 0.01)")
  1555. dataDict[key.solarCurrent] = String(format: "%.2f", num2!.floatValue * 0.01)
  1556. // realDataDict[key.solarCurrent] = String(format: "%.2f", num2!.floatValue * 0.01)
  1557. // 太阳能功率 0109
  1558. let num2_1 = UUUtils.numberHexString(str0.subString(start: 47 + 4 + 4, length: 4))
  1559. print("太阳能功率 \(num2_1!)")
  1560. dataDict[key.solarPower] = "\(num2_1!)"
  1561. // realDataDict[key.solarPower] = "\(num2_1!)"
  1562. //蓄电池 电压
  1563. let num3 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 4, length: 4))
  1564. print("蓄电池电压 \(num3!.floatValue * 0.1)")
  1565. dataDict[key.batteryVoltage] = String(format: "%.1f", num3!.floatValue * 0.1)
  1566. // realDataDict[key.batteryVoltage] = String(format: "%.1f", num3!.floatValue * 0.1)
  1567. let num4 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 5, length: 4))
  1568. print("蓄电池电流\(num4!.floatValue * 0.01)")
  1569. dataDict[key.batteryCurrent] = String(format: "%.2f", num4!.floatValue * 0.01)
  1570. // realDataDict[key.batteryCurrent] = String(format: "%.2f", num4!.floatValue * 0.01)
  1571. let num5 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 6 + 2 , length: 2))
  1572. print("温度 \(temp(with: num5!))")
  1573. dataDict[key.batteryTemp] = "\(temp(with: num5!))"
  1574. // realDataDict[key.batteryTemp] = "\(temp(with: num5!))"
  1575. //负载
  1576. let num6 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 7, length: 4))
  1577. print("负载电压\(num6!.floatValue * 0.1)")
  1578. dataDict[key.loadVoltage] = String(format: "%.1f", num6!.floatValue * 0.1)
  1579. // realDataDict[key.loadVoltage] = String(format: "%.1f", num6!.floatValue * 0.1)
  1580. let num7 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 8, length: 4))
  1581. print("负载电流\(num7!.floatValue * 0.01)")
  1582. dataDict[key.loadCurrent] = String(format: "%.2f", num7!.floatValue * 0.01)
  1583. // realDataDict[key.loadCurrent] = String(format: "%.2f", num7!.floatValue * 0.01)
  1584. let num8 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 9, length: 4))
  1585. print("功率 \(num8! )")
  1586. dataDict[key.loadPower] = "\(num8!)"
  1587. // realDataDict[key.loadPower] = "\(num8!)"
  1588. }
  1589. func realdata_0100(_ reciveData : String) {
  1590. let str0 = reciveData
  1591. log.debug("处理真实数据 - \(str0)")
  1592. //太阳能电压 0107
  1593. let num1 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 7, length: 4))
  1594. print("太阳能电压 \(num1!.floatValue * 0.1)" )
  1595. let solarV = String(format: "%.1f", num1!.floatValue * 0.1)
  1596. dataDict[key.solarVoltage] = "\(solarV)"
  1597. // realDataDict[key.solarVoltage] = "\(solarV)"
  1598. // 太阳能电流 0108
  1599. let num2 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 8, length: 4))
  1600. print("太阳能电流 \(num2!.floatValue * 0.01)")
  1601. dataDict[key.solarCurrent] = String(format: "%.2f", num2!.floatValue * 0.01)
  1602. //realDataDict[key.solarCurrent] = String(format: "%.2f", num2!.floatValue * 0.01)
  1603. // 太阳能功率 0109
  1604. let num2_1 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 9, length: 4))
  1605. print("太阳能功率 \(num2_1!)")
  1606. dataDict[key.solarPower] = "\(num2_1!)"
  1607. // realDataDict[key.solarPower] = "\(num2_1!)"
  1608. //蓄电池 电压 101
  1609. let num3 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 1, length: 4))
  1610. print("蓄电池电压 \(num3!.floatValue * 0.1)")
  1611. dataDict[key.batteryVoltage] = String(format: "%.1f", num3!.floatValue * 0.1)
  1612. // realDataDict[key.batteryVoltage] = String(format: "%.1f", num3!.floatValue * 0.1)
  1613. //蓄电池电流 102
  1614. let num4 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 2, length: 4))
  1615. print("蓄电池电流\(num4!.floatValue * 0.01)")
  1616. dataDict[key.batteryCurrent] = String(format: "%.2f", num4!.floatValue * 0.01)
  1617. // realDataDict[key.batteryCurrent] = String(format: "%.2f", num4!.floatValue * 0.01)
  1618. //蓄电池温度 0103
  1619. let num5 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 3 , length: 2))
  1620. print("温度 \(temp(with: num5!))")
  1621. dataDict[key.batteryTemp] = "\(temp(with: num5!))"
  1622. //realDataDict[key.batteryTemp] = "\(temp(with: num5!))"
  1623. //负载电压 0104
  1624. let num6 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 4, length: 4))
  1625. print("负载电压\(num6!.floatValue * 0.1)")
  1626. dataDict[key.loadVoltage] = String(format: "%.1f", num6!.floatValue * 0.1)
  1627. // realDataDict[key.loadVoltage] = String(format: "%.1f", num6!.floatValue * 0.1)
  1628. //负载电流 0105
  1629. let num7 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 5, length: 4))
  1630. print("负载电流\(num7!.floatValue * 0.01)")
  1631. dataDict[key.loadCurrent] = String(format: "%.2f", num7!.floatValue * 0.01)
  1632. //realDataDict[key.loadCurrent] = String(format: "%.2f", num7!.floatValue * 0.01)
  1633. //负载功率 0106
  1634. let num8 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 6, length: 4))
  1635. print("功率 \(num8! )")
  1636. // realDataDict[key.loadPower] = "\(num8!)"
  1637. dataDict[key.loadPower] = "\(num8!)"
  1638. }
  1639. func realdata_0120(_ reciveData : String) {
  1640. let str0 = reciveData
  1641. log.debug("处理真实数据 - \(str0)")
  1642. var num0A = UUUtils.getBinaryByHex(str0.subString(start: 11 , length: 8))
  1643. let num0A_1 = num0A?.subString(start: 0, length: 16)
  1644. let num0A_2 = num0A?.subString(start: 16, length: 16)
  1645. let num0A_II = num0A_2! + num0A_1!
  1646. num0A = num0A_II
  1647. log.debug("太阳能板状态\(num0A!)")
  1648. var solarStatusStr = ""
  1649. let B30 = num0A?.subString(start: 1, length: 1)
  1650. let B29 = num0A?.subString(start: 2, length: 1)
  1651. let B28 = num0A?.subString(start: 3, length: 1)
  1652. let B27 = num0A?.subString(start: 4, length: 1)
  1653. let B26 = num0A?.subString(start: 5, length: 1)
  1654. let B25 = num0A?.subString(start: 6, length: 1)
  1655. let B24 = num0A?.subString(start: 7, length: 1)
  1656. let B23 = num0A?.subString(start: 8, length: 1)
  1657. //负载
  1658. let B20 = num0A?.subString(start: 11, length: 1)
  1659. let B19 = num0A?.subString(start: 12, length: 1)
  1660. let B15 = num0A?.subString(start: 16, length: 1)
  1661. //系统
  1662. let B21 = num0A?.subString(start: 10, length: 1)
  1663. let B14 = num0A?.subString(start: 17, length: 1)
  1664. let B13 = num0A?.subString(start: 18, length: 1)
  1665. //蓄电池状态
  1666. let B22 = num0A?.subString(start: 9, length: 1)
  1667. let B18 = num0A?.subString(start: 13, length: 1)
  1668. let B17 = num0A?.subString(start: 14, length: 1)
  1669. let B16 = num0A?.subString(start: 15, length: 1)
  1670. let B12 = num0A?.subString(start: 19, length: 1)
  1671. let B11 = num0A?.subString(start: 20, length: 1)
  1672. let B10 = num0A?.subString(start: 21, length: 1)
  1673. var batteryStatusStr = ""
  1674. if B10 == "1" {
  1675. batteryStatusStr = "BMS_overcharge_protection".da_localizedStr()
  1676. }
  1677. if B11 == "1" {
  1678. batteryStatusStr = "Battery_low_temperature_protection,_stop_charging".da_localizedStr()
  1679. }
  1680. if B12 == "1" {
  1681. batteryStatusStr = "Battery_reverse_polarity".da_localizedStr()
  1682. }
  1683. if B16 == "1" {
  1684. batteryStatusStr = "Battery_over_discharge".da_localizedStr()
  1685. }
  1686. if B17 == "1" {
  1687. batteryStatusStr = "Battery_overvoltage".da_localizedStr()
  1688. }
  1689. if B18 == "1" {
  1690. batteryStatusStr = "Undervoltage_warning".da_localizedStr()
  1691. }
  1692. if B22 == "1" {
  1693. batteryStatusStr = "External_ambient_temperature_is_too_high".da_localizedStr()
  1694. }
  1695. if B10 == "0" && B11 == "0" && B12 == "0" && B16 == "0" && B17 == "0" && B18 == "0" && B22 == "0"{
  1696. batteryStatusStr = "Normal".da_localizedStr()
  1697. }
  1698. dataDict[key.batteryStatus] = batteryStatusStr
  1699. // realDataDict[key.batteryStatus] = batteryStatusStr
  1700. //太阳能状态
  1701. if B23 == "1" {
  1702. solarStatusStr = "PV_input_power_is_too_large".da_localizedStr()
  1703. }
  1704. if B24 == "1" {
  1705. solarStatusStr = "Short_circuit_of_photovoltaic_input".da_localizedStr()
  1706. }
  1707. if B25 == "1" {
  1708. solarStatusStr = "Overvoltage_at_the_photovoltaic_input".da_localizedStr()
  1709. }
  1710. if B26 == "1" {
  1711. solarStatusStr = "Solar_panel_countercurrent".da_localizedStr()
  1712. }
  1713. if B27 == "1" {
  1714. solarStatusStr = "Solar_panel_working_point_overpressure".da_localizedStr()
  1715. }
  1716. if B28 == "1" {
  1717. solarStatusStr = "Solar_panel_reverse_connection".da_localizedStr()
  1718. }
  1719. dataDict[key.solarStatus] = solarStatusStr
  1720. //0120
  1721. let num0 = UUUtils.getBinaryByHex(str0.subString(start: 7, length: 4))
  1722. log.debug("0120 --- \(num0!)")
  1723. //0000 0000 0000 0000 从后往前数。 1000 0000 0000 0000
  1724. let loadStatus = "\(num0?.subString(start: 0, length: 1) ?? "")"
  1725. //di低八位
  1726. let low_8_bit = UUUtils.numberHexString(str0.subString(start: 9, length: 2))
  1727. log.debug("low_8_bit - \(low_8_bit)")
  1728. //1000000000000000
  1729. var changeStatus = ""
  1730. if low_8_bit == 0 {
  1731. changeStatus = "Charging_is_not_turned_on".da_localizedStr()
  1732. }else if low_8_bit == 1 {
  1733. changeStatus = "Start_charging_mode".da_localizedStr()
  1734. }else if low_8_bit == 2 {
  1735. changeStatus = "MPPT_charge_mode".da_localizedStr()
  1736. }else if low_8_bit == 3 {
  1737. changeStatus = "Equalizing_charge_mode".da_localizedStr()
  1738. }else if low_8_bit == 4 {
  1739. changeStatus = "Floating_charge_mode".da_localizedStr()
  1740. }else if low_8_bit == 5 {
  1741. changeStatus = "Boost_charge_mode".da_localizedStr()
  1742. }else if low_8_bit == 6 {
  1743. changeStatus = "Current_limiting_(super_power)".da_localizedStr()
  1744. }else if low_8_bit == 7 {
  1745. changeStatus = "Fully_charged".da_localizedStr()
  1746. }
  1747. // realDataDict[key.solarStatus] = solarStatusStr
  1748. if B23 == "0" && B24 == "0" && B25 == "0" && B26 == "0" && B27 == "0" && B28 == "0" {
  1749. dataDict[key.solarStatus] = changeStatus
  1750. }
  1751. var loadStatusStr = ""
  1752. if B15 == "1" {
  1753. loadStatusStr = "Open_load".da_localizedStr()
  1754. }
  1755. if B19 == "1" {
  1756. loadStatusStr = "Load_short_circuit".da_localizedStr()
  1757. }
  1758. if B20 == "1" {
  1759. loadStatusStr = "Excessive_load_power_or_overload".da_localizedStr()
  1760. }
  1761. if B15 == "0" && B19 == "0" && B20 == "0" {
  1762. if loadStatus == "0" {
  1763. loadStatusStr = "Load_is_off".da_localizedStr()
  1764. }else if loadStatus == "1" {
  1765. loadStatusStr = "Load_is_on".da_localizedStr()
  1766. }
  1767. }
  1768. log.debug("负载状态 :\(num0?.subString(start: 7, length: 1))") // 低八位
  1769. //充电状态 --- \(0120低八位)
  1770. dataDict[key.loadStatus] = loadStatusStr
  1771. // realDataDict[key.loadStatus] = loadStatusStr
  1772. }
  1773. //处理历史数据
  1774. func hisData(_ reciveData:String) {
  1775. let str0 = reciveData
  1776. log.debug("处理真实数据 - \(str0)")
  1777. var F000Dict = [String: String]()
  1778. //蓄电池当天最低电压
  1779. let num0 = UUUtils.numberHexString(str0.subString(start: 7, length: 4))
  1780. log.debug("蓄电池当天最低电压: \( String(format: "%.1f", num0!.floatValue * 0.1))")
  1781. F000Dict[key.his_batteryVoltageMin] = String(format: "%.1f", num0!.floatValue * 0.1)
  1782. //蓄电池当天最高电压
  1783. let num1 = UUUtils.numberHexString(str0.subString(start: 7 + 4, length: 4))
  1784. log.debug("蓄电池当天最高电压\(String(format: "%.1f", num1!.floatValue * 0.1))")
  1785. F000Dict[key.his_batteryVoltageMax] = String(format: "%.1f", num1!.floatValue * 0.1)
  1786. let num2 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 2, length: 4))
  1787. log.debug("当天充电最大电流\(String(format: "%.2f", num2!.floatValue * 0.01))")
  1788. F000Dict[key.his_chargingCurrentMax] = String(format: "%.2f", num2!.floatValue * 0.01)
  1789. let num3 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 3, length: 4))
  1790. log.debug("当天放电最大电流\(String(format: "%.2f", num3!.floatValue * 0.01))")
  1791. F000Dict[key.his_dischargeCurrentmax] = String(format: "%.2f", num3!.floatValue * 0.01)
  1792. let num4 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 4, length: 4))
  1793. log.debug("当天充电最大功率\(num4!)" )
  1794. F000Dict[key.his_chargingPowerMax] = "\(num4!)"
  1795. let num5 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 5 , length: 4))
  1796. log.debug("当天放电最大功率\(num5!)")
  1797. F000Dict[key.his_dischargePowerMax] = "\(num5!)"
  1798. let num6 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 6, length: 4))
  1799. log.debug("充电安时数 \(num6!)")
  1800. F000Dict[key.his_chargingAmpereHour] = "\(num6!)"
  1801. let num7 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 7 , length: 4))
  1802. log.debug("放电安时数 \(num7!)")
  1803. F000Dict[key.his_dischargeAmpereHour] = "\(num7!)"
  1804. let num8 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 8, length: 4))
  1805. log.debug("当天发电量 \(num8!)")
  1806. F000Dict[key.his_generationCapacity] = "\(num8!)"
  1807. let num9 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 9, length: 4))
  1808. log.debug("充电量 \(num9!)")
  1809. F000Dict[key.his_dischargeCapacity] = "\(num9!)"
  1810. historyDataArr.append(F000Dict)
  1811. }
  1812. func realdata_F000(_ reciveData : String) {
  1813. hisData(reciveData)
  1814. }
  1815. func realdata_F001(_ reciveData : String) {
  1816. hisData(reciveData)
  1817. }
  1818. func realdata_F002(_ reciveData : String) {
  1819. hisData(reciveData)
  1820. }
  1821. func realdata_F003(_ reciveData : String) {
  1822. hisData(reciveData)
  1823. }
  1824. func realdata_F004(_ reciveData : String) {
  1825. hisData(reciveData)
  1826. }
  1827. func realdata_F005(_ reciveData : String) {
  1828. hisData(reciveData)
  1829. }
  1830. func realdata_F006(_ reciveData : String) {
  1831. hisData(reciveData)
  1832. //dataDict[key.historydatas] = historyDataArr
  1833. }
  1834. func realdata_F007(_ reciveData : String) {
  1835. hisData(reciveData)
  1836. dataDict[key.historydatas] = historyDataArr
  1837. }
  1838. func realdata_0115(_ reciveData :String) {
  1839. let str0 = reciveData
  1840. log.debug("处理真实数据 - \(str0)")
  1841. //
  1842. let num0 = UUUtils.numberHexString(str0.subString(start: 7 , length: 4))
  1843. log.debug("总运行天数 - \(num0!)")
  1844. dataDict[key.runTime] = ("\(num0!)")
  1845. let num1 = UUUtils.numberHexString(str0.subString(start: 7 + 4 , length: 4))
  1846. log.debug("蓄电池总过放次数 - \(num1!)")
  1847. dataDict[key.overTimes] = "\(num1!)"
  1848. let num2 = UUUtils.numberHexString(str0.subString(start: 7 + 8 , length: 4))
  1849. log.debug("蓄电池总充满次数 - \(num2!)")
  1850. dataDict[key.fullTimes] = "\(num2!)"
  1851. let num3 = UUUtils.numberHexString(str0.subString(start: 7 + 12 , length: 8))
  1852. log.debug("蓄电池总充电安时数 - \(num3!)")
  1853. dataDict[key.chargingAmpereHour] = "\(num3!)"
  1854. let num4 = UUUtils.numberHexString(str0.subString(start: 7 + 12 + 8 , length: 8))
  1855. log.debug("蓄电池总放电安时数 - \(num4!)")
  1856. dataDict[key.dischargeAmpereHour] = "\(num4!)"
  1857. let num5 = UUUtils.numberHexString(str0.subString(start: 7 + 12 + 8 * 2 , length: 8))
  1858. log.debug("累计发电量 - \(num5!)")
  1859. dataDict[key.generatEnergyTotal] = "\(num5!)"
  1860. let num6 = UUUtils.numberHexString(str0.subString(start: 7 + 12 + 8 * 3, length: 8))
  1861. log.debug("累计用电量 - \(num6!)")
  1862. dataDict[key.usedEnergyTotal] = "\(num6!)"
  1863. }
  1864. func realdata_E001(_ reciveData : String) {
  1865. let str0 = reciveData
  1866. log.debug("处理真实数据 - \(str0)")
  1867. //e001
  1868. // 判断是否自动识别
  1869. let num0 = UUUtils.numberHexString(str0.subString(start: 7 , length: 4))
  1870. log.debug(" 最大充电电流- \(String(format: "%.2f", num0!.floatValue * 0.01))")
  1871. dataDict[key.MaxChargeCurrent] = "\(String(format: "%.2f", num0!.floatValue * 0.01))"
  1872. }
  1873. func realdata_E003(_ reciveData : String) {
  1874. let str0 = reciveData
  1875. log.debug("处理真实数据 - \(str0)")
  1876. //e003
  1877. let FF = str0.subString(start: 7, length: 2)
  1878. if FF == "FF" || FF == "ff"{
  1879. dataDict[key.autoRecognition] = "1"
  1880. // 判断是否自动识别
  1881. let num0 = UUUtils.numberHexString(str0.subString(start: 7 + 2 , length: 2))
  1882. log.debug("系统电压设置 - \(num0!)")
  1883. if num0 == 0 {
  1884. dataDict[key.BatRateVolt] = "Auto".da_localizedStr()
  1885. dataDict[key.autoRecognition] = "1"
  1886. }else{
  1887. dataDict[key.BatRateVolt] = "\(num0!)"
  1888. }
  1889. }else{
  1890. dataDict[key.autoRecognition] = "0"
  1891. // 判断是否自动识别
  1892. let num0 = UUUtils.numberHexString(str0.subString(start: 7 , length: 2))
  1893. log.debug("系统电压设置 - \(num0!)")
  1894. if num0 == 0 {
  1895. dataDict[key.BatRateVolt] = "Auto".da_localizedStr()
  1896. dataDict[key.autoRecognition] = "1"
  1897. }else{
  1898. dataDict[key.BatRateVolt] = "\(num0!)"
  1899. }
  1900. }
  1901. // // FF
  1902. // let FF = str0.subString(start: 7 , length: 2)
  1903. // if FF == "FF"{
  1904. // dataDict[key.autoRecognition] = "1"
  1905. // //paramDataDict[key.autoRecognition] = "1"
  1906. // }else{
  1907. // dataDict[key.autoRecognition] = "0"
  1908. // // paramDataDict[key.autoRecognition] = "0"
  1909. // }
  1910. // 判断是否自动识别
  1911. let num0 = UUUtils.numberHexString(str0.subString(start: 7 + 2 , length: 2))
  1912. log.debug("系统电压设置 - \(num0!)")
  1913. dataDict[key.BatRateVolt] = "\(num0!)"
  1914. // paramDataDict[key.BatRateVolt] = "\(num0!)"
  1915. //e004
  1916. let num1 = UUUtils.numberHexString(str0.subString(start: 7 + 4 , length: 4))
  1917. var batterytype = ""
  1918. switch num1?.intValue {
  1919. case 0:
  1920. batterytype = "User-defined".da_localizedStr()
  1921. case 1:
  1922. batterytype = "Vented_lead_acid_battery_FLD".da_localizedStr()
  1923. case 2:
  1924. batterytype = "Lead_acid_battery_SLD".da_localizedStr()
  1925. case 3:
  1926. batterytype = "Gel_lead_acid_GEL".da_localizedStr()
  1927. case 4:
  1928. batterytype = "Lithium_iron_phosphate_battery_LI".da_localizedStr()
  1929. case 5:
  1930. batterytype = "三元锂电池".da_localizedStr()
  1931. case 6:
  1932. batterytype = "Lithium_iron_phosphate_battery_LI".da_localizedStr()
  1933. case 7:
  1934. batterytype = "超级电容".da_localizedStr()
  1935. case 10:
  1936. batterytype = "自定义(铅酸专用)".da_localizedStr()
  1937. default: break
  1938. }
  1939. log.debug("电池类型: \(batterytype)")
  1940. dataDict[key.BatTypeSet] = "\(batterytype)"
  1941. //e005
  1942. let num2 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 2 , length: 4))
  1943. log.debug("超压电压: \(num2!.floatValue * 0.1)")
  1944. dataDict[key.BatOverVolt] = "\(num2!.floatValue * 0.1)"
  1945. //e006
  1946. let num3 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 3, length: 4))
  1947. //BatChgLimitVolt
  1948. log.debug("充电限制电压:\(num3!.floatValue * 0.1)")
  1949. dataDict[key.BatChgLimitVolt] = "\(num3!.floatValue * 0.1)"
  1950. //e007
  1951. let num4 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 4, length: 4))
  1952. log.debug("均衡充电电压\(num4!.floatValue * 0.1)")
  1953. dataDict[key.BatConstChgVolt] = String(format: "%.1f", num4!.floatValue * 0.1)
  1954. //e008
  1955. let num5 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 5, length: 4))
  1956. log.debug("提升充电电压/过充返回电压\(num5!.floatValue * 0.1)")
  1957. dataDict[key.BatImprovChgVolt] = "\(String(format: "%.1f", num5!.floatValue * 0.1))"
  1958. //e009
  1959. let num6 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 6, length: 4))
  1960. log.debug("浮充充电电压/过充返回电压\(num6!.floatValue * 0.1)")
  1961. dataDict[key.BatFloatChgVolt] = "\(num6!.floatValue * 0.1)"
  1962. //e00a
  1963. let num7 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 7, length: 4))
  1964. log.debug("提升充电返回电压\(num7!.floatValue * 0.1)")
  1965. dataDict[key.BatImprovChgBackVolt] = String(format: "%.1f", num7!.floatValue * 0.1)
  1966. //e00b
  1967. let num8 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 8, length: 4))
  1968. log.debug("过放返回电压\(num8!.floatValue * 0.1)")
  1969. dataDict[key.BatOverDischgBackVolt] = "\(num8!.floatValue * 0.1)"
  1970. //paramDataDict[key.BatOverDischgBackVolt] = "\(num8!.floatValue * 0.1)"
  1971. //e00c
  1972. let num9 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 9, length: 4))
  1973. log.debug("欠压警告电压\(num9!.floatValue * 0.1)")
  1974. dataDict[key.BatUnderVolt] = "\(num9!.floatValue * 0.1)"
  1975. //e00d
  1976. let num10 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 10, length: 4))
  1977. log.debug("过放电压\(num10!.floatValue * 0.1)")
  1978. dataDict[key.BatOverDischgVolt] = "\(num10!.floatValue * 0.1)"
  1979. //e00e
  1980. let num11 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 11, length: 4))
  1981. log.debug("放电限制电压\(num11!.floatValue * 0.1)")
  1982. dataDict[key.BatDischgLimitVolt] = "\(num11!.floatValue * 0.1)"
  1983. //e010
  1984. let num13 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 13, length: 4))
  1985. log.debug("过放延时时间\(num13!)")
  1986. dataDict[key.BatOverDischgDelayTime] = "\(num13!)"
  1987. //e011
  1988. let num14 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 14, length: 4))
  1989. log.debug("均衡充电时间\(num14!)")
  1990. dataDict[key.BatConstChgTime] = "\(num14!)"
  1991. //e012
  1992. let num15 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 15, length: 4))
  1993. log.debug("提升充电时间 \(num15!)")
  1994. dataDict[key.BatImprovChgTime] = "\(num15!)"
  1995. //e013
  1996. let num16 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 16, length: 4))
  1997. log.debug("均衡充电间隔\(num16!)")
  1998. dataDict[key.BatConstChgGapTime] = "\(num16!)"
  1999. // paramDataDict[key.BatConstChgGapTime] = "\(num16!)"
  2000. //e014
  2001. let num17 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 17, length: 4))
  2002. log.debug("温度补偿系数\(num17!)")
  2003. dataDict[key.CoeffTemperCompen] = "\(num17!)"
  2004. // paramDataDict[key.CoeffTemperCompen] = "\(num17!)"
  2005. //e015
  2006. let num18 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 18, length: 4))
  2007. log.debug("充电上限温度\(temp(with: num18!))")
  2008. dataDict[key.ChgMaxTemper] = "\(temp(with: num18!))"
  2009. // paramDataDict[key.ChgMaxTemper] = "\(temp(with: num18!))"
  2010. //e016
  2011. let num19 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 19, length: 4))
  2012. log.debug("充电下限温度\(temp(with: num19!))")
  2013. dataDict[key.ChgMinTemper] = "\(temp(with: num19!))"
  2014. // paramDataDict[key.ChgMinTemper] = "\(temp(with: num19!))"
  2015. //e017
  2016. let num20 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 20, length: 4))
  2017. log.debug("放电上限温度\(temp(with: num20!))")
  2018. dataDict[key.DisChagMaxTemper] = "\(temp(with: num20!))"
  2019. /// paramDataDict[key.DisChagMaxTemper] = "\(temp(with: num20!))"
  2020. //e018
  2021. let num21 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 21, length: 4))
  2022. log.debug("放电下限温度\(temp(with: num21!))")
  2023. dataDict[key.DisChaMinTemper] = "\(temp(with: num21!))"
  2024. // paramDataDict[key.DisChaMinTemper] = "\(temp(with: num21!))"
  2025. //e01d
  2026. let num26 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 26, length: 4))
  2027. var loadwordmode = ""
  2028. switch num26?.intValue {
  2029. case 0:
  2030. loadwordmode = "Light_control".da_localizedStr()
  2031. case 1:
  2032. loadwordmode = "Light_control,_delay_1_hour".da_localizedStr()
  2033. case 2:
  2034. loadwordmode = "Light_control,_delay_2_hours".da_localizedStr()
  2035. case 3:
  2036. loadwordmode = "Light_control,_delay_3_hours".da_localizedStr()
  2037. case 4:
  2038. loadwordmode = "Light_control,_delay_4_hours".da_localizedStr()
  2039. case 5:
  2040. loadwordmode = "Light_control,_delay_5_hours".da_localizedStr()
  2041. case 6:
  2042. loadwordmode = "Light_control,_delay_6_hours".da_localizedStr()
  2043. case 7:
  2044. loadwordmode = "Light_control,_delay_7_hours".da_localizedStr()
  2045. case 8:
  2046. loadwordmode = "Light_control,_delay_8_hours".da_localizedStr()
  2047. case 9:
  2048. loadwordmode = "Light_control,_delay_9_hours".da_localizedStr()
  2049. case 10:
  2050. loadwordmode = "Light_control,_delay_10_hours".da_localizedStr()
  2051. case 11:
  2052. loadwordmode = "Light_control,_delay_11_hours".da_localizedStr()
  2053. case 12:
  2054. loadwordmode = "Light_control,_delay_12_hours".da_localizedStr()
  2055. case 13:
  2056. loadwordmode = "Light_control,_delay_13_hours".da_localizedStr()
  2057. case 14:
  2058. loadwordmode = "Light_control,_delay_14_hours".da_localizedStr()
  2059. case 15:
  2060. loadwordmode = "Manual_mode".da_localizedStr()
  2061. case 16:
  2062. loadwordmode = "Debug_mode".da_localizedStr()
  2063. case 17:
  2064. loadwordmode = "Normal-on_mode".da_localizedStr()
  2065. case 18:
  2066. loadwordmode = "Timed_mode".da_localizedStr()
  2067. default:
  2068. break
  2069. }
  2070. log.debug("直流负载工作模式\(loadwordmode)")
  2071. dataDict[key.DcLoadMode] = "\(loadwordmode)"
  2072. // paramDataDict[key.DcLoadMode] = "\(loadwordmode)"
  2073. //e01e
  2074. let num27 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 27, length: 4))
  2075. log.debug("光控延时时间\(num27!)")
  2076. dataDict[key.LightCtrlDelayTime] = "\(num27!)"
  2077. // paramDataDict[key.LightCtrlDelayTime] = "\(num27!)"
  2078. //d01f
  2079. let num28 = UUUtils.numberHexString(str0.subString(start: 7 + 4 * 28, length: 4))
  2080. log.debug("光控电压\(num28!)")
  2081. dataDict[key.LightCtrlVolt] = "\(num28!)"
  2082. /// paramDataDict[key.LightCtrlVolt] = "\(num28!)"
  2083. }
  2084. func realdata_DF00(_ reciveData : String) {
  2085. let str0 = reciveData
  2086. log.debug("处理真实数据 - \(str0)")
  2087. // <ff030200009190>
  2088. let num0 = UUUtils.numberHexString(str0.subString(start: 7, length: 4))
  2089. log.debug("开关控制 —— \(num0)")
  2090. var cmd = ""
  2091. if num0?.intValue == 0 {
  2092. cmd = "Off".da_localizedStr()
  2093. }else if num0?.intValue == 1{
  2094. cmd = "On".da_localizedStr()
  2095. }
  2096. dataDict[key.CmdPowerOnOff] = cmd
  2097. /// paramDataDict[key.CmdPowerOnOff] = cmd
  2098. }
  2099. func realdata_DF0D(_ reciveData : String) {
  2100. let str0 = reciveData
  2101. log.debug("处理真实数据 - \(str0)")
  2102. // <ff030200 009190>
  2103. let num0 = UUUtils.numberHexString(str0.subString(start: 7, length: 4))
  2104. log.debug("手动均衡 —— \(num0)")
  2105. var canhandle = ""
  2106. if num0?.intValue == 0 {
  2107. canhandle = "Prohibited".da_localizedStr()
  2108. }else if num0?.intValue == 1{
  2109. canhandle = "Enable".da_localizedStr()
  2110. }
  2111. dataDict[key.BattEqualChgImmediate] = canhandle
  2112. /// paramDataDict[key.BattEqualChgImmediate] = canhandle
  2113. }
  2114. func realdata_E021(_ reciveData : String) {
  2115. let str0 = reciveData
  2116. log.debug("处理真实数据 - \(str0)")
  2117. // <ff030201009000> str0.subString(start: 7, length: 4)
  2118. let num0 = UUUtils.getBinaryByHex(str0.subString(start: 7, length: 4))
  2119. log.debug("E021 —— \(num0)")
  2120. //RX
  2121. let rx = num0?.subString(start: 2, length: 3)
  2122. var rx_function = ""
  2123. if rx == "000"{
  2124. rx_function = "Normal_communication".da_localizedStr()
  2125. }
  2126. log.debug("Rx端口 \(rx_function)")
  2127. dataDict[key.rx] = "\(rx_function)"
  2128. // paramDataDict[key.rx] = "\(rx_function)"
  2129. let tx = num0?.subString(start: 5, length: 3)
  2130. log.debug("")
  2131. var tx_function = ""
  2132. if tx == "000" {
  2133. tx_function = "Normal_communication".da_localizedStr()
  2134. }
  2135. log.debug("tx端口 \(tx_function)")
  2136. dataDict[key.tx] = "\(tx_function)"
  2137. }
  2138. }