From d8c7a9e2c585eaaa5b04099dedd8857a3d79ae73 Mon Sep 17 00:00:00 2001 From: Eveldee Date: Sat, 27 Apr 2019 11:29:31 +0200 Subject: [PATCH] Add commands for Broker, autosave and clean logging --- PlantBox.Broker/Broker.cs | 47 +++++++++++++++++++++++++--- PlantBox.Broker/ClientManager.cs | 2 ++ PlantBox.Broker/HistoricManager.cs | 9 ++++++ PlantBox.Broker/PlantBox.cs | 7 ++++- PlantBox.Broker/PlantBoxesManager.cs | 15 ++++++++- PlantBox.Broker/TcpManager.cs | 2 +- 6 files changed, 75 insertions(+), 7 deletions(-) diff --git a/PlantBox.Broker/Broker.cs b/PlantBox.Broker/Broker.cs index e1db821..47daa76 100644 --- a/PlantBox.Broker/Broker.cs +++ b/PlantBox.Broker/Broker.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Text; +using System.Threading; using System.Threading.Tasks; namespace PlantBox.Broker @@ -31,15 +33,19 @@ namespace PlantBox.Broker Task.Run(() => ClientManager.Start()); Task.Run(() => ServerManager.Start()); + // Auto-Save + new Timer(OnSave, null, TimeSpan.FromMinutes(30), TimeSpan.FromMinutes(30)); + string input; do { input = Console.ReadLine().ToLowerInvariant(); - if (input == "save") - { - PlantBoxesManager.Save(); - } + string[] split = input.Split(" "); + input = split[0]; + string[] args = split.Skip(1).ToArray(); + + ExeCommand(input.ToLower(), args); } while (input != "exit" && input != "stop" && input != "quit"); Console.WriteLine("Stopping Broker..."); @@ -47,5 +53,38 @@ namespace PlantBox.Broker IsRunning = false; PlantBoxesManager.Save(); } + + private void OnSave(object state) + { + PlantBoxesManager.Save(); + } + + private void ExeCommand(string command, string[] arguments) + { + switch (command) + { + case "save": + PlantBoxesManager.Save(); + break; + case "clear": + Console.WriteLine($"Clearing {arguments[0]}..."); + if (ulong.TryParse(arguments[0], out ulong id) && PlantBoxesManager[id] != null) + { + PlantBoxesManager.ClearPlantBox(PlantBoxesManager[id]); + Console.WriteLine($"{id} cleared"); + } + else + { + Console.WriteLine("Invalid id"); + } + break; + case "list": + foreach (var plant in PlantBoxesManager) + { + Console.WriteLine(plant); + } + break; + } + } } } diff --git a/PlantBox.Broker/ClientManager.cs b/PlantBox.Broker/ClientManager.cs index 7b0cace..5ea2895 100644 --- a/PlantBox.Broker/ClientManager.cs +++ b/PlantBox.Broker/ClientManager.cs @@ -109,6 +109,8 @@ namespace PlantBox.Broker throw new InvalidOperationException($"This PlantBox (${id}) does not exist"); } + plantBox.UpdateState(); + var response = new InfoResponse ( plantBox.Name, plantBox.Type, plantBox.State, diff --git a/PlantBox.Broker/HistoricManager.cs b/PlantBox.Broker/HistoricManager.cs index 4c607b3..146309c 100644 --- a/PlantBox.Broker/HistoricManager.cs +++ b/PlantBox.Broker/HistoricManager.cs @@ -72,6 +72,15 @@ namespace PlantBox.Broker } } + public void Clear() + { + _minutesHistoric.Clear(); + _hoursHistoric.Clear(); + _daysHistoric.Clear(); + _weeksHistoric.Clear(); + _monthsHistoric.Clear(); + } + private (double humidity, double luminosity, double temperature) GetAverage(IEnumerable captorsValues) { return diff --git a/PlantBox.Broker/PlantBox.cs b/PlantBox.Broker/PlantBox.cs index a06a401..8ad64d7 100644 --- a/PlantBox.Broker/PlantBox.cs +++ b/PlantBox.Broker/PlantBox.cs @@ -42,10 +42,15 @@ namespace PlantBox.Broker { State = PlantState.Warning; } - if ((DateTime.Now - LastMeasureDate).TotalMinutes > 6) + if ((DateTime.Now - LastMeasureDate).TotalMinutes >= 7) { State = PlantState.Bad; } } + + public override string ToString() + { + return $"{ID}:\n Name: {Name}\n Type: {Type}\n State: {State}"; + } } } diff --git a/PlantBox.Broker/PlantBoxesManager.cs b/PlantBox.Broker/PlantBoxesManager.cs index 70d9c55..bdab415 100644 --- a/PlantBox.Broker/PlantBoxesManager.cs +++ b/PlantBox.Broker/PlantBoxesManager.cs @@ -1,12 +1,13 @@ using Newtonsoft.Json; using System; +using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; namespace PlantBox.Broker { - class PlantBoxesManager + class PlantBoxesManager : IEnumerable { public string FilePath => Path.Combine(Environment.CurrentDirectory, FileName); public string FileName => "storage.json"; @@ -70,5 +71,17 @@ namespace PlantBox.Broker Console.WriteLine("Storage saved"); } + + public void ClearPlantBox(PlantBox plantBox) + { + if (plantBox != null) + { + plantBox.HistoricManager.Clear(); + } + } + + public IEnumerator GetEnumerator() => _plantBoxes.Values.GetEnumerator(); + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } } diff --git a/PlantBox.Broker/TcpManager.cs b/PlantBox.Broker/TcpManager.cs index 360995d..c43c46c 100644 --- a/PlantBox.Broker/TcpManager.cs +++ b/PlantBox.Broker/TcpManager.cs @@ -87,7 +87,7 @@ namespace PlantBox.Broker protected void Log(string message) { - Console.WriteLine($"[{LogPrefix}] {message}"); + Console.WriteLine($"[{LogPrefix}]({DateTime.Now:HH:mm:ss}) {message}"); } protected abstract void CaptorsCommand(CommandStream commandStream, CommandPacket packet);