@@ -11,23 +11,13 @@ import HealthKit
1111
1212@_cdecl ( " generatePrediction " ) // Use @_cdecl to expose the function with a C-compatible name
1313public func generatePrediction( jsonData: UnsafePointer < Int8 > ? ) -> UnsafeMutablePointer < Double > {
14- /// To generate a lib file, run:
15- /// swiftc -emit-library -o libMySwiftModule.dylib Sources/LoopAlgorithmToPython/LoopAlgorithmToPython.swift
16-
1714 // TODO: Add opportunity to get prediction effects from only one factor at a time
18- let decoder = JSONDecoder ( )
19- decoder. dateDecodingStrategy = . iso8601
20-
21- guard let jsonData = jsonData else {
22- fatalError ( " No JSON data provided " )
23- }
2415
25- // Convert JSON data to Data
26- let data = Data ( bytes: jsonData, count: strlen ( jsonData) )
16+ let data = getDataFromJson ( jsonData: jsonData)
2717
2818 do {
2919 // Decode JSON data
30- let input = try decoder . decode ( LoopPredictionInput . self, from: data)
20+ let input = try getDecoder ( ) . decode ( LoopPredictionInput . self, from: data)
3121
3222 let prediction = LoopAlgorithm . generatePrediction (
3323 start: input. glucoseHistory. last? . startDate ?? Date ( ) ,
@@ -55,24 +45,12 @@ public func generatePrediction(jsonData: UnsafePointer<Int8>?) -> UnsafeMutableP
5545 }
5646}
5747
58- @_cdecl ( " getPredictionDates " ) // Use @_cdecl to expose the function with a C-compatible name
48+ @_cdecl ( " getPredictionDates " )
5949public func getPredictionDates( jsonData: UnsafePointer < Int8 > ? ) -> UnsafePointer < CChar > {
60- /// To generate a lib file, run:
61- /// swiftc -emit-library -o libMySwiftModule.dylib Sources/LoopAlgorithmToPython/LoopAlgorithmToPython.swift
62-
63- let decoder = JSONDecoder ( )
64- decoder. dateDecodingStrategy = . iso8601
65-
66- guard let jsonData = jsonData else {
67- fatalError ( " No JSON data provided " )
68- }
69-
70- // Convert JSON data to Data
71- let data = Data ( bytes: jsonData, count: strlen ( jsonData) )
72-
50+ let data = getDataFromJson ( jsonData: jsonData)
51+
7352 do {
74- // Decode JSON data
75- let input = try decoder. decode ( LoopPredictionInput . self, from: data)
53+ let input = try getDecoder ( ) . decode ( LoopPredictionInput . self, from: data)
7654
7755 let prediction = LoopAlgorithm . generatePrediction (
7856 start: input. glucoseHistory. last? . startDate ?? Date ( ) ,
@@ -99,3 +77,47 @@ public func getPredictionDates(jsonData: UnsafePointer<Int8>?) -> UnsafePointer<
9977}
10078
10179
80+ @_cdecl ( " getActiveCarbs " )
81+ public func getActiveCarbs( jsonData: UnsafePointer < Int8 > ? ) -> Double {
82+ let data = getDataFromJson ( jsonData: jsonData)
83+
84+ do {
85+ let input = try getDecoder ( ) . decode ( AlgorithmInputFixture . self, from: data)
86+ let output = LoopAlgorithm . run ( input: input)
87+
88+ return output. activeCarbs!
89+ } catch {
90+ fatalError ( " Error reading or decoding JSON file: \( error) " )
91+ }
92+ }
93+
94+
95+ @_cdecl ( " getActiveInsulin " )
96+ public func getActiveInsulin( jsonData: UnsafePointer < Int8 > ? ) -> Double {
97+ let data = getDataFromJson ( jsonData: jsonData)
98+
99+ do {
100+ let input = try getDecoder ( ) . decode ( AlgorithmInputFixture . self, from: data)
101+ let output = LoopAlgorithm . run ( input: input)
102+
103+ return output. activeInsulin!
104+ } catch {
105+ fatalError ( " Error reading or decoding JSON file: \( error) " )
106+ }
107+ }
108+
109+ func getDataFromJson( jsonData: UnsafePointer < Int8 > ? ) -> Data {
110+ guard let jsonData = jsonData else {
111+ fatalError ( " No JSON data provided " )
112+ }
113+ // Convert JSON data to Data
114+ let data = Data ( bytes: jsonData, count: strlen ( jsonData) )
115+ return data
116+ }
117+
118+ func getDecoder( ) -> JSONDecoder {
119+ let decoder = JSONDecoder ( )
120+ decoder. dateDecodingStrategy = . iso8601
121+ return decoder
122+ }
123+
0 commit comments