2015-12-30 02:51:22 -08:00
|
|
|
//
|
|
|
|
|
// MainViewController.swift
|
|
|
|
|
// XIONControlPanel
|
|
|
|
|
//
|
|
|
|
|
// Created by Charles Magahern on 12/29/15.
|
|
|
|
|
// Copyright © 2015 XION. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
|
2016-07-24 00:14:04 -07:00
|
|
|
class MainViewController: UIViewController, SwitchesViewControllerDelegate
|
|
|
|
|
{
|
2017-05-13 00:40:03 -07:00
|
|
|
fileprivate var _server: WemoServer
|
|
|
|
|
fileprivate var _visualizationController: VisualizationViewController = VisualizationViewController()
|
|
|
|
|
fileprivate var _switchesController: SwitchesViewController = SwitchesViewController()
|
|
|
|
|
fileprivate var _headerView: HeaderView = HeaderView()
|
|
|
|
|
fileprivate var _updateDevices: Bool = false
|
2015-12-30 02:51:22 -08:00
|
|
|
|
2017-05-13 00:40:03 -07:00
|
|
|
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?)
|
2015-12-31 19:51:51 -08:00
|
|
|
{
|
2017-05-13 00:40:03 -07:00
|
|
|
let url = URL(string: "http://midna.xionsf.com:5000")
|
2015-12-31 19:51:51 -08:00
|
|
|
_server = WemoServer(url!)
|
|
|
|
|
|
|
|
|
|
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
required init?(coder aDecoder: NSCoder)
|
|
|
|
|
{
|
|
|
|
|
fatalError("unsupported")
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-30 16:56:15 -08:00
|
|
|
// MARK: Overrides
|
|
|
|
|
|
2015-12-30 02:51:22 -08:00
|
|
|
override func viewDidLoad()
|
|
|
|
|
{
|
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
|
2017-05-13 00:40:03 -07:00
|
|
|
self.view.backgroundColor = UIColor.black
|
2015-12-30 02:51:22 -08:00
|
|
|
|
2019-09-24 22:17:16 -07:00
|
|
|
self.addChild(_visualizationController)
|
2015-12-30 18:47:13 -08:00
|
|
|
self.view.addSubview(_visualizationController.view)
|
2015-12-30 16:56:15 -08:00
|
|
|
|
2015-12-31 17:30:23 -08:00
|
|
|
_switchesController.delegate = self
|
2019-09-24 22:17:16 -07:00
|
|
|
self.addChild(_switchesController)
|
2015-12-31 15:41:32 -08:00
|
|
|
self.view.addSubview(_switchesController.view)
|
2015-12-30 16:56:15 -08:00
|
|
|
|
2015-12-30 18:47:13 -08:00
|
|
|
self.view.addSubview(_headerView)
|
2015-12-31 17:30:23 -08:00
|
|
|
|
2017-05-13 00:40:03 -07:00
|
|
|
_updateConnectivityStatus(.disconnected)
|
2015-12-30 02:51:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func viewDidLayoutSubviews()
|
|
|
|
|
{
|
|
|
|
|
super.viewDidLayoutSubviews()
|
|
|
|
|
|
|
|
|
|
let bounds = self.view.bounds
|
2016-07-24 00:14:04 -07:00
|
|
|
let horizontalSizeClass = self.traitCollection.horizontalSizeClass
|
|
|
|
|
|
2015-12-30 18:47:13 -08:00
|
|
|
let headerBounds = CGRect(
|
|
|
|
|
x: 0.0,
|
|
|
|
|
y: 0.0,
|
|
|
|
|
width: bounds.size.width,
|
2016-07-24 00:14:04 -07:00
|
|
|
height: rint(bounds.size.height / 8.0)
|
2015-12-30 18:47:13 -08:00
|
|
|
)
|
|
|
|
|
let bodyBounds = CGRect(
|
|
|
|
|
x: 0.0,
|
2017-05-13 00:40:03 -07:00
|
|
|
y: headerBounds.maxY,
|
2015-12-30 18:47:13 -08:00
|
|
|
width: bounds.size.width,
|
|
|
|
|
height: bounds.size.height - headerBounds.size.height
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
_headerView.frame = headerBounds
|
|
|
|
|
|
|
|
|
|
let visualizationFrame = CGRect(
|
|
|
|
|
x: bodyBounds.origin.x,
|
|
|
|
|
y: bodyBounds.origin.y,
|
2016-01-01 00:30:13 -08:00
|
|
|
width: rint(0.55 * bodyBounds.size.width),
|
2015-12-30 18:47:13 -08:00
|
|
|
height: bodyBounds.size.height
|
|
|
|
|
)
|
|
|
|
|
_visualizationController.view.frame = visualizationFrame
|
|
|
|
|
|
2016-07-24 00:14:04 -07:00
|
|
|
var switchesOriginX: CGFloat = 0.0
|
|
|
|
|
var switchesWidth: CGFloat = 0.0
|
2017-05-13 00:40:03 -07:00
|
|
|
if (horizontalSizeClass == .regular) {
|
|
|
|
|
switchesOriginX = visualizationFrame.maxX
|
2016-07-24 00:14:04 -07:00
|
|
|
switchesWidth = bodyBounds.size.width - visualizationFrame.size.width
|
|
|
|
|
} else {
|
|
|
|
|
switchesOriginX = 0.0
|
|
|
|
|
switchesWidth = bodyBounds.size.width
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-31 15:41:32 -08:00
|
|
|
let switchesControllerFrame = CGRect(
|
2016-07-24 00:14:04 -07:00
|
|
|
x: switchesOriginX,
|
2015-12-30 18:47:13 -08:00
|
|
|
y: bodyBounds.origin.y,
|
2016-07-24 00:14:04 -07:00
|
|
|
width: switchesWidth,
|
2015-12-30 18:47:13 -08:00
|
|
|
height: bodyBounds.size.height
|
|
|
|
|
)
|
2015-12-31 15:41:32 -08:00
|
|
|
_switchesController.view.frame = switchesControllerFrame
|
2015-12-30 02:51:22 -08:00
|
|
|
}
|
2015-12-30 16:56:15 -08:00
|
|
|
|
2017-05-13 00:40:03 -07:00
|
|
|
override func viewDidAppear(_ animated: Bool)
|
2015-12-30 18:47:13 -08:00
|
|
|
{
|
2016-01-03 13:21:50 -08:00
|
|
|
super.viewDidAppear(animated)
|
|
|
|
|
|
2015-12-31 10:55:54 -08:00
|
|
|
_headerView.xionLogoView.beginAnimating()
|
2015-12-31 19:51:51 -08:00
|
|
|
|
2016-01-01 01:16:21 -08:00
|
|
|
if (!_server.connected) {
|
2017-05-13 00:40:03 -07:00
|
|
|
_updateConnectivityStatus(.connecting)
|
|
|
|
|
_server.connect { (error: Error?) -> Void in
|
2016-01-01 01:16:21 -08:00
|
|
|
if (error == nil) {
|
|
|
|
|
self._reloadDevices()
|
|
|
|
|
self._startUpdatingDevices()
|
|
|
|
|
} else {
|
2017-05-13 00:40:03 -07:00
|
|
|
self._updateConnectivityStatus(.error)
|
2016-01-01 01:16:21 -08:00
|
|
|
}
|
2015-12-31 19:51:51 -08:00
|
|
|
}
|
|
|
|
|
}
|
2015-12-30 18:47:13 -08:00
|
|
|
}
|
2015-12-30 16:56:15 -08:00
|
|
|
|
2017-05-13 00:40:03 -07:00
|
|
|
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator)
|
2016-07-24 00:14:04 -07:00
|
|
|
{
|
2017-05-13 00:40:03 -07:00
|
|
|
super.viewWillTransition(to: size, with: coordinator)
|
2016-07-24 00:14:04 -07:00
|
|
|
_updateSizeClassPresentation()
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-13 00:40:03 -07:00
|
|
|
override var prefersStatusBarHidden : Bool
|
2015-12-30 16:56:15 -08:00
|
|
|
{
|
2015-12-30 18:47:13 -08:00
|
|
|
return true
|
2015-12-30 16:56:15 -08:00
|
|
|
}
|
2015-12-31 17:30:23 -08:00
|
|
|
|
|
|
|
|
// MARK: API
|
|
|
|
|
|
|
|
|
|
var devices: [WemoDevice] = []
|
|
|
|
|
{
|
|
|
|
|
didSet
|
|
|
|
|
{
|
|
|
|
|
_switchesController.devices = devices
|
|
|
|
|
_updateVisualization(false)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: SwitchesViewControllerDelegate
|
|
|
|
|
|
2017-05-13 00:40:03 -07:00
|
|
|
func switchesViewControllerDidToggleDevices(_ controller: SwitchesViewController, devices: [WemoDevice])
|
2015-12-31 17:30:23 -08:00
|
|
|
{
|
|
|
|
|
_updateVisualization(true)
|
2015-12-31 19:51:51 -08:00
|
|
|
|
|
|
|
|
for device in devices {
|
2017-05-13 00:40:03 -07:00
|
|
|
_server.toggleDevice(device, state: device.state, completion: { (error: Error?) -> Void in })
|
2015-12-31 19:51:51 -08:00
|
|
|
}
|
2015-12-31 17:30:23 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: Internal
|
|
|
|
|
|
2016-07-24 00:14:04 -07:00
|
|
|
internal func _updateSizeClassPresentation()
|
|
|
|
|
{
|
|
|
|
|
let horizontalSizeClass = self.traitCollection.horizontalSizeClass
|
2017-05-13 00:40:03 -07:00
|
|
|
if (horizontalSizeClass == .regular) {
|
|
|
|
|
_visualizationController.view.isHidden = false
|
2016-07-24 00:14:04 -07:00
|
|
|
} else {
|
2017-05-13 00:40:03 -07:00
|
|
|
_visualizationController.view.isHidden = true
|
2016-07-24 00:14:04 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-13 00:40:03 -07:00
|
|
|
internal func _updateVisualization(_ animated: Bool)
|
2015-12-31 17:30:23 -08:00
|
|
|
{
|
|
|
|
|
var activatedDevicesCount = 0
|
|
|
|
|
for device in self.devices {
|
2017-05-13 00:40:03 -07:00
|
|
|
if (device.state == .on) {
|
2016-07-06 22:11:48 -07:00
|
|
|
activatedDevicesCount += 1
|
2015-12-31 17:30:23 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-31 23:34:10 -08:00
|
|
|
let percentageActivated = (self.devices.count > 0 ? Float(activatedDevicesCount) / Float(self.devices.count) : 0.0)
|
2015-12-31 18:20:50 -08:00
|
|
|
if (percentageActivated != _visualizationController.percentActivated) {
|
|
|
|
|
_visualizationController.setPercentActivated(percentageActivated, animated: animated)
|
|
|
|
|
}
|
2015-12-31 17:30:23 -08:00
|
|
|
}
|
2015-12-31 19:51:51 -08:00
|
|
|
|
2017-05-13 00:40:03 -07:00
|
|
|
internal func _updateConnectivityStatus(_ status: ConnectionStatus)
|
2015-12-31 19:51:51 -08:00
|
|
|
{
|
2017-05-13 00:40:03 -07:00
|
|
|
DispatchQueue.main.async { () -> Void in
|
2015-12-31 19:51:51 -08:00
|
|
|
self._headerView.connectionStatusView.connectivityStatus = status
|
2016-07-24 00:14:04 -07:00
|
|
|
self._headerView.setNeedsLayout()
|
2016-01-01 00:30:13 -08:00
|
|
|
self._visualizationController.connectionStatus = status
|
2015-12-31 19:51:51 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal func _reloadDevices()
|
|
|
|
|
{
|
2017-05-13 00:40:03 -07:00
|
|
|
_server.fetchDevices({ (devices: [WemoDevice], error: Error?) -> Void in
|
|
|
|
|
DispatchQueue.main.async { () -> Void in
|
2015-12-31 19:51:51 -08:00
|
|
|
if (error == nil) {
|
|
|
|
|
self.devices = devices
|
2017-05-13 00:40:03 -07:00
|
|
|
self._updateConnectivityStatus(.connected)
|
2015-12-31 19:51:51 -08:00
|
|
|
} else {
|
|
|
|
|
self.devices = []
|
2017-05-13 00:40:03 -07:00
|
|
|
self._updateConnectivityStatus(.error)
|
2015-12-31 19:51:51 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
2015-12-31 23:29:48 -08:00
|
|
|
|
|
|
|
|
internal func _startUpdatingDevices()
|
|
|
|
|
{
|
|
|
|
|
_updateDevices = true
|
|
|
|
|
|
2017-05-13 00:40:03 -07:00
|
|
|
let interval = DispatchTime.now() + Double(Int64(10 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)
|
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: interval) { () -> Void in
|
2015-12-31 23:29:48 -08:00
|
|
|
if (self._updateDevices) {
|
|
|
|
|
self._reloadDevices()
|
|
|
|
|
self._startUpdatingDevices()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal func _stopUpdatingDevices()
|
|
|
|
|
{
|
|
|
|
|
_updateDevices = false
|
|
|
|
|
}
|
2015-12-30 02:51:22 -08:00
|
|
|
}
|