Skip to content

Commit

Permalink
Adds parser implementations for Link and Server Destination
Browse files Browse the repository at this point in the history
  • Loading branch information
CoryFoy committed Mar 28, 2024
1 parent e318a9f commit bd922b5
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 4 deletions.
20 changes: 20 additions & 0 deletions Sources/SwiftTAK/COT/COTModels/COTDetail.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,26 @@ public struct COTDetail : COTNode, Equatable {
}
return chat
}

public var cotLink: COTLink? {
var link: COTLink?
childNodes.forEach {
if($0 is COTLink) {
link = $0 as? COTLink
}
}
return link
}

public var cotServerDestination: COTServerDestination? {
var serverDest: COTServerDestination?
childNodes.forEach {
if($0 is COTServerDestination) {
serverDest = $0 as? COTServerDestination
}
}
return serverDest
}

public func toXml() -> String {
return COTXMLHelper.generateXML(
Expand Down
12 changes: 11 additions & 1 deletion Sources/SwiftTAK/COT/COTModels/COTLink.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation

public struct COTLink : COTNode {
public struct COTLink : COTNode, Equatable {
public init(parentCallsign: String = "", productionTime: String = "", relation: String, type: String, uid: String, callsign: String = "") {
self.parentCallsign = parentCallsign
self.productionTime = productionTime
Expand All @@ -34,4 +34,14 @@ public struct COTLink : COTNode {
attrs["callsign"] = callsign
return COTXMLHelper.generateXML(nodeName: "link", attributes: attrs, message: "")
}

public static func == (lhs: COTLink, rhs: COTLink) -> Bool {
return
lhs.uid == rhs.uid &&
lhs.relation == rhs.relation &&
lhs.type == rhs.type &&
lhs.callsign == rhs.callsign &&
lhs.productionTime == rhs.productionTime &&
lhs.parentCallsign == rhs.parentCallsign
}
}
10 changes: 7 additions & 3 deletions Sources/SwiftTAK/COT/COTModels/COTServerDestination.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@

import Foundation

public struct COTServerDestination : COTNode {
public struct COTServerDestination : COTNode, Equatable {
public init(destinations: String = "") {
self.destinations = destinations
}



public var destinations: String = ""

public func toXml() -> String {
var attrs: [String:String] = [:]
attrs["destinations"] = destinations
return COTXMLHelper.generateXML(nodeName: "__serverdestination", attributes: attrs, message: "")
}

public static func == (lhs: COTServerDestination, rhs: COTServerDestination) -> Bool {
return
lhs.destinations == rhs.destinations
}
}
23 changes: 23 additions & 0 deletions Sources/SwiftTAK/Parsers/COTXMLParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,29 @@ class COTXMLParser {
detail.childNodes.append(chat)
}

if let cotLink = cot["event"]["detail"]["link"].element {
let linkAttributes = cotLink.allAttributes
let link = COTLink(
parentCallsign: linkAttributes["parent_callsign"]?.text ?? "",
productionTime: linkAttributes["production_time"]?.text ?? "",
relation: linkAttributes["relation"]?.text ?? "",
type: linkAttributes["type"]?.text ?? "",
uid: linkAttributes["uid"]?.text ?? UUID().uuidString,
callsign: linkAttributes["callsign"]?.text ?? ""
)

detail.childNodes.append(link)
}

if let cotServerDestination = cot["event"]["detail"]["__serverdestination"].element {
let destinationAttributes = cotServerDestination.allAttributes
let destination = COTServerDestination(
destinations: destinationAttributes["destinations"]?.text ?? ""
)

detail.childNodes.append(destination)
}

event.childNodes.append(detail)
}

Expand Down
22 changes: 22 additions & 0 deletions Tests/SwiftTAKTests/Parsers/COTXMLParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,5 +152,27 @@ class COTXMLParserTests: SwiftTAKTestCase {
XCTAssertEqual(expected, actual)
}

func testProperlyCreatesCOTLinkFromXML() {
let expected = COTLink(
relation: "p-p",
type: "a-f-G-E-V-C",
uid: "9AEDF55F-FB33-44FD-9BF9-3F51605ADF6A"
)
let event = parser.parse(CHAT_EVENT)
let detail = event?.cotDetail
let actual = detail?.cotLink
XCTAssertEqual(expected, actual)
}

func testProperlyCreatesCOTServerDestinationFromXML() {
// <__serverdestination destinations="192.168.0.79:4242:tcp:9AEDF55F-FB33-44FD-9BF9-3F51605ADF6A" />
let expected = COTServerDestination(destinations: "192.168.0.79:4242:tcp:9AEDF55F-FB33-44FD-9BF9-3F51605ADF6A"
)
let event = parser.parse(CHAT_EVENT)
let detail = event?.cotDetail
let actual = detail?.cotServerDestination
XCTAssertEqual(expected, actual)
}

//Chat, Link, ServerDestination
}

0 comments on commit bd922b5

Please sign in to comment.