From a6721d172ca86118f998f3bbe66fa176b17b0ded Mon Sep 17 00:00:00 2001 From: Patrick McDonagh Date: Fri, 25 May 2018 15:44:13 -0500 Subject: [PATCH] Enable writing channel values --- .../ChannelDetailViewController.swift | 84 ++++++++++++++++++- pocloud/Info.plist | 10 +-- pocloud/View/Base.lproj/Main.storyboard | 73 +++++++--------- 3 files changed, 116 insertions(+), 51 deletions(-) diff --git a/pocloud/Controller/ChannelDetailViewController.swift b/pocloud/Controller/ChannelDetailViewController.swift index 28d3f21..75a551e 100644 --- a/pocloud/Controller/ChannelDetailViewController.swift +++ b/pocloud/Controller/ChannelDetailViewController.swift @@ -19,6 +19,8 @@ class ChannelDetailViewController: UIViewController, UITableViewDelegate, UITabl let formatter = DateFormatter() + + var user : User? var thisDevice : Device? var thisChannel : Channel? @@ -42,8 +44,8 @@ class ChannelDetailViewController: UIViewController, UITableViewDelegate, UITabl @IBOutlet weak var helpDescriptionLabel: UILabel! @IBOutlet weak var meshifyNameLabel: UILabel! @IBOutlet weak var dataTypeLabel: UILabel! - @IBOutlet weak var readWriteLabel: UILabel! @IBOutlet weak var historyTableView: UITableView! + override func viewDidLoad() { super.viewDidLoad() @@ -64,6 +66,9 @@ class ChannelDetailViewController: UIViewController, UITableViewDelegate, UITabl } } + @IBAction func writeButtonPressed(_ sender: UIButton) { + } + //MARK: - TABLE METHODS func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { @@ -88,8 +93,38 @@ class ChannelDetailViewController: UIViewController, UITableViewDelegate, UITabl helpDescriptionLabel.text = thisChannel?.helpExplanation ?? "Unknown" meshifyNameLabel.text = thisChannel?.name ?? "Unknown" dataTypeLabel.text = channelDataTypes[thisChannel?.dataType ?? 0] - readWriteLabel.text = (thisChannel?.io ?? false) ? "Read/Write" : "Read Only" + self.navigationItem.rightBarButtonItem?.isEnabled = (thisChannel?.io)! + } + + + @IBAction func openWriteValuePressed(_ sender: UIBarButtonItem) { + var textField = UITextField() + let alert = UIAlertController(title: "Write Value", message: "What value would you like to write to \(thisChannel!.name)?", preferredStyle: .alert) + let action = UIAlertAction(title: "Write", style: .default) { (action) in + // what will happen once the user clicks the add item button on our UIAlert + SVProgressHUD.show() + firstly { + self.writeChannelValue(value: textField.text!) + }.then{ _ in + self.getChannelHistory() + }.done { _ in + self.historyTableView.reloadData() + SVProgressHUD.dismiss() + }.catch { error in + print("ERROR IN openWriteValue: \(error)") + } + + } + alert.addTextField { (valueTextField) in + valueTextField.placeholder = "123.4567" + textField = valueTextField + } + + alert.addAction(action) + alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil)) + + present(alert, animated: true, completion: nil) } func getChannelHistory() -> Promise { @@ -105,12 +140,14 @@ class ChannelDetailViewController: UIViewController, UITableViewDelegate, UITabl case .success: let channelHistoryJSON : JSON = JSON(response.result.value!) + self.channelHistory.removeAll() for(_, json) : (String, JSON) in channelHistoryJSON { let chHistVal = ChannelHistoryValue() chHistVal.timestamp = Date(timeIntervalSince1970: Double(json["timestamp"].intValue)) chHistVal.value = json["value"].stringValue self.channelHistory.append(chHistVal) } + self.channelHistory.reverse() promise.fulfill(true) case .failure(let error): promise.reject(error) @@ -118,6 +155,49 @@ class ChannelDetailViewController: UIViewController, UITableViewDelegate, UITabl } } } + + func writeChannelValue(value : String) -> Promise { + let timestamp: Int = Int(Date().timeIntervalSince1970) + let channelName = thisChannel?.name + var channelJson : JSON = JSON() + channelJson["name"].string = channelName + channelJson["timestamp"].int = timestamp + channelJson["value"].string = value + + var listJson = JSON() + listJson.arrayObject = [channelJson] + + let url = "\(baseURL)/devices/\(thisDevice!.id)/values" + let headers : HTTPHeaders = [ + "Authorization": user!.authToken + ] + + + + return Promise { promise in + Alamofire.request(url, method: .put, parameters: [:], encoding: listJson.rawString()!, headers: headers) + .response { response in + if response.response?.statusCode == 200 { + promise.fulfill(true) + } else { + promise.reject(String(response.response!.statusCode) as! Error) + } + } + } + + } + + } +extension String: ParameterEncoding { + + public func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest { + var request = try urlRequest.asURLRequest() + request.httpBody = data(using: .utf8, allowLossyConversion: false) + return request + } + +} + diff --git a/pocloud/Info.plist b/pocloud/Info.plist index fba1b0c..91497c0 100644 --- a/pocloud/Info.plist +++ b/pocloud/Info.plist @@ -2,10 +2,6 @@ - NSLocationUsageDescription - Your location is needed to show devices near you. - NSLocationWhenInUseUsageDescription - Your location is needed to show devices near you. CFBundleDevelopmentRegion $(DEVELOPMENT_LANGUAGE) CFBundleExecutable @@ -21,9 +17,13 @@ CFBundleShortVersionString 1.0 CFBundleVersion - 1 + 2 LSRequiresIPhoneOS + NSLocationUsageDescription + Your location is needed to show devices near you. + NSLocationWhenInUseUsageDescription + Your location is needed to show devices near you. UILaunchStoryboardName LaunchScreen UIMainStoryboardFile diff --git a/pocloud/View/Base.lproj/Main.storyboard b/pocloud/View/Base.lproj/Main.storyboard index 2b8c4fe..9bafe0e 100644 --- a/pocloud/View/Base.lproj/Main.storyboard +++ b/pocloud/View/Base.lproj/Main.storyboard @@ -115,7 +115,7 @@ - + @@ -154,13 +154,13 @@ - + - + @@ -245,7 +245,7 @@ - + @@ -305,7 +305,8 @@ - + + @@ -323,7 +324,7 @@ - + @@ -383,7 +384,7 @@ - + - + - + - + - - - - - - - - - - - - - +