Enable writing channel values

This commit is contained in:
Patrick McDonagh
2018-05-25 15:44:13 -05:00
parent 909925a334
commit a6721d172c
3 changed files with 116 additions and 51 deletions

View File

@@ -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<Any> {
@@ -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<Any> {
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
}
}

View File

@@ -2,10 +2,6 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSLocationUsageDescription</key>
<string>Your location is needed to show devices near you.</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Your location is needed to show devices near you.</string>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
@@ -21,9 +17,13 @@
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<string>2</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSLocationUsageDescription</key>
<string>Your location is needed to show devices near you.</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Your location is needed to show devices near you.</string>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>

View File

@@ -115,7 +115,7 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<mapView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" mapType="standard" translatesAutoresizingMaskIntoConstraints="NO" id="Rrc-5h-OVW">
<rect key="frame" x="0.0" y="20" width="375" height="603"/>
<rect key="frame" x="0.0" y="64" width="375" height="559"/>
</mapView>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
@@ -154,13 +154,13 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<mapView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" mapType="standard" translatesAutoresizingMaskIntoConstraints="NO" id="xug-gE-FQz">
<rect key="frame" x="0.0" y="65" width="375" height="200"/>
<rect key="frame" x="0.0" y="109" width="375" height="200"/>
<constraints>
<constraint firstAttribute="height" constant="200" id="tu9-2Y-yHN"/>
</constraints>
</mapView>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="GatewayName" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="izu-lT-xWY">
<rect key="frame" x="8" y="20" width="359" height="45"/>
<rect key="frame" x="8" y="64" width="359" height="45"/>
<constraints>
<constraint firstAttribute="height" constant="45" id="jbO-lj-xVg"/>
</constraints>
@@ -169,7 +169,7 @@
<nil key="highlightedColor"/>
</label>
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="28" sectionFooterHeight="28" translatesAutoresizingMaskIntoConstraints="NO" id="tRH-fh-elp">
<rect key="frame" x="0.0" y="275" width="375" height="348"/>
<rect key="frame" x="0.0" y="319" width="375" height="304"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<prototypes>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" insetsLayoutMarginsFromSafeArea="NO" selectionStyle="blue" accessoryType="disclosureIndicator" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" reuseIdentifier="dataViewCell" rowHeight="60" id="5Sz-TR-mVj" customClass="MapDetailDeviceCell" customModule="pocloud" customModuleProvider="target">
@@ -245,7 +245,7 @@
</objects>
<point key="canvasLocation" x="1984.8" y="126.38680659670166"/>
</scene>
<!--Device Detail View Controller-->
<!--Title-->
<scene sceneID="q7c-Eh-Thg">
<objects>
<tableViewController id="eOb-fR-aFg" customClass="DeviceDetailViewController" customModule="pocloud" customModuleProvider="target" sceneMemberID="viewController">
@@ -305,7 +305,8 @@
</connections>
</tableView>
<toolbarItems/>
<simulatedToolbarMetrics key="simulatedBottomBarMetrics"/>
<navigationItem key="navigationItem" title="Title" id="sUT-g4-ZVO"/>
<nil key="simulatedBottomBarMetrics"/>
<connections>
<segue destination="peV-M2-goO" kind="show" identifier="goToChannelView" id="rTg-Ze-P5c"/>
</connections>
@@ -323,7 +324,7 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Device Name" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Q4o-w1-JdP">
<rect key="frame" x="16" y="20" width="343" height="35"/>
<rect key="frame" x="16" y="64" width="343" height="35"/>
<constraints>
<constraint firstAttribute="height" constant="35" id="dKi-mG-adc"/>
</constraints>
@@ -332,7 +333,7 @@
<nil key="highlightedColor"/>
</label>
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="28" sectionFooterHeight="28" translatesAutoresizingMaskIntoConstraints="NO" id="M2H-Lw-iw3">
<rect key="frame" x="0.0" y="326" width="375" height="297"/>
<rect key="frame" x="0.0" y="330" width="375" height="337"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<prototypes>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" indentationWidth="10" reuseIdentifier="channelHistoryCell" id="Xwn-T9-c0D" customClass="ChannelHistoryCell" customModule="pocloud" customModuleProvider="target">
@@ -383,7 +384,7 @@
</prototypes>
</tableView>
<stackView opaque="NO" contentMode="scaleToFill" distribution="fillProportionally" translatesAutoresizingMaskIntoConstraints="NO" id="UQ2-66-Iuf">
<rect key="frame" x="16" y="63" width="343" height="45"/>
<rect key="frame" x="16" y="107" width="343" height="45"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Name" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="3Kr-ir-6ec">
<rect key="frame" x="0.0" y="0.0" width="147" height="45"/>
@@ -404,7 +405,7 @@
</constraints>
</stackView>
<stackView opaque="NO" contentMode="scaleToFill" distribution="fillProportionally" translatesAutoresizingMaskIntoConstraints="NO" id="hri-QF-EMo">
<rect key="frame" x="16" y="108" width="343" height="45"/>
<rect key="frame" x="16" y="152" width="343" height="45"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Help Description" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="rZ9-3F-zM0">
<rect key="frame" x="0.0" y="0.0" width="147" height="45"/>
@@ -427,7 +428,7 @@
</constraints>
</stackView>
<stackView opaque="NO" contentMode="scaleToFill" distribution="fillProportionally" translatesAutoresizingMaskIntoConstraints="NO" id="Du3-iX-bbt">
<rect key="frame" x="16" y="153" width="343" height="45"/>
<rect key="frame" x="16" y="197" width="343" height="45"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Meshify Name" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="c7H-ve-EJA">
<rect key="frame" x="0.0" y="0.0" width="147" height="45"/>
@@ -449,7 +450,7 @@
<viewLayoutGuide key="safeArea" id="fkq-b0-OQe"/>
</stackView>
<stackView opaque="NO" contentMode="scaleToFill" distribution="fillProportionally" translatesAutoresizingMaskIntoConstraints="NO" id="3v6-ya-lsb">
<rect key="frame" x="16" y="198" width="343" height="45"/>
<rect key="frame" x="16" y="242" width="343" height="45"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Data Type" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="b1W-dX-Rd6">
<rect key="frame" x="0.0" y="0.0" width="147" height="45"/>
@@ -470,37 +471,15 @@
</constraints>
<viewLayoutGuide key="safeArea" id="mEU-NK-S1e"/>
</stackView>
<stackView opaque="NO" contentMode="scaleToFill" distribution="fillProportionally" translatesAutoresizingMaskIntoConstraints="NO" id="FuW-4S-8AG">
<rect key="frame" x="16" y="243" width="343" height="45"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Read/Write" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Sfe-J6-8GG">
<rect key="frame" x="0.0" y="0.0" width="147" height="45"/>
<fontDescription key="fontDescription" name="HelveticaNeue-Light" family="Helvetica Neue" pointSize="18"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Title" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="1Ex-fE-IZY">
<rect key="frame" x="147" y="0.0" width="196" height="45"/>
<fontDescription key="fontDescription" name="HelveticaNeue-Light" family="Helvetica Neue" pointSize="20"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<constraints>
<constraint firstAttribute="height" constant="45" id="heX-yY-Mdb"/>
<constraint firstItem="1Ex-fE-IZY" firstAttribute="width" secondItem="Sfe-J6-8GG" secondAttribute="width" multiplier="4:3" id="uSO-dX-ELj"/>
</constraints>
<viewLayoutGuide key="safeArea" id="HpH-5e-te0"/>
</stackView>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="qKa-BE-rae">
<rect key="frame" x="16" y="292" width="343" height="1"/>
<rect key="frame" x="16" y="295" width="343" height="1"/>
<color key="backgroundColor" white="0.66666666666666663" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstAttribute="height" constant="1" id="pNS-G2-AR8"/>
</constraints>
</view>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Channel History" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="7j2-Cq-PoN">
<rect key="frame" x="8" y="296" width="359" height="30"/>
<rect key="frame" x="8" y="300" width="359" height="30"/>
<constraints>
<constraint firstAttribute="height" constant="30" id="PSh-IF-s9g"/>
</constraints>
@@ -522,26 +501,33 @@
<constraint firstItem="M2H-Lw-iw3" firstAttribute="trailing" secondItem="Eh7-ay-CB3" secondAttribute="trailing" id="Owt-WR-jEW"/>
<constraint firstItem="M2H-Lw-iw3" firstAttribute="top" secondItem="7j2-Cq-PoN" secondAttribute="bottom" id="Wre-RL-KKv"/>
<constraint firstItem="hri-QF-EMo" firstAttribute="top" secondItem="UQ2-66-Iuf" secondAttribute="bottom" id="X8X-hY-04u"/>
<constraint firstItem="Eh7-ay-CB3" firstAttribute="trailing" secondItem="FuW-4S-8AG" secondAttribute="trailing" constant="16" id="XX4-h0-uOA"/>
<constraint firstItem="M2H-Lw-iw3" firstAttribute="leading" secondItem="Eh7-ay-CB3" secondAttribute="leading" id="YRv-Ke-HWg"/>
<constraint firstItem="M2H-Lw-iw3" firstAttribute="bottom" secondItem="Eh7-ay-CB3" secondAttribute="bottom" id="Yfd-NH-7Xf"/>
<constraint firstItem="Eh7-ay-CB3" firstAttribute="trailing" secondItem="hri-QF-EMo" secondAttribute="trailing" constant="16" id="Zon-Z1-wuL"/>
<constraint firstItem="qKa-BE-rae" firstAttribute="top" secondItem="3v6-ya-lsb" secondAttribute="bottom" constant="8" id="ZuF-RB-KyH"/>
<constraint firstItem="qKa-BE-rae" firstAttribute="leading" secondItem="s1w-I3-Ih9" secondAttribute="leadingMargin" id="aHD-lW-E7R"/>
<constraint firstItem="7j2-Cq-PoN" firstAttribute="leading" secondItem="Eh7-ay-CB3" secondAttribute="leading" constant="8" id="gg8-U6-eWJ"/>
<constraint firstItem="Du3-iX-bbt" firstAttribute="top" secondItem="hri-QF-EMo" secondAttribute="bottom" id="iWP-KW-RTS"/>
<constraint firstItem="FuW-4S-8AG" firstAttribute="top" secondItem="3v6-ya-lsb" secondAttribute="bottom" id="k31-NW-IpJ"/>
<constraint firstItem="Du3-iX-bbt" firstAttribute="leading" secondItem="Eh7-ay-CB3" secondAttribute="leading" constant="16" id="kYH-OA-ctU"/>
<constraint firstItem="Eh7-ay-CB3" firstAttribute="trailing" secondItem="3v6-ya-lsb" secondAttribute="trailing" constant="16" id="lZX-ay-clU"/>
<constraint firstItem="7j2-Cq-PoN" firstAttribute="top" secondItem="FuW-4S-8AG" secondAttribute="bottom" constant="8" id="nXo-mX-etp"/>
<constraint firstItem="7j2-Cq-PoN" firstAttribute="top" secondItem="qKa-BE-rae" secondAttribute="bottom" constant="4" id="nXo-mX-etp"/>
<constraint firstItem="Q4o-w1-JdP" firstAttribute="leading" secondItem="Eh7-ay-CB3" secondAttribute="leading" constant="16" id="pB7-gv-T5z"/>
<constraint firstItem="Eh7-ay-CB3" firstAttribute="trailing" secondItem="Q4o-w1-JdP" secondAttribute="trailing" constant="16" id="piC-TD-4GF"/>
<constraint firstItem="qKa-BE-rae" firstAttribute="top" secondItem="FuW-4S-8AG" secondAttribute="bottom" constant="4" id="r8J-5F-qIR"/>
<constraint firstItem="Q4o-w1-JdP" firstAttribute="top" secondItem="Eh7-ay-CB3" secondAttribute="top" id="rDC-Kx-1JX"/>
<constraint firstItem="Eh7-ay-CB3" firstAttribute="trailing" secondItem="Du3-iX-bbt" secondAttribute="trailing" constant="16" id="w0L-Tf-Bfd"/>
<constraint firstItem="FuW-4S-8AG" firstAttribute="leading" secondItem="Eh7-ay-CB3" secondAttribute="leading" constant="16" id="wU6-B3-cc7"/>
</constraints>
<viewLayoutGuide key="safeArea" id="Eh7-ay-CB3"/>
</view>
<toolbarItems/>
<navigationItem key="navigationItem" id="r5B-Wd-npP">
<barButtonItem key="rightBarButtonItem" title="Set Value" id="Q0k-HG-KvU">
<connections>
<action selector="openWriteValuePressed:" destination="peV-M2-goO" id="SWN-W0-Jp5"/>
</connections>
</barButtonItem>
</navigationItem>
<simulatedNavigationBarMetrics key="simulatedTopBarMetrics" prompted="NO"/>
<nil key="simulatedBottomBarMetrics"/>
<connections>
<outlet property="dataTypeLabel" destination="ob5-Lj-Lv3" id="zSJ-p3-2PA"/>
<outlet property="deviceNameLabel" destination="Q4o-w1-JdP" id="ofm-mR-Z5z"/>
@@ -549,7 +535,6 @@
<outlet property="historyTableView" destination="M2H-Lw-iw3" id="g8r-Mi-ZdZ"/>
<outlet property="meshifyNameLabel" destination="8qC-e5-vYy" id="8Or-Th-npX"/>
<outlet property="nameLabel" destination="EN0-bN-bCT" id="X32-Yc-ZLy"/>
<outlet property="readWriteLabel" destination="1Ex-fE-IZY" id="0ba-mG-vv1"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="qLG-JD-OG3" userLabel="First Responder" sceneMemberID="firstResponder"/>
@@ -615,7 +600,7 @@
<image name="POCloud" width="2100" height="512"/>
</resources>
<inferredMetricsTieBreakers>
<segue reference="23K-gs-PXS"/>
<segue reference="4nz-VA-pdT"/>
<segue reference="CI7-x2-asH"/>
<segue reference="wUb-ND-zk7"/>
</inferredMetricsTieBreakers>
</document>