Skip to content

Commit

Permalink
Implemented Loggable protocol
Browse files Browse the repository at this point in the history
that provides a standard logger for each conforming type with the correct Subsystem and Category
  • Loading branch information
TheMisfit68 committed Apr 7, 2024
1 parent 273f138 commit 3a454c1
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import SoftPLC
import ModbusDriver
import IOTypes
import JVSwift
import JVSwiftCore
import WeatherKit
import JVWeather
import CoreLocation
import OSLog

// MARK: - PLC level class
class SmartSprinkler:PLCClassAccessoryDelegate{
class SmartSprinkler:PLCClassAccessoryDelegate, Loggable{

// MARK: - Accessory binding
typealias AccessorySubclass = Accessory.SmartSprinkler
Expand All @@ -32,8 +33,7 @@ class SmartSprinkler:PLCClassAccessoryDelegate{

private let weatherReporter = WeatherReporter()
private var needsIrrigation:Bool{
let logger = Logger(subsystem: "be.oneclick.HAPiNest", category: "Smartsprinkler")
logger.warning( "Drystate \(self.weatherReporter.wasDry)/\(self.weatherReporter.isDry)/\(self.weatherReporter.willBeDry)/\(self.weatherReporter.isWindy)")
SmartSprinkler.logger.warning( "Drystate \(self.weatherReporter.wasDry)/\(self.weatherReporter.isDry)/\(self.weatherReporter.willBeDry)/\(self.weatherReporter.isWindy)")

return (weatherReporter.wasDry && weatherReporter.isDry && weatherReporter.willBeDry && !weatherReporter.isWindy)
}
Expand Down
6 changes: 3 additions & 3 deletions Accessory Delegates/AccessoryDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import Foundation
import HAP
import JVSwift
import JVSwiftCore
import SoftPLC
import OSLog

Expand All @@ -18,7 +19,7 @@ import OSLog
/// An object (typically some hardware driver) capable of
/// receiving events from a HomeKit-Accessory and
/// process them accordingly.
public protocol AccessoryDelegate: HAP.AccessoryDelegate{
public protocol AccessoryDelegate: HAP.AccessoryDelegate, Loggable{

var name:String{get}

Expand All @@ -37,8 +38,7 @@ extension AccessoryDelegate{
ofAccessory accessory: HAP.Accessory,
didChangeValue newValue: T?){

let logger = Logger(subsystem: "be.oneclick.HAPiNest", category: "AccessoryDelegate")
logger.info("✴️\tValue '\(characteristic.description ?? "", privacy: .public)' of '\(accessory.info.name.value ?? "", privacy: .public)/\(service.label ?? "", privacy: .public)' changed to \(String(describing:newValue), privacy: .public)")
Self.logger.info("✴️\tValue '\(characteristic.description ?? "", privacy: .public)' of '\(accessory.info.name.value ?? "", privacy: .public)/\(service.label ?? "", privacy: .public)' changed to \(String(describing:newValue), privacy: .public)")

characteristicChanged.set()

Expand Down
8 changes: 4 additions & 4 deletions Accessory Delegates/LeafAccessoryDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
import Foundation
import HAP
import LeafDriver
import JVSwiftCore
import OSLog
import JVNetworking

class LeafAccessoryDelegate:LeafDriver, AccessoryDelegate, AccessorySource{
let logger = Logger(subsystem: "be.oneclick.HAPiNest", category: "LeafAccessoryDelegate")

var name: String{
return String(localized:"Electric Car")
Expand Down Expand Up @@ -68,7 +68,7 @@ class LeafAccessoryDelegate:LeafDriver, AccessoryDelegate, AccessorySource{
startCharging = characteristic.value as! Bool

default:
logger.warning( "Unhandled characteristic change for accessory \(accessory.info.name.value ?? "")")
LeafAccessoryDelegate.logger.warning( "Unhandled characteristic change for accessory \(accessory.info.name.value ?? "")")
}

case accessory.aircoService:
Expand All @@ -79,11 +79,11 @@ class LeafAccessoryDelegate:LeafDriver, AccessoryDelegate, AccessorySource{
setAirco = characteristic.value as! Bool

default:
logger.warning( "Unhandled characteristic change for accessory \(accessory.info.name.value ?? "")")
LeafAccessoryDelegate.logger.warning( "Unhandled characteristic change for accessory \(accessory.info.name.value ?? "")")
}

default:
logger.warning( "Unhandled characteristic change for accessory \(accessory.info.name.value ?? "")")
LeafAccessoryDelegate.logger.warning( "Unhandled characteristic change for accessory \(accessory.info.name.value ?? "")")
}


Expand Down
7 changes: 3 additions & 4 deletions Accessory Delegates/MilightAccessoryDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import Foundation
import HAP
import JVSwift
import JVSwiftCore
import MilightDriver
import OSLog

Expand Down Expand Up @@ -55,8 +56,7 @@ class MilightAccessoryDelegate:MilightDriverV6, AccessoryDelegate{
if inWhiteMode != oldValue{
let currentBrightness = brightness
if inWhiteMode{
let logger = Logger(subsystem: "be.oneclick.MilightDriver", category: "MilightDriverV6")
logger.info("Switching to dedicated whitemode")
MilightAccessoryDelegate.logger.info("Switching to dedicated whitemode")

hue = 0
saturation = 0
Expand Down Expand Up @@ -94,8 +94,7 @@ class MilightAccessoryDelegate:MilightDriverV6, AccessoryDelegate{
saturation = Int(characteristic.value as! Float)

default:
let logger = Logger(subsystem: "be.oneclick.HAPiNest", category: "MilightAccessoryDelegate")
logger.warning("Unhandled characteristic change for accessory \(self.name)")
MilightAccessoryDelegate.logger.warning("Unhandled characteristic change for accessory \(self.name)")
}

}
Expand Down
5 changes: 2 additions & 3 deletions Accessory Delegates/TizenAccessoryDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

import Foundation
import HAP
import JVSwiftCore
import TizenDriver
import OSLog

class TizenAccessoryDelegate:TizenDriver, AccessoryDelegate, AccessorySource{

Expand Down Expand Up @@ -59,8 +59,7 @@ class TizenAccessoryDelegate:TizenDriver, AccessoryDelegate, AccessorySource{
}

default:
let logger = Logger(subsystem: "be.oneclick.HAPiNest", category: "TizenAccessoryDelegate")
logger.warning("Unhandled characteristic change for accessory \(self.name)")
TizenAccessoryDelegate.logger.warning("Unhandled characteristic change for accessory \(self.name)")
}
characteristicChanged.set()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@
<key>EndingColumnNumber</key>
<string>0</string>
<key>EndingLineNumber</key>
<string>43</string>
<string>44</string>
<key>StartingColumnNumber</key>
<string>0</string>
<key>StartingLineNumber</key>
<string>42</string>
<string>43</string>
<key>Timestamp</key>
<string>730460898.103404</string>
<string>734182256.930252</string>
</dict>
<key>rebasable-url</key>
<dict>
Expand Down
2 changes: 1 addition & 1 deletion HAPiNest/HAPiNestApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import ModbusDriver
/// by translating those changes into actions for the hardware.
/// The main accessory-delegate in this App is a SoftPLC that can interact with the outside world by means of a number of Input and Output-modules.
@main
struct HAPiNestApp: App {
struct HAPiNestApp: App, Loggable {
@SwiftUI.Environment(\.scenePhase) var scenePhase

let appcontroller:AppController = AppController(name: "HAPiNest", terminal: TerminalDriver())
Expand Down
5 changes: 2 additions & 3 deletions HAPiNest/HomekitServer/HomeKitServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import OSLog
import SwiftUI
import LeafDriver

public class HomeKitServer:Singleton{
let logger = Logger(subsystem: "be.oneclick.HAPiNest", category: "HomeKitServer")
public class HomeKitServer:Singleton, Loggable{
public static var shared: HomeKitServer = HomeKitServer()
public var dashboard:HomeKitServerView?
public var name:String
Expand All @@ -41,7 +40,7 @@ public class HomeKitServer:Singleton{
AppController(name: "Console", location: .systemUtilities, terminal: TerminalDriver()).startIfInstalled()
AppController(name: "Home", location: .systemApps, terminal: TerminalDriver()).startIfInstalled()
#endif
logger.info("Initializing the server \(self.name, privacy: .public)...")
HomeKitServer.logger.info("Initializing the server \(self.name, privacy: .public)...")


// TODO: - reimplement SMAInverter
Expand Down
5 changes: 2 additions & 3 deletions HAPiNest/ServerBasedAutomations/WakeupLights.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class WakeupLights:PLCClass{
// use a helper class a.k.a. composition instead of inheritance.
class BrightnessTimer {

// Public property for time to make a 100% change
private var timer: Timer?

private func adjust(brightness:inout Int?, to targetBrightness:Int?, over totalTime:TimeInterval = 5.0){
Expand Down Expand Up @@ -61,11 +60,11 @@ class BrightnessTimer {
brightness = newBrightness

}else{
stop()
self.stop()
}
}

func stop() {
private func stop() {
timer?.invalidate()
timer = nil
}
Expand Down

0 comments on commit 3a454c1

Please sign in to comment.