Reimplemented sendTime() and sendTime on connect (#1364)

* Reimplemented sendTime() and sendTime on connect

* Update AccessoryManager+ToRadio.swift

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Jake-B <jake-b@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
jake-b 2025-08-30 11:00:03 -04:00 committed by GitHub
parent ae9fa586ba
commit 2e690da9fb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 31 additions and 3 deletions

View file

@ -129,8 +129,10 @@ extension AccessoryManager {
// Step 7: Update UI and status to connected
Step { @MainActor _ in
Logger.transport.info("🔗👟 [Connect] Step 7: Update UI and status")
Logger.transport.info("🔗👟 [Connect] Step 7: Update Time, UI and status")
// Send time to device
try? await self.sendTime()
// We have an active connection
self.updateDevice(deviceId: device.id, key: \.connectionState, value: .connected)
self.updateState(.subscribed)

View file

@ -162,7 +162,33 @@ extension AccessoryManager {
}
await self.heartbeatResponseTimer?.reset(delay: .seconds(5.0))
}
public func sendTime() async throws {
guard let deviceNum = self.activeDeviceNum.map({ UInt32($0) }) else {
Logger.mesh.error("🚫 Unable to send time, connected node is disconnected or invalid")
return
}
var adminPacket = AdminMessage()
adminPacket.setTimeOnly = UInt32(Date().timeIntervalSince1970)
var meshPacket: MeshPacket = MeshPacket()
meshPacket.to = deviceNum
meshPacket.from = deviceNum
meshPacket.id = UInt32.random(in: UInt32(UInt8.max)..<UInt32.max)
meshPacket.priority = MeshPacket.Priority.reliable
meshPacket.wantAck = true
meshPacket.channel = 0
var dataMessage = DataMessage()
if let serializedData: Data = try? adminPacket.serializedData() {
dataMessage.payload = serializedData
dataMessage.portnum = PortNum.adminApp
meshPacket.decoded = dataMessage
} else {
throw AccessoryError.ioFailed("sendTime: Unable to serialize admin packet")
}
let messageDescription = "🕛 Sent Set Time Admin Message to the connected node."
try await sendAdminMessageToRadio(meshPacket: meshPacket, adminDescription: messageDescription)
}
public func sendShutdown(fromUser: UserEntity, toUser: UserEntity) async throws {
var adminPacket = AdminMessage()
adminPacket.shutdownSeconds = 5