214 lines
8.0 KiB
C#
214 lines
8.0 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using MotoTRBO_GW;
|
|||
|
using MotoTRBO_XNL_Cmd;
|
|||
|
using SafeMobileLib;
|
|||
|
using System.Threading;
|
|||
|
using System.Collections;
|
|||
|
|
|||
|
namespace MotoTrbo_GW
|
|||
|
{
|
|||
|
class RemoteStunThread
|
|||
|
{
|
|||
|
private static int id = 0;
|
|||
|
private int localID = 0;
|
|||
|
private string user4radioCon = "RemoteEnable_disable";
|
|||
|
private Int32 port;
|
|||
|
private UdpMulticast udpMulticast;
|
|||
|
private string trboRadioIP;
|
|||
|
private String mIP;
|
|||
|
private Int32 mPort;
|
|||
|
private int radioGWid;
|
|||
|
private RadioConnection rc;
|
|||
|
|
|||
|
public RemoteStunThread(RadioConnection rc_var,string ip, int radioGWid, Int32 emergencyPort, String multicastID, String multicastPort)
|
|||
|
{
|
|||
|
id++;
|
|||
|
localID = id;
|
|||
|
SafeMobileLib.Utils.WriteLine("RemoteStun CONSTRUCTOR for:" + ip);
|
|||
|
port = emergencyPort;
|
|||
|
trboRadioIP = ip;
|
|||
|
mIP = multicastID;
|
|||
|
mPort = Int32.Parse(multicastPort);
|
|||
|
this.radioGWid = radioGWid;
|
|||
|
|
|||
|
//rc = new RadioConnection(ip, port, user4radioCon);
|
|||
|
rc = rc_var;
|
|||
|
rc.OnRemoteControlEvent += new RadioConnection.RemoteControlEvent(RemoteStunThread_OnRemoteControlEvent);
|
|||
|
//rc.OnAuthDone2 += new RadioConnection.AuthDoneDEl2(RemoteStunThread_OnAuthDone);
|
|||
|
//rc.Start();
|
|||
|
}
|
|||
|
|
|||
|
void RemoteStunThread_OnAuthDone(string user)
|
|||
|
{
|
|||
|
if(user == user4radioCon)
|
|||
|
SafeMobileLib.Utils.WriteLine("Remote Stun thread connected to base station:" + trboRadioIP);
|
|||
|
}
|
|||
|
|
|||
|
void RemoteStunThread_OnRemoteControlEvent(bool status, REMOTE_RADIO_FUNCS func, int remote_radioId)
|
|||
|
{
|
|||
|
if (func == REMOTE_RADIO_FUNCS.Enable)
|
|||
|
{
|
|||
|
if (status)
|
|||
|
{
|
|||
|
string seqID = "0.0";
|
|||
|
SafeMobileLib.Utils.WriteLine("got REMOTE_RADIO Enable response(status=true). sending it to message bus");
|
|||
|
//build string
|
|||
|
string test = "#250#" + remote_radioId.ToString() + "#1#";
|
|||
|
SendOnMsgBuss(seqID, test);
|
|||
|
//SafeMobileLib.Utils.WriteLine("message sent");
|
|||
|
}
|
|||
|
else
|
|||
|
SafeMobileLib.Utils.WriteLine("got REMOTE_RADIO Enable response(status=false).radioID:" + remote_radioId);
|
|||
|
}
|
|||
|
if (func == REMOTE_RADIO_FUNCS.Disable)
|
|||
|
{
|
|||
|
if (status)
|
|||
|
{
|
|||
|
string seqID = "0.0";
|
|||
|
SafeMobileLib.Utils.WriteLine("got REMOTE_RADIO Disable response(status=true). sending it to message bus");
|
|||
|
//build string
|
|||
|
string test = "#250#" + remote_radioId.ToString() + "#0#";
|
|||
|
SendOnMsgBuss(seqID, test);
|
|||
|
}
|
|||
|
else
|
|||
|
SafeMobileLib.Utils.WriteLine("got REMOTE_RADIO Disable response(status=false).radioID:" + remote_radioId);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public void handleConnection()
|
|||
|
{
|
|||
|
|
|||
|
SafeMobileLib.Utils.WriteLine("RemoteStun Thread - Initialized on Port " + port);
|
|||
|
try
|
|||
|
{
|
|||
|
udpMulticast = new UdpMulticast(mIP, mPort);
|
|||
|
udpMulticast.OnNewDataRecv += new UdpMulticast.newData4Send(udpMulticast_OnNewDataRecv);
|
|||
|
udpMulticast.StartListen(Main.LocalIP);
|
|||
|
SafeMobileLib.Utils.WriteLine("RemoteStun thread successfully registered to multicast group");
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
SafeMobileLib.Utils.WriteLine("RemoteStun Thread exception while joining the multicast group: " + ex.ToString());
|
|||
|
}
|
|||
|
|
|||
|
while (true)
|
|||
|
{ Thread.Sleep(100); }
|
|||
|
}
|
|||
|
|
|||
|
void udpMulticast_OnNewDataRecv(byte[] data, int dataLen)
|
|||
|
{
|
|||
|
string str = System.Text.Encoding.ASCII.GetString(data, 0, dataLen);
|
|||
|
//SafeMobileLib.Utils.WriteLine("RX: " + str.Trim(), false);
|
|||
|
try
|
|||
|
{
|
|||
|
BusMessageParser(str, dataLen);
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
SafeMobileLib.Utils.WriteLine(ex.ToString());
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public void SendOnMsgBuss(string seqID, string test)
|
|||
|
{
|
|||
|
String cmdok = "#" + seqID + test; Int32 tmp = cmdok.Length + 1; tmp += tmp.ToString().Length; cmdok = "#" + tmp.ToString() + cmdok;
|
|||
|
System.Text.Encoding enc = System.Text.Encoding.ASCII; byte[] buf = enc.GetBytes(cmdok);
|
|||
|
udpMulticast.Send(buf, buf.Length);
|
|||
|
SafeMobileLib.Utils.WriteLine("TX:" + cmdok);
|
|||
|
}
|
|||
|
|
|||
|
string seqID = "";
|
|||
|
private bool BusMessageParser(string data, int actualLen)
|
|||
|
{
|
|||
|
string[] tempArr = data.Split('#');
|
|||
|
if (tempArr.Length == 0)
|
|||
|
{
|
|||
|
SafeMobileLib.Utils.WriteLine("incorect message=" + data);
|
|||
|
return false;
|
|||
|
}
|
|||
|
int messLen = Convert.ToInt32(tempArr[1]);
|
|||
|
|
|||
|
if (actualLen != messLen)
|
|||
|
{
|
|||
|
SafeMobileLib.Utils.WriteLine($"RadioComHandler.cs->BusMessageParser ->message length({messLen}) != actual length({actualLen})");
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
int opCode = Convert.ToInt32(tempArr[3]);
|
|||
|
seqID = tempArr[2];
|
|||
|
string identifiers = tempArr[4];
|
|||
|
int gwid_recv = 0;
|
|||
|
int radioid_recv = 0;
|
|||
|
string[] tempIdent = identifiers.Split('.');
|
|||
|
if (tempIdent.Length > 1)
|
|||
|
{
|
|||
|
gwid_recv = Convert.ToInt32(tempIdent[0]);
|
|||
|
radioid_recv = Convert.ToInt32(tempIdent[1]);
|
|||
|
}
|
|||
|
|
|||
|
switch (opCode)
|
|||
|
{
|
|||
|
case 150:
|
|||
|
if (gwid_recv == Main.GWID)
|
|||
|
{
|
|||
|
int id = Convert.ToInt32((tempArr[4].Split('.'))[2]);
|
|||
|
int status = Convert.ToInt32(tempArr[5]);
|
|||
|
|
|||
|
if (status == 1)
|
|||
|
{
|
|||
|
SafeMobileLib.Utils.WriteLine(localID+"Got radio enable current radioGWid:" + radioGWid + " recv_radioGWid:" + radioid_recv);
|
|||
|
if (Main.capacityPlus)
|
|||
|
{
|
|||
|
rc.SendRemoteEnable(id);
|
|||
|
}
|
|||
|
else
|
|||
|
if (radioGWid == radioid_recv)
|
|||
|
{
|
|||
|
rc.SendRemoteEnable(id);
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
SafeMobileLib.Utils.WriteLine(localID + "Got radio disable current radioGWid:" + radioGWid + " recv_radioGWid:" + radioid_recv);
|
|||
|
if (Main.capacityPlus)
|
|||
|
{
|
|||
|
rc.SendRemoteDisable(id);
|
|||
|
}
|
|||
|
else
|
|||
|
if (radioGWid == radioid_recv)
|
|||
|
{
|
|||
|
rc.SendRemoteDisable(id);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
break;
|
|||
|
default:
|
|||
|
//SafeMobileLib.Utils.WriteLine("Unknown opcode for BusMessageParser file: RADIOCOMHANDLER.cs " + opCode);
|
|||
|
break;
|
|||
|
}
|
|||
|
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
public void Disconnect()
|
|||
|
{
|
|||
|
/*
|
|||
|
if (rc != null)
|
|||
|
{
|
|||
|
rc.Stop();
|
|||
|
rc = null;
|
|||
|
}
|
|||
|
*/
|
|||
|
if (udpMulticast != null)
|
|||
|
{
|
|||
|
udpMulticast.StopListen();
|
|||
|
udpMulticast = null;
|
|||
|
}
|
|||
|
rc.OnRemoteControlEvent -= new RadioConnection.RemoteControlEvent(RemoteStunThread_OnRemoteControlEvent);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|