Skip to content

Commit

Permalink
Separated Button functions for toggling garage and checking status. R…
Browse files Browse the repository at this point in the history
…emoved distance restriction on checking status. Maybe add an override to open/close garage if needed.
  • Loading branch information
mvartani76 committed Jun 10, 2018
1 parent e28f5d1 commit 3714aca
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
2 changes: 2 additions & 0 deletions iOS/IoT-Sample/Swift/IoTSampleSwift/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ let PolicyName = "YourPolicyName"
let GarageTOGGLEButton1_GPIO = 17
let GarageTOGGLEButton2_GPIO = 27
let RequestSTATUSButton_GPIO = 22

let HomeDistanceThresh = 200.0
30 changes: 14 additions & 16 deletions iOS/IoT-Sample/Swift/IoTSampleSwift/PublishViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class PublishViewController: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()

let homeLocation: CLLocation = CLLocation(latitude: 42.572215, longitude: -83.488498)
let homeDistanceThresh: Double = 200.0


var timer = Timer()

Expand Down Expand Up @@ -90,28 +90,18 @@ class PublishViewController: UIViewController, CLLocationManagerDelegate {
}

@IBAction func wasGarageTOGGLEButton1Pressed(_ sender: UIButton) {
sendPublishStringCommandWith(buttonState: "TOGGLE", gpioNum: GarageTOGGLEButton1_GPIO, homeDistanceThresh: homeDistanceThresh, indicatorLabel: statusLabel)
sendGarageToggleCommandWith(buttonState: "TOGGLE", gpioNum: GarageTOGGLEButton1_GPIO, homeDistanceThresh: HomeDistanceThresh, indicatorLabel: statusLabel)
}

@IBAction func wasGarageTOGGLEButton2Pressed(_ sender: UIButton) {
sendPublishStringCommandWith(buttonState: "TOGGLE", gpioNum: GarageTOGGLEButton2_GPIO, homeDistanceThresh: homeDistanceThresh, indicatorLabel: statusLabel)
sendGarageToggleCommandWith(buttonState: "TOGGLE", gpioNum: GarageTOGGLEButton2_GPIO, homeDistanceThresh: HomeDistanceThresh, indicatorLabel: statusLabel)
}

@IBAction func wasRequestSTATUSButtonPressed(_ sender: UIButton) {
sendPublishStringCommandWith(buttonState: "REQUEST_STATUS", gpioNum: RequestSTATUSButton_GPIO, homeDistanceThresh: homeDistanceThresh, indicatorLabel: statusLabel)
}


@IBAction func sliderValueChanged(_ sender: UISlider) {
print("\(sender.value)")

let iotDataManager = AWSIoTDataManager.default()
let tabBarViewController = tabBarController as! IoTSampleTabBarController

iotDataManager?.publishString("\(sender.value)", onTopic:tabBarViewController.topic, qoS:.messageDeliveryAttemptedAtMostOnce)
sendGarageStatusCommandWith(buttonState: "REQUEST_STATUS", gpioNum: RequestSTATUSButton_GPIO, indicatorLabel: statusLabel)
}

func sendPublishStringCommandWith(buttonState: String, gpioNum: Int, homeDistanceThresh: Double, indicatorLabel: UILabel) {
func sendGarageToggleCommandWith(buttonState: String, gpioNum: Int, homeDistanceThresh: Double, indicatorLabel: UILabel) {

guard let distanceToHome = locationManager.location?.distance(from: homeLocation) else { return }

Expand All @@ -127,6 +117,14 @@ class PublishViewController: UIViewController, CLLocationManagerDelegate {
timer = Timer.scheduledTimer(timeInterval: 4, target: self, selector: (#selector(clearIndicatorLabel)), userInfo: nil, repeats: false)
}

func sendGarageStatusCommandWith(buttonState: String, gpioNum: Int, indicatorLabel: UILabel) {

let iotDataManager = AWSIoTDataManager.default()
iotDataManager?.publishString("{\"state\":{\"reported\":{\"ON_OFF\":\"\(buttonState)\",\"GPIO\":\(gpioNum)}}}", onTopic:"Garage", qoS:.messageDeliveryAttemptedAtMostOnce)

timer = Timer.scheduledTimer(timeInterval: 4, target: self, selector: (#selector(clearIndicatorLabel)), userInfo: nil, repeats: false)
}

@objc func clearIndicatorLabel() {
statusLabel.text = ""
}
Expand All @@ -137,7 +135,7 @@ class PublishViewController: UIViewController, CLLocationManagerDelegate {
self.locationManager.stopUpdatingLocation()

statusLabel.text = "Distance to Home = \(round(distanceToHome*10)/10) meters"
if distanceToHome > homeDistanceThresh {
if distanceToHome > HomeDistanceThresh {
PanelView.backgroundColor = UIColor(red: CGFloat(255/255.0), green: CGFloat(153/255.0), blue: CGFloat(204/255.0), alpha: CGFloat(1.0))
}
else {
Expand Down

0 comments on commit 3714aca

Please sign in to comment.