-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Observe Characteristic changes and HAP getValues #55
base: master
Are you sure you want to change the base?
Conversation
Interesting idea; I haven't thought of this use-case. I think we should not try to combine this in the existing
The Regarding the API renaming, in your proposed |
Interesting thought on
Good idea on the renaming, I have refactored |
It may even make sense to call onGetValue() asynchronously, to ensure that it doesn't block the read. |
Ah yes, slightly different use-case. Instead of binding to the GET event, I think a nicer approach would be to hook into the characteristic listeners. So we would have the following two events for those interested:
In your application, you would hook into these events; you could periodically pull the latest data from the actual devices you're bridging and update the characteristics asynchronously. I'm thinking that the API for this should be added to the Device, instead of doing this all on the characteristic. Maybe we might even need to introduce a |
I considered concentrating the callback for getValue at Accessory level and at Device level, and using delegates, but I found that it was much less elegant. When dealing with more than a handful of characteristics and accessories, it would necessitate a big It is much more simple and elegant for an
This pattern is also very similar to A DeviceObserver could be an alternative to Device.onCharacteristicChange(), permitting a host app to monitor changes and update its UI or data structures accordingly, however the implementation of accessories needs an I'm thinking I should rename it to onDidGetValue() and calling asynchronously it on the |
I've added a |
I've moved things around a little in |
The linux travis builds were failing, as version 1.0.12 of libsodium is hardcoded in the |
.travis.yml
Outdated
- cd libsodium-1.0.12 | ||
- wget https://download.libsodium.org/libsodium/releases/libsodium-1.0.16.tar.gz | ||
- tar xzf libsodium-1.0.16.tar.gz | ||
- cd libsodium-1.0.16 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally we don't compile the library each time, but use a packaged version. However libsodium is not packaged for Ubuntu 14.04, and at the time a third-party repository included an older version that we couldn't use. So while this is ok for now, hopefully Travis provides newer Ubuntu's in the future and we can switch to a packaged version instead.
I've pushed a new I haven't gotten to changing the subscribe / unsubscribe events like I've explained above here, as it probably also includes the removal of Please let me know how this change works for you. I know it's not quite what you had in mind, but I still think you can create a structure similarly to the callbacks by registering those with your own delegate. |
Can't say this is my preference. It solves the problem of the app monitoring the HAP server, and I have updated my other PR to use your DeviceDelegate accordingly. However it doesn't help for accessory implementation. I have an idea how to address this, and will revise this branch for discussion. |
c7c361b
to
19a762d
Compare
I've pulled out the changes which are now covered by |
19a762d
to
bd23e11
Compare
What do you mean by this, what use-case isn't covered? |
Here is a pretty lame diagram trying to explain what I mean:
Instead of the closure hooks in my original implementation, I'm experimenting now with an I've not explained this well, but I hope you get a clearer picture of what I mean. I'm still testing a few different use cases, so I'm not ready for review yet. |
OK I've tested this in a couple of different use cases, and am fairly happy this is now ready for review. Essentially what it provides is an ability for an Accessory implementation e.g "MySpecialLamp" to be notified after certain characteristics were queried or set by HAP (such as lamp brightness), and to act accordingly. This is provided by an AccessoryDelegate protocol, in much the same way as we now have for DeviceDelegate (which is used by the calling app, rather than the underlying accessory implementations). Also added a function to DeviceDelegate to notify the app of changes to the accessory list, and fixed another race condition in Server, as the Evergreen.Logger framework doesn't seem to be thread safe. |
I have added an example accessory which uses the AccessoryDelegate protocol to provide a Temperature sensor and Humidity sensor fed by OpenWeatherMap. In main.swift, add your own The branch has been rebased to the latest commit on master. The code checks fail because travis cannot find sonar-scanner. |
If you wish, I thought it would be helpful to a developer to include some example code in the hap-server target. This code is not intended to be included in the core HAP framework itself. |
7cb7c55
to
d7f7c10
Compare
I realise I had inadvertently left some changes in this PR which are now covered by PR #62 and PR #64 Perhaps it would be helpful for me to summarise the changes to master that are proposed in this PR as they have been a number of evolutions since the initial posts. This PR adds an
The first method is called whenever HomeKit sets the value of a characteristic. The second method is called whenever HomeKit gets the value of a characteristic. This simplifies the implementation of an Accessory subclass, by using the delegate function in order to catch changes made by HomeKit, or to lazily update its state only when HomeKit is interested in it. In order to implement the above, the internal function Example An example of using the
In order to function, |
As this PR has a lot of changes along the way, would you prefer I open a fresh PR with a single commit ? |
No need, if/when I merge this PR I can just squash-merge it. |
5ab417c
to
594d9d3
Compare
I've rebased this PR on the current master, so is now compatible with the nio network stack |
Removed the OpenWeather class, so hopefully this PR can now be merged into master. |
I'm considering to add additional targets including demos and other examples. Maybe we can include the openweather as such. E.g.
Sorry, no progress in this area yet. I'd like to land on some API that feels solid to call v1 of this library. |
Sure, happy to add it back to a contrib section at some point.
Why can't this be part of the v1 API ? The |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should add libsodium.23.dylib?
func getValue(fromChannel channel: Channel?) -> JSONValueType? { | ||
let currentValue = _value | ||
DispatchQueue.main.async { [weak self] in | ||
if let this = self, let service = this.service { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
preferred guard let self = self, let service = self.service else { return }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed
Sources/HAP/Base/Accessory.swift
Outdated
/// For example, you might want to update the value of certain characteristics | ||
/// if the HAP controller is showing interest or makes a change. | ||
/// | ||
public protocol AccessoryDelegate: class { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
preferred use AnyObject
instead class
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed
… Accessory implementations Separate functions to obtain the value of a characteristic for a HAP server and to just get a jason formatted value. Notify device delegate when an accessory changes it's value
b4d9bb1
to
3f5a154
Compare
…ass. Add identification notification to accessory delegate
I have rebased this on the latest master, fixed the couple of comments mentioned above, and added one more function to the accessory delegate for accessory identification. Can we merge this into master now ? |
This PR provides hooks for an app using HAP to
See
GenericCharacteristic.onGetValue
See
Device.onCharacteristicChange
It also enforces Characteristic read/writes to occur on the main thread using a precondition and renames
Device.notify(characteristicListeners: Characteristic, ..)
to the clearerDevice.notifyChangeOf(characteristic: Characteristic, ..)
as the first parameter is the Characteristic itself, not a listener.