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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
extern crate serde;
extern crate serde_json;

use crate::annalisa::LoadableDevice as _;
use crate::samplic::LoadableDevice as _;
use crate::{
    file_load_json, CalibrationDescription, DeviceDescription,
    LoadPathPattern, LoadableDevice, Result, Sampling,
};
use indexmap::IndexSet;
use std::path::{Path, PathBuf};

#[derive(Debug)]
pub struct Device<P: AsRef<Path>> {
    pub system_path: P,
    pub config_path: P,
    pub data_path: P,
    pub product_id: String,
}

impl<P: AsRef<Path>> Device<P> {
    pub fn samplic(&self) -> crate::samplic::filesystem::Device<P> {
        crate::samplic::filesystem::Device::new(self)
    }

    pub fn ommui(&self) -> crate::ommui::filesystem::Device<P> {
        crate::ommui::filesystem::Device::new(self)
    }

    pub fn annalisa(&self) -> crate::annalisa::filesystem::Device<P> {
        crate::annalisa::filesystem::Device::new(self)
    }

    pub fn xio(&self) -> crate::xio::filesystem::Device<P> {
        crate::xio::filesystem::Device::new(self)
    }

    pub fn system_dir(&self) -> PathBuf {
        self.system_path.as_ref().to_path_buf()
    }

    pub fn config_dir(&self) -> PathBuf {
        self.config_path.as_ref().to_path_buf()
    }

    pub fn data_dir(&self) -> PathBuf {
        self.data_path.as_ref().to_path_buf()
    }

    pub fn samplic_dir(&self) -> PathBuf {
        self.system_dir().join("samplic")
    }

    pub fn description_dir(&self) -> PathBuf {
        self.system_dir().join("description")
    }

    pub fn description_calibrations_dir(&self) -> PathBuf {
        self.description_dir().join("calibration")
    }

    pub fn samplings_dir(&self) -> PathBuf {
        self.description_dir().join("sampling")
    }

    pub fn sampling_dir(&self, sampling_id: &str) -> PathBuf {
        self.samplings_dir().join(sampling_id)
    }

    pub fn description_calibration_dir(
        &self,
        calibration_id: &str,
    ) -> PathBuf {
        self.description_calibrations_dir().join(calibration_id)
    }

    pub fn device_dir(&self) -> PathBuf {
        self.description_dir().join("device")
    }
}

impl<P: AsRef<Path>> LoadableDevice for Device<P> {
    fn load_calibration_description_index(
        &self,
    ) -> Result<IndexSet<String>> {
        IndexSet::load_path_pattern_from_dir(
            self.description_calibrations_dir(),
            "*",
        )
    }

    fn load_calibration_description(
        &self,
        calibration_id: &str,
    ) -> Result<CalibrationDescription> {
        use crate::ommui::LoadableDevice;
        Ok(CalibrationDescription {
            ommui: self
                .ommui()
                .load_calibration_description(calibration_id)?,
        })
    }

    fn load_sampling_index(&self) -> Result<IndexSet<String>> {
        IndexSet::load_path_pattern_from_dir(self.samplings_dir(), "*")
    }

    fn load_sampling(&self, sampling_id: &str) -> Result<Sampling> {
        Ok(Sampling {
            samplic: self.samplic().load_sampling(sampling_id)?,
            annalisa: self.annalisa().load_sampling(sampling_id)?,
        })
    }

    fn load_device_description(&self) -> Result<DeviceDescription> {
        Ok(DeviceDescription {
            samplic: self.samplic().load_device_description()?,
        })
    }

    fn load_device(&self) -> Result<crate::Device> {
        Ok(crate::Device {
            description: self.load_device_description()?,
            calibrations: self.load_calibration_descriptions()?,
            system_profiles: self.samplic().load_system_profiles()?,
            user_profiles: self.samplic().load_user_profiles()?,
            samplings: self.load_samplings()?,
            icons: self.samplic().load_icons()?,
            units: self.samplic().load_units()?,
            translations: self.samplic().load_translations()?,
        })
    }
}

pub struct DeviceBuilder {
    override_system_path: Option<PathBuf>,
    override_base_config_path: Option<PathBuf>,
    override_base_data_path: Option<PathBuf>,
    product_id: String,
    company_id: String,
}

fn default_system_path_for_company_id(company_id: &str) -> PathBuf {
    Path::new("/").join("etc").join(company_id).join("device")
}

impl DeviceBuilder {
    pub fn new(product_id: &str, company_id: &str) -> Self {
        DeviceBuilder {
            override_system_path: None,
            override_base_config_path: None,
            override_base_data_path: None,
            product_id: product_id.to_string(),
            company_id: company_id.to_string(),
        }
    }

    pub fn base_system_path<P: AsRef<Path>>(mut self, p: P) -> Self {
        self.override_system_path = Some(p.as_ref().to_path_buf());
        self
    }

    pub fn base_config_path<P: AsRef<Path>>(mut self, p: P) -> Self {
        self.override_base_config_path = Some(p.as_ref().to_path_buf());
        self
    }

    pub fn base_data_path<P: AsRef<Path>>(mut self, p: P) -> Self {
        self.override_base_data_path = Some(p.as_ref().to_path_buf());
        self
    }

    pub fn build(self) -> Result<Device<PathBuf>> {
        let DeviceBuilder {
            override_system_path,
            override_base_config_path,
            override_base_data_path,
            product_id,
            company_id,
        } = self;

        let dirs =
            directories::ProjectDirs::from("at", &company_id, &product_id)
                .expect("Could not determine project directory");

        let system_path = override_system_path.unwrap_or_else(|| {
            default_system_path_for_company_id(&company_id)
        });

        let description =
            file_load_json::<crate::samplic::DeviceDescription, _>(
                system_path
                    .join("description")
                    .join("device")
                    .join("samplic")
                    .join("device.json"),
            )?;
        let device_id = description.uuid.to_hyphenated().to_string();
        let base_config_path = override_base_config_path
            .unwrap_or_else(|| dirs.config_dir().to_path_buf());
        let config_path = base_config_path.join(&device_id);

        let base_data_path = override_base_data_path
            .unwrap_or_else(|| dirs.data_dir().to_path_buf());
        let data_path = base_data_path.join(&device_id);

        Ok(Device {
            system_path,
            config_path,
            data_path,
            product_id,
        })
    }
}