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
|
|
|
|
|
|
2020-03-14 18:33:02 -07:00
|
|
|
class MainViewController: UIViewController, SwitchesViewControllerDelegate, ServerMultiplexDelegate
|
2016-07-24 00:14:04 -07:00
|
|
|
{
|
2020-03-14 18:33:02 -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
|
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
|
|
|
{
|
|
|
|
|
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
|
2020-03-14 18:33:02 -07:00
|
|
|
|
|
|
|
|
_serverMultiplex.delegate = self
|
2025-06-19 01:02:51 -07:00
|
|
|
_serverMultiplex.addServer(HomeAssistantServer(URL(string: "https://ha.xaibatsu.com/api")!))
|
|
|
|
|
// _serverMultiplex.addServer(HubitatServer(URL(string: "https://midna.xionsf.com:6000/apps/api/1/")!))
|
|
|
|
|
// _serverMultiplex.addServer(WemoServer(URL(string: "http://midna.xionsf.com:5000")!))
|
2015-12-31 19:51:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
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
|
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)
|
|
|
|
|
|
2019-10-13 15:40:13 -07:00
|
|
|
UIApplication.shared.isIdleTimerDisabled = true
|
|
|
|
|
|
2015-12-31 10:55:54 -08:00
|
|
|
_headerView.xionLogoView.beginAnimating()
|
2015-12-31 19:51:51 -08:00
|
|
|
|
2020-03-14 18:33:02 -07:00
|
|
|
if _serverMultiplex.devices.count == 0 {
|
|
|
|
|
// Do initial refresh
|
2017-05-13 00:40:03 -07:00
|
|
|
_updateConnectivityStatus(.connecting)
|
2020-03-14 18:33:02 -07:00
|
|
|
_serverMultiplex.refreshDevices()
|
|
|
|
|
_startUpdatingDevices()
|
2015-12-31 19:51:51 -08:00
|
|
|
}
|
2015-12-30 18:47:13 -08:00
|
|
|
}
|
2015-12-30 16:56:15 -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 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: SwitchesViewControllerDelegate
|
|
|
|
|
|
2020-03-14 18:33:02 -07:00
|
|
|
func switchesViewControllerDidToggleDevices(_ controller: SwitchesViewController, devices: [AnyDevice])
|
2015-12-31 17:30:23 -08:00
|
|
|
{
|
|
|
|
|
_updateVisualization(true)
|
2015-12-31 19:51:51 -08:00
|
|
|
|
|
|
|
|
for device in devices {
|
2020-03-14 18:33:02 -07:00
|
|
|
_serverMultiplex.toggleDeviceState(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
|
|
|
|
|
|
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)
|
2015-12-31 17:30:23 -08:00
|
|
|
{
|
2020-03-14 18:33:02 -07:00
|
|
|
let activatedDevicesCount = _serverMultiplex.devices.filter { $0.state == .on }.count
|
|
|
|
|
let totalDeviceCount = _serverMultiplex.devices.count
|
2015-12-31 17:30:23 -08:00
|
|
|
|
2020-03-14 18:33:02 -07:00
|
|
|
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)
|
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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) {
|
2020-03-14 18:33:02 -07:00
|
|
|
self._serverMultiplex.refreshDevices()
|
2015-12-31 23:29:48 -08:00
|
|
|
self._startUpdatingDevices()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal func _stopUpdatingDevices()
|
|
|
|
|
{
|
|
|
|
|
_updateDevices = false
|
|
|
|
|
}
|
2020-03-14 18:33:02 -07:00
|
|
|
|
|
|
|
|
// MARK: Server Multiplex Delegate
|
|
|
|
|
|
|
|
|
|
func serverMultiplex(_ multiplex: ServerMultiplex, didAddDevices devices: [AnyDevice])
|
|
|
|
|
{
|
2020-03-14 19:53:18 -07:00
|
|
|
_switchesController.devices = Array(multiplex.devices)
|
2020-03-14 18:33:02 -07:00
|
|
|
_updateVisualization(false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func serverMultiplex(_ multiplex: ServerMultiplex, devicesStateChanged devices: [AnyDevice])
|
|
|
|
|
{
|
|
|
|
|
_switchesController.devicesStateChanged(devices)
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-04 19:43:50 -07:00
|
|
|
func serverMultiplex(_ multiplex: ServerMultiplex, didReceiveAcknowledgementFromServer server: Server)
|
|
|
|
|
{
|
|
|
|
|
// Update connection status too, if applicable
|
|
|
|
|
let groupConnectionStatus = multiplex.groupConnectionStatus()
|
|
|
|
|
_updateConnectivityStatus(groupConnectionStatus)
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-14 18:33:02 -07:00
|
|
|
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)
|
2020-03-14 18:33:02 -07:00
|
|
|
}
|
2015-12-30 02:51:22 -08:00
|
|
|
}
|