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
//! Module providing network connections sensor functionality.

use super::{FromSensorTemplate, SensorMetadata, SensorTemplate, SensorTemplateError, Sensors};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq, Eq)]
pub struct NetworkConnectionsSensor {
    #[serde(flatten)]
    pub metadata: SensorMetadata,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub machines: Option<Vec<NetworkConnectionMachine>>,
    #[serde(skip_serializing_if = "Option::is_none", rename = "type")]
    pub kind: Option<NetworkConnectionKind>,
    pub value: u64,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum NetworkConnectionKind {
    Wifi,
    Cable,
    Spacenet,
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq, Eq)]
pub struct NetworkConnectionMachine {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    pub mac: String,
}

#[derive(Debug, Clone)]
pub struct NetworkConnectionsSensorTemplate {
    pub metadata: SensorMetadata,
    pub kind: Option<NetworkConnectionKind>,
}

impl FromSensorTemplate<NetworkConnectionsSensorTemplate> for NetworkConnectionsSensor {
    fn try_from_template(
        template: &NetworkConnectionsSensorTemplate,
        value: &str,
    ) -> Result<Self, SensorTemplateError> {
        Ok(Self {
            metadata: template.metadata.clone(),
            kind: template.kind.clone(),
            value: value.parse()?,
            ..Default::default()
        })
    }
}

impl SensorTemplate for NetworkConnectionsSensorTemplate {
    fn try_to_sensor(&self, value_str: &str, sensors: &mut Sensors) -> Result<(), SensorTemplateError> {
        sensors
            .network_connections
            .push(NetworkConnectionsSensor::try_from_template(self, value_str)?);
        Ok(())
    }
}