Back to Parent

import UIKit

class ViewController: UIViewController {

    
    @IBOutlet weak var isControlled: UILabel!
    var myPhoton : SparkDevice?
    var toggleString = "on"
    var lightOn = false

    @IBOutlet weak var isOnLabel: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
        
        isControlled.text = "Not User Controlled"
        SparkCloud.sharedInstance().login(withUser: "tjchambe@andrew.cmu.edu", password: "kmk123") { (error:Error?) -> Void in
            if let _ = error {
                print("Wrong credentials or no internet connectivity, please try again")
            }
            else {
                print("Logged in")
                SparkCloud.sharedInstance().getDevices { (sparkDevices: [Any]?, error: Error?) -> Void in
                    if error != nil {
                        print("Check your internet connectivity")
                    }
                    else {
                        if let devices = sparkDevices as? [SparkDevice] {
                            for device in devices {
                                if device.name == "tcham" {
                                    self.myPhoton = device
                                    self.myPhoton!.callFunction("connected", withArguments: ["on"], completion: { (resultCode : NSNumber?, error : Error?) -> Void in
                                        if (error == nil) {
                                            self.isOn()
                                            SparkCloud.sharedInstance().subscribeToAllEvents(withPrefix: "test-", handler: self.subscribeHandler)
                                        }
                                    })
                                }
                            }
                        }
                    }
                }
            }
        }
        
    }
    
    @IBAction func switchFlipped(_ sender: UISwitch) {
        SparkCloud.sharedInstance().publishEvent(withName: "switch", data: String(sender.isOn), isPrivate: false, ttl: 60, completion: { (error:Error?) -> Void in
            if error != nil
            {
                print("Error publishing event \(error?.localizedDescription)")
                self.isControlled.text = "Currently User Controlled"
            } else {
                print("Error \(error?.localizedDescription)")
            }
        })
    }
    
    
    @IBAction func sliderChanged(_ sender: UISlider) {
        SparkCloud.sharedInstance().publishEvent(withName: "slider", data: String(sender.value), isPrivate: false, ttl: 60, completion: { (error:Error?) -> Void in
            if error != nil
            {
                print("Error publishing event \(error?.localizedDescription)")
            }
        })
    }
    
    
    func subscribeHandler(event: SparkEvent?, error: Error?)->() {
        if (error == nil)
        {
            DispatchQueue.main.async(execute: {
                print("Got: \(event?.event) and data: \(event?.data)")
            })
            
        } else {
            NSLog("Error occured:\(error?.localizedDescription)")
        }
    }

    func isOn() {
        self.myPhoton?.getVariable("isOn", completion: {(_ result: Any?, _ error: Error?) -> Void in
            
            if(error == nil) {
                self.lightOn = (result! as! Int == 1) ? true : false
                print(self.lightOn)
                self.toggleString = (self.lightOn) ? "on" : "off"
                self.isOnLabel.text = (self.lightOn) ? "Light is On" : "Light is Off"
            } else {
                print(error)
            }
        })
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func toggle(_ sender: Any) {
        if(toggleString == "on") {
            toggleString = "off"
        } else {
            toggleString = "on"
        }
        let funcArgs = [toggleString]
        let task = myPhoton!.callFunction("led", withArguments: funcArgs) { (resultCode : NSNumber?, error : Error?) -> Void in
            if (error == nil) {
                print("LED on D7 successfully turned on")
            }
        }
        //        _; : Int64 = task.countOfBytesExpectedToReceive
    }

}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0