/// <summary> /// Create an new Instance of the TCP-Listener on Port 5000 /// </summary> internal Server() { try { AnrlDB.AnrlDataContext db = new AnrlDB.AnrlDataContext(); if (!db.DatabaseExists()) { db.CreateDatabase(); } CalculateTabels = new System.Timers.Timer(20000); CalculateTabels.Elapsed += new ElapsedEventHandler(CalculateTabels_Elapsed); CalculateTabels.Start(); running = true; this.tcpListener = new TcpListener(IPAddress.Any, 5000); this.listenThread = new Thread(new ThreadStart(ListenForClients)); this.listenThread.Start(); db.Dispose(); } catch (Exception ex) { Logger.Log("Exception in Server.Server" + ex.ToString(), 11); } }
public Root PostData(Root root) { WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*"); WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Methods", "GET, POST, OPTIONS"); WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Headers", "Content-Type"); WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Max-Age", "1728000"); AnrlDataContext db = new AnrlDataContext(); Root response = new Root(); response.response = new Response(); response.response.countAdded = 0; try { if (root != null && root.gpsdata.Count > 0) { t_Tracker tracker; if (db.t_Trackers.Count(p => p.IMEI == root.gpsdata[0].identifier) == 1) { tracker = db.t_Trackers.Single(p => p.IMEI == root.gpsdata[0].identifier); } else { tracker = new t_Tracker(); tracker.IMEI = root.gpsdata[0].identifier; db.t_Trackers.InsertOnSubmit(tracker); db.SubmitChanges(); } foreach (GPSData data in root.gpsdata) { t_Daten t_d = new t_Daten(); t_d.Accuracy = data.accuracy; t_d.Altitude = data.altitude; t_d.Bearing = data.bearing; t_d.Latitude = data.latitude; t_d.Longitude = data.longitude; t_d.Speed = data.speed; t_d.Timestamp = data.timestampGPS; t_d.ID_Tracker = tracker.ID; db.t_Datens.InsertOnSubmit(t_d); response.response.countAdded++; } db.SubmitChanges(); } } catch (Exception ex) { #if !DEBUG //Logger.Log("Exception in GPSRequestProcessor.proccessRequest" + ex.ToString(), 9); #else //System.Console.WriteLine("Exception in GPSRequestProcessor.proccessRequest " + ex.ToString()); #endif response.exception = ex.ToString(); } finally { db.Dispose(); } return response; }
public GPSRequestProcessor() { AnrlDataContext db = new AnrlDataContext(); if (!db.DatabaseExists()) { db.CreateDatabase(); } db.Dispose(); }
internal static void Log(AnrlDataContext db, string text, int level) { try { t_Log l = new t_Log(); l.level = level; l.timestamp = DateTime.Now; l.Text = text; db.t_Logs.InsertOnSubmit(l); db.SubmitChanges(); } catch (Exception ex) { System.Console.Out.WriteLine(ex.ToString()); } }
public RequestProcessor() { //Make sure DB is existent AnrlDataContext db = new AnrlDataContext(); if (!db.DatabaseExists()) { db.CreateDatabase(); } db.Dispose(); processorMap = new Dictionary<int, IProcessor>(); processorMap.Add((int)EObjectType.Picture, new PictureProcessor()); processorMap.Add((int)EObjectType.Penalty, new PenaltyProcessor()); processorMap.Add((int)EObjectType.Map, new MapProcessor()); processorMap.Add((int)EObjectType.Parcour, new ParcourProcessor()); processorMap.Add((int)EObjectType.Pilot, new PilotProcessor()); processorMap.Add((int)EObjectType.Tracker, new TrackerProcessor()); processorMap.Add((int)EObjectType.Team, new TeamProcessor()); processorMap.Add((int)EObjectType.GPSData, new GPSDataProcessor((TrackerProcessor)processorMap[(int)EObjectType.Tracker])); processorMap.Add((int)EObjectType.Competition, new CompetitionProcessor()); processorMap.Add((int)EObjectType.CompetitionSet, new CompetitionSetProcesor()); }
/// <summary> /// Handels the Data recieved and processed in the Message_Received_Processor /// Adds the Data to the Database /// For Threat-Security /// </summary> /// <param name="GPSData">THe Data</param> private void ProcessRecievedGPSData(object GPSData) { try { String incomingData = GPSData as String; if (incomingData != null) { AnrlDB.AnrlDataContext db = new AnrlDB.AnrlDataContext(); try { String trimedGPSData = incomingData.Trim(new char[] { '!', '$' }); String[] GPScoords = trimedGPSData.Split(new char[] { ',', '*' }); if (db.t_Trackers.Count(p => p.IMEI == GPScoords[0]) == 0) { t_Tracker t = new t_Tracker(); t.IMEI = GPScoords[0]; db.t_Trackers.InsertOnSubmit(t); db.SubmitChanges(); } string yy = GPScoords[3].Substring(4, 2); string mm = GPScoords[3].Substring(2, 2); string dd = GPScoords[3].Substring(0, 2); if (yy != "00" && mm != "00" && dd != "00") //Only save sensefull data { t_GPS_IN new_position = new t_GPS_IN(); new_position.IMEI = GPScoords[0]; new_position.Status = Int32.Parse(GPScoords[1]); new_position.GPS_fix = Int32.Parse(GPScoords[2]); new_position.TimestampTracker = new DateTime( Int32.Parse("20" + yy), Int32.Parse(mm), Int32.Parse(dd), Int32.Parse(GPScoords[4].Substring(0, 2)), Int32.Parse(GPScoords[4].Substring(2, 2)), Int32.Parse(GPScoords[4].Substring(4, 2))); new_position.longitude = GPScoords[5]; new_position.latitude = GPScoords[6]; new_position.altitude = GPScoords[7]; new_position.speed = GPScoords[8]; new_position.heading = GPScoords[9]; new_position.nr_used_sat = Int32.Parse(GPScoords[10]); new_position.HDOP = GPScoords[11]; new_position.Timestamp = DateTime.Now; new_position.Processed = false; db.t_GPS_INs.InsertOnSubmit(new_position); db.SubmitChanges(); } } catch (Exception ex) { Logger.Log("Exception in Server.ProcessRecievedGPSData incomming:"+incomingData + " " + ex.ToString(), 12); } finally { db.Dispose(); } } } catch { } }
/// <summary> /// Calculate the Tables of t_Data and Insert all needed Entries /// Will be trigered form a 1 sec-Timer /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void CalculateTabels_Elapsed(object sender, ElapsedEventArgs e) { AnrlDB.AnrlDataContext db = new AnrlDB.AnrlDataContext(); try { List<t_GPS_IN> Positions = db.t_GPS_INs.Where(a => !a.Processed).OrderBy(t => t.TimestampTracker).ToList(); List<t_Tracker> Trackers = db.t_Trackers.ToList(); foreach (t_Tracker tr in Trackers) { try { List<t_GPS_IN> Positions_Tracker = Positions.Where(a => a.IMEI.Trim() == tr.IMEI.Trim()).OrderBy(a => a.TimestampTracker).ToList(); foreach (t_GPS_IN GPS_IN in Positions_Tracker) { try { t_Daten InsertData = new t_Daten(); InsertData.t_Tracker = tr; InsertData.Timestamp = GPS_IN.TimestampTracker.Ticks; InsertData.Latitude = ConvertCoordinates(GPS_IN.latitude); InsertData.Longitude = ConvertCoordinates(GPS_IN.longitude); InsertData.Altitude = double.Parse(GPS_IN.altitude, NumberFormatInfo.InvariantInfo); db.t_Datens.InsertOnSubmit(InsertData); } catch (Exception ex) { Logger.Log("Exception in Server.CalculateTabels_Elapsed" + ex.ToString(), 18); } GPS_IN.Processed = true; } } catch (Exception ex) { Logger.Log("Exception in Server.CalculateTabels_Elapsed" + ex.ToString(), 19); } } db.SubmitChanges(); } catch (Exception ex) { Logger.Log("Exception in Server.CalculateTabels_Elapsed" + ex.ToString(), 20); } finally { db.Dispose(); } }
public RootMessage proccessRequest(RootMessage request) { AnrlDataContext db = new AnrlDataContext(); RootMessage response = new RootMessage(); response.response = new Response(); response.response.countAdded = 0; try { if (request != null && request.gpsdata.Count > 0) { t_Tracker tracker; if (db.t_Trackers.Count(p => p.IMEI == request.gpsdata[0].identifier) == 1) { tracker = db.t_Trackers.Single(p => p.IMEI == request.gpsdata[0].identifier); } else { tracker = new t_Tracker(); tracker.IMEI = request.gpsdata[0].identifier; db.t_Trackers.InsertOnSubmit(tracker); db.SubmitChanges(); } foreach (GPSData data in request.gpsdata) { t_Daten t_d = new t_Daten(); t_d.Accuracy = data.accuracy; t_d.Altitude = data.altitude; t_d.Bearing = data.bearing; t_d.Latitude = data.latitude; t_d.Longitude = data.longitude; t_d.Speed = data.speed; //Convert Javas millis to C#'s nano- Ticks //Hack to fix the Bug from Android (One day in future) long day = TimeSpan.TicksPerDay; long hours = TimeSpan.TicksPerHour; long timestampGPS = UTCBaseTime.Add(new TimeSpan(data.timestampGPS * TimeSpan.TicksPerMillisecond)).Ticks; long timestampSender = UTCBaseTime.Add(new TimeSpan(data.timestampSender * TimeSpan.TicksPerMillisecond)).Ticks; if ((timestampGPS - day + hours) > timestampSender) { timestampGPS -= day; } t_d.Timestamp = timestampGPS; t_d.ID_Tracker = tracker.ID; db.t_Datens.InsertOnSubmit(t_d); response.response.countAdded++; } db.SubmitChanges(); } } catch (Exception ex) { #if !DEBUG Logger.Log("Exception in GPSRequestProcessor.proccessRequest" + ex.ToString(), 9); #else //System.Console.WriteLine("Exception in GPSRequestProcessor.proccessRequest " + ex.ToString()); #endif response.exception = ex.ToString(); } finally { db.Dispose(); } return response; }
private Root proccessRegister(Root request) { Root r = new Root(); r.ResponseParameters = new ResponseParameters(); AnrlDataContext db = new AnrlDataContext(); if (db.t_Users.Count(p => p.Name == request.AuthInfo.Username) == 0) { t_User user = new t_User(); user.Name = request.AuthInfo.Username; user.Password = request.AuthInfo.Password; user.ID_Role = 0; db.t_Users.InsertOnSubmit(user); db.SubmitChanges(); r.AuthInfo = new AuthenticationInfo(); } else { r.ResponseParameters.Exception = "Username already in use"; } return r; }
private Root proccessLogin(Root request) { Root r = new Root(); r.AuthInfo = new AuthenticationInfo(); r.ResponseParameters = new ResponseParameters(); AnrlDataContext db = new AnrlDataContext(); if (db.t_Users.Count(p => p.Name == request.AuthInfo.Username && p.Password == request.AuthInfo.Password) == 1) { r.AuthInfo.Token = db.t_Users.Single(p => p.Name == request.AuthInfo.Username && p.Password == request.AuthInfo.Password).ID.ToString(); } else { r.ResponseParameters.Exception = "Username / Password wrong"; } return r; }