Files
POCloud-iOS/pocloud/Controller/Devices/MaxWaterSystemViewController.swift
2018-07-02 10:33:17 -05:00

139 lines
6.1 KiB
Swift

//
// MaxWaterSystemViewController.swift
// pocloud
//
// Created by Patrick McDonagh on 6/11/18.
// Copyright © 2018 patrickjmcd. All rights reserved.
//
import UIKit
import SVProgressHUD
import PromiseKit
import ChameleonFramework
class MaxWaterSystemViewController: BaseDeviceViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var startButton: UIButton!
@IBOutlet weak var stopButton: UIButton!
@IBOutlet weak var resetFaultsButton: UIButton!
@IBOutlet weak var tableView: UITableView!
let channelLabels : [String] = ["Status", "Fluid Level", "Flow Rate", "Intake Pressure", "Intake Temperature", "Motor Frequency", "Motor Current", "Tubing Pressure"]
let channelNames : [String] = ["wellstatus", "fluidlevel", "flowrate", "intakepressure", "intaketemperature", "vfdfrequency", "vfdcurrent", "tubingpressure"]
let formatter = DateFormatter()
override func viewDidLoad() {
super.viewDidLoad()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
self.title = thisDevice?.vanityName
tableView.delegate = self
tableView.dataSource = self
tableView.estimatedRowHeight = 80
tableView.rowHeight = UITableViewAutomaticDimension
startButton.backgroundColor = UIColor.flatWhiteColorDark()
startButton.setTitleColor(UIColor.flatForestGreen(), for: .normal)
startButton.layer.cornerRadius = 5
startButton.layer.borderWidth = 1
startButton.layer.borderColor = UIColor.flatForestGreen().cgColor
stopButton.backgroundColor = UIColor.flatWhiteColorDark()
stopButton.setTitleColor(UIColor.flatRed(), for: .normal)
stopButton.layer.cornerRadius = 5
stopButton.layer.borderWidth = 1
stopButton.layer.borderColor = UIColor.flatRed().cgColor
resetFaultsButton.backgroundColor = UIColor.flatWhiteColorDark()
resetFaultsButton.setTitleColor(UIColor.flatBlue(), for: .normal)
resetFaultsButton.layer.cornerRadius = 5
resetFaultsButton.layer.borderWidth = 1
resetFaultsButton.layer.borderColor = UIColor.flatBlue().cgColor
}
//MARK: - Table View Methods
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return channelLabels.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "deviceChannelCell", for: indexPath) as! DeviceChannelTableCell
cell.nameLabel.text = channelLabels[indexPath.row]
if let meshVal = values[channelNames[indexPath.row]] {
cell.valueLabel.text = tryRound(value: meshVal.value, places: 3)
cell.timestampLabel.text = formatter.string(from: Date(timeIntervalSince1970: Double(meshVal.timestamp)))
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedChannel = thisDevice?.parentDeviceType.first?.channels[indexPath.row]
performSegue(withIdentifier: "goToChannelView", sender: self)
}
override func updateChartData() {
tableView.reloadData()
}
//MARK: - Control Button Methods
@IBAction func startButtonPressed(_ sender: Any) {
let alert = UIAlertController(title: "Start Well", message: "Are you sure you want to start the well?", preferredStyle: .alert)
let action = UIAlertAction(title: "Start", style: .default) { (action) in
// what will happen once the user clicks the add item button on our UIAlert
firstly {
setChannelValue(deviceId: (self.thisDevice?.id)!, channelName: "writeplctag", value: "{'tag': 'cmd_Start', 'val': 1}", baseURL: self.baseURL, authToken: (self.user?.authToken)!)
}.done { _ in
self.initializeData()
}.catch { error in
print("ERROR IN startButtonPressed: \(error)")
}
}
alert.addAction(action)
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
present(alert, animated: true, completion: nil)
}
@IBAction func resetButtonPressed(_ sender: UIButton) {
let alert = UIAlertController(title: "Reset Well", message: "Are you sure you want to reset the well?", preferredStyle: .alert)
let action = UIAlertAction(title: "Reset", style: .default) { (action) in
// what will happen once the user clicks the add item button on our UIAlert
firstly {
setChannelValue(deviceId: (self.thisDevice?.id)!, channelName: "writeplctag", value: "{'tag': 'cmd_ResetAlarms', 'val': 1}", baseURL: self.baseURL, authToken: (self.user?.authToken)!)
}.done { _ in
self.initializeData()
}.catch { error in
print("ERROR IN resetButtonPressed: \(error)")
}
}
alert.addAction(action)
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
present(alert, animated: true, completion: nil)
}
@IBAction func stopButtonPressed(_ sender: Any) {
let alert = UIAlertController(title: "Stop Well", message: "Are you sure you want to stop the well?", preferredStyle: .alert)
let action = UIAlertAction(title: "Stop", style: .default) { (action) in
// what will happen once the user clicks the add item button on our UIAlert
firstly {
setChannelValue(deviceId: (self.thisDevice?.id)!, channelName: "writeplctag", value: "{'tag': 'cmd_Stop', 'val': 1}", baseURL: self.baseURL, authToken: (self.user?.authToken)!)
}.done { _ in
self.initializeData()
}.catch { error in
print("ERROR IN stopButtonPressed: \(error)")
}
}
alert.addAction(action)
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
present(alert, animated: true, completion: nil)
}
}