SafeDispatch/SipComponent/Simoco/SP6.cs

117 lines
3.6 KiB
C#
Raw Permalink Normal View History

2024-02-22 16:43:59 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SipComponent.Simoco
{
static class SP6
{
// Short data (Over-the-Air) PDU
// byte 0 - Rezerved (Header) 0
// byte 1 - Type (Header) 0 (Management)
// byte 2...end - Management payload
// Management payload
// byte 2 - Protocol Control 0
// byte 3 - Type 0 - GPS, 1 - Device Status, 255 - Ping
// byte 4 - Command depends on Tpe
// byte 5...end - Parameters
private static void AddManagementShortData(byte[] data, byte type, byte command)
{
data[3] = type;
data[4] = command;
}
public static byte GetType(byte[] managementShortData)
{
return managementShortData[3];
}
public static byte GetCommand(byte[] managementShortData)
{
return managementShortData[4];
}
public static AlarmType GetAlarmType(byte[] deviceStatusReport)
{
// bytes 5..6 User Status
// bytes 7..10 Alarm Conditions Flags
// bytes 11..14 GPS Latitude
// bytes 15..18 GPS Longitude
return (AlarmType)BitConverter.ToUInt32(deviceStatusReport, 7);
}
public static void GetGPSFromStatusReport(byte[] deviceStatusReport, out double latitude, out double longitude)
{
byte[] latitudeBytes = new byte[4];
byte[] longitudeBytes = new byte[4];
Array.Copy(deviceStatusReport, 11, latitudeBytes, 0, 4);
Array.Reverse(latitudeBytes);
Array.Copy(deviceStatusReport, 15, longitudeBytes, 0, 4);
Array.Reverse(longitudeBytes);
latitude = BitConverter.ToInt32(latitudeBytes, 0) / 10000000.0;
longitude = BitConverter.ToInt32(longitudeBytes, 0) / 10000000.0;
}
public static byte[] PingCommand(params byte[] arbitraryData)
{
// lungimea in oceti = 5 + arbitrary data
int n = 5 + arbitraryData.Length;
byte[] ping = new byte[n];
AddManagementShortData(ping, 0xFF, 0);
Array.Copy(arbitraryData, 0, ping, 5, arbitraryData.Length);
return ping;
}
public static byte[] GPSRequestCommand()
{
byte[] GPSRequest = new byte[5];
AddManagementShortData(GPSRequest, 0, 1);
return GPSRequest;
}
public static byte[] StatusRequestCommand()
{
byte[] StatusRequest = new byte[5];
AddManagementShortData(StatusRequest, 1, 0);
return StatusRequest;
}
public static byte[] CapabilityRequestCommand()
{
byte[] deviceCapabilityRequest = new byte[5];
AddManagementShortData(deviceCapabilityRequest, 1, 3);
return deviceCapabilityRequest;
}
}
/// <summary>
/// Contains a list of Alarm types
/// </summary>
public enum AlarmType : uint
{
/// <summary>
/// Alarm generated by pressing a button
/// </summary>
USER_GENERATED = 0x01000000,
/// <summary>
/// MAN DOWN
/// </summary>
MAN_DOWN = 0x02000000,
/// <summary>
/// LONE WORKER
/// </summary>
LONE_WORKER = 0x04000000,
/// <summary>
/// NO ALARM
/// </summary>
NO_ALARM = 0x00000000
}
}