The Network Kit is the piece of Swift code every Homylon app and agent uses to talk to the Engine. The Monitor, the remotes and the Hue, IKEA, MQTT and Matter agents all run on it, and it is the same code the Engine itself uses. If you want to write your own app, tool or agent in Swift, this is the place to start. It is open source under the MIT licence, so you are free to use it in your own projects.
It does the network work for you. It sends your messages to the Engine (small ones by UDP, large ones by TCP, with a timestamp and your house's security key), it receives what the Engine sends back (with a UDP and a TCP server), it finds the Engine on your network (Bonjour first, then the last Engine that answered, then the address from your settings) and it tells you your own address as the Engine sees it.
One kit, two ways onto the network
Inside the kit are two "transports" with exactly the same classes and functions, so your own code is the same everywhere. Which one is used is decided when your app is built; you do not choose it in your code.
- Apple (iPhone, iPad, Mac, Apple TV): Apple's Network.framework. It is part of the operating system, so nothing extra is downloaded, your app stays small, and it is kept up to date with every system update. It runs from iOS 12, macOS 11.7 and tvOS 12, so older devices are welcome too.
- Linux (PC, Raspberry Pi): Network.framework does not exist on Linux, so there the kit uses SwiftNIO, Apple's open source network library that also powers server software. It is downloaded automatically the first time you build on Linux (Swift 5.9 or later). Apple apps never carry it.
Both are made and maintained by Apple. The built-in one is the natural choice on Apple devices; SwiftNIO is what makes the same code run on Linux. On a Mac, developers can try the Linux variant with HOMYLON_NIO=1 swift test. Finding the Engine with Bonjour is Apple-only for now; on Linux, give the Engine's address.
How to use it
Download the sources and unzip them: you get the folder homylon-kit-swift. In Xcode, drag that folder into your project (or choose File, Add Package Dependencies, Add Local) and add HomylonKit to your app. On Linux, add it to your own Package.swift with .package(path: "../homylon-kit-swift"). Then write import HomylonKit at the top of your code.
Here is the Simple Python Monitor from the Monitors page again, now in Swift with the kit. It runs on a Mac and on Linux:
// The first monitor again, now in Swift with the Network Kit.
import Foundation
import HomylonKit
let engine = "127.0.0.1" // the Engine's address
let myAddress = "127.0.0.1" // this computer's address
let port: UInt16 = 55261 // where the Engine sends its lines
final class Monitor: UDPServerDelegate {
func udpDidReceiveData(_ data: Data?, from: String) {
guard let data = data,
let line = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else { return }
print(line["monitorMessageType"] ?? "", line["monitorMessageText"] ?? "")
}
func udpDidStartSuccessfully(onPort: Int) { print("Waiting for the Engine on port \(onPort)") }
func udpDidFailToStartWithError(_ error: Error) { print("Cannot listen: \(error)") }
}
let monitor = Monitor()
let server = UDPServer()
server.delegate = monitor
server.start(onPort: port, queue: .main)
// Say hello every second, so we see the Engine start.
Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { _ in
Homylon.send(["action": "registermonitor",
"monitor": ["name": "firstmonitor", "address": myAddress, "port": Int(port),
"protocol": "udp", "types": "all"]],
to: engine, port: 55051)
}
RunLoop.main.run()
For bigger examples, the sources of the Homylon Monitor and of the agents (see their pages) show the kit at work in real apps.
Download here: