144 lines
5.0 KiB
Swift
144 lines
5.0 KiB
Swift
//
|
|
// BaseDeviceViewController.swift
|
|
// pocloud
|
|
//
|
|
// Created by Patrick McDonagh on 6/11/18.
|
|
// Copyright © 2018 patrickjmcd. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import RealmSwift
|
|
import FirebaseDatabase
|
|
import SVProgressHUD
|
|
import PromiseKit
|
|
|
|
class BaseDeviceViewController: UIViewController {
|
|
let realm = try! Realm()
|
|
var ref: DatabaseReference!
|
|
let baseURL = (UIApplication.shared.delegate as! AppDelegate).baseURL
|
|
let user = (UIApplication.shared.delegate as! AppDelegate).user
|
|
|
|
var thisDevice : Device?
|
|
var deviceTypes : Results<DeviceType>?
|
|
var selectedChannel : Channel?
|
|
|
|
var channelLookup : [String : Int] = [:]
|
|
var values : [String : MeshifyValue] = [:]
|
|
var updatedChannelNames : [String] = [String]()
|
|
var changedChannelNames : [String] = [String]()
|
|
let numFormatter = NumberFormatter()
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
ref = Database.database().reference()
|
|
|
|
numFormatter.minimumFractionDigits = 0
|
|
numFormatter.maximumFractionDigits = 3
|
|
numFormatter.minimumIntegerDigits = 1
|
|
|
|
initializeData()
|
|
}
|
|
|
|
func initializeData() {
|
|
let macAddress = String((thisDevice?.macAddress.replacingOccurrences(of: ":", with: "").uppercased().dropLast(4))!)
|
|
let deviceTypeName = (thisDevice?.parentDeviceType.first?.name)!
|
|
SVProgressHUD.show()
|
|
firstly {
|
|
loadData()
|
|
}.then { _ in
|
|
getChannels(deviceTypeId: self.thisDevice!.deviceTypeId, baseURL: self.baseURL, authToken: (self.user?.authToken)!)
|
|
}.then { _ in
|
|
getChannelValues(deviceId: self.thisDevice!.id, baseURL: self.baseURL, authToken: (self.user?.authToken)!)
|
|
}.done { (valueDict) in
|
|
self.values = valueDict
|
|
self.updateGauges()
|
|
self.updateChartData()
|
|
SVProgressHUD.dismiss()
|
|
if let dev = self.thisDevice {
|
|
for chanIndex in 0..<dev.values.count {
|
|
let chanName = dev.values[chanIndex].name
|
|
self.channelLookup[chanName] = chanIndex
|
|
}
|
|
}
|
|
self.getFirebaseDeviceValues(deviceMacAddress: macAddress, deviceTypeName: deviceTypeName)
|
|
}.catch { error in
|
|
SVProgressHUD.showError(withStatus: "\(error)")
|
|
print("Error fetching data in DeviceDetailViewController: \(error)")
|
|
}
|
|
}
|
|
|
|
override func viewWillDisappear(_ animated: Bool) {
|
|
Database.database().reference().removeAllObservers()
|
|
}
|
|
|
|
func getFirebaseDeviceValues(deviceMacAddress : String, deviceTypeName : String) {
|
|
Database.database().reference().child("devices")
|
|
.child(deviceMacAddress)
|
|
.child(deviceTypeName)
|
|
.child("channels")
|
|
.observe(.childChanged) { snapshot in
|
|
let value = snapshot.value as! NSDictionary
|
|
let chanVal = MeshifyValue()
|
|
|
|
if let name = value["name"] as? String {
|
|
chanVal.name = name
|
|
}
|
|
|
|
if let timestamp = value["timestamp"] as? String {
|
|
chanVal.timestamp = Int(Double(timestamp)!)
|
|
}
|
|
|
|
if let readValue = value["value"] as? String {
|
|
chanVal.value = readValue
|
|
}
|
|
|
|
let prevChanValue = self.values[chanVal.name]?.value
|
|
if prevChanValue != chanVal.value {
|
|
self.changedChannelNames.append(chanVal.name)
|
|
} else {
|
|
self.updatedChannelNames.append(chanVal.name)
|
|
}
|
|
self.values[chanVal.name] = chanVal
|
|
self.updateGauges()
|
|
self.updateChartData()
|
|
}
|
|
|
|
}
|
|
|
|
func updateGauges(){
|
|
return
|
|
}
|
|
|
|
func updateChartData(){
|
|
return
|
|
}
|
|
|
|
func tryRound(value : String, places numberOfPlaces: Int) -> String {
|
|
if let doubledValue = Double(value) {
|
|
if let formattedString = numFormatter.string(from: NSNumber(value: doubledValue)) {
|
|
return formattedString
|
|
} else {
|
|
return value
|
|
}
|
|
} else {
|
|
return value
|
|
}
|
|
}
|
|
|
|
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
|
|
if segue.identifier == "goToChannelView" {
|
|
let destinationVC = segue.destination as! ChannelDetailViewController
|
|
destinationVC.thisDevice = thisDevice!
|
|
destinationVC.thisChannel = selectedChannel!
|
|
}
|
|
}
|
|
|
|
func loadData() -> Promise<Void> {
|
|
return Promise { promise in
|
|
deviceTypes = realm.objects(DeviceType.self)
|
|
thisDevice = realm.objects(Device.self).filter("id == %d", thisDevice!.id).first!
|
|
promise.fulfill(())
|
|
}
|
|
}
|
|
}
|