Pulls channel history from Firebase, not meshify
This commit is contained in:
@@ -12,10 +12,11 @@ import SwiftyJSON
|
||||
import PromiseKit
|
||||
import Alamofire
|
||||
import SVProgressHUD
|
||||
import FirebaseDatabase
|
||||
|
||||
class ChannelDetailViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
|
||||
let realm = try! Realm()
|
||||
|
||||
var ref : DatabaseReference!
|
||||
let baseURL = (UIApplication.shared.delegate as! AppDelegate).baseURL
|
||||
let user = (UIApplication.shared.delegate as! AppDelegate).user
|
||||
let channelDataTypes = (UIApplication.shared.delegate as! AppDelegate).channelDataTypes
|
||||
@@ -25,6 +26,8 @@ class ChannelDetailViewController: UIViewController, UITableViewDelegate, UITabl
|
||||
var thisChannel : Channel?
|
||||
var channelHistory : [ChannelHistoryValue] = [ChannelHistoryValue]()
|
||||
|
||||
|
||||
|
||||
@IBOutlet weak var deviceNameLabel: UILabel!
|
||||
@IBOutlet weak var nameLabel: UILabel!
|
||||
@IBOutlet weak var helpDescriptionLabel: UILabel!
|
||||
@@ -42,12 +45,18 @@ class ChannelDetailViewController: UIViewController, UITableViewDelegate, UITabl
|
||||
historyTableView.dataSource = self
|
||||
setupChannelDisplay()
|
||||
SVProgressHUD.show()
|
||||
ref = Database.database().reference()
|
||||
let macAddress = String((thisDevice?.macAddress.replacingOccurrences(of: ":", with: "").uppercased().dropLast(4))!)
|
||||
let deviceTypeName = (thisDevice?.parentDeviceType.first?.name)!
|
||||
let channelName = thisChannel?.name
|
||||
|
||||
firstly {
|
||||
getChannelHistory(deviceId: (self.thisDevice?.id)!, channelId: (self.thisChannel?.id)!, baseURL: self.baseURL, authToken: (self.user?.authToken)!)
|
||||
getFirebaseChannelHistory(deviceMacAddress: macAddress, deviceTypeName: deviceTypeName, channelName: channelName!)
|
||||
}.done { (hist) in
|
||||
self.channelHistory = hist
|
||||
self.historyTableView.reloadData()
|
||||
SVProgressHUD.dismiss()
|
||||
self.ref.removeAllObservers()
|
||||
}.catch { error in
|
||||
print("Error in ChannelDetailViewController promise: \(error)")
|
||||
}
|
||||
@@ -121,4 +130,32 @@ class ChannelDetailViewController: UIViewController, UITableViewDelegate, UITabl
|
||||
present(alert, animated: true, completion: nil)
|
||||
}
|
||||
|
||||
func getFirebaseChannelHistory(deviceMacAddress: String, deviceTypeName : String, channelName : String) -> Promise<[ChannelHistoryValue]>{
|
||||
return Promise { promise in
|
||||
var channelHist : [ChannelHistoryValue] = [ChannelHistoryValue]()
|
||||
let getVals = self.ref.child("devices")
|
||||
.child(deviceMacAddress)
|
||||
.child(deviceTypeName)
|
||||
.child("history")
|
||||
.child(channelName)
|
||||
.observe(.value) { snapshot in
|
||||
if let snapValue = snapshot.value as? NSDictionary {
|
||||
for (_, value) in snapValue {
|
||||
let valueJSON : JSON = JSON(value)
|
||||
let chVal = ChannelHistoryValue()
|
||||
chVal.value = valueJSON["value"].stringValue
|
||||
chVal.timestamp = Date(timeIntervalSince1970: Double(valueJSON["timestamp"].intValue))
|
||||
channelHist.append(chVal)
|
||||
}
|
||||
promise.fulfill(channelHist.sorted(by: { (firstVal, secondVal) -> Bool in
|
||||
return firstVal.timestamp! > secondVal.timestamp!
|
||||
}))
|
||||
} else {
|
||||
print("ref: \(snapshot.ref)")
|
||||
promise.reject("Bad NSDictionary in getFirebaseChannelHistory" as! Error)
|
||||
}
|
||||
}
|
||||
ref.removeObserver(withHandle: getVals)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ import FirebaseDatabase
|
||||
|
||||
class DeviceDetailViewController: UITableViewController {
|
||||
let realm = try! Realm()
|
||||
var ref: DatabaseReference!
|
||||
let baseURL = (UIApplication.shared.delegate as! AppDelegate).baseURL
|
||||
let user = (UIApplication.shared.delegate as! AppDelegate).user
|
||||
|
||||
@@ -30,7 +31,7 @@ class DeviceDetailViewController: UITableViewController {
|
||||
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
|
||||
ref = Database.database().reference()
|
||||
let macAddress = String((thisDevice?.macAddress.replacingOccurrences(of: ":", with: "").uppercased().dropLast(4))!)
|
||||
let deviceTypeName = (thisDevice?.parentDeviceType.first?.name)!
|
||||
|
||||
|
||||
@@ -32,7 +32,6 @@ class HistoryGraphViewController: UIViewController, ChartDelegate{
|
||||
func loadChartData() {
|
||||
switch channelDataTypes[(thisChannel?.dataType)!] {
|
||||
case "Float", "Integer":
|
||||
print("Float or Integer")
|
||||
let seriesData : [(x: Int, y: Double)] = channelHistory.map { (chHist) -> (x: Int, y: Double) in
|
||||
(x: Int(chHist.timestamp!.timeIntervalSince1970), y:Double(chHist.value)!)
|
||||
}
|
||||
@@ -41,7 +40,7 @@ class HistoryGraphViewController: UIViewController, ChartDelegate{
|
||||
|
||||
|
||||
default:
|
||||
let alert = UIAlertController(title: "Invalid Type", message: "This channel is of type \(channelDataTypes[(thisChannel?.channelType)!] ?? "ERROR") and cannot be graphed yet.", preferredStyle: .alert)
|
||||
let alert = UIAlertController(title: "Invalid Type", message: "This channel is of type \(channelDataTypes[(thisChannel?.dataType)!] ?? "ERROR") and cannot be graphed yet.", preferredStyle: .alert)
|
||||
let ok = UIAlertAction(title: "OK", style: .cancel, handler: { _ in
|
||||
self.navigationController?.popViewController(animated: true)
|
||||
})
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>1.0</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>2</string>
|
||||
<string>4</string>
|
||||
<key>LSRequiresIPhoneOS</key>
|
||||
<true/>
|
||||
<key>NSLocationUsageDescription</key>
|
||||
|
||||
Reference in New Issue
Block a user