245 lines
8.2 KiB
C#
245 lines
8.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Net.Sockets;
|
|
using System.Xml;
|
|
using System.Threading;
|
|
|
|
namespace SafeNetLib
|
|
{
|
|
public class Address
|
|
{
|
|
private string value;
|
|
public string Value
|
|
{
|
|
get { return this.value; }
|
|
set { this.value = value; }
|
|
}
|
|
|
|
public string lat;
|
|
public string lng;
|
|
public string addr_hash;
|
|
public htCell_t cell;
|
|
|
|
private TcpClient tcpClient;
|
|
private NetworkStream stream;
|
|
private Boolean running = true;
|
|
|
|
public Address()
|
|
{
|
|
}
|
|
|
|
public Address(htCell_t cell)
|
|
{
|
|
lat = cell.lat;
|
|
lng = cell.lng;
|
|
addr_hash = Utils.Compute4digitALG(cell.d_lat, cell.d_lng);
|
|
this.cell = cell;
|
|
}
|
|
|
|
public Address(double lat, double lng)
|
|
{
|
|
addr_hash = Utils.Compute4digitALG(lat, lng);
|
|
this.lat = lat.ToString();
|
|
this.lng = lng.ToString();
|
|
}
|
|
|
|
public void Compute()
|
|
{
|
|
while (running)
|
|
{
|
|
try
|
|
{
|
|
if (SN_Queues.computeAddressQueue.Count > 0)
|
|
{
|
|
Address gps_addr = SN_Queues.computeAddressQueue.GetItem(100);
|
|
if (gps_addr != null)
|
|
{
|
|
bool OK;
|
|
//string addr = GetADDrFromGeocoder(gps_addr.lat, gps_addr.lng, out OK);
|
|
//string addr = GetADDrFromServer(gps_addr.lat, gps_addr.lng, out OK);
|
|
string addr = GetADDrFromGoogle(gps_addr.lat, gps_addr.lng, out OK);
|
|
Utils.ConsWrite(DebugMSG_Type.Debug, "lat:" + gps_addr.lat + " lng:" + gps_addr.lng + " Addr:" + addr);
|
|
|
|
if (OK)
|
|
{
|
|
//add data to our own queue
|
|
lock (SN_Queues.ht_addressList.SyncRoot)
|
|
{
|
|
if (SN_Queues.ht_addressList.ContainsKey(gps_addr.addr_hash))
|
|
{
|
|
((Address)SN_Queues.ht_addressList[gps_addr.addr_hash]).Value = addr;
|
|
}
|
|
}
|
|
//add to DB
|
|
gps_addr.Value = addr;
|
|
}
|
|
else
|
|
{
|
|
//add to DB
|
|
gps_addr.Value = "N/A";
|
|
}
|
|
|
|
|
|
DBhandle.UpdateAddressValue(gps_addr);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
DBhandle.GetUnComputeAddr();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.always, ex.ToString());
|
|
}
|
|
Thread.Sleep(10);
|
|
}
|
|
|
|
|
|
}
|
|
|
|
public string GetADDrFromGoogle(string lat, string lng, out bool OK)
|
|
{
|
|
string addr = "";
|
|
OK = false;
|
|
try
|
|
{
|
|
XmlDocument doc = new XmlDocument();
|
|
doc.Load("http://maps.googleapis.com/maps/api/geocode/xml?latlng=" + lat + "," + lng + "&sensor=false");
|
|
|
|
XmlNode element = doc.SelectSingleNode("//GeocodeResponse/status");
|
|
if (element.InnerText == "OK")
|
|
{
|
|
element = doc.SelectSingleNode("//GeocodeResponse/result/formatted_address");
|
|
addr = element.InnerText.Replace("'", "`");
|
|
OK = true;
|
|
}
|
|
else
|
|
{
|
|
OK = false;
|
|
Utils.ConsWrite(DebugMSG_Type.Address, "Failed to retrevi address for lat:" + lat + " lng:" + lng + " response:" + element.InnerText);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.always, ex.ToString());
|
|
}
|
|
return addr;
|
|
}
|
|
|
|
public string GetADDrFromGeocoder(string lat, string lng, out bool OK)
|
|
{
|
|
string addr = "";
|
|
OK = false;
|
|
try
|
|
{
|
|
XmlDocument doc = new XmlDocument();
|
|
//doc.Load("http://maps.googleapis.com/maps/api/geocode/xml?latlng=" + lat + "," + lng + "&sensor=false");
|
|
doc.Load("http://geocoder.ca/?auth=156773750766443301201x1900&latt=" + lat + "&longt=" + lng + "&reverse=1&geoit=xml");
|
|
XmlNode element = doc.SelectSingleNode("//geodata/error");
|
|
if (element == null)
|
|
{
|
|
element = doc.SelectSingleNode("//geodata/stnumber");
|
|
string strnr = element.InnerText.Replace("'", "`");
|
|
|
|
element = doc.SelectSingleNode("//geodata/staddress");
|
|
string staddress = element.InnerText.Replace("'", "`");
|
|
|
|
element = doc.SelectSingleNode("//geodata/city");
|
|
string city = element.InnerText.Replace("'", "`");
|
|
|
|
element = doc.SelectSingleNode("//geodata/prov");
|
|
string prov = element.InnerText.Replace("'", "`");
|
|
|
|
element = doc.SelectSingleNode("//geodata/postal");
|
|
string postal = element.InnerText.Replace("'", "`");
|
|
|
|
addr = strnr + " " + staddress + " " + city + " " + prov + " " + postal;
|
|
OK = true;
|
|
}
|
|
else
|
|
{
|
|
OK = false;
|
|
Utils.ConsWrite(DebugMSG_Type.Address, "Failed to retrevi address for lat:" + lat + " lng:" + lng);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.always, ex.ToString());
|
|
}
|
|
return addr;
|
|
}
|
|
|
|
static ASCIIEncoding encoder = new ASCIIEncoding();
|
|
|
|
public string GetADDrFromServer(string lat, string lng, out bool OK)
|
|
{
|
|
string addr = "";
|
|
OK = false;
|
|
|
|
try
|
|
{
|
|
if (tcpClient != null)
|
|
{
|
|
if (!tcpClient.Connected)
|
|
{
|
|
tcpClient.Close();
|
|
tcpClient = null;
|
|
//start TCP
|
|
StartTCP();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
StartTCP();
|
|
}
|
|
if (tcpClient.Connected && stream.CanWrite)
|
|
{
|
|
string temp = "#" + lat + "#" + lng + "#";
|
|
byte[] data = Encoding.ASCII.GetBytes(temp);
|
|
stream.Write(data, 0, data.Length);
|
|
|
|
data = new byte[1024];
|
|
int recv = stream.Read(data, 0, data.Length);
|
|
string str = System.Text.Encoding.ASCII.GetString(data, 0, recv);
|
|
str = str.Replace("\n", "");
|
|
Console.WriteLine("############## Str: " + str + " ##################");
|
|
if (str != "<Lookup Failed>" && str != "")
|
|
{
|
|
OK = true;
|
|
addr = str;
|
|
}
|
|
else
|
|
{
|
|
addr = "N/A";
|
|
OK = false;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.always, ex.ToString());
|
|
}
|
|
return addr;
|
|
}
|
|
|
|
public void StartTCP()
|
|
{
|
|
tcpClient = new TcpClient("173.167.145.205", 10116);
|
|
|
|
stream = tcpClient.GetStream();
|
|
}
|
|
|
|
public void StopAddressThread()
|
|
{
|
|
try
|
|
{
|
|
running = false;
|
|
tcpClient.Close();
|
|
}
|
|
catch (Exception) { }
|
|
}
|
|
}
|
|
}
|