6579cd61dfc26bc772eaf43a23f90648f19a51da
[master-thesis.git] / Parasitemia / ParasitemiaUI / Utils.fs
1 module ParasitemiaUI.Utils
2
3 open System.IO
4
5 open Newtonsoft.Json
6 open Newtonsoft.Json.Converters
7
8 open Types
9
10 let listAsStr (s: 'a seq) =
11 s |> Seq.fold (fun acc obj -> acc + (if acc = "" then "" else ", ") + obj.ToString()) ""
12
13 let percentText (nbTotal: int, nb: int) : string =
14 if nbTotal = 0
15 then
16 ""
17 else
18 let percent = 100. * (float nb) / (float nbTotal)
19 sprintf "%.1f %% (%d / %d)" percent nb nbTotal
20
21 let roamingDir =
22 Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData), "Parasitemia")
23
24 let predefinedPPIFilename = "predefined-ppi.json"
25 let predefinedPPIFilepath = Path.Combine(roamingDir, predefinedPPIFilename)
26
27 let sensorSizesFilename = "sensor-sizes.json"
28 let sensorSizesFilepath = Path.Combine(roamingDir, sensorSizesFilename)
29
30 let private savePredefinedPPIToFile (predefinedPPI: PredefinedPPI list) =
31 try
32 use file = new StreamWriter(predefinedPPIFilepath)
33 file.Write(JsonConvert.SerializeObject(predefinedPPI, JsonSerializerSettings(Formatting = Formatting.Indented)))
34 with
35 ex ->
36 Logger.Log.Error("Unable to save predefined PPI to file \"{0}\": {1}", predefinedPPIFilepath, ex)
37
38 let private saveSensorSizesToFile (sensorSizes: SensorSize list) =
39 try
40 use file = new StreamWriter(sensorSizesFilepath)
41 file.Write(JsonConvert.SerializeObject(sensorSizes, JsonSerializerSettings(Formatting = Formatting.Indented)))
42 with
43 ex ->
44 Logger.Log.Error("Unable to save sensor sizes to file \"{0}\": {1}", sensorSizesFilepath, ex)
45
46 let predefinedPPI : PredefinedPPI list =
47 try
48 use file = new StreamReader(predefinedPPIFilepath)
49 JsonConvert.DeserializeObject<PredefinedPPI list>(file.ReadToEnd())
50 with
51 | ex ->
52 savePredefinedPPIToFile defaultPredefinedPPI
53 defaultPredefinedPPI
54
55 let sensorSizes : SensorSize list =
56 try
57 use file = new StreamReader(sensorSizesFilepath)
58 JsonConvert.DeserializeObject<SensorSize list>(file.ReadToEnd())
59 with
60 | ex ->
61 saveSensorSizesToFile defaultSensorSizes
62 defaultSensorSizes