Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix NightscoutAPI fetchLastGlucose #775

Merged
merged 3 commits into from
Jul 26, 2024

Conversation

bastiaanv
Copy link
Contributor

@bastiaanv bastiaanv commented Jul 21, 2024

Currently, if you want to backfill via Nightscout or use Nightscout as a CGM, you will get the following warning:
Glucose fetching error: The data couldn’t be read because it isn’t in the correct format.

This is due to the property sgv being marked as Int but Nightscout returns it as a double/decimal.
This PR fixes this issue by first trying to parse the property as an Int and then try to parse as Double (with casting to Int) and only after that throw an error.

Debugged error thrown by swift:
dataCorrupted - Context(codingPath: [], debugDescription: \"The given data was not valid JSON.\", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 \"Number 241.2 is not representable in Swift.\" UserInfo={NSDebugDescription=Number 241.2 is not representable in Swift.}))

Nightscout API response (partial):

[
  {
    "_id": "669d5a7a8de7c615a2661098",
    "device": "MiaomiaoClient",
    "date": 1721588346033.709,
    "type": "sgv",
    "direction": "Flat",
    "trend": 4,
    "isCalibration": false,
    "sgv": 241.2,
    "dateString": "2024-07-21T18:59:06.000Z",
    "utcOffset": 0,
    "sysTime": "2024-07-21T18:59:06.000Z"
  },
]

Code to debug the error:

return service.run(request)
            .retry(Config.retryCount)
            .decode(type: [BloodGlucose].self, decoder: JSONCoding.decoder)
            .catch { error -> AnyPublisher<[BloodGlucose], Swift.Error> in
                warning(.nightscout, "Glucose fetching error: \(error.localizedDescription)")
                var str = ""

                switch error {
                case let DecodingError.keyNotFound(key, context):
                    str = "keyNotFound - key: \(key)"
                case let DecodingError.dataCorrupted(context):
                    str = "dataCorrupted - \(context)"
                case let DecodingError.valueNotFound(value, context):
                    str = "valueNotFound - value: \(value)"
                case let DecodingError.typeMismatch(type, context):
                    str = "typeMismatch - type: \(type)"
                default:
                    str = "UNKNWON"
                }
                return Just([]).setFailureType(to: Swift.Error.self).eraseToAnyPublisher()
            }
            .map { glucose in
                glucose
                    .map {
                        var reading = $0
                        reading.glucose = $0.sgv
                        return reading
                    }
            }
            .eraseToAnyPublisher()

close #605

@Jon-b-m Jon-b-m merged commit f9c3ddd into Artificial-Pancreas:dev Jul 26, 2024
@Jon-b-m Jon-b-m mentioned this pull request Aug 1, 2024
Jon-b-m added a commit that referenced this pull request Aug 1, 2024
Omnipod updates ported by @itsmojo:
LoopKit/OmniBLE#125 & LoopKit/OmniKit#36, fix bolusState & basalDeliveryState use !isFinished() again
LoopKit/OmniKit#36, update OmniKit setStateWithResult() to match OmniBLE version
LoopKit/OmniBLE#126 & LoopKit/OmniKit#37, fix incorrect pod suspended message on bolus after an error
Remove no longer needed APSManager updateStatus() to avoid extra get pod status commands

Nightscout Glucose backfill resolved:
#775

Resolves several old issues with import/upload to Nightscout and to the statistics database.
Prepare for the configuration profiles and the onboarding Views coming in v4.9, for instance backup of your CoreData.
f0d1d52

Make Oref0 error string in pop-up more accessible: 41d2b44

Localization of the Sharing strings, by @Mirko-tri

Crowdin translations from translators: Hung Nguyen Phuteleco, @Mirko-tri, Abdulrahman Alfantokh, @vanzaam, Mykola Yroslavadudko and Typ1er.
@rhys-jones rhys-jones mentioned this pull request Aug 3, 2024
@TrachYuri TrachYuri mentioned this pull request Aug 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants