Simulatore multithread OK come primo test (manca reset...)
This commit is contained in:
+57
-7
@@ -5,23 +5,73 @@ 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
|
||||
using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("127.0.0.1:6379"))
|
||||
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(redisConf);
|
||||
ISubscriber sub = redis.GetSubscriber();
|
||||
|
||||
|
||||
|
||||
#if false
|
||||
// ciclo principale
|
||||
string input;
|
||||
do
|
||||
{
|
||||
ISubscriber sub = redis.GetSubscriber();
|
||||
input = Console.ReadLine();
|
||||
sub.Publish("allarmi", input);
|
||||
} while (input != "exit");
|
||||
#endif
|
||||
|
||||
Console.WriteLine("please enter any character and exit to exit");
|
||||
// avvio tutti i thread...
|
||||
Thread threadStatus = new Thread(simStatus);
|
||||
Thread threadAlarms = new Thread(simAlarms);
|
||||
|
||||
string input;
|
||||
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
|
||||
{
|
||||
input = Console.ReadLine();
|
||||
sub.Publish("allarmi", input);
|
||||
} while (input != "exit");
|
||||
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}");
|
||||
}
|
||||
Reference in New Issue
Block a user