Commit 56078144 authored by Dirk Hildebrand's avatar Dirk Hildebrand
Browse files

started with logging

parent 5ec482f6
......@@ -17,7 +17,7 @@
<key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>72</string>
<string>109</string>
<key>ITSAppUsesNonExemptEncryption</key>
<false/>
<key>LSRequiresIPhoneOS</key>
......
......@@ -11,24 +11,64 @@ import Network
struct LogView: View {
@State private var connection: NWConnection?
@State private var log: String? = "You need to be on the same Network, as the HAA Device, wich sends the logs, or you need to configure your HAA Device to send logs directly to this iOS Device."
var body: some View {
Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/).onAppear(perform: {
udpSocket()
Text(log ?? "")
.fontWeight(.light)
.padding()
.onAppear(perform: {
findUDP()
})
}
func udpSocket() {
connection = NWConnection(host: "255.255.255.255", port: 45678, using: .udp)
connection?.stateUpdateHandler = { (newState) in
struct LogView_Previews: PreviewProvider {
static var previews: some View {
LogView()
}
}
@State private var udpListener: NWListener?
@State private var udpConnection: NWConnection?
@State private var backgroundQueueUdpListener = DispatchQueue.main
func findUDP() {
let params = NWParameters.udp
udpListener = try? NWListener(using: params, on: 45678)
udpListener?.service = NWListener.Service.init(type: "_hap._tcp")
udpListener?.stateUpdateHandler = { update in
print("update")
print(update)
switch update {
case .failed:
print("failed")
default:
print("default update")
}
}
self.udpListener?.newConnectionHandler = { connection in
print("connection")
print(connection)
self.createConnection(connection: connection)
self.udpListener?.cancel()
}
udpListener?.start(queue: self.backgroundQueueUdpListener)
}
func createConnection(connection: NWConnection) {
udpConnection = connection
udpConnection?.stateUpdateHandler = { (newState) in
switch (newState) {
case .ready:
print("ready")
connection?.receiveMessage { (data, context, isComplete, error) in
print("Got it")
print(data!)
self.udpConnection?.receiveMessage { (data, context, isComplete, error) in
let backToString = String(decoding: data!, as : UTF8.self)
print("UDP - \(backToString)")
log = log ?? "" + "\n" + backToString
findUDP()
}
case .setup:
print("setup")
......@@ -38,17 +78,17 @@ struct LogView: View {
print("Preparing")
default:
print("waiting or failed")
}
}
self.connection?.start(queue: .global())
self.udpConnection?.start(queue: .global())
}
struct LogView_Previews: PreviewProvider {
static var previews: some View {
LogView()
func endConnection() {
self.udpConnection?.cancel()
}
}
}
......@@ -133,8 +133,8 @@ struct MainView: View {
for service in self.services {
if service.port == -1 {
print("service \(service.name) of type \(service.type)" +
" not yet resolved")
//print("service \(service.name) of type \(service.type)" +
//" not yet resolved")
service.delegate = self
service.resolve(withTimeout:10)
} else {
......@@ -149,7 +149,7 @@ struct MainView: View {
do {
filteredDevice = try self.managedObjectContext.fetch(request)
} catch {
print(error)
//print(error)
}
if filteredDevice.count < 1 {
let nDevice = Device(context: self.managedObjectContext)
......@@ -160,7 +160,7 @@ struct MainView: View {
do {
try self.managedObjectContext.save()
} catch {
print(error)
//print(error)
}
}
......@@ -175,7 +175,7 @@ struct MainView: View {
}
func netServiceBrowser(_ aNetServiceBrowser: NetServiceBrowser, didFind aNetService: NetService, moreComing: Bool) {
print("adding a service")
//print("adding a service")
self.services.append(aNetService)
if !moreComing {
self.updateInterface()
......@@ -184,9 +184,9 @@ struct MainView: View {
func netServiceBrowser(_ aNetServiceBrowser: NetServiceBrowser, didRemove aNetService: NetService, moreComing: Bool) {
if let ix = self.services.firstIndex(of:aNetService) {
let deviceName = services[ix].hostName ?? ""
//let deviceName = services[ix].hostName ?? ""
self.services.remove(at:ix)
print("removing service \(deviceName)")
//print("removing service \(deviceName)")
if !moreComing {
self.updateInterface()
}
......@@ -202,7 +202,7 @@ struct MainView: View {
do {
filteredDevice = try managedObjectContext.fetch(request)
} catch {
print(error)
//print(error)
}
for d in filteredDevice {
let ipadress = d.value(forKeyPath: "ipadress") as! String
......@@ -218,7 +218,7 @@ struct MainView: View {
do {
device = try managedObjectContext.fetch(request)
} catch {
print(error)
//print(error)
}
if error != nil{
......@@ -226,7 +226,7 @@ struct MainView: View {
do {
try managedObjectContext.save()
} catch {
print(error)
//print(error)
}
}
......@@ -240,21 +240,19 @@ struct MainView: View {
do {
try managedObjectContext.save()
} catch {
print(error)
//print(error)
}
} else {
device.first?.setValue(false, forKey: "reachable")
do {
try managedObjectContext.save()
} catch {
print(error)
//print(error)
}
}
print("HTTP Status: \(httpResponse.statusCode)")
//print("HTTP Status: \(httpResponse.statusCode)")
}
}.resume()
print("I am done")
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment