SafeDispatch/MotoTrbo_GW/MessageBusMessage.cs
2024-02-22 18:43:59 +02:00

292 lines
9.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SafeMobileLib;
namespace MotoTrbo_GW
{
class MessageBusMessage
{
public bool OK;
//identifiers
private int _OPcode;
public int OPcode
{
get { return _OPcode; }
set { _OPcode = value; }
}
private string seqID;
public string SeqID
{
get { return seqID; }
set { seqID = value; }
}
private int gwAPPid = 0;
public int GwAPPid
{
get { return gwAPPid; }
set { gwAPPid = value; }
}
private int radioGWid = 0;
public int RadioGWid
{
get { return radioGWid; }
set { radioGWid = value; }
}
private DateTime time;
public DateTime Time
{
get { return time; }
set { time = value; }
}
//zones
private int zoneNr;
public int ZoneNr
{
get { return zoneNr; }
set { zoneNr = value; }
}
private int channelNr;
public int ChannelNr
{
get { return channelNr; }
set { channelNr = value; }
}
//call
private string voiceMulticastIP4Send;
public string VoiceMulticastIP4Send
{
get { return voiceMulticastIP4Send; }
set { voiceMulticastIP4Send = value; }
}
private int suid;
public int SUID
{
get { return suid; }
set { suid = value; }
}
private int groupID;
public int GroupID
{
get { return groupID; }
set { groupID = value; }
}
private AlarmTypes alarmType;
public AlarmTypes AlarmType
{
get { return alarmType; }
set { alarmType = value; }
}
//error string
private string error;
public string Error
{
get { return error; }
set { error = value; }
}
public string versionNumber = "";
public MessageBusMessage(string data, int actualLen)
{
OK = ProcessMsg(data,actualLen);
}
private bool ProcessMsg(string data, int actualLen)
{
time = DateTime.Now;
string[] tempArr = data.Split('#');
if (tempArr.Length == 0)
{
Error = "incorect message=" + data;
return false;
}
int messLen = Convert.ToInt32(tempArr[1]);
if (actualLen != messLen)
{
Error = "msgLen:" + messLen + " != acutulLen:" + actualLen;
return false;
}
//test if the message contains opCode && seqID && identifiers
if (tempArr.Length > 5)
{
OPcode = Convert.ToInt32(tempArr[3]);
seqID = tempArr[2];
string identifiers = tempArr[4];
string[] tempIdent = identifiers.Split('.');
if (tempIdent.Length > 1)
{
gwAPPid = Convert.ToInt32(tempIdent[0]);
radioGWid = Convert.ToInt32(tempIdent[1]);
}
else
{
Error = "msg identifiers missing" + tempArr.Length;
return false;
}
}
#region LINX
#if LINXB
// Check for refresh cmd (209)
else if (Convert.ToInt32(tempArr[3]) == (int)(MessageBusCmds.RefreshDatabaseCommand))
{
bool processMessage = false;
if (tempArr[4] != null)
{
int reasonForRefresh;
if (int.TryParse(tempArr[4], out reasonForRefresh))
{
if (reasonForRefresh == (int)RefreshCommandReason.ModifiedUnitList)
{
processMessage = true;
OPcode = (int)MessageBusCmds.RefreshDatabaseCommand;
seqID = tempArr[2];
}
}
}
return processMessage;
}
#endif
#endregion
else
{
Error = "msg doesnt contain identifiers. tempArr.length:" + tempArr.Length;
return false;
}
//continue processing payload
bool payloadOK = ProcessPaylaod(data, actualLen);
return payloadOK;
}
private bool ProcessPaylaod(string data, int actualLen)
{
string[] tempArr = data.Split('#');
switch (OPcode)
{
//zones & channels
case 104:
//SafeMobileLib.Utils.WriteLine("Got a zone and channel change request ");
string[] zoneAndChannelStrList = tempArr[5].Split('.');
zoneNr = Convert.ToInt32(zoneAndChannelStrList[0]);
channelNr = Convert.ToInt32(zoneAndChannelStrList[1]);
break;
case 94:
//SafeMobileLib.Utils.WriteLine("Got a SOFTWARE zone and channel change request ");
break;
//call messages
case 101:
//SafeMobileLib.Utils.WriteLine("Got all call request");
//process only first call patching start call request
if (tempArr.Length > 6)
{
if (tempArr[6] == "1")
{
if (!Main.gatewayState.ContainsKey(tempArr[4]))
return false;
if (Main.gatewayState[tempArr[4]] == true)
return false;
if (Main.gatewayState[tempArr[4]] == false)
Main.gatewayState[tempArr[4]] = true;
}
}
voiceMulticastIP4Send = tempArr[5];
break;
case 111:
//SafeMobileLib.Utils.WriteLine("Got all call stop");
//process only first call patching end call request
if (tempArr.Length > 6)
{
if (tempArr[6] == "1")
{
if (!Main.gatewayState.ContainsKey(tempArr[4]))
return false;
if (Main.gatewayState[tempArr[4]] == false)
return false;
if (Main.gatewayState[tempArr[4]] == true)
Main.gatewayState[tempArr[4]] = false;
}
}
break;
case 102:
//SafeMobileLib.Utils.WriteLine("Got private call request");
SUID = Convert.ToInt32((tempArr[4].Split('.'))[2]);
voiceMulticastIP4Send = tempArr[5];
break;
case 112:
//TODO test if this message is for this radio( until then try this id an all radios)
//SafeMobileLib.Utils.WriteLine("Got private call stop");
break;
case 103:
//SafeMobileLib.Utils.WriteLine("Got group call request");
string identifiers2 = tempArr[4];
string[] tempIdent2 = identifiers2.Split('.');
GroupID = Convert.ToInt32(tempIdent2[2]);
voiceMulticastIP4Send = tempArr[5];
break;
case 113:
//SafeMobileLib.Utils.WriteLine("Got group call stop");
break;
case 158:
versionNumber = tempArr[5];
break;
case 160:
//SafeMobileLib.Utils.WriteLine("Got DeKey");
break;
case 161:
SUID = Convert.ToInt32((tempArr[4].Split('.'))[2]);
//SafeMobileLib.Utils.WriteLine("Got remote monitor request." + "Id for remote monitor" + suid);
break;
case 238:
SUID = Convert.ToInt32((tempArr[4].Split('.'))[2]);
if (tempArr.Length > 5 && tempArr[5].Trim().Length > 0)
try
{
alarmType = (AlarmTypes)Enum.Parse(typeof(AlarmTypes), tempArr[5]);
}
catch(Exception ex)
{
SafeMobileLib.Utils.WriteLine("Emergency type was not in a correct format: " + ex.ToString() + "\n" + data, ConsoleColor.Red);
alarmType = AlarmTypes.unknown;
}
//SafeMobileLib.Utils.WriteLine("Got remote monitor request." + "Id for remote monitor" + suid);
break;
#region LINX
#if LINXB
case 209:
// prevent the method to return false
break;
#endif
#endregion
default:
Error="Unknown opcode" +OPcode;
return false;
//break;
}
return true;
}
}
}