0

I am trying to connect with my smart lamp by LIFXClient framework, but unfortunately I do not understand how to use this library. The library looks very simple, here is link.

I'm trying changing color by this function:

LIFXClient.connect(host: .ipv4(IPv4Address("192.168.1.4")!)).then { client in
    return client.light.setColor(color: .orange)
}

But this creating new connection with different local port every time I use it. Because of lack of free ports after changing colors quickly I can not make a new connection with lamp.

I have been trying a few days not to create a new connection, but to use just one, unfortunately I do not know how to do this.

The function from the LIFXClient to connect to lamp:

public class LIFXClient: LIFXConnection {
    public class func connect(host: NWEndpoint.Host, port: NWEndpoint.Port = 56700, queue: DispatchQueue = DispatchQueue(label: "LIFX Queue"), source: UInt32 = UInt32.random(), target: UInt64 = 0) -> Promise<LIFXClient> {
        return NWConnection(host: host, port: port, using: .udp).connect(queue: queue).map { connection in
            return LIFXClient(connection: connection, source: source, target: target)
        }
    }
}

If I only could save client.light reference it would be great, but unfortunately I can't do that:

var myClient: LIFXClient

LIFXClient.connect(host: .ipv4(IPv4Address("192.168.1.4")!)).then { client in
    myClient = client.light
    return client.light.setColor(color: .orange)
}

Developer of this framework does not write back to me, and I really have no ideas how to solve it. Thank you very much for all your help.

Madrox24
  • 31
  • 1
  • 5
  • `LIFXClient` is a subclass of `LIFXConnection` - So, once you have received the client in the closure, store a reference to it in a property and you can perform other operations on it. You can't `return` the client from the closure because it is asynchronous – Paulw11 Apr 18 '19 at 22:31
  • When I'm using last code (myClient = client.light) from my main post, I'm getting "Unable to infer complex closure return type; add explicit type to disambiguate". I don't quite know what other way I can do it. – Madrox24 Apr 18 '19 at 23:37
  • You can't return a value from the closure. You have to do all of your work in the closure (or in a function you call from the closure) because the connection operation is asynchronous – Paulw11 Apr 18 '19 at 23:39
  • I am sorry, but I do not fully understand about which return you write. This first code from the post works, it changes the color of the lamp. It's the code given by the developer. I have to modify function from framework (public class func connect()) or LIFXClient.connect() from my code? Could you give me an example of where I should call the function for saving client.light reference? I got a little lost. – Madrox24 Apr 18 '19 at 23:54
  • Delete the `return` from the code they have given and just continue to execute commands on `client` - Or you can append another promise clause (`.then`) to use the client. – Paulw11 Apr 18 '19 at 23:59
  • After deleting return .then { client in myGlobalLampClient = client } gives me error - "Cannot convert value of type '(LIFXClient) -> ()' to expected argument type '(_) -> _'" – Madrox24 Apr 19 '19 at 00:33
  • Ok. They have chosen to build promisekit into their framework so you will need to concatenate additional promises – Paulw11 Apr 19 '19 at 00:35
  • I just want to extract client reference. Do you know how to concatenate additional promises? – Madrox24 Apr 19 '19 at 00:38
  • Just use `.then...` after the current closure. Personally, I would look for a different framework that didn't require the use of promisekit. Otherwise you will need to familiarise yourself with how it works https://github.com/mxcl/PromiseKit – Paulw11 Apr 19 '19 at 00:41

0 Answers0