Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.DS_Store
CLAUDE.md
.claude/
25 changes: 0 additions & 25 deletions AirTouchDemo/AirTouchDemo/Classes/AirTouch2PlusClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,6 @@ public class AirTouch2PlusClient {
public func connect(endpoint: NWEndpoint) {
self.connection?.disconnect()

let connec = AirTouch2PlusConnection() { connection, state in

} packetReceivedHandler: { connection, packet in
switch packet.airTouch2PlusPacket {
case let message as GroupStatusMessage:
// Handle message here
break

case let message as UnitStatusMessage:
// Handle message here
break

default:
break
}
}

// Request uni status
let message = UnitStatusMessage(units: [])
let packet = Packet.request(messageID: nil, message)

connec.send(packet: packet) { error in

}

let connection = AirTouch2PlusConnection(stateUpdateHandler: stateUpdateHandler, packetReceivedHandler: packetReceivedHandler)
connection.connect(endpoint: endpoint)
self.connection = connection
Expand Down
21 changes: 19 additions & 2 deletions AirTouchDemo/AirTouchMockServer/AirTouch2PlusMockServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,24 @@ public class AirTouch2PlusMockServer {
}

case let m as GroupControlMessage:
message = GroupStatusMessage(groups: [])
for group in m.groups {
if var existing = systemState.groupStatuses[group.groupID] {
if let power = group.power {
switch power {
case .on:
existing = .init(groupID: existing.groupID, power: .on, openPercentage: existing.openPercentage, turboIsSupported: existing.turboIsSupported, spillIsActive: existing.spillIsActive)
case .off:
existing = .init(groupID: existing.groupID, power: .off, openPercentage: existing.openPercentage, turboIsSupported: existing.turboIsSupported, spillIsActive: existing.spillIsActive)
case .turbo:
existing = .init(groupID: existing.groupID, power: .turbo, openPercentage: existing.openPercentage, turboIsSupported: existing.turboIsSupported, spillIsActive: existing.spillIsActive)
case .next:
break
}
}
systemState.groupStatuses[group.groupID] = existing
}
}
message = GroupStatusMessage(groups: systemState.sortedGroupStatuses)

case let m as GroupNameRequestMessage:
var groups: [GroupNameResponseMessage.Group] = []
Expand All @@ -91,7 +108,7 @@ public class AirTouch2PlusMockServer {
message = GroupNameResponseMessage(groups: groups)

case let m as GroupStatusMessage:
message = GroupStatusMessage(groups: [])
message = GroupStatusMessage(groups: systemState.sortedGroupStatuses)

case let m as UnitAbilitiesRequestMessage:
switch m {
Expand Down
14 changes: 14 additions & 0 deletions AirTouchDemo/AirTouchMockServer/SystemState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,16 @@ class SystemState {

var unitStatuses: [UnitID: UnitStatusMessage.Unit]
var unitAbilities: [UnitID: UnitAbilitiesResponseMessage.Unit]
var groupStatuses: [GroupID: GroupStatusMessage.Group]

public init() {
groupStatuses = [
0: .init(groupID: 0, power: .on, openPercentage: 80, turboIsSupported: true, spillIsActive: false),
1: .init(groupID: 1, power: .on, openPercentage: 60, turboIsSupported: true, spillIsActive: false),
2: .init(groupID: 2, power: .off, openPercentage: 0, turboIsSupported: false, spillIsActive: false),
3: .init(groupID: 3, power: .off, openPercentage: 0, turboIsSupported: false, spillIsActive: false),
]

unitStatuses = [
0: .init(
unitID: 0,
Expand Down Expand Up @@ -47,6 +55,12 @@ class SystemState {
String(format: "Group \(id)")
}

var sortedGroupStatuses: [GroupStatusMessage.Group] {
groupStatuses.map { $0.value }.sorted { a, b in
a.groupID < b.groupID
}
}

var sortedUnitStatuses: [UnitStatusMessage.Unit] {
unitStatuses.map { $0.value }.sorted { a, b in
a.unitID < b.unitID
Expand Down
8 changes: 3 additions & 5 deletions Sources/AirTouch2Plus/Messages/GroupNameResponseMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,16 @@ extension GroupNameResponseMessage: ByteCodable {

var groups: [Group] = []

while idx < bytes.count {
defer {
idx += repeatingLength
}

while idx + repeatingLength <= bytes.count {
let bytes = bytes[idx ..< idx + repeatingLength]

let group = Group(bytes: Array(bytes))

if let group {
groups.append(group)
}

idx += repeatingLength
}

self.groups = groups
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ extension UnitAbilitiesResponseMessage.Unit: ByteCodable {
numGroups,
modesByte,
fanSpeedsByte,
UInt8(minSetPoint.value),
UInt8(maxSetPoint.value)
UInt8(clamping: Int(minSetPoint.value.rounded())),
UInt8(clamping: Int(maxSetPoint.value.rounded()))
]
}

Expand Down Expand Up @@ -205,18 +205,16 @@ extension UnitAbilitiesResponseMessage: ByteCodable {

var units: [Unit] = []

while idx < bytes.count {
defer {
idx += repeatingLength
}

while idx + repeatingLength <= bytes.count {
let bytes = bytes[idx ..< idx + repeatingLength]

let unit = Unit(bytes: Array(bytes))

if let unit {
units.append(unit)
}

idx += repeatingLength
}

self.units = units
Expand Down
27 changes: 20 additions & 7 deletions Sources/AirTouch2Plus/Packet/Packet+Codable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,36 @@ extension Packet: ByteDecodable {
idx = maxIdx

let dataLength = header.body.length
maxIdx = min(idx + Int(dataLength), bytes.count)
maxIdx = idx + Int(dataLength)

guard maxIdx <= bytes.count else {
return nil
}

guard maxIdx > idx else {
self.data = []
self.crc16 = 0
return
return nil
}

self.data = Array(bytes[idx ..< maxIdx])
idx = maxIdx

guard idx + 2 <= bytes.count else {
return nil
}

let crc16Bytes: [UInt8] = [
bytes[safe: idx] ?? 0x0,
bytes[safe: idx + 1] ?? 0x0
bytes[idx],
bytes[idx + 1]
]
idx += 2

self.crc16 = crc16Bytes.int16
let crc16 = crc16Bytes.int16
let expectedCrc16 = (header.body.bytes + data).crc16Modbus

guard crc16 == expectedCrc16 else {
return nil
}

self.crc16 = crc16
}
}
17 changes: 10 additions & 7 deletions Sources/AirTouch2Plus/Packet/Packet+RepeatingData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,11 @@ extension Packet.RepeatingData {
if normalBytesLength > 0 {
let maxIdx = idx + Int(normalBytesLength)

if maxIdx < bytes.count {
normalBytes = Array(bytes[idx ..< maxIdx])
guard maxIdx <= bytes.count else {
return nil
}

normalBytes = Array(bytes[idx ..< maxIdx])
idx = maxIdx
}

Expand All @@ -91,12 +92,14 @@ extension Packet.RepeatingData {
for _ in 0 ..< repeatsCount {
let maxIdx = idx + Int(repeatsLength)

if maxIdx <= bytes.count {
let repeatBytes = Array(bytes[idx ..< maxIdx])
guard maxIdx <= bytes.count else {
break
}

let repeatBytes = Array(bytes[idx ..< maxIdx])

if let rep = T(bytes: repeatBytes) {
repeats.append(rep)
}
if let rep = T(bytes: repeatBytes) {
repeats.append(rep)
}

idx = maxIdx
Expand Down
Loading