78 lines
1.3 KiB
Swift
78 lines
1.3 KiB
Swift
//
|
|
// DeviceProtocol.swift
|
|
// XIONControlPanel
|
|
//
|
|
// Created by James Magahern on 3/13/20.
|
|
// Copyright © 2020 XION. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
enum DeviceState : String, Codable
|
|
{
|
|
case off
|
|
case on
|
|
}
|
|
|
|
enum DeviceType : String, Codable
|
|
{
|
|
case `switch`
|
|
case light
|
|
}
|
|
|
|
protocol Device
|
|
{
|
|
// Read-only properties
|
|
var name: String { get }
|
|
var serial: String { get }
|
|
var type: DeviceType { get }
|
|
|
|
// Writable properties
|
|
var state: DeviceState { get set }
|
|
}
|
|
|
|
extension Device where Self: Hashable
|
|
{
|
|
func hash(into hasher: inout Hasher)
|
|
{
|
|
hasher.combine(self.serial)
|
|
}
|
|
|
|
static func == (lhs: Self, rhs: Self) -> Bool
|
|
{
|
|
return lhs.serial == rhs.serial
|
|
}
|
|
}
|
|
|
|
class AnyDevice : Device
|
|
{
|
|
private var device: Device
|
|
|
|
public var name: String
|
|
{ return device.name }
|
|
|
|
public var serial: String
|
|
{ return device.serial }
|
|
|
|
public var type: DeviceType
|
|
{ return device.type }
|
|
|
|
public var state: DeviceState {
|
|
get { return device.state }
|
|
set { device.state = newValue }
|
|
}
|
|
|
|
init(_ device: Device)
|
|
{
|
|
self.device = device
|
|
}
|
|
}
|
|
|
|
extension AnyDevice : Hashable {}
|
|
|
|
extension Device {
|
|
func eraseToAnyDevice() -> AnyDevice {
|
|
AnyDevice(self)
|
|
}
|
|
}
|