85 lines
2.4 KiB
C#
85 lines
2.4 KiB
C#
// See https://aka.ms/new-console-template for more information
|
|
using MP.MONO.Data.Controllers;
|
|
using Newtonsoft.Json;
|
|
using StackExchange.Redis;
|
|
using System.Configuration;
|
|
|
|
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}!");
|
|
Console.WriteLine(lineSep);
|
|
Console.WriteLine("");
|
|
Console.WriteLine("Running - press CTRL-C to stop SIM");
|
|
Console.WriteLine("");
|
|
|
|
//Create a connection
|
|
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(redisConf);
|
|
ISubscriber sub = redis.GetSubscriber();
|
|
IDatabase? redisDb = redis.GetDatabase();
|
|
|
|
|
|
// avvio tutti i thread...
|
|
Thread threadStatus = new Thread(simStatus);
|
|
Thread threadAlarms = new Thread(simAlarms);
|
|
|
|
threadStatus.Start();
|
|
threadAlarms.Start();
|
|
|
|
|
|
|
|
void simStatus()
|
|
{
|
|
int minPeriod = 2000;
|
|
int maxPeriod = 3000;
|
|
|
|
do {
|
|
// 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);
|
|
}
|
|
void simAlarms()
|
|
{
|
|
int minPeriod = 1000;
|
|
int maxPeriod = 2000;
|
|
|
|
do
|
|
{
|
|
var alarmCode = rand.Next(1, 100);
|
|
saveAndSendMessage(mHash("Machine:Alarms:Current"), $"{alarmCode:000} | Allarme PLC {alarmCode}", "allarms", $"{alarmCode:000}");
|
|
// attesa random
|
|
Thread.Sleep(rand.Next(minPeriod, maxPeriod));
|
|
} while (true);
|
|
}
|
|
|
|
|
|
void saveAndSendMessage(string memKey, string value, string notifyChannel, string message)
|
|
{
|
|
// effettuo la scrittura nell'area di memoria indicata
|
|
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}";
|
|
} |