77 lines
2.0 KiB
C#
77 lines
2.0 KiB
C#
// See https://aka.ms/new-console-template for more information
|
|
using StackExchange.Redis;
|
|
using System.Configuration;
|
|
|
|
string lineSep = "------------------------------------------------------";
|
|
string redisConf = "127.0.0.1:6379";
|
|
|
|
Random rand = new Random();
|
|
|
|
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();
|
|
|
|
|
|
|
|
#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);
|
|
|
|
threadStatus.Start();
|
|
threadAlarms.Start();
|
|
|
|
|
|
|
|
void simStatus()
|
|
{
|
|
int minPeriod = 2000;
|
|
int maxPeriod = 3000;
|
|
|
|
do {
|
|
var statusCode = rand.Next(1, 5);
|
|
saveAndSendMessage("Machine:Status:Current", $"{statusCode} | Stato {statusCode:00}", "status", $"{statusCode}");
|
|
// 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("Machine:Alarms:Current", $"{alarmCode:000} | Allarme PLC {alarmCode}", "allarmi", $"{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
|
|
redis.GetDatabase().SetAdd(memKey, value);
|
|
|
|
// invio notifica tramite il canale richiesto
|
|
sub.Publish(notifyChannel, message);
|
|
Console.WriteLine($"[{notifyChannel}] key: {memKey} | val: {value} | message: {message}");
|
|
} |