Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. #[macro_use]
  2.  
  3. pub trait Report {
  4. fn report(&mut self, value: f64);
  5. fn field(&self) -> &'static str;
  6. }
  7.  
  8. struct Sampler {
  9. field: &'static str,
  10. value: f64,
  11. }
  12.  
  13. struct Aggregated {
  14. field: &'static str,
  15. values: Vec<f64>,
  16. }
  17.  
  18. impl Report for Sampler {
  19. fn report(&mut self, value: f64) {
  20. self.value = value;
  21. }
  22.  
  23. fn field(&self) -> &'static str {
  24. self.field
  25. }
  26. }
  27.  
  28. impl Report for Aggregated {
  29. fn report(&mut self, value: f64) {
  30. self.values.push(value);
  31. }
  32.  
  33. fn field(&self) -> &'static str {
  34. self.field
  35. }
  36. }
  37.  
  38. struct Client {
  39. server: &'static str,
  40. }
  41.  
  42. struct Telemetry {
  43. client: Client,
  44. battery_mv: Sampler,
  45. current_ma: Aggregated,
  46. }
  47.  
  48. impl Telemetry {
  49. pub fn new() -> Telemetry {
  50. let client = Client {
  51. server: "localhost:1337"
  52. };
  53. Telemetry {
  54. client: client,
  55. battery_mv: Sampler {field: "field_battery_mv", value: 0.0},
  56. current_ma: Aggregated {field: "field_current_ma", values: Vec::new()},
  57. }
  58. }
  59.  
  60. pub fn report(&mut self, field: &'static str, value: f64) {
  61. println!("{:?}:{:?}", field, value);
  62. //self.client.send(field, value);
  63. }
  64.  
  65. pub fn spin(&mut self) {
  66. // kuidas teha siia for loop, et ei peaks käsitsi neid siia lisama?
  67. self.report(self.battery_mv.field(), self.battery_mv.value)
  68. }
  69. }
  70.  
  71. fn main() {
  72. let mut telemetry = Telemetry::new();
  73. //telemetry.report("battery_mv", 17.00);
  74. telemetry.battery_mv.report(17.00);
  75. telemetry.current_ma.report(100.00);
  76. telemetry.spin();
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement