202 lines
6.9 KiB
C#
202 lines
6.9 KiB
C#
namespace Xylem.Common.Production.ProductionUiCordonel.ProductionProcesses.RFTelegrams
|
|
{
|
|
using CommonConsole.RFTelegrams.Pams;
|
|
using global::Common.Hardware.SIRT;
|
|
using global::Common.Hardware.SIRT.Tasks;
|
|
|
|
using Pams;
|
|
|
|
using System;
|
|
|
|
internal class SIRTServer
|
|
{
|
|
private static Action<string> messageCallback;
|
|
private static Action<string> errorMessageCallback;
|
|
private static readonly SIRTHub sirtHub = new SIRTHub();
|
|
|
|
/// <summary>
|
|
/// Connects SIRT's globaly for the application
|
|
/// </summary>
|
|
/// <param name="messageHandler">Action that receives info messages</param>
|
|
/// <param name="errorMessageHandler">Action that receives error messages</param>
|
|
/// <param name="comPorts">The COM-Ports to be connected</param>
|
|
/// <returns><c>true</c> - if all ports connected otherwise <c>false</c></returns>
|
|
public static bool StartServer(Action<string> messageHandler, Action<string> errorMessageHandler, params int[] comPorts)
|
|
{
|
|
string sirtId;
|
|
int frequency;
|
|
bool succeeded = true;
|
|
|
|
messageHandler?.Invoke(new string('-', 100));
|
|
|
|
foreach (var comport in comPorts)
|
|
{
|
|
succeeded = ConnectSIRT(comport, out sirtId, out frequency);
|
|
|
|
if (succeeded)
|
|
{
|
|
messageHandler?.Invoke($"COM{comport}|{sirtId}|{frequency}MHz - connected");
|
|
messageHandler?.Invoke(new string('-', 100));
|
|
}
|
|
else
|
|
{
|
|
errorMessageHandler?.Invoke($"SIRT at COM{comport} not connected!");
|
|
errorMessageHandler?.Invoke(new string('-', 100));
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (succeeded)
|
|
{
|
|
messageCallback = messageHandler;
|
|
errorMessageCallback = errorMessageHandler;
|
|
}
|
|
|
|
return succeeded;
|
|
}
|
|
|
|
public static bool StartProgramming(int frequency, uint address, string encryptionKey, int repeatsCount = 3)
|
|
{
|
|
var succeeded = true;
|
|
var encryptionBytes = encryptionKey.GetBytes();
|
|
|
|
if (succeeded)
|
|
{
|
|
succeeded = SendPAM(task =>
|
|
{
|
|
task.WakeUpCmd = true;
|
|
task.DelPamSemi = true;
|
|
task.WakeUpModul = true;
|
|
task.IsEncrypted = true;
|
|
task.Frequency = frequency;
|
|
task.RequestAddress = address;
|
|
task.EncryptionKey = encryptionBytes;
|
|
task.Data = new DummyPam();
|
|
}
|
|
, repeatsCount);
|
|
}
|
|
|
|
if (succeeded)
|
|
{
|
|
succeeded = SendPAM(task =>
|
|
{
|
|
task.DelPamSemi = true;
|
|
task.IsEncrypted = true;
|
|
task.Frequency = frequency;
|
|
task.RequestAddress = address;
|
|
task.EncryptionKey = encryptionBytes;
|
|
task.Data = new ResetAlarms(0xFF);
|
|
}
|
|
, repeatsCount);
|
|
}
|
|
|
|
if (succeeded)
|
|
{
|
|
succeeded = SendPAM(task =>
|
|
{
|
|
task.DelPamSemi = true;
|
|
task.IsEncrypted = true;
|
|
task.Frequency = frequency;
|
|
task.RequestAddress = address;
|
|
task.EncryptionKey = encryptionBytes;
|
|
task.Data = new ResetAlarms(0xFE);
|
|
}
|
|
, repeatsCount);
|
|
}
|
|
|
|
//if (succeeded)
|
|
//{
|
|
// succeeded = SendPAM(task =>
|
|
// {
|
|
// task.DelPamSemi = true;
|
|
// task.IsEncrypted = true;
|
|
// task.Frequency = frequency;
|
|
// task.RequestAddress = address;
|
|
// task.EncryptionKey = encryptionBytes;
|
|
// task.Data = ChangeWakeUp.GoSleepAutoOn;
|
|
// }
|
|
// , repeatsCount);
|
|
//}
|
|
|
|
return succeeded;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Dispose all SIRT's globaly for the application.
|
|
/// </summary>
|
|
public static void Dispose()
|
|
{
|
|
void LogState(object state) => messageCallback?.Invoke(state?.ToString());
|
|
|
|
sirtHub.StateLogging += LogState;
|
|
sirtHub.Stop();
|
|
sirtHub.StateLogging -= LogState;
|
|
|
|
messageCallback = null;
|
|
errorMessageCallback = null;
|
|
}
|
|
|
|
private static bool ConnectSIRT(int portno, out string sirtId, out int frequency)
|
|
=> sirtHub.TryOpen($"COM{portno}", out sirtId, out frequency);
|
|
|
|
private static bool SendPAM(Action<RFTask> taskOptions, int repeatsCount = 3)
|
|
{
|
|
void LogMessage(SIRTMessage message) => messageCallback?.Invoke(message.ToString());
|
|
|
|
var dashes = new string('-', 100);
|
|
var cordonelTask = new RFTask();
|
|
var succeeded = false;
|
|
|
|
taskOptions(cordonelTask);
|
|
|
|
cordonelTask.MessageSent += LogMessage;
|
|
cordonelTask.MessageReceived += LogMessage;
|
|
|
|
for (var i = 1; i <= repeatsCount && cordonelTask.State != SIRTTaskState.Completed && cordonelTask.State != SIRTTaskState.Cancelled; i++)
|
|
{
|
|
if (sirtHub.Start(cordonelTask))
|
|
{
|
|
cordonelTask.Wait();
|
|
}
|
|
}
|
|
|
|
cordonelTask.MessageReceived -= LogMessage;
|
|
cordonelTask.MessageSent -= LogMessage;
|
|
|
|
if (cordonelTask.State == SIRTTaskState.Completed)
|
|
{
|
|
succeeded = true;
|
|
|
|
messageCallback?.Invoke($"{"CMD",20}: {cordonelTask}");
|
|
messageCallback?.Invoke($"{"State",20}: {cordonelTask.State}");
|
|
|
|
var pam = new PAMStatus(cordonelTask.SEMI.Payload[11]);
|
|
messageCallback?.Invoke($"{"PAM Status",20}: {pam}");
|
|
|
|
var alarm = new AlarmMask(cordonelTask.SEMI.Payload[10]);
|
|
messageCallback?.Invoke(alarm.ToString());
|
|
messageCallback?.Invoke(dashes);
|
|
|
|
var semi = new SEMI(cordonelTask.SEMI.Payload, 868);
|
|
}
|
|
else
|
|
{
|
|
succeeded = false;
|
|
|
|
messageCallback?.Invoke($"{"CMD",20}: {cordonelTask}");
|
|
messageCallback?.Invoke($"{"State",20}: {cordonelTask.State}");
|
|
|
|
var pam = new PAMStatus(cordonelTask.SEMI.Payload[11]);
|
|
messageCallback?.Invoke($"{"PAM Status",20}: {pam}");
|
|
|
|
var alarm = new AlarmMask(cordonelTask.SEMI.Payload[10]);
|
|
messageCallback?.Invoke(alarm.ToString());
|
|
messageCallback?.Invoke(dashes);
|
|
}
|
|
|
|
return succeeded;
|
|
}
|
|
}
|
|
}
|