Add commands for Broker, autosave and clean logging

This commit is contained in:
2019-04-27 11:29:31 +02:00
parent f67d070fc3
commit d8c7a9e2c5
6 changed files with 75 additions and 7 deletions

View File

@@ -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;
}
}
}
}