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
pub mod cloud;
pub mod db;
pub mod i2c;
pub mod modbus;
pub mod net;
pub mod serial;

use std::sync::Arc;

use crate::*;

pub(crate) trait Service {
  fn new(config: config::Values) -> Self;
}

#[derive(Debug)]
struct Values {
  db: db::Service,
  cloud: cloud::Service,
  modbus: modbus::Service,
  net: net::Service,
  i2c: i2c::Service,
  serial: serial::Service,
}

#[derive(Debug, Clone)]
pub(crate) struct Container {
  values: Arc<Values>,
}

impl Container {
  pub(crate) fn new(config: config::Values) -> Self {
    Self {
      values: Arc::new(Values {
        db: db::Service::new(config.clone()),
        cloud: cloud::Service::new(config.clone()),
        modbus: modbus::Service::new(config.clone()),
        net: net::Service::new(config.clone()),
        i2c: i2c::Service::new(config.clone()),
        serial: serial::Service::new(config.clone()),
      }),
    }
  }

  #[inline]
  pub(crate) fn db(&self) -> &db::Service {
    &self.values.db
  }

  #[inline]
  pub(crate) fn cloud(&self) -> &cloud::Service {
    &self.values.cloud
  }

  #[inline]
  pub(crate) fn modbus(&self) -> &modbus::Service {
    &self.values.modbus
  }

  #[inline]
  pub(crate) fn net(&self) -> &net::Service {
    &self.values.net
  }

  #[inline]
  pub(crate) fn i2c(&self) -> &i2c::Service {
    &self.values.i2c
  }

  #[inline]
  pub(crate) fn serial(&self) -> &serial::Service {
    &self.values.serial
  }
}