update SIM x generare status
This commit is contained in:
+23
-15
@@ -1,4 +1,6 @@
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
using MP.MONO.Data.Controllers;
|
||||
using Newtonsoft.Json;
|
||||
using StackExchange.Redis;
|
||||
using System.Configuration;
|
||||
|
||||
@@ -6,6 +8,7 @@ string lineSep = "------------------------------------------------------";
|
||||
string redisConf = "127.0.0.1:6379";
|
||||
|
||||
Random rand = new Random();
|
||||
SimController simGen = new SimController();
|
||||
|
||||
Console.WriteLine(lineSep);
|
||||
Console.WriteLine($"Starting Machine SIM - redis server on {redisConf}!");
|
||||
@@ -17,19 +20,9 @@ Console.WriteLine("");
|
||||
//Create a connection
|
||||
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(redisConf);
|
||||
ISubscriber sub = redis.GetSubscriber();
|
||||
IDatabase? redisDb = redis.GetDatabase();
|
||||
|
||||
|
||||
|
||||
#if false
|
||||
// ciclo principale
|
||||
string input;
|
||||
do
|
||||
{
|
||||
input = Console.ReadLine();
|
||||
sub.Publish("allarmi", input);
|
||||
} while (input != "exit");
|
||||
#endif
|
||||
|
||||
// avvio tutti i thread...
|
||||
Thread threadStatus = new Thread(simStatus);
|
||||
Thread threadAlarms = new Thread(simAlarms);
|
||||
@@ -45,8 +38,11 @@ void simStatus()
|
||||
int maxPeriod = 3000;
|
||||
|
||||
do {
|
||||
var statusCode = rand.Next(1, 5);
|
||||
saveAndSendMessage("Machine:Status:Current", $"{statusCode} | Stato {statusCode:00}", "status", $"{statusCode}");
|
||||
// recupero uno stato simulato
|
||||
var newStatus = simGen.MachineGetStatus();
|
||||
string rawData = JsonConvert.SerializeObject(newStatus);
|
||||
saveAndSendMessage(mHash("Machine:Status:Current"), rawData, "status", rawData);
|
||||
|
||||
// attesa random
|
||||
Thread.Sleep(rand.Next(minPeriod, maxPeriod));
|
||||
} while (true);
|
||||
@@ -59,7 +55,7 @@ void simAlarms()
|
||||
do
|
||||
{
|
||||
var alarmCode = rand.Next(1, 100);
|
||||
saveAndSendMessage("Machine:Alarms:Current", $"{alarmCode:000} | Allarme PLC {alarmCode}", "allarmi", $"{alarmCode:000}");
|
||||
saveAndSendMessage(mHash("Machine:Alarms:Current"), $"{alarmCode:000} | Allarme PLC {alarmCode}", "allarms", $"{alarmCode:000}");
|
||||
// attesa random
|
||||
Thread.Sleep(rand.Next(minPeriod, maxPeriod));
|
||||
} while (true);
|
||||
@@ -69,9 +65,21 @@ void simAlarms()
|
||||
void saveAndSendMessage(string memKey, string value, string notifyChannel, string message)
|
||||
{
|
||||
// effettuo la scrittura nell'area di memoria indicata
|
||||
redis.GetDatabase().SetAdd(memKey, value);
|
||||
redisDb.StringSetAsync(memKey, value); ;
|
||||
//redis.GetDatabase().SetAdd(memKey, value);
|
||||
//redis.GetDatabase().StringSetAsync(memKey, value);
|
||||
|
||||
// invio notifica tramite il canale richiesto
|
||||
sub.Publish(notifyChannel, message);
|
||||
Console.WriteLine($"[{notifyChannel}] key: {memKey} | val: {value} | message: {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Hash Redis contenente i dati MP di una specifico TYPE (es StatusMacchina, StateMachineIngressi, ...)
|
||||
/// </summary>
|
||||
/// <param name="dataType"></param>
|
||||
/// <returns></returns>
|
||||
string mHash(string dataType)
|
||||
{
|
||||
return $"MAPO-MONO:{dataType}";
|
||||
}
|
||||
Reference in New Issue
Block a user