Loss operation simplified and integrated

This commit is contained in:
2017-06-15 18:52:01 +02:00
parent 4062b0ed9c
commit 93cbf6d19a
8 changed files with 273 additions and 7 deletions
+25 -5
View File
@@ -5,6 +5,8 @@
// Created by Kilian Hofmann on 15.06.17.
// Copyright © 2017 Kilian Hofmann. All rights reserved.
//
// Folder Structure "~/KDLog/YEAR.docsisplist2/MONTH/DAY/FILE"
//
import Cocoa
@@ -12,6 +14,11 @@ import Cocoa
class AppDelegate: NSObject, NSApplicationDelegate {
// MARK: - Things that have to be kept alive for the application lifetime
// lock for mutex access to files
var lock: pthread_mutex_t = pthread_mutex_t()
// Date formaters
let justDate: DateFormatter = DateFormatter()
let justTime: DateFormatter = DateFormatter()
// Status Item
var statusItem: NSStatusItem = NSStatusItem()
// URL Session for DOCSIS data and Internet check
@@ -21,8 +28,6 @@ class AppDelegate: NSObject, NSApplicationDelegate {
// Timers
var timerFreqs: Timer = Timer()
var timerFails: Timer = Timer()
// Document content list
var content: NSMutableDictionary = NSMutableDictionary()
// MARK: - Application Lifecycle
@@ -33,6 +38,19 @@ class AppDelegate: NSObject, NSApplicationDelegate {
pref.showWindow(nil)
}
// Init lock
pthread_mutex_init(&((NSApp.delegate as! AppDelegate).lock), nil)
// Setup date formatters
justDate.dateStyle = DateFormatter.Style.medium
justDate.timeStyle = DateFormatter.Style.none
justDate.locale = Locale(identifier: "de_DE")
justTime.dateStyle = .none
justTime.timeStyle = .medium
justTime.timeZone = NSTimeZone.local
justTime.locale = Locale(identifier: "de_DE")
// Set status item
statusItem = NSStatusBar.system().statusItem(withLength: -2)
statusItem.title = "AL"
@@ -52,9 +70,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
// Add menu
statusItem.menu = menu
// Check if directory exists, creat or exit
// Check if directory exists, create or exit
let fileManager = FileManager.default
var directory: ObjCBool = ObjCBool(false)
pthread_mutex_lock(&((NSApp.delegate as! AppDelegate).lock))
let exists: Bool = fileManager.fileExists(atPath: ("~/KDLog" as NSString).expandingTildeInPath, isDirectory: &directory)
if exists && directory.boolValue {
} else if exists {
@@ -70,6 +89,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
exit(1);
}
}
pthread_mutex_unlock(&((NSApp.delegate as! AppDelegate).lock))
// Init timers
if UserDefaults.standard.string(forKey: "upstream") != nil || UserDefaults.standard.string(forKey: "downstream") != nil {
@@ -90,8 +110,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
func logFails(_: Timer) {
// let connectionLossOperation: ConnectionLossOperation = ConnectionLossOperation()
// OperationQueue.main.addOperation(connectionLossOperation)
let connectionLossOperation: ConnectionLossOperation = ConnectionLossOperation()
OperationQueue.main.addOperation(connectionLossOperation)
}
// MARK: - General functions
+71
View File
@@ -0,0 +1,71 @@
//
// ConnectionLossOperation.swift
// Docsis Toolkit
//
// 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: "%@\n", (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.txt", 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 {
log(entry, path: 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 {
log(entry, path: file)
self.result1 = false;
NSLog("Internet loss logged!")
}
else if error != nil && response == nil && data == nil {
self.result2 = true;
}
}
// Start tasks
task1.resume()
task2.resume()
}
}
+42
View File
@@ -0,0 +1,42 @@
//
// FrequencyOperation.swift
// Docsis Toolkit
//
// Created by Kilian Hofmann on 15.06.17.
// Copyright © 2017 Kilian Hofmann. All rights reserved.
//
import Cocoa
class FrequencyOperation: Operation {
// DOCSIS 3.0 Upstream Power Limits as per Specification March 5. 2015
let upstreamOne = ["qpsk": "62.18", "8qam": "58.21", "16qam": "58.21", "32qam": "57", "64qam": "57", "128qam": "-"]
let upstreamTwo = ["qpsk": "59.18", "8qam": "55.21", "16qam": "55.21", "32qam": "54", "64qam": "54", "128qam": "-"]
let upstreamThreeOrFour = ["qpsk": "56.18", "8qam": "52.21", "16qam": "52.21", "32qam": "51", "64qam": "51", "128qam": "-"]
let modulationAdjust: NSDictionary = ["qpsk": "-1.18", "8qam": "-0.21", "16qam": "-0.21", "32qam" : "0", "64qam": "0", "128qam": "0.05"]
// XML Parser
let parser: XMLDictionaryParser = XMLDictionaryParser()
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: ".")
// File manager
let fileManager: FileManager = FileManager.default
// 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))
}
}
+1 -1
View File
@@ -17,7 +17,7 @@
<key>CFBundleShortVersionString</key>
<string>4.0</string>
<key>CFBundleVersion</key>
<string>6</string>
<string>13</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>LSUIElement</key>
+26
View File
@@ -0,0 +1,26 @@
//
// Shared.swift
// Docsis Toolkit
//
// Created by Kilian Hofmann on 15.06.17.
// Copyright © 2017 Kilian Hofmann. All rights reserved.
//
import Cocoa
func log(_ entry: String, path: String) {
let file: FileHandle? = FileHandle(forUpdatingAtPath: path)
if file == nil {
do{
try (entry).write(toFile: path, atomically: true, encoding: String.Encoding.utf8)
}
catch {
NSLog("WRITE TO FILE FAILED\n");
}
} else {
file?.seekToEndOfFile();
let data = (entry as NSString).data(using: String.Encoding.utf8.rawValue)
file?.write(data!)
file?.closeFile()
}
}
+1 -1
View File
@@ -1,2 +1,2 @@
version 4.0
build 6
build 13