SafeDispatch/SafeMobileLIB_DLL/MessageDecoders/TelemetryDecoder.cs
2024-02-22 18:43:59 +02:00

153 lines
4.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace SafeMobileLib
{
public class TelemetryDecoder
{
private string radioID;
private byte[] data;
private DBtelemetryManager DBtel;
private Boolean hytera;
private bool[] di;
public Boolean[] DI
{
get { return di; }
set { di = value; }
}
public TelemetryDecoder(string radioID, byte[] data, DBtelemetryManager DBtel, Boolean hytera)
{
this.radioID = radioID;
this.data = data;
this.DBtel = DBtel;
this.hytera = hytera;
if (hytera)
DecodeHytPacket(data);
else
DecodePacket(data);
}
public TelemetryDecoder()
{
}
public void DecodePacket(Byte[] data)
{
SM.Debug("Decoding data for telemetry");
try
{
//test opcode 0x10 = Announce Status; 0x0C =QueryStatusResponse
if (data[2] == 0x10)
{
//get DI that fired up the event
SM.Debug("Got telemetry opcode 0x10 =Announce Status");
di = ComputeDI(data[3]);
//get old value
TelemetryPOS tPOS = DBtel.GetTelemetryPOS(radioID);
if (tPOS == null)
{
//this is first value
tPOS = new TelemetryPOS(0, radioID, 0, (int)data[3]);
DBtel.AddTelemetryPOS(tPOS);
SM.Debug("first telemetry message for " + radioID );
}
else
{
//update
tPOS.ByteValue = tPOS.ByteValue | (int)data[3];
SM.Debug("telemetry message for " + radioID );
DBtel.UpdateTelemetryPOS(tPOS);
}
}
else
{
SM.Debug("Got telemetry opcode 0x0C =QUerry response");
di = ComputeDI(data[3]); //add by bigu
//get old value
TelemetryPOS tPOS = DBtel.GetTelemetryPOS(radioID);
if (tPOS == null)
{
//this is first value
tPOS = new TelemetryPOS(0, radioID, 0, (int)data[3]);
DBtel.AddTelemetryPOS(tPOS);
SM.Debug("first telemetry message for " + radioID + " adding it to DB");
}
else
{
//update
int newByteValue = (int)data[3];
SM.Debug("telemetry message for " + radioID + " UPDATING");
tPOS.ByteValue = newByteValue;
DBtel.UpdateTelemetryPOS(tPOS);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
public void DecodeHytPacket(Byte[] data)
{
SM.Debug("Decoding data for Hytera telemetry");
try
{
//get DI that fired up the event
SM.Debug("Got telemetry opcode 0xA081 = Status Report");
di = ComputeDI(data[0]);
//get old value
TelemetryPOS tPOS = DBtel.GetTelemetryPOS(radioID);
if (tPOS == null)
{
//this is first value
tPOS = new TelemetryPOS(0, radioID, 0, (Int16)data[0]);
DBtel.AddTelemetryPOS(tPOS);
SM.Debug("first telemetry message for " + radioID);
}
else
{
//update
tPOS.ByteValue = (Int16)data[0];
SM.Debug("telemetry message for " + radioID);
DBtel.UpdateTelemetryPOS(tPOS);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
uint DateTo70Format(DateTime param)
{
long nOfSeconds;
System.DateTime dt70 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
TimeSpan span = param - dt70;
nOfSeconds = (long)span.TotalSeconds;
return ((uint)nOfSeconds);
}
private bool[] ComputeDI(byte value)
{
bool[] diRet = new bool[] {false,false,false,false,false,false};
for (int i = 0; i < 6; i++)
if (((byte) Math.Pow(2, i) & value) == Math.Pow(2, i))
diRet[i] = true;
return diRet;
}
}
}