Add commands for Broker, autosave and clean logging
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace PlantBox.Broker
|
namespace PlantBox.Broker
|
||||||
@@ -31,15 +33,19 @@ namespace PlantBox.Broker
|
|||||||
Task.Run(() => ClientManager.Start());
|
Task.Run(() => ClientManager.Start());
|
||||||
Task.Run(() => ServerManager.Start());
|
Task.Run(() => ServerManager.Start());
|
||||||
|
|
||||||
|
// Auto-Save
|
||||||
|
new Timer(OnSave, null, TimeSpan.FromMinutes(30), TimeSpan.FromMinutes(30));
|
||||||
|
|
||||||
string input;
|
string input;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
input = Console.ReadLine().ToLowerInvariant();
|
input = Console.ReadLine().ToLowerInvariant();
|
||||||
|
|
||||||
if (input == "save")
|
string[] split = input.Split(" ");
|
||||||
{
|
input = split[0];
|
||||||
PlantBoxesManager.Save();
|
string[] args = split.Skip(1).ToArray();
|
||||||
}
|
|
||||||
|
ExeCommand(input.ToLower(), args);
|
||||||
} while (input != "exit" && input != "stop" && input != "quit");
|
} while (input != "exit" && input != "stop" && input != "quit");
|
||||||
|
|
||||||
Console.WriteLine("Stopping Broker...");
|
Console.WriteLine("Stopping Broker...");
|
||||||
@@ -47,5 +53,38 @@ namespace PlantBox.Broker
|
|||||||
IsRunning = false;
|
IsRunning = false;
|
||||||
PlantBoxesManager.Save();
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -109,6 +109,8 @@ namespace PlantBox.Broker
|
|||||||
throw new InvalidOperationException($"This PlantBox (${id}) does not exist");
|
throw new InvalidOperationException($"This PlantBox (${id}) does not exist");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
plantBox.UpdateState();
|
||||||
|
|
||||||
var response = new InfoResponse
|
var response = new InfoResponse
|
||||||
(
|
(
|
||||||
plantBox.Name, plantBox.Type, plantBox.State,
|
plantBox.Name, plantBox.Type, plantBox.State,
|
||||||
|
|||||||
@@ -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<CaptorsValue> captorsValues)
|
private (double humidity, double luminosity, double temperature) GetAverage(IEnumerable<CaptorsValue> captorsValues)
|
||||||
{
|
{
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -42,10 +42,15 @@ namespace PlantBox.Broker
|
|||||||
{
|
{
|
||||||
State = PlantState.Warning;
|
State = PlantState.Warning;
|
||||||
}
|
}
|
||||||
if ((DateTime.Now - LastMeasureDate).TotalMinutes > 6)
|
if ((DateTime.Now - LastMeasureDate).TotalMinutes >= 7)
|
||||||
{
|
{
|
||||||
State = PlantState.Bad;
|
State = PlantState.Bad;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return $"{ID}:\n Name: {Name}\n Type: {Type}\n State: {State}";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace PlantBox.Broker
|
namespace PlantBox.Broker
|
||||||
{
|
{
|
||||||
class PlantBoxesManager
|
class PlantBoxesManager : IEnumerable<PlantBox>
|
||||||
{
|
{
|
||||||
public string FilePath => Path.Combine(Environment.CurrentDirectory, FileName);
|
public string FilePath => Path.Combine(Environment.CurrentDirectory, FileName);
|
||||||
public string FileName => "storage.json";
|
public string FileName => "storage.json";
|
||||||
@@ -70,5 +71,17 @@ namespace PlantBox.Broker
|
|||||||
|
|
||||||
Console.WriteLine("Storage saved");
|
Console.WriteLine("Storage saved");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void ClearPlantBox(PlantBox plantBox)
|
||||||
|
{
|
||||||
|
if (plantBox != null)
|
||||||
|
{
|
||||||
|
plantBox.HistoricManager.Clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerator<PlantBox> GetEnumerator() => _plantBoxes.Values.GetEnumerator();
|
||||||
|
|
||||||
|
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ namespace PlantBox.Broker
|
|||||||
|
|
||||||
protected void Log(string message)
|
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);
|
protected abstract void CaptorsCommand(CommandStream commandStream, CommandPacket packet);
|
||||||
|
|||||||
Reference in New Issue
Block a user