istsos 0.1.3

A package to get data from istSOS and to check, calibrate and analyse them.
Documentation
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
// Copyright (C) 2021 IST-SUPSI (www.supsi.ch/ist)
// 
// Author: Daniele Strigaro
// 
// This file is part of istsos.
// 
// istsos is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// istsos is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with istsos.  If not, see <http://www.gnu.org/licenses/>.

use std::collections::HashMap;
use serde::Deserialize;
use serde::Serialize;
use serde;
use chrono::prelude::*;
use chrono::TimeZone;
use statrs::statistics::Distribution;
use statrs::statistics::Statistics;
use statrs::statistics::Data;
use chrono::prelude::*;

#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct BasicResult {
    pub success: bool,
    pub message: String
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct KeycloakToken {
    pub access_token: String,
    pub expires_in: i32,
    pub refresh_expires_in: i32,
    pub refresh_token: String,
    pub token_type: String,
    pub session_state: String,
    pub scope: String,
}

#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct Contact {
    pub administrativeArea: String,
    pub city: String,
    pub country: String,
    pub deliveryPoint: String,
    pub email: String,
    pub fax: String,
    pub individualName: String,
    pub organizationName: String,
    pub postalcode: String,
    pub role: String,
    pub voice: String,
    pub web: String
}

#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct Item {
    pub name: String,
    pub definition: String,
    pub value: String
}

#[derive(Debug, Serialize, Deserialize)]
pub struct GOparams {
    pub request: String,
    pub service: String,
    pub version: String,
    pub observedProperty: String,
    pub procedure: String,
    pub qualityIndex: String,
    pub responseFormat: String,
    pub offering: String,
    pub eventTime: String
}

#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct ItemSmall {
    pub name: String
}

#[derive(Clone, Debug, Serialize, Deserialize)]
struct NumCoords([f64; 3]);

#[derive(Clone, Debug, Serialize, Deserialize)]
struct StringCoords([String; 3]);

#[derive(Clone, Debug, Serialize, Deserialize)]
enum Coords {
    NumCoords,
    StringCoords,
}

#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct Point {
    pub r#type: String,
    pub coordinates: [f64; 3]
}

#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct Crs {
    pub r#type: String,
    pub properties: ItemSmall
}

#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct Location {
    pub r#type: String,
    pub geometry: Point,
    pub crs: Crs,
    pub properties: ItemSmall
} 

#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct Output {
    pub name: String,
    pub definition: String,
    pub uom: String,
    pub description: String,
    pub constraint: HashMap<String, StringVec>
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(untagged)]
pub enum StringVec {
    A(Vec<String>),
    B(String)
}

#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct SimpleOutput {
    pub name: String,
    pub definition: String,
    pub uom: String
}

#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct MinimalOutput {
    pub name: String,
    pub definition: String
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(untagged)]
pub enum OutputGen {
    A(Output),
    B(SimpleOutput),
    C(MinimalOutput),
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Procedure {
    pub system_id: String,
    pub assignedSensorId: String,
    pub system: String,
    pub description: String,
    pub keywords: String,
    pub identification: Vec<Item>,
    pub classification: Vec<Item>,
    pub characteristics: String,
    pub contacts: Vec<Contact>,
    pub documentation: Vec<String>,
    pub capabilities: Vec<String>,
    pub location: Location,
    pub interfaces: String,
    pub inputs: Vec<String>,
    pub outputs: Vec<Output>,
    pub history: Vec<String>,
    pub mqtt: String
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ProcedureItem {
    pub id: i32,
    pub name: String,
    pub description: String,
    pub assignedid: String,
    pub sensortype: String,
    pub samplingTime: SamplingTimeItem,
    pub observedproperties: Vec<SimpleOutput>,
    pub offerings: Vec<String>
}

// #[derive(Clone, Serialize, Deserialize, Debug)]
// pub enum OutputData {
//     Procedure
// }

#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct DataResult {
    pub success: bool,
    pub message: String,
    pub data: Procedure,
    pub total: i32
}

#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct DataResultGO {
    pub success: bool,
    pub message: String,
    pub data: Vec<ObservationType>,
    pub total: i32
}

#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct ObservationCollection {
    pub description: String,
    pub name: String,
    pub member: Vec<ObservationTypeGO>
}

#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct ResultGO {
    pub ObservationCollection: ObservationCollection,
}

#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct DataResultGetProcedures {
    pub success: bool,
    pub message: String,
    pub data: Vec<ProcedureItem>,
    pub total: i32
}

// INSERT OBSERVATION

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(untagged)]
pub enum ValueIO {
    V(f64),
    I(i64),
    DT(DateTime<FixedOffset>)
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(untagged)]
pub enum SamplingTimeType {
    V(SamplingTime),
    Empty {},
}

#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct SamplingTime {
    pub beginPosition: String,
    pub endPosition: String,
    pub duration: String
}

#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct SamplingTimeItem {
    pub beginposition: String,
    pub endposition: String
}

// #[derive(Clone, Debug, Serialize, Deserialize)]
// pub struct DataArrayElement {
//     pub elementCount: String,
//     pub field: Vec<Output>,
//     pub values: Vec<Vec<ValueIO>>
// }

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct DataArrayElement {
    pub elementCount: String,
    pub field: Vec<OutputGen>,
    pub values: Vec<Vec<ValueIO>>
}


#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct DataArrayElementGO {
    pub elementCount: String,
    pub field: Vec<OutputGen>,
    pub values: Vec<Vec<ValueIO>>
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Phenomenon {
    pub id: String,
    pub dimension: String,
    pub name: String
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ObservedProperty {
    pub CompositePhenomenon: Phenomenon,
    pub component: Vec<String>
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct FeatureOfInterest {
    pub name: String,
    pub geom: String
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ObservationType {
    pub name: String,
    pub samplingTime: SamplingTimeType,
    pub procedure: String,
    pub observedProperty: ObservedProperty,
    pub featureOfInterest: FeatureOfInterest,
    pub result: ResultElement
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ObservationTypeGO {
    pub name: String,
    pub samplingTime: SamplingTimeType,
    pub procedure: String,
    pub observedProperty: ObservedProperty,
    pub featureOfInterest: FeatureOfInterest,
    pub result: ResultElementGO
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ResultElement {
    pub DataArray: DataArrayElement
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ResultElementGO {
    pub DataArray: DataArrayElementGO
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct InsertObservation {
    pub AssignedSensorId: String,
    pub ForceInsert: String,
    pub Observation: ObservationType
}

// maintenance record
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct RecordMaintenance {
    pub begin: DateTime<FixedOffset>,
    pub end: DateTime<FixedOffset>
}

// maintenance record
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct RecordCalibration {
    pub begin: DateTime<FixedOffset>,
    pub end: DateTime<FixedOffset>,
    pub coeff: f64,
    pub procedure: String,
}

pub async fn get_token(oauth_params: &HashMap<&str, &str>, url: &str) -> Result<KeycloakToken, Box<dyn std::error::Error>> {
    let client_1 = reqwest::Client::new();
    let res = client_1
        .post(url)
        .header(
            reqwest::header::CONTENT_TYPE,
            "application/x-www-form-urlencoded"
        )
        .form(&oauth_params)
        .send()
        .await?;

    let t = res
        .text()
        .await?;
    let token: KeycloakToken = serde_json::from_str(&t).unwrap();
    return Ok(token)
}

pub async fn refresh_token(oauth_params: &HashMap<&str, &str>, url: &str) -> Result<KeycloakToken, Box<dyn std::error::Error>> {
    let client_1 = reqwest::Client::new();
    let res = client_1
        .post(url)
        .header(
            reqwest::header::CONTENT_TYPE,
            "application/x-www-form-urlencoded"
        )
        .form(&oauth_params)
        .send()
        .await?;

    let t = res
        .text()
        .await?;
    let token: KeycloakToken = serde_json::from_str(&t).unwrap();
    return Ok(token)
}

pub async fn describe_sensor(url: &str, auth: &str, name: &str) -> Result<Procedure, Box<dyn std::error::Error>> {
    let client_1 = reqwest::Client::new();
    let res = client_1
        .get(format!("{}/procedures/{}", url, name))
        .header(
            reqwest::header::AUTHORIZATION,
            auth
        )
        .send()
        .await?;
    let t = res
        .text()
        .await?;
    let proc: DataResult = serde_json::from_str(&t).unwrap();
    return Ok(proc.data)
}

pub async fn get_observation_last(
    url: &str,
    offering: &str,
    procedure: &str,
    auth: &str
) -> Result<Vec<ObservationType>, Box<dyn std::error::Error>> {
    let client_1 = reqwest::Client::new();
    // println!("{}", url);
    let res = client_1
        .get(
            format!(
                "{}/operations/getobservation/offerings/{}/procedures/{}/observedproperties/:/eventtime/last",
                url,
                offering,
                procedure
            )
        )
        .header(
            reqwest::header::AUTHORIZATION,
            auth
        )
        .header(
            reqwest::header::CONTENT_TYPE,
            "application/json"
        )
        .send()
        .await?;
    let t = res
        .text()
        .await?;
    let go: DataResultGO = serde_json::from_str(&t).unwrap();
    return Ok(go.data)
}

pub async fn get_observation(
    url: &str,
    service: &str,
    params: GOparams,
    auth: &str
) -> Result<ResultGO, Box<dyn std::error::Error>> {
    let client_1 = reqwest::Client::new();

    println!("{:?}", params);
    let res = client_1
        .get(
            format!(
                "{}/{}",
                url,
                service
            )
        )
        .header(
            reqwest::header::AUTHORIZATION,
            auth
        )
        .header(
            reqwest::header::CONTENT_TYPE,
            "application/json"
        )
        .query(&params)
        .send()
        .await?;
    let t = res
        .text()
        .await?;
    let go: ResultGO = serde_json::from_str(&t).unwrap();
    return Ok(go)
}

pub async fn get_string_observations(
    url: &str, auth: &str
) -> Result<String, Box<dyn std::error::Error>> {
    let client_1 = reqwest::Client::new();
    let res = client_1
        .get(url)
        .header(
            reqwest::header::AUTHORIZATION,
            auth
        )
        .send()
        .await?;
    let t = res
        .text()
        .await?;
    Ok(t)
}

pub async fn insert_observation(
        url: String,
        auth: String,
        body: InsertObservation
    ) -> Result<String, Box<dyn std::error::Error>> {
    let client_1 = reqwest::Client::new();
    let res = client_1
        .post(url)
        .header(
            reqwest::header::AUTHORIZATION,
            auth
        )
        .json(&body)
        .send()
        .await?;

    let t = res
        .text()
        .await?;
    return Ok(t.to_string())
}

pub async fn register_sensor(
        url: &str,
        auth: &str,
        body: Procedure
    ) -> Result<BasicResult, Box<dyn std::error::Error>> {
    let client_1 = reqwest::Client::new();
    let res = client_1
        .post(
            format!(
                "{}/procedures",
                url
            )
        )
        .header(
            reqwest::header::AUTHORIZATION,
            auth
        )
        .json(&body)
        .send()
        .await?;
    let t = res
        .text()
        .await?;
    let rs: BasicResult = serde_json::from_str(&t).unwrap();
    Ok(rs)
}

pub async fn fast_insert(
    url: String,
    user: &str,
    password: &str,
    body: String
) -> Result<String, Box<dyn std::error::Error>> {
    let client_1 = reqwest::Client::new();
    let res = client_1
        .post(url)
        .basic_auth(
            user,
            Some(password)
        )
        .body(body)
        .send()
        .await?;

    let t = res
        .text()
        .await?;
    return Ok(t.to_string())
}



pub async fn get_procedures(
    url: &str,
    auth: &str
) -> Result<Vec<ProcedureItem>, Box<dyn std::error::Error>> {
    let client = reqwest::Client::new();
    let res = client
        .get(format!("{}{}", url, "/procedures/operations/getlist"))
        .header(
            reqwest::header::AUTHORIZATION,
            auth
        )
        .send()
        .await?;

    let t = res
        .text()
        .await?;
    let go: DataResultGetProcedures = serde_json::from_str(&t).unwrap();
    Ok(go.data)
}

//
// Stats and Limnological functions
//

pub async fn linear_equation_find_y(xs: [f64; 2], ys: [f64; 2], x: f64) -> Result<f64, Box<dyn std::error::Error>> {
    let my =  ys[1]-ys[0];
    let mx =  xs[1]-xs[0];
    let mx2 = x-xs[0];
    let y = ((mx2/mx)*(my))+ys[0];
    Ok(y)
}

pub async fn oxygen_conc_saturation(
        temp: f64,
        sal: f64
    ) -> Result<f64, Box<dyn std::error::Error>> {
    let t = temp + 273.15;
    let c = -173.4292 + 
        (249.6339*(100.0/t)) +
        (143.3483*(t/100.0).ln())
        - 21.8492*(t/100.0)
        + (sal * (
            -0.033096 + (0.014259*(t/100.0))
            - (0.0017*(t/100.0).powf(2.0))
        ));
    let oxygen_sat = c.powf(1.423);
    Ok(oxygen_sat)
}

pub async fn oxygen_saturation_corrected(
        oxygen_saturation: f64,
        pressure: f64
    ) -> Result<f64, Box<dyn std::error::Error>> {
    let cf = ((pressure * 0.0987)-0.0112)/100.0;
    let os = oxygen_saturation * cf;
    Ok(os)
}

pub async fn step_test(x: Vec<f64>) -> Result<bool, Box<dyn std::error::Error>> {
    if x.len() == 3 {
        let sum_abs_val = (x[1] - x[0]).abs() + (x[1] - x[2]).abs();
        let s_data = Data::new(x);
        let four_std = 4.0_f64*s_data.std_dev().unwrap();
        if sum_abs_val <= four_std {
            Ok(true)
        } else {
            Ok(false)
        }
    } else {
        Ok(false)
    }
}