Skip to content

Commit

Permalink
fix: output unknown outputs as regular log lines
Browse files Browse the repository at this point in the history
  • Loading branch information
mkellerman committed Jul 4, 2024
1 parent 85bf02a commit ee481b6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 37 deletions.
18 changes: 12 additions & 6 deletions FreeAPS/Sources/APS/APSManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,14 @@ final class BaseAPSManager: APSManager, Injectable {
// Loop entry point
private func loop() {
// check the last start of looping is more the loopInterval but the previous loop was completed
if lastLoopDate > lastStartLoopDate {
guard lastStartLoopDate.addingTimeInterval(Config.loopInterval) < Date() else {
debug(.apsManager, "too close to do a loop : \(lastStartLoopDate)")
return
#if !targetEnvironment(simulator)
if lastLoopDate > lastStartLoopDate {
guard lastStartLoopDate.addingTimeInterval(Config.loopInterval) < Date() else {
debug(.apsManager, "too close to do a loop : \(lastStartLoopDate)")
return
}
}
}
#endif

guard !isLooping.value else {
warning(.apsManager, "Loop already in progress. Skip recommendation.")
Expand Down Expand Up @@ -321,14 +323,18 @@ final class BaseAPSManager: APSManager, Injectable {
}

private func autosens() -> AnyPublisher<Bool, Never> {
#if targetEnvironment(simulator)
return openAPS.autosense()
.map { $0 != nil }
.eraseToAnyPublisher()
#endif
guard let autosens = storage.retrieve(OpenAPS.Settings.autosense, as: Autosens.self),
(autosens.timestamp ?? .distantPast).addingTimeInterval(30.minutes.timeInterval) > Date()
else {
return openAPS.autosense()
.map { $0 != nil }
.eraseToAnyPublisher()
}

return Just(false).eraseToAnyPublisher()
}

Expand Down
56 changes: 25 additions & 31 deletions FreeAPS/Sources/APS/OpenAPS/JavaScriptWorker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ final class JavaScriptWorker {

var logOutput = ""
var jsonOutput = ""
if logFormatting == "Middleware" {

switch logFormatting {
case "Middleware":
jsonOutput += "{\n"
combinedLogs.replacingOccurrences(of: ";", with: ",")
.replacingOccurrences(of: "\\s?:\\s?,?", with: ": ", options: .regularExpression)
Expand All @@ -86,58 +88,50 @@ final class JavaScriptWorker {
jsonOutput += " "
logLine.split(separator: ",").forEach { logItem in
let keyPair = logItem.split(separator: ":")
if keyPair.count != 2 {
jsonOutput += "\"unknown\": \"\(logItem)\", "
} else {
if keyPair.count == 2 {
let key = keyPair[0].trimmingCharacters(in: .whitespacesAndNewlines).pascalCased
let value = keyPair[1].trimmingCharacters(in: .whitespacesAndNewlines)
jsonOutput += "\"\(key)\": \"\(value)\", "
} else {
logOutput += "\(logItem)\n"
}
}
jsonOutput += "\n"
}
jsonOutput += "}"
}
jsonOutput = jsonOutput.replacingOccurrences(of: "\\s+\\n+", with: "\n", options: .regularExpression)

if logFormatting == "prepare/autosens.js" {
case "prepare/autosens.js":
logOutput += combinedLogs.replacingOccurrences(
of: "((?:[\\=\\+\\-]\\n)+)?\\d+h\\n((?:[\\=\\+\\-]\\n)+)?",
with: "",
options: .regularExpression
)
// case "prepare/autotune-prep.js"
// case "prepare/autotune-core.js"
default:
debug(.openAPS, "JavaScript Format: \(logFormatting)")
logOutput = combinedLogs
}

if logFormatting == "prepare/autotune-prep.js" {
// print(combinedLogs)
}

if logFormatting == "prepare/autotune-core.js" {
// print(combinedLogs)
}

debug(.openAPS, "JavaScript Format: \(logFormatting)")

// Check if combinedLogs is a valid JSON string. If so, print it as JSON, if not, print it as a string
if let jsonData = "\(jsonOutput)".data(using: .utf8) {
do {
let jsonObject = try JSONSerialization.jsonObject(with: jsonData, options: [])
_ = try JSONSerialization.data(withJSONObject: jsonObject, options: .prettyPrinted)
debug(.openAPS, "JavaScript log: \(jsonOutput)")
return
} catch {
// debug(.openAPS, "JavaScript log: \(combinedLogs)")
if !jsonOutput.isEmpty {
if let jsonData = "\(jsonOutput)".data(using: .utf8) {
do {
let jsonObject = try JSONSerialization.jsonObject(with: jsonData, options: [])
_ = try JSONSerialization.data(withJSONObject: jsonObject, options: .prettyPrinted)
debug(.openAPS, "JavaScript log: \(jsonOutput)")
} catch {
logOutput = combinedLogs
}
}
}

if !logOutput.isEmpty {
logOutput.split(separator: "\n").forEach { logLine in
debug(.openAPS, "JavaScript log: \(logLine)")
if !"\(logLine)".trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
debug(.openAPS, "JavaScript log: \(logLine)")
}
}
return
}

combinedLogs.split(separator: "\n").forEach { logLine in
debug(.openAPS, "JavaScript log: \(logLine)")
}
}

Expand Down

0 comments on commit ee481b6

Please sign in to comment.