109 lines
4.6 KiB
Swift
109 lines
4.6 KiB
Swift
//
|
|
// ConnectionLossOperation.swift
|
|
// Logger4
|
|
//
|
|
// Created by Kilian Hofmann on 15.06.17.
|
|
// Copyright © 2017 Kilian Hofmann. All rights reserved.
|
|
//
|
|
|
|
import Cocoa
|
|
|
|
class ConnectionLossOperation: Operation {
|
|
var result1: Bool = false
|
|
var result2: Bool = false
|
|
|
|
override init() {
|
|
|
|
}
|
|
|
|
override func main() {
|
|
// Get date for folder structure
|
|
let time : Date = Date()
|
|
let start: Array<String> = (NSApp.delegate as! AppDelegate).justDate.string(from: time).components(separatedBy: ".")
|
|
// Line to write
|
|
let entry: String = String(format: "%@", (NSApp.delegate as! AppDelegate).justTime.string(from: time))
|
|
// File manager
|
|
let fileManager: FileManager = FileManager.default
|
|
// Path to file
|
|
let file: String = NSString(format: "~/KDLog/%@.docsisplist2/%@/%@/KDLog.hex", start[2], start[1], start[0]).expandingTildeInPath
|
|
// Make all relevant directories if not present
|
|
pthread_mutex_lock(&((NSApp.delegate as! AppDelegate).lock))
|
|
let dir: NSString = NSString(format: "~/KDLog/%@.docsisplist2/%@/%@", start[2], start[1], start[0])
|
|
do {
|
|
try fileManager.createDirectory(atPath: dir.expandingTildeInPath, withIntermediateDirectories: true, attributes: nil)
|
|
}
|
|
catch let error as NSError{
|
|
NSLog("ERROR ON SUBDIRECTORY CREATION: \(error.localizedDescription)")
|
|
}
|
|
pthread_mutex_unlock(&((NSApp.delegate as! AppDelegate).lock))
|
|
// Check connection requests
|
|
let request: URLRequest = URLRequest.init(url: URL.init(string: "http://checkip.dyndns.org")!, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10)
|
|
let request2: URLRequest = URLRequest.init(url: URL.init(string: "http://www.google.de")!, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10)
|
|
// Generate tasks
|
|
let task1: URLSessionDataTask = (NSApp.delegate as! AppDelegate).urlSession.dataTask(with: request) {
|
|
data, response, error in
|
|
|
|
if error != nil && data == nil && self.result2 {
|
|
/*
|
|
let fileH: FileHandle? = FileHandle(forUpdatingAtPath: file)
|
|
if fileH == nil {
|
|
do{
|
|
try entry.write(toFile: file, atomically: true, encoding: .utf8)
|
|
}
|
|
catch {
|
|
NSLog("WRITE TO FILE FAILED\n");
|
|
}
|
|
} else {
|
|
fileH?.seekToEndOfFile();
|
|
let data = (entry as NSString).data(using: String.Encoding.utf8.rawValue)
|
|
fileH?.write(data!)
|
|
fileH?.closeFile()
|
|
}
|
|
|
|
let sep = entry.components(separatedBy: ":")
|
|
FileOperations.logHours(UInt8(sep[0])!, minutes: UInt8(sep[1])!, seconds: UInt8(sep[2])!, toLog: file)
|
|
*/
|
|
FileOperations.log(data: entry, toLog: file)
|
|
self.result2 = false;
|
|
NSLog("Internet loss logged!")
|
|
}
|
|
else if error != nil && response == nil && data == nil {
|
|
self.result1 = true;
|
|
}
|
|
}
|
|
let task2: URLSessionDataTask = (NSApp.delegate as! AppDelegate).urlSession.dataTask(with: request2) {
|
|
data, response, error in
|
|
|
|
if error != nil && data == nil && self.result1 {
|
|
/*
|
|
let fileH: FileHandle? = FileHandle(forUpdatingAtPath: file)
|
|
if fileH == nil {
|
|
do{
|
|
try entry.write(toFile: file, atomically: true, encoding: .utf8)
|
|
}
|
|
catch {
|
|
NSLog("WRITE TO FILE \(file) FAILED\n");
|
|
}
|
|
} else {
|
|
fileH?.seekToEndOfFile();
|
|
let data = (entry as NSString).data(using: String.Encoding.utf8.rawValue)
|
|
fileH?.write(data!)
|
|
fileH?.closeFile()
|
|
}
|
|
let sep = entry.components(separatedBy: ":")
|
|
FileOperations.logHours(UInt8(sep[0])!, minutes: UInt8(sep[1])!, seconds: UInt8(sep[2])!, toLog: file)
|
|
*/
|
|
FileOperations.log(data: entry, toLog: file)
|
|
self.result2 = false;
|
|
NSLog("Internet loss logged!")
|
|
}
|
|
else if error != nil && response == nil && data == nil {
|
|
self.result2 = true;
|
|
}
|
|
}
|
|
// Start tasks
|
|
task1.resume()
|
|
task2.resume()
|
|
}
|
|
}
|