mirror of
https://github.com/home-assistant/iOS.git
synced 2026-02-19 17:31:56 -06:00
<!-- Thank you for submitting a Pull Request and helping to improve Home Assistant. Please complete the following sections to help the processing and review of your changes. Please do not delete anything from this template. --> ## Summary <!-- Provide a brief summary of the changes you have made and most importantly what they aim to achieve --> ## Screenshots <!-- If this is a user-facing change not in the frontend, please include screenshots in light and dark mode. --> ## Link to pull request in Documentation repository <!-- Pull requests that add, change or remove functionality must have a corresponding pull request in the Companion App Documentation repository (https://github.com/home-assistant/companion.home-assistant). Please add the number of this pull request after the "#" --> Documentation: home-assistant/companion.home-assistant# ## Any other notes <!-- If there is any other information of note, like if this Pull Request is part of a bigger change, please include it here. -->
74 lines
2.1 KiB
Swift
74 lines
2.1 KiB
Swift
import Foundation
|
|
import RealmSwift
|
|
|
|
/// Contains data about an event that occurred on the client, used for logging.
|
|
public class ClientEvent: Object {
|
|
/// The type of event being logged.
|
|
public enum EventType: String {
|
|
case notification
|
|
case serviceCall
|
|
case locationUpdate
|
|
case networkRequest
|
|
case settings
|
|
case unknown
|
|
}
|
|
|
|
public convenience init(text: String, type: EventType, payload: [String: Any]? = nil) {
|
|
self.init()
|
|
self.text = text
|
|
self.type = type
|
|
self.jsonPayload = payload
|
|
}
|
|
|
|
/// The date the event occurred.
|
|
@objc public dynamic var date: Date = Current.date()
|
|
|
|
/// The text describing the event.
|
|
@objc public dynamic var text: String = ""
|
|
@objc private dynamic var typeString: String = EventType.unknown.rawValue
|
|
|
|
/// The even type
|
|
public var type: EventType {
|
|
get { EventType(rawValue: typeString) ?? .unknown }
|
|
set { typeString = newValue.rawValue }
|
|
}
|
|
|
|
@objc private dynamic var jsonData: Data?
|
|
|
|
/// The payload for the event.
|
|
public var jsonPayload: [String: Any]? {
|
|
get {
|
|
guard let payloadData = jsonData,
|
|
let jsonObject = try? JSONSerialization.jsonObject(with: payloadData),
|
|
let dictionary = jsonObject as? [String: Any] else {
|
|
return nil
|
|
}
|
|
|
|
return dictionary
|
|
}
|
|
|
|
set {
|
|
guard let payload = newValue else {
|
|
jsonData = nil
|
|
return
|
|
}
|
|
|
|
do {
|
|
let writeOptions: JSONSerialization.WritingOptions = [.prettyPrinted, .withoutEscapingSlashes]
|
|
|
|
jsonData = try JSONSerialization.data(withJSONObject: payload, options: writeOptions)
|
|
} catch {
|
|
Current.Log.error("Error serializing json payload: \(error)")
|
|
}
|
|
}
|
|
}
|
|
|
|
public var jsonPayloadDescription: String? {
|
|
jsonData.flatMap { String(data: $0, encoding: .utf8) }
|
|
}
|
|
|
|
override public static func indexedProperties() -> [String] {
|
|
["date", "typeString"]
|
|
}
|
|
}
|