1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
use crate::ommui::{ CalibrationDescription, DeviceSettings, LoadableDevice, SampleCachedInformation, SamplingSettings, StorableDevice, }; use crate::{error, file_load_json as load_json, LoadPathPattern, Result}; use arnalisa::Calibration; use indexmap::{IndexMap, IndexSet}; use snafu::ResultExt; use std::path::PathBuf; use uuid::Uuid; #[derive(Debug)] pub struct Device { inner: crate::filesystem::Device, } impl Device { pub fn new(inner: crate::filesystem::Device) -> Self { Device { inner } } fn product_id(&self) -> String { self.inner.product_id.to_string() } pub fn device_dir(&self) -> PathBuf { self.inner.device_dir().join(self.product_id()) } pub fn config_dir(&self) -> PathBuf { self.inner.config_dir().join(self.product_id()) } pub fn cache_dir(&self) -> PathBuf { self.inner.cache_dir().join(self.product_id()) } pub fn system_profiles_file(&self) -> PathBuf { self.device_dir().join("profiles.json") } pub fn user_profiles_file(&self) -> PathBuf { self.config_dir().join("profiles.json") } pub fn device_settings_file(&self) -> PathBuf { self.config_dir().join("settings.json") } pub fn description_calibration_dir( &self, calibration_id: &str, ) -> PathBuf { self.inner .description_calibration_dir(calibration_id) .join(self.product_id()) } pub fn calibration_description_file( &self, calibration_id: &str, ) -> PathBuf { self.description_calibration_dir(calibration_id) .join("calibration.json") } pub fn sampling_dir(&self, sampling_id: &str) -> PathBuf { self.inner.sampling_dir(sampling_id).join(self.product_id()) } pub fn sampling_settings_file(&self, sampling_id: &str) -> PathBuf { self.sampling_dir(sampling_id).join("settings.json") } pub fn calibrations_dir(&self) -> PathBuf { self.config_dir().join("calibration") } pub fn calibration_dir(&self, calibration_id: &str) -> PathBuf { self.calibrations_dir().join(calibration_id) } pub fn calibration_file(&self, calibration_id: &str) -> PathBuf { self.calibration_dir(calibration_id) .join("calibration.json") } pub fn sample_cached_information_file(&self) -> PathBuf { self.cache_dir().join("sample_cache.json") } } impl LoadableDevice for Device { fn load_system_profile_index(&self) -> Result<IndexSet<Uuid>> { load_json(self.system_profiles_file()) } fn load_user_profile_index(&self) -> Result<IndexSet<Uuid>> { load_json(self.user_profiles_file()) } fn load_calibration_index(&self) -> Result<IndexSet<String>> { IndexSet::load_path_pattern_from_dir( self.calibrations_dir(), "*/calibration.json", ) } fn load_device_settings(&self) -> Result<DeviceSettings> { load_json(self.device_settings_file()) } fn load_calibration_description( &self, calibration_id: &str, ) -> Result<CalibrationDescription> { load_json(self.calibration_description_file(calibration_id)) } fn load_sampling_settings( &self, sampling_id: &str, ) -> Result<SamplingSettings> { load_json(self.sampling_settings_file(sampling_id)) } fn load_calibration( &self, calibration_id: &str, ) -> Result<Calibration> { load_json(self.calibration_file(calibration_id)) } fn load_sample_cached_information( &self, ) -> Result<IndexMap<Uuid, SampleCachedInformation>> { load_json(self.sample_cached_information_file()) } } impl StorableDevice for Device { fn store_calibration( &mut self, calibration_id: &str, calibration: &Calibration, ) -> Result<()> { std::fs::create_dir_all(self.calibration_dir(calibration_id)) .context(error::Io)?; let writer = std::fs::File::create(self.calibration_file(calibration_id)) .context(error::Io)?; serde_json::to_writer_pretty(writer, calibration) .context(error::Serde)?; Ok(()) } fn store_device_settings( &mut self, settings: &DeviceSettings, ) -> Result<()> { std::fs::create_dir_all(self.config_dir()).context(error::Io)?; let writer = std::fs::File::create(self.device_settings_file()) .context(error::Io)?; serde_json::to_writer_pretty(writer, settings) .context(error::Serde)?; Ok(()) } fn store_sample_cached_information( &self, cached_information: &IndexMap<Uuid, SampleCachedInformation>, ) -> Result<()> { std::fs::create_dir_all(self.cache_dir()).context(error::Io)?; let writer = std::fs::File::create(self.sample_cached_information_file()) .context(error::Io)?; serde_json::to_writer_pretty(writer, cached_information) .context(error::Serde)?; Ok(()) } }