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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#[macro_use]
extern crate serde_derive;

#[cfg(test)]
#[macro_use]
extern crate serde_json;

extern crate ommui_string_patterns;
extern crate xio_base_datatypes;

mod extract_captions;

pub use extract_captions::{CommandCaption, ExtractCaptions};
use ommui_string_patterns::freetextstring;
use std::collections::BTreeMap;
use xio_base_datatypes as base;

#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
/// A complete job set.
pub struct JobSet {
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub parameters: Vec<ParameterSet>,
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub jobs: BTreeMap<String, Job>,
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub channels: BTreeMap<String, ChannelAssignment>,
}

#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ParameterSet {
    pub name: String,
    pub descriptions: BTreeMap<String, ParameterDescription>,
    pub values: BTreeMap<String, base::DataValueDescriptive>,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ParameterDescription {
    #[serde(rename = "_caption")]
    pub caption: String,
    #[serde(rename = "type")]
    pub data_type: base::DataType,
    pub storage: base::StorageType,
    #[serde(rename = "override")]
    pub override_option: base::OverrideOption,
}

#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Job {
    pub commands: Vec<Command>,
}

#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Command {
    #[serde(rename = "_caption", with = "freetextstring")]
    pub caption: String,

    #[serde(rename = "type")]
    pub command_type: String,

    // TODO: verify that the key is an idstring
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub parameters: BTreeMap<String, String>,

    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub conditions: Vec<Condition>,
}

#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Condition {
    #[serde(rename = "_caption", with = "freetextstring")]
    pub caption: String,

    #[serde(rename = "type")]
    pub command_type: String,

    pub exit_job: bool,

    // TODO: verify that the key is an idstring
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub parameters: BTreeMap<String, String>,
}

#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ChannelAssignment {
    pub capability: String,
    pub channel: String,
}

#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
/// The assignment of variables mapped to a hardware board
pub struct HardwareAssignment {
    pub hardware_board: String,
    pub mapping: BTreeMap<String, ChannelAssignment>,
}

#[cfg(test)]
mod tests {
    use base::{DataType, DataValue, DataValueDescriptive, OverrideOption,
               StorageType};
    use std::collections::BTreeSet;
    use {serde_json, BTreeMap, ChannelAssignment, Command, Condition,
         Job, JobSet, ParameterDescription, ParameterSet};

    #[test]
    fn example_jobset() {
        let json = json!({
            "channels":{
                "a1":{
                    "capability":"motor",
                    "channel":"speed"
                },
                "a3":{
                    "capability":"adc",
                    "channel":"voltage"
                }
            },
            "jobs":{
                "init":{
                    "commands":[
                        {
                            "_caption":"init job command 1",
                            "conditions":[
                            {
                                "_caption":"init job cmd 1 condition 1",
                                "exitJob":true,
                                "parameters":{"a":"a1","b":"b1"},
                                "type":"equals"
                            }
                            ],
                            "parameters":{"a":"b1","b":"a1"},
                            "type":"wait"
                        },
                        {
                            "_caption":"init job command 2",
                            "parameters":{"a":"b1"},
                            "type":"wait"
                        }
                    ]
                },
                "measurement":{
                    "commands": [
                        {
                            "_caption":"measurement job command 1",
                            "type":"wait"
                        },
                        {
                            "_caption":"measurement job command 2",
                            "type":"wait"
                        }
                    ]
                }
            },
            "parameters":[
                {
                    "descriptions":{
                        "a1":{
                            "_caption":"Parameter A 1",
                            "override":"forbid",
                            "storage":"channel",
                            "type":"uint8"
                        },
                        "a2":{
                            "_caption":"Parameter A 2",
                            "override":"enforce",
                            "storage":"fixed",
                            "type":"parametermask"
                        },
                        "a3":{
                            "_caption":"Parameter A 3",
                            "override":"allow",
                            "storage":"channel",
                            "type":"boolean"
                        }
                    },
                    "name":"base",
                    "values":{"a1":3,"a2":["a3","b1"]}
                },
                {
                    "descriptions":{
                        "b1":{
                            "_caption":"Parameter B 1",
                            "override":"forbid",
                            "storage":"channel",
                            "type":"uint8"
                        }
                    },
                    "name":"overlay",
                    "values":{"b1":14}
                }
            ]
        });
        let parameterset1 = ParameterSet {
            name: "base".to_string(),
            descriptions: vec![
                (
                    "a1".to_string(),
                    ParameterDescription {
                        caption: "Parameter A 1".to_string(),
                        data_type: DataType::UInt8,
                        storage: StorageType::Channel,
                        override_option: OverrideOption::Forbid,
                    },
                ),
                (
                    "a2".to_string(),
                    ParameterDescription {
                        caption: "Parameter A 2".to_string(),
                        data_type: DataType::ParameterMask,
                        storage: StorageType::Fixed,
                        override_option: OverrideOption::Enforce,
                    },
                ),
                (
                    "a3".to_string(),
                    ParameterDescription {
                        caption: "Parameter A 3".to_string(),
                        data_type: DataType::Boolean,
                        storage: StorageType::Channel,
                        override_option: OverrideOption::Allow,
                    },
                ),
            ].into_iter()
                .collect::<BTreeMap<String, ParameterDescription>>(),
            values: vec![
                ("a1".to_string(), DataValue::UInt8(3u8)),
                (
                    "a2".to_string(),
                    DataValue::ParameterMask(
                        vec!["a3".to_string(), "b1".to_string()]
                            .into_iter()
                            .collect::<BTreeSet<String>>(),
                    ),
                ),
            ].into_iter()
                .collect::<BTreeMap<String, DataValueDescriptive>>(),
        };
        let parameterset2 = ParameterSet {
            name: "overlay".to_string(),
            descriptions: vec![(
                "b1".to_string(),
                ParameterDescription {
                    caption: "Parameter B 1".to_string(),
                    data_type: DataType::UInt8,
                    storage: StorageType::Channel,
                    override_option: OverrideOption::Forbid,
                },
            )].into_iter()
                .collect::<BTreeMap<String, ParameterDescription>>(),
            values: vec![("b1".to_string(), DataValue::UInt8(14u8))]
                .into_iter()
                .collect::<BTreeMap<String, DataValueDescriptive>>(),
        };
        let job1 = Job {
            commands: vec![
                Command {
                    caption: "init job command 1".to_string(),
                    conditions: vec![Condition {
                        caption: "init job cmd 1 condition 1".to_string(),
                        exit_job: true,
                        parameters: vec![("a", "a1"), ("b", "b1")]
                            .into_iter()
                            .map(|(k, v)| (k.to_string(), v.to_string()))
                            .collect(),
                        command_type: "equals".to_string(),
                    }],
                    command_type: "wait".to_string(),
                    parameters: vec![("a", "b1"), ("b", "a1")]
                        .into_iter()
                        .map(|(k, v)| (k.to_string(), v.to_string()))
                        .collect(),
                },
                Command {
                    caption: "init job command 2".to_string(),
                    conditions: Vec::new(),
                    command_type: "wait".to_string(),
                    parameters: vec![("a", "b1")]
                        .into_iter()
                        .map(|(k, v)| (k.to_string(), v.to_string()))
                        .collect(),
                },
            ],
        };
        let job2 = Job {
            commands: vec![
                Command {
                    caption: "measurement job command 1".to_string(),
                    conditions: Vec::new(),
                    command_type: "wait".to_string(),
                    parameters: BTreeMap::new(),
                },
                Command {
                    caption: "measurement job command 2".to_string(),
                    conditions: Vec::new(),
                    command_type: "wait".to_string(),
                    parameters: BTreeMap::new(),
                },
            ],
        };
        let jobset = JobSet {
            parameters: vec![parameterset1, parameterset2],
            jobs: vec![
                ("init".to_string(), job1),
                ("measurement".to_string(), job2),
            ].into_iter()
                .collect(),
            channels: vec![
                (
                    "a1".to_string(),
                    ChannelAssignment {
                        capability: "motor".to_string(),
                        channel: "speed".to_string(),
                    },
                ),
                (
                    "a3".to_string(),
                    ChannelAssignment {
                        capability: "adc".to_string(),
                        channel: "voltage".to_string(),
                    },
                ),
            ].into_iter()
                .collect(),
        };
        let deserialized = serde_json::from_value(json.clone()).unwrap();

        assert_eq!(jobset, deserialized);

        let serialized = serde_json::to_value(&jobset).unwrap();
        assert_eq!(serialized, json);
    }
}