Files
Xaibatsu-Control-Panel/XIONControlPanel/Controllers/MainViewController.swift

229 lines
7.2 KiB
Swift
Raw Normal View History

//
// MainViewController.swift
// XIONControlPanel
//
// Created by Charles Magahern on 12/29/15.
// Copyright © 2015 XION. All rights reserved.
//
import UIKit
class MainViewController: UIViewController, SwitchesViewControllerDelegate, ServerMultiplexDelegate
2016-07-24 00:14:04 -07:00
{
fileprivate var _serverMultiplex: ServerMultiplex = ServerMultiplex()
2017-05-13 00:40:03 -07:00
fileprivate var _visualizationController: VisualizationViewController = VisualizationViewController()
fileprivate var _switchesController: SwitchesViewController = SwitchesViewController()
fileprivate var _headerView: HeaderView = HeaderView()
fileprivate var _updateDevices: Bool = false
2017-05-13 00:40:03 -07:00
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?)
{
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
_serverMultiplex.delegate = self
_serverMultiplex.addServer(HubitatServer(URL(string: "https://midna.xionsf.com:6000/apps/api/1/")!))
_serverMultiplex.addServer(WemoServer(URL(string: "http://midna.xionsf.com:5000")!))
}
required init?(coder aDecoder: NSCoder)
{
fatalError("unsupported")
}
// MARK: Overrides
override func viewDidLoad()
{
super.viewDidLoad()
2017-05-13 00:40:03 -07:00
self.view.backgroundColor = UIColor.black
2019-09-24 22:17:16 -07:00
self.addChild(_visualizationController)
2015-12-30 18:47:13 -08:00
self.view.addSubview(_visualizationController.view)
_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 18:47:13 -08:00
self.view.addSubview(_headerView)
2017-05-13 00:40:03 -07:00
_updateConnectivityStatus(.disconnected)
}
override func viewDidLayoutSubviews()
{
super.viewDidLayoutSubviews()
let bounds = self.view.bounds
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,
2019-10-13 15:40:13 -07:00
width: rint(0.5 * 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
2019-10-13 15:40:13 -07:00
if (_visualizationShouldBeVisible()) {
2017-05-13 00:40:03 -07:00
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
}
2017-05-13 00:40:03 -07:00
override func viewDidAppear(_ animated: Bool)
2015-12-30 18:47:13 -08:00
{
super.viewDidAppear(animated)
2019-10-13 15:40:13 -07:00
UIApplication.shared.isIdleTimerDisabled = true
2015-12-31 10:55:54 -08:00
_headerView.xionLogoView.beginAnimating()
if _serverMultiplex.devices.count == 0 {
// Do initial refresh
2017-05-13 00:40:03 -07:00
_updateConnectivityStatus(.connecting)
_serverMultiplex.refreshDevices()
_startUpdatingDevices()
}
2015-12-30 18:47:13 -08:00
}
2019-10-13 15:40:13 -07:00
override func viewDidDisappear(_ animated: Bool)
{
super.viewDidDisappear(animated)
UIApplication.shared.isIdleTimerDisabled = false
}
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 18:47:13 -08:00
return true
}
// MARK: SwitchesViewControllerDelegate
func switchesViewControllerDidToggleDevices(_ controller: SwitchesViewController, devices: [AnyDevice])
{
_updateVisualization(true)
for device in devices {
_serverMultiplex.toggleDeviceState(device, state: device.state, completion: { (error: Error?) -> Void in })
}
}
// MARK: Internal
2019-10-13 15:40:13 -07:00
internal func _visualizationShouldBeVisible() -> Bool
2016-07-24 00:14:04 -07:00
{
2019-10-13 15:40:13 -07:00
switch self.traitCollection.horizontalSizeClass {
case .regular:
return true
case .compact:
return false
default:
return false
2016-07-24 00:14:04 -07:00
}
}
2019-10-13 15:40:13 -07:00
internal func _updateSizeClassPresentation()
{
_visualizationController.view.isHidden = !_visualizationShouldBeVisible()
}
2017-05-13 00:40:03 -07:00
internal func _updateVisualization(_ animated: Bool)
{
let activatedDevicesCount = _serverMultiplex.devices.filter { $0.state == .on }.count
let totalDeviceCount = _serverMultiplex.devices.count
let percentageActivated = (totalDeviceCount > 0 ? Float(activatedDevicesCount) / Float(totalDeviceCount) : 0.0)
2015-12-31 18:20:50 -08:00
if (percentageActivated != _visualizationController.percentActivated) {
_visualizationController.setPercentActivated(percentageActivated, animated: animated)
}
}
2017-05-13 00:40:03 -07:00
internal func _updateConnectivityStatus(_ status: ConnectionStatus)
{
2017-05-13 00:40:03 -07:00
DispatchQueue.main.async { () -> Void in
self._headerView.connectionStatusView.connectivityStatus = status
2016-07-24 00:14:04 -07:00
self._headerView.setNeedsLayout()
self._visualizationController.connectionStatus = status
}
}
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
if (self._updateDevices) {
self._serverMultiplex.refreshDevices()
self._startUpdatingDevices()
}
}
}
internal func _stopUpdatingDevices()
{
_updateDevices = false
}
// MARK: Server Multiplex Delegate
func serverMultiplex(_ multiplex: ServerMultiplex, didAddDevices devices: [AnyDevice])
{
_switchesController.devices = Array(multiplex.devices)
_updateVisualization(false)
// Update connection status too, if applicable
if _serverMultiplex.numServers == _serverMultiplex.numberOfConnectedServers() {
_updateConnectivityStatus(.connected)
}
}
func serverMultiplex(_ multiplex: ServerMultiplex, devicesStateChanged devices: [AnyDevice])
{
_switchesController.devicesStateChanged(devices)
}
func serverMultiplex(_ multiplex: ServerMultiplex, didEncounterError error: Error)
{
_updateConnectivityStatus(.error)
2020-05-01 00:16:10 -07:00
var stderr = StandardErrorOutputStream()
print(error.localizedDescription, to: &stderr)
}
}