Update the statusMessage and edit the required changes in the informmiddleware function

This commit is contained in:
atef 2023-11-22 12:25:39 +01:00
parent 229321cd1c
commit 6301cf38a0
2 changed files with 128 additions and 62 deletions

View File

@ -3,9 +3,8 @@ namespace InnovEnergy.App.SaliMax.MiddlewareClasses;
public class StatusMessage
{
public required Int32 InstallationId { get; set; }
public required Int32 Status { get; set; }
public List<AlarmOrWarning> Warnings { get; set; }
public List<AlarmOrWarning> Alarms { get; set; }
public List<AlarmOrWarning>? Warnings { get; set; }
public List<AlarmOrWarning>? Alarms { get; set; }
}

View File

@ -59,7 +59,7 @@ internal static class Program
private static IModel? _channel;
private static Boolean _subscribedToQueue = false;
private static Boolean _subscribeToQueueForTheFirstTime = false;
private static SalimaxAlarmState _prevSalimaxState = SalimaxAlarmState.Green;
private static Int32 _prevSalimaxState = 0; // 0 is a green status
static Program()
{
@ -257,7 +257,7 @@ internal static class Program
// ReSharper disable once FunctionNeverReturns
}
private static void SendSalimaxStateAlarm(SalimaxAlarmState currentSalimaxState)
private static void SendSalimaxStateAlarm(StatusMessage currentSalimaxState)
{
var s3Bucket = Config.Load().S3?.Bucket;
@ -267,17 +267,17 @@ internal static class Program
_subscribeToQueueForTheFirstTime = true;
SubscribeToQueue(currentSalimaxState, s3Bucket);
if (_subscribedToQueue && currentSalimaxState != _prevSalimaxState)
if (_subscribedToQueue && currentSalimaxState.Status != _prevSalimaxState)
{
_prevSalimaxState = currentSalimaxState;
_prevSalimaxState = currentSalimaxState.Status;
}
}
//If already subscribed to the queue and the status has been changed, update the queue
else if (_subscribedToQueue && currentSalimaxState != _prevSalimaxState)
else if (_subscribedToQueue && currentSalimaxState.Status != _prevSalimaxState)
{
_prevSalimaxState = currentSalimaxState;
_prevSalimaxState = currentSalimaxState.Status;
if (s3Bucket != null)
InformMiddleware(s3Bucket, (Int32)currentSalimaxState);
InformMiddleware(s3Bucket, currentSalimaxState);
}
//If there is an available message from the RabbitMQ Broker, subscribe to the queue
@ -301,7 +301,7 @@ internal static class Program
}
}
private static void SubscribeToQueue(SalimaxAlarmState currentSalimaxState, String? s3Bucket)
private static void SubscribeToQueue(StatusMessage currentSalimaxState, String? s3Bucket)
{
try
{
@ -313,7 +313,7 @@ internal static class Program
Console.WriteLine("The controller sends its status to the middleware for the first time");
if (s3Bucket != null) InformMiddleware(s3Bucket, (Int32)currentSalimaxState);
if (s3Bucket != null) InformMiddleware(s3Bucket, currentSalimaxState);
_subscribedToQueue = true;
}
@ -345,31 +345,11 @@ internal static class Program
return IPAddress.None;
}
private static void InformMiddleware(String? bucket, int status)
private static void InformMiddleware(String? bucket, StatusMessage status)
{
int.TryParse(bucket[0].ToString(), out var installationId);
var jsonObject = new StatusMessage
{
InstallationId = installationId,
Status = status,
};
if (status == 2)
{
jsonObject.Date = DateTime.Now.ToString("yyyy-MM-dd");
jsonObject.Time = DateTime.Now.ToString("HH:mm:ss");
jsonObject.Description = "Battery Overvoltage";
jsonObject.CreatedBy = "Battery/4";
}
else if (status == 1)
{
jsonObject.Date = DateTime.Now.ToString("yyyy-MM-dd");
jsonObject.Time = DateTime.Now.ToString("HH:mm:ss");
jsonObject.Description = "Temp warning message";
jsonObject.CreatedBy = "Battery/4";
}
var message = JsonSerializer.Serialize(jsonObject);
var message = JsonSerializer.Serialize(status);
var body = Encoding.UTF8.GetBytes(message);
_channel.BasicPublish(exchange: string.Empty,
@ -380,20 +360,98 @@ internal static class Program
Console.WriteLine($"Producer sent message: {message}");
}
private static SalimaxAlarmState GetSalimaxStateAlarm(StatusRecord record)
private static StatusMessage GetSalimaxStateAlarm(StatusRecord record)
{
var alarmCondition = record.DetectAlarmStates();
var s3Bucket = Config.Load().S3?.Bucket;
var alarmList = new List<AlarmOrWarning>();
var warningList = new List<AlarmOrWarning>();
if (alarmCondition is not null)
{
alarmCondition.LogInfo();
alarmList.Add(new AlarmOrWarning
{
Date = DateTime.Now.ToString("yyyy-MM-dd"),
Time = DateTime.Now.ToString("HH:mm:ss"),
CreatedBy = "Salimax",
Description = alarmCondition
});
}
// record.Log = new SystemLog
// {
// Led = alarmCondition is null ? LedState.Green : LedState.Red,
// Message = alarmCondition
// };
foreach (var alarm in record.AcDc.Alarms)
{
alarmList.Add(new AlarmOrWarning
{
Date = DateTime.Now.ToString("yyyy-MM-dd"),
Time = DateTime.Now.ToString("HH:mm:ss"),
CreatedBy = "AcDc",
Description = alarm.ToString()
});
}
foreach (var alarm in record.DcDc.Alarms)
{
alarmList.Add(new AlarmOrWarning
{
Date = DateTime.Now.ToString("yyyy-MM-dd"),
Time = DateTime.Now.ToString("HH:mm:ss"),
CreatedBy = "DcDc",
Description = alarm.ToString()
});
}
if (record.Battery != null)
{
foreach (var alarm in record.Battery.Alarms)
{
alarmList.Add(new AlarmOrWarning
{
Date = DateTime.Now.ToString("yyyy-MM-dd"),
Time = DateTime.Now.ToString("HH:mm:ss"),
CreatedBy = "Battery",
Description = alarm
});
}
}
foreach (var warning in record.AcDc.Alarms)
{
warningList.Add(new AlarmOrWarning
{
Date = DateTime.Now.ToString("yyyy-MM-dd"),
Time = DateTime.Now.ToString("HH:mm:ss"),
CreatedBy = "AcDc",
Description = warning.ToString()
});
}
foreach (var warning in record.DcDc.Alarms)
{
warningList.Add(new AlarmOrWarning
{
Date = DateTime.Now.ToString("yyyy-MM-dd"),
Time = DateTime.Now.ToString("HH:mm:ss"),
CreatedBy = "DcDc",
Description = warning.ToString()
});
}
if (record.Battery != null)
{
foreach (var warning in record.Battery.Alarms)
{
warningList.Add(new AlarmOrWarning
{
Date = DateTime.Now.ToString("yyyy-MM-dd"),
Time = DateTime.Now.ToString("HH:mm:ss"),
CreatedBy = "Battery",
Description = warning
});
}
}
var salimaxAlarmsState = (record.Battery is not null && record.Battery.Warnings.Any())
| record.AcDc.Warnings.Any()
@ -410,8 +468,17 @@ internal static class Program
? SalimaxAlarmState.Red
: salimaxAlarmsState; // this will be replaced by LedState
int.TryParse(s3Bucket[0].ToString(), out var installationId);
return salimaxAlarmsState;
var returnedStatus = new StatusMessage
{
InstallationId = installationId,
Status = (Int16)salimaxAlarmsState,
Alarms = alarmList,
Warnings = warningList
};
return returnedStatus;
}
private static String? DetectAlarmStates(this StatusRecord r) => r.Relays switch