Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #[macro_use]
- pub trait Report {
- fn report(&mut self, value: f64);
- fn field(&self) -> &'static str;
- }
- struct Sampler {
- field: &'static str,
- value: f64,
- }
- struct Aggregated {
- field: &'static str,
- values: Vec<f64>,
- }
- impl Report for Sampler {
- fn report(&mut self, value: f64) {
- self.value = value;
- }
- fn field(&self) -> &'static str {
- self.field
- }
- }
- impl Report for Aggregated {
- fn report(&mut self, value: f64) {
- self.values.push(value);
- }
- fn field(&self) -> &'static str {
- self.field
- }
- }
- struct Client {
- server: &'static str,
- }
- struct Telemetry {
- client: Client,
- battery_mv: Sampler,
- current_ma: Aggregated,
- }
- impl Telemetry {
- pub fn new() -> Telemetry {
- let client = Client {
- server: "localhost:1337"
- };
- Telemetry {
- client: client,
- battery_mv: Sampler {field: "field_battery_mv", value: 0.0},
- current_ma: Aggregated {field: "field_current_ma", values: Vec::new()},
- }
- }
- pub fn report(&mut self, field: &'static str, value: f64) {
- println!("{:?}:{:?}", field, value);
- //self.client.send(field, value);
- }
- pub fn spin(&mut self) {
- // kuidas teha siia for loop, et ei peaks käsitsi neid siia lisama?
- self.report(self.battery_mv.field(), self.battery_mv.value)
- }
- }
- fn main() {
- let mut telemetry = Telemetry::new();
- //telemetry.report("battery_mv", 17.00);
- telemetry.battery_mv.report(17.00);
- telemetry.current_ma.report(100.00);
- telemetry.spin();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement