SwiftyHue
使用 Swift 编写的 Philips Hue SDK
正在改进...
安装
Carthage
Carthage 是一个去中心化的依赖关系管理工具,它自动化了将框架添加到您的 Cocoa 应用程序的过程。
要使用 Carthage 将 SwiftyHue 集成到您的 Xcode 项目中,请在您的 Cartfile
中指定它
github "Spriter/SwiftyHue"
CocoaPods
CocoaPods 是 Cocoa 项目的依赖关系管理工具。您可以使用以下命令安装它
$ gem install cocoapods
要使用 CocoaPods 将 SwiftyHue 集成到您的 Xcode 项目中,请在您的 Podfile
中指定它
source 'https://github.com/CocoaPods/Specs.git'
use_frameworks!
pod 'SwiftyHue', '0.4.1'
然后,运行以下命令
$ pod install
示例使用子模块
SwiftyHue/Base: SwiftyHue的核心功能。
SwiftyHue/BridgeServices: 提供用于在网络中查找桥接器和对其进行身份验证的类。
target 'MyApp' do
use_frameworks!
pod ’SwiftyHue’, '0.3.3'
end
target 'MyApp tvOS' do
use_frameworks!
pod ’SwiftyHue’, '0.3.3'
end
target 'MyApp watchOS Extension' do
use_frameworks!
pod ’SwiftyHue/Base’, '0.3.3'
end
注意:您可以使用SwiftyHue/Base子模块在watchOS上使用SwiftyHue。
使用
查找桥接器
第一步是在网络中查找桥接器。
let bridgeFinder = BridgeFinder()
bridgeFinder.delegate = self;
bridgeFinder.start()
实现BridgeFinderDelegate协议以获取搜索结果。
extension BridgeSelectionTableViewController: BridgeFinderDelegate {
func bridgeFinder(finder: BridgeFinder, didFinishWithResult bridges: [HueBridge]) {
let bridges = bridges;
}
}
桥接器身份验证器
在您连接到桥接器之前,需要创建用户。
var bridgeAuthenticator: BridgeAuthenticator! = BridgeAuthenticator(bridge: bridge, uniqueIdentifier: "swiftyhue#\(UIDevice.currentDevice().name)")
实现BridgeAuthenticatorDelegate协议以获取身份验证事件。
extension BridgePushLinkViewController: BridgeAuthenticatorDelegate {
func bridgeAuthenticator(authenticator: BridgeAuthenticator, didFinishAuthentication username: String) {
}
func bridgeAuthenticator(authenticator: BridgeAuthenticator, didFailWithError error: NSError) {
}
// you should now ask the user to press the link button
func bridgeAuthenticatorRequiresLinkButtonPress(authenticator: BridgeAuthenticator) {
}
// user did not press the link button in time, you restart the process and try again
func bridgeAuthenticatorDidTimeout(authenticator: BridgeAuthenticator) {
}
}
心跳
设置心跳以更新资源。
let bridgeAccessConfig= BridgeAccessConfig(bridgeId: "YOUR_BRIDGE_ID", ipAddress: "YOUR_BRIDGE_IP", username: "YOUR_BRIDGE_USERNAME")
let swiftyHue: SwiftyHue = SwiftyHue();
swiftyHue.setBridgeAccessConfig(bridgeAccessConfig)
swiftyHue.setLocalHeartbeatInterval(10, forResourceType: .Lights)
swiftyHue.setLocalHeartbeatInterval(10, forResourceType: .Groups)
swiftyHue.setLocalHeartbeatInterval(10, forResourceType: .Rules)
swiftyHue.setLocalHeartbeatInterval(10, forResourceType: .Scenes)
swiftyHue.setLocalHeartbeatInterval(10, forResourceType: .Schedules)
swiftyHue.setLocalHeartbeatInterval(10, forResourceType: .Sensors)
swiftyHue.setLocalHeartbeatInterval(10, forResourceType: .Config)
swiftyHue.startHeartbeat()
注册更新事件。
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.lightChanged), name: ResourceCacheUpdateNotification.LightsUpdated.rawValue, object: nil)
使用资源缓存。
if let cache = swiftyHue.resourceCache {
// Dict of lights [String: Light]. Keys are the light identifiers.
let lights = cache.lights
}
发送API
您可以使用SendAPI向桥接器发送请求。例如,回忆场景或创建组。
let sendAPI = swiftyHue.brideSendAPI
更多功能即将推出...
资源API
您可以使用资源API从桥请求资源。例如,所有组。
let resourceAPI = swiftyHue.resourceAPI
resourceAPI.fetchGroups { (result) in
guard let groups = result.value else {
//...
return
}
//...
}
生成文档
安装 jazzy
$ [sudo] gem install jazzy
运行生成脚本
$ ./generate_doc.sh
日志
我们使用'日志'进行日志记录。'日志'是一个强大的日志框架,它提供了内置主题和格式化工具,以及一个很好的API来定义您自己的日志。
通过安装
XcodeColors
和KZLinkedConsole
最大限度地发挥Log
的功能。
贡献
我们很高兴看到您为改进这个仓库提出的想法!最好的贡献方式是提交一个拉取请求。我们将竭尽全力尽快回应您的补丁。如果您发现错误,也可以提交一个Issue。
请确保遵循我们的通用编码风格,并为新功能添加测试覆盖率!
需求
您需要Carthage来工作于SwiftyHue项目。如果您已经安装了Carthage,只需在项目根目录下运行
$ carthage bootstrap
以构建SwiftyHue框架的目标,以便运行示例应用程序的目标。