Git Product home page Git Product logo

Comments (2)

kentnz avatar kentnz commented on August 26, 2024 1

Thanks Bouke,

This is what I ended up doing to get this working.

in the .GET for characteristics().swift, after the 'responses.append(response) about line 90

            }
            responses.append(response)

            device.controllerDidGetCharacteristics(channel, forCharacteristic: characteristic)
        }

in 'Device.swift', I found the 'addSubscriber' func about line 400, duplicated the code and renamed both the 'func' and also the 'delegate?.'

 // Add an object which would be notified of changes to Characterisics
func controllerDidGetCharacteristics(_ channel: Channel, forCharacteristic characteristic: Characteristic) {
    subscribersSyncQueue.sync {
        if !characteristicSubscribers.keys.contains(ObjectIdentifier(characteristic)) {
            characteristicSubscribers[ObjectIdentifier(characteristic)] = []
        }
        characteristicSubscribers[ObjectIdentifier(characteristic)]!.insert(ObjectIdentifier(channel))
    }

    if let service = characteristic.service, let accessory = service.accessory {
        delegate?.controllerDidGetCharacteristics(accessory,
                                                     service: service,
                                                     characteristic: AnyCharacteristic(characteristic))
    }
}

in 'DeviceDelegate.swift' I added the following two chunks (near top and about line 80)

public protocol DeviceDelegate: class {
func controllerDidGetCharacteristics(
_ accessory: Accessory,
service: Service,
characteristic: AnyCharacteristic )

public extension DeviceDelegate {
func controllerDidGetCharacteristics(
_ accessory: Accessory,
service: Service,
characteristic: AnyCharacteristic) { }

Finally, back in my 'main.swift' I added the following into the 'DeviceDelegate' class.

class MyDeviceDelegate: DeviceDelegate {
func controllerDidGetCharacteristics(_ accessory: Accessory, service: Service, characteristic: AnyCharacteristic) {
logger.info("Client GET - initiate refresh")
}

This appears to be working - as when the user opens the App, the first thing it does it GET /characteristics which then triggers the DeviceDelegate call, so I can initiate a data refresh.

Kent.

from hap.

Bouke avatar Bouke commented on August 26, 2024

This is related to this: #13. It's not necessarily subscribing, but looking up. Currently there's no callback for this, but it might be worth considering to add this. Mind you; you still need to poll your device for status, as you will still be serving stale data initially. For my home, I just poll the device every 30 seconds, which is often enough to not be considered stale.

You could add a callback to characteristics() endpoint here:

responses.append(response)
.

from hap.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.