use_api/
use_api.rs

1use chrono::{Duration, Local};
2use solar_api::{data_period, details, energy, list, overview, power, DataPeriod, TimeUnit};
3use std::{env, error::Error};
4use uom::{
5    fmt::DisplayStyle,
6    si::{
7        energy::{megawatt_hour, watt_hour},
8        power::{kilowatt, watt},
9    },
10};
11
12fn main() -> Result<(), Box<dyn Error>> {
13    env_logger::init();
14
15    let args: Vec<String> = env::args().collect();
16    if args.len() < 3 {
17        println!("usage: use_api <API_KEY> <SITE_ID>");
18        return Ok(());
19    }
20    let api_key: &str = args[1].as_ref();
21    let site_id: u32 = args[2].parse()?;
22    println!("Accessing API using {api_key} for site {site_id}");
23
24    println!("Getting information of all sites of customer");
25    for site in list(api_key)? {
26        println!("Id: {}\tName: {}", site.id, site.name);
27    }
28
29    println!("Getting information of site {site_id}");
30    let site_details = details(api_key, site_id)?;
31    println!(
32        "Id = {}\tstatus: {}\t peak_power: {}",
33        site_details.id,
34        site_details.status,
35        site_details
36            .peak_power
37            .into_format_args(kilowatt, uom::fmt::DisplayStyle::Description)
38    );
39
40    println!("Getting period of available data of site {site_id}");
41    let data_period = data_period(api_key, site_id)?;
42    println!(
43        "Data available from {} until {}",
44        data_period.start_date, data_period.end_date
45    );
46
47    println!("Getting overview of site {site_id}");
48    let overview = overview(api_key, site_id)?;
49    println!(
50        "Site generated {:.2} since installation and is currently generating {:.2}",
51        overview
52            .life_time_data
53            .energy
54            .into_format_args(megawatt_hour, DisplayStyle::Abbreviation),
55        overview
56            .current_power
57            .power
58            .into_format_args(watt, DisplayStyle::Description)
59    );
60
61    println!("Getting energy generation of past day");
62    let now = Local::now().naive_local();
63    let period: DataPeriod = DataPeriod {
64        start_date: now.date(),
65        end_date: now.date(),
66    };
67    let energy = energy(api_key, site_id, period, TimeUnit::Hour)?;
68    for e in energy.values() {
69        println!(
70            "\t{} - {}",
71            e.date,
72            e.value
73                .map(|v| format!(
74                    "{:7.2}",
75                    v.into_format_args(watt_hour, DisplayStyle::Abbreviation)
76                ))
77                .unwrap_or_else(|| "     No value".to_string())
78        );
79    }
80
81    println!("Getting power generation of past hour");
82    let now = Local::now().naive_local();
83    let power = power(api_key, site_id, now - Duration::hours(1), now)?;
84    for e in power.values() {
85        println!(
86            "\t{} - {}",
87            e.date,
88            e.value
89                .map(|v| format!("{:7.2}", v.into_format_args(watt, DisplayStyle::Description)))
90                .unwrap_or_else(|| "No value".to_string())
91        );
92    }
93
94    Ok(())
95}