Function solar_api::overview

source ·
pub fn overview(api_key: &str, site_id: u32) -> Result<Overview, SolarApiError>
Expand description

Display the site overview data.

Examples found in repository?
examples/use_api.rs (line 48)
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
fn main() -> Result<(), Box<dyn Error>> {
    env_logger::init();

    let args: Vec<String> = env::args().collect();
    if args.len() < 3 {
        println!("usage: use_api <API_KEY> <SITE_ID>");
        return Ok(());
    }
    let api_key: &str = args[1].as_ref();
    let site_id: u32 = args[2].parse()?;
    println!("Accessing API using {api_key} for site {site_id}");

    println!("Getting information of all sites of customer");
    for site in list(api_key)? {
        println!("Id: {}\tName: {}", site.id, site.name);
    }

    println!("Getting information of site {site_id}");
    let site_details = details(api_key, site_id)?;
    println!(
        "Id = {}\tstatus: {}\t peak_power: {}",
        site_details.id,
        site_details.status,
        site_details
            .peak_power
            .into_format_args(kilowatt, uom::fmt::DisplayStyle::Description)
    );

    println!("Getting period of available data of site {site_id}");
    let data_period = data_period(api_key, site_id)?;
    println!(
        "Data available from {} until {}",
        data_period.start_date, data_period.end_date
    );

    println!("Getting overview of site {site_id}");
    let overview = overview(api_key, site_id)?;
    println!(
        "Site generated {:.2} and today {:.2}",
        overview
            .life_time_data
            .energy
            .into_format_args(megawatt_hour, DisplayStyle::Abbreviation),
        overview
            .current_power
            .power
            .into_format_args(kilowatt, DisplayStyle::Description)
    );

    println!("Getting energy generation of past day");
    let now = Local::now().naive_local();
    let period: DataPeriod = DataPeriod {
        start_date: now.date(),
        end_date: now.date(),
    };
    let time_unit = TimeUnit::Hour;
    let energy = energy(api_key, site_id, period, time_unit)?;
    for e in energy.values() {
        println!(
            "\t{} - {}",
            e.date,
            e.value
                .map(|v| format!(
                    "{}",
                    v.into_format_args(watt_hour, DisplayStyle::Abbreviation)
                ))
                .unwrap_or_else(|| "No value".to_string())
        );
    }
    println!("Getting power generation of past hour");
    let now = Local::now().naive_local();
    let power = power(api_key, site_id, now - Duration::hours(1), now)?;
    for e in power.values() {
        println!(
            "\t{} - {}",
            e.date,
            e.value
                .map(|v| format!("{}", v.into_format_args(watt, DisplayStyle::Description)))
                .unwrap_or_else(|| "No value".to_string())
        );
    }

    Ok(())
}