Skip to content

Commit

Permalink
Merge pull request nightscout#434 from nightscout/release/0.2.2
Browse files Browse the repository at this point in the history
Release v0.2.2
  • Loading branch information
Sjoerd-Bo3 authored Oct 19, 2024
2 parents 2e86678 + c8e66a6 commit d97ee5e
Show file tree
Hide file tree
Showing 17 changed files with 94 additions and 17 deletions.
2 changes: 1 addition & 1 deletion CGMBLEKit
2 changes: 1 addition & 1 deletion CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ representative at an online or offline event.
## Enforcement

Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported to the Discord server admins. Please join our [Discord server](https://discord.gg/dbe5Twav8D) to contact
reported to the Discord server admins. Please join our [Discord server](http://discord.diy-trio.org) to contact
them directly for any enforcement issues. All complaints will be reviewed and
investigated promptly and fairly.

Expand Down
2 changes: 1 addition & 1 deletion Config.xcconfig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
APP_DISPLAY_NAME = Trio
APP_VERSION = 0.2.1
APP_VERSION = 0.2.2
APP_BUILD_NUMBER = 1
COPYRIGHT_NOTICE =
DEVELOPER_TEAM = ##TEAM_ID##
Expand Down
12 changes: 11 additions & 1 deletion FreeAPS/Sources/APS/FetchGlucoseManager.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Combine
import Foundation
import HealthKit
import LoopKit
import LoopKitUI
import SwiftDate
Expand Down Expand Up @@ -98,6 +99,14 @@ final class BaseFetchGlucoseManager: FetchGlucoseManager, Injectable {
settingsManager.settings.uploadGlucose = cgmM.shouldSyncToRemoteService
}

private func updateManagerUnits(_ manager: CGMManagerUI?) {
let units = settingsManager.settings.units
let managerName = cgmManager.map { "\(type(of: $0))" } ?? "nil"
let loopkitUnits: HKUnit = units == .mgdL ? .milligramsPerDeciliter : .millimolesPerLiter
print("manager: \(managerName) is changing units to: \(loopkitUnits.description) ")
manager?.unitDidChange(to: loopkitUnits)
}

func updateGlucoseSource(cgmGlucoseSourceType: CGMType, cgmGlucosePluginId: String, newManager: CGMManagerUI?) {
// if changed, remove all calibrations
if self.cgmGlucoseSourceType != cgmGlucoseSourceType || self.cgmGlucosePluginId != cgmGlucosePluginId {
Expand All @@ -120,6 +129,8 @@ final class BaseFetchGlucoseManager: FetchGlucoseManager, Injectable {
removeCalibrations()
} else if self.cgmGlucoseSourceType == .plugin, cgmManager == nil, let rawCGMManager = rawCGMManager {
cgmManager = cgmManagerFromRawValue(rawCGMManager)
updateManagerUnits(cgmManager)

} else {
saveConfigManager()
}
Expand Down Expand Up @@ -151,7 +162,6 @@ final class BaseFetchGlucoseManager: FetchGlucoseManager, Injectable {
else {
return nil
}

return Manager.init(rawState: rawState)
}

Expand Down
60 changes: 57 additions & 3 deletions FreeAPS/Sources/APS/OpenAPS/JavaScriptWorker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,32 @@ import JavaScriptCore

private let contextLock = NSRecursiveLock()

extension String {
var lowercasingFirst: String { prefix(1).lowercased() + dropFirst() }
var uppercasingFirst: String { prefix(1).uppercased() + dropFirst() }
var camelCased: String {
guard !isEmpty else { return "" }
let parts = components(separatedBy: .alphanumerics.inverted)
let first = parts.first!.lowercasingFirst
let rest = parts.dropFirst().map(\.uppercasingFirst)
return ([first] + rest).joined()
}

var pascalCased: String {
guard !isEmpty else { return "" }
let parts = components(separatedBy: .alphanumerics.inverted)
let first = parts.first!.uppercasingFirst
let rest = parts.dropFirst().map(\.uppercasingFirst)
return ([first] + rest).joined()
}
}

final class JavaScriptWorker {
private let processQueue = DispatchQueue(label: "DispatchQueue.JavaScriptWorker")
private let virtualMachine: JSVirtualMachine
@SyncAccess(lock: contextLock) private var commonContext: JSContext? = nil
private var consoleLogs: [String] = []
private var logContext: String = ""

init() {
virtualMachine = processQueue.sync { JSVirtualMachine()! }
Expand All @@ -20,18 +42,49 @@ final class JavaScriptWorker {
}
}
let consoleLog: @convention(block) (String) -> Void = { message in
debug(.openAPS, "JavaScript log: \(message)")
let trimmedMessage = message.trimmingCharacters(in: .whitespacesAndNewlines)
if !trimmedMessage.isEmpty {
self.consoleLogs.append("\(trimmedMessage)")
}
}

context.setObject(
consoleLog,
forKeyedSubscript: "_consoleLog" as NSString
)
return context
}

// New method to flush aggregated logs
private func outputLogs() {
var outputLogs = consoleLogs.joined(separator: "\n").trimmingCharacters(in: .whitespacesAndNewlines)
consoleLogs.removeAll()

if outputLogs.isEmpty { return }

if logContext == "autosens.js" {
outputLogs = outputLogs.split(separator: "\n").map { logLine in
logLine.replacingOccurrences(
of: "^[-+=x!]|u\\(|\\)|\\d{1,2}h$",
with: "",
options: .regularExpression
)
}.joined(separator: "\n")
}

if !outputLogs.isEmpty {
outputLogs.split(separator: "\n").forEach { logLine in
if !"\(logLine)".trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
debug(.openAPS, "\(logContext): \(logLine)")
}
}
}
}

@discardableResult func evaluate(script: Script) -> JSValue! {
evaluate(string: script.body)
logContext = URL(fileURLWithPath: script.name).lastPathComponent
let result = evaluate(string: script.body)
outputLogs()
return result
}

private func evaluate(string: String) -> JSValue! {
Expand All @@ -52,6 +105,7 @@ final class JavaScriptWorker {
commonContext = createContext()
defer {
commonContext = nil
outputLogs()
}
return execute(self)
}
Expand Down
4 changes: 2 additions & 2 deletions FreeAPS/Sources/APS/OpenAPS/OpenAPS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -635,11 +635,11 @@ final class OpenAPS {

private func middlewareScript(name: String) -> Script? {
if let body = storage.retrieveRaw(name) {
return Script(name: "Middleware", body: body)
return Script(name: name, body: body)
}

if let url = Foundation.Bundle.main.url(forResource: "javascript/\(name)", withExtension: "") {
return Script(name: "Middleware", body: try! String(contentsOf: url))
return Script(name: name, body: try! String(contentsOf: url))
}

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ extension DataTable {
formatter.minimumFractionDigits = 0
formatter.maximumFractionDigits = 1
}
formatter.roundingMode = .down
formatter.roundingMode = .halfUp
return formatter
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ protocol PreferencesEditorProvider: Provider {
var preferences: Preferences { get }
func savePreferences(_ preferences: Preferences)
func migrateUnits()
func updateManagerUnits()
}

protocol PreferencesSettable: AnyObject {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import Foundation
import HealthKit

extension PreferencesEditor {
final class Provider: BaseProvider, PreferencesEditorProvider {
@Injected() private var settingsManager: SettingsManager!
@Injected() var fetchGlucoseManager: FetchGlucoseManager!
private let processQueue = DispatchQueue(label: "PreferencesEditorProvider.processQueue")

var preferences: Preferences {
Expand All @@ -17,6 +19,15 @@ extension PreferencesEditor {
}
}

func updateManagerUnits() {
var manager = fetchGlucoseManager.cgmManager
let managerName = manager.map { "\(type(of: $0))" } ?? "nil"
let units = settingsManager.settings.units
let loopkitUnits: HKUnit = units == .mgdL ? .milligramsPerDeciliter : .millimolesPerLiter
print("manager: \(managerName) is changing units to: \(loopkitUnits.description) ")
manager?.unitDidChange(to: loopkitUnits)
}

func migrateUnits() {
migrateTargets()
migrateISF()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ extension PreferencesEditor {
subscribeSetting(\.units, on: $unitsIndex.map { $0 == 0 ? GlucoseUnits.mgdL : .mmolL }) {
unitsIndex = $0 == .mgdL ? 0 : 1
} didSet: { [weak self] _ in
self?.provider.updateManagerUnits()
self?.provider.migrateUnits()
}

Expand Down
2 changes: 1 addition & 1 deletion G7SensorKit
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Instructions in greater detail, but not Trio-specific:

# Documentation

[Discord Trio - Server ](https://discord.gg/KepAG6RdYZ)
[Discord Trio - Server ](http://discord.diy-trio.org)

[Trio documentation](https://docs.diy-trio.org/en/latest/)

Expand Down
2 changes: 1 addition & 1 deletion TidepoolService

0 comments on commit d97ee5e

Please sign in to comment.