public static GpsInfo DecodeGps(byte[] data) { // Header begin// string crc = ByteArrayToHexString(data.Skip(2).Take(2).ToArray()); int length = int.Parse(ByteArrayToHexString(data.Skip(4).Take(2).ToArray())); int seqId = int.Parse(ByteArrayToHexString(data.Skip(6).Take(2).ToArray())); string imei = ByteArrayToHexString(data.Skip(8).Take(8).ToArray());//.Substring(0, 15); // Header End// // Data Begin// var time = int.Parse(ByteArrayToHexString(data.Skip(16).Take(4).ToArray())); DateTime gpsTime = ConvertFromUnixTimestamp(time); var rtime = int.Parse(ByteArrayToHexString(data.Skip(24).Take(4).ToArray())); DateTime recTime = ConvertFromUnixTimestamp(rtime); double lon = double.Parse(ByteArrayToHexString(data.Skip(28).Take(4).ToArray())) * 0.000001; double lat = double.Parse(ByteArrayToHexString(data.Skip(32).Take(4).ToArray())) * 0.000001; int heading = int.Parse(ByteArrayToHexString(data.Skip(36).Take(2).ToArray())); int reportId = int.Parse(ByteArrayToHexString(data.Skip(38).Take(1).ToArray())); double odometer = double.Parse(ByteArrayToHexString(data.Skip(39).Take(4).ToArray())) * 0.1; int speed = int.Parse(ByteArrayToHexString(data.Skip(46).Take(2).ToArray())); // Data End// return(GpsInfo.Create(imei, seqId, length, crc, gpsTime, recTime, lon, lat, heading, odometer, 0, speed, 0, "", "", true)); }
public static GpsInfo AsciiDecodeGps(string[] data, string imei) { int reportId = int.Parse(data[6]); DateTime gpsTime = ConvertFromUnixTimestamp(Convert.ToDouble(data[0])).AddHours(4); DateTime recTime = ConvertFromUnixTimestamp(Convert.ToDouble(data[2])).AddHours(4); double lon = double.Parse(data[3]) * 0.000001; double lat = double.Parse(data[4]) * 0.000001; int heading = int.Parse(data[5]); double odometer = double.Parse(data[7]) * 0.1; int speed = int.Parse(data[10]); string driverID = data[13]; string eventType = reportId.ToString(); return(GpsInfo.Create(imei, 0, 0, "", gpsTime, recTime, lon, lat, heading, odometer, 0, speed, reportId, driverID, eventType, true)); }
public static void DbInsert(GpsInfo data) { SqlConnection conn = new SqlConnection(GetConnectionString()); string sql = "INSERT INTO [dbo].[TBL_ATRACK_MASTER]" + " (IMEI,Length,Crc,SeqId,GPS_DateTime,Received_DateTime,Longitude,Latitude,Odometer,Heading,Speed) " + "VALUES " + "(@IMEI,@Length,@Crc,@SeqId,@GPS_DateTime,@Received_DateTime,@Longitude,@Latitude,@Odometer,@Heading,@Speed) SELECT SCOPE_IDENTITY()"; try { if (conn.State != ConnectionState.Open) { conn.Open(); } SqlCommand cmd = new SqlCommand(sql, conn); List <SqlParameter> sp = new List <SqlParameter>(); sp.Add(new SqlParameter() { ParameterName = "@IMEI", SqlDbType = SqlDbType.NVarChar, Value = data.IMEI }); sp.Add(new SqlParameter() { ParameterName = "@Length", SqlDbType = SqlDbType.Int, Value = data.Length }); sp.Add(new SqlParameter() { ParameterName = "@Crc", SqlDbType = SqlDbType.NVarChar, Value = data.Crc }); sp.Add(new SqlParameter() { ParameterName = "@SeqId", SqlDbType = SqlDbType.Int, Value = data.SeqId }); sp.Add(new SqlParameter() { ParameterName = "@GPS_DateTime", SqlDbType = SqlDbType.DateTime, Value = data.GPS_DateTime }); sp.Add(new SqlParameter() { ParameterName = "@Received_DateTime", SqlDbType = SqlDbType.DateTime, Value = data.Received_DateTime }); sp.Add(new SqlParameter() { ParameterName = "@Longitude", SqlDbType = SqlDbType.Float, Value = data.Longitude }); sp.Add(new SqlParameter() { ParameterName = "@Latitude", SqlDbType = SqlDbType.Float, Value = data.Latitude }); sp.Add(new SqlParameter() { ParameterName = "@Odometer", SqlDbType = SqlDbType.Float, Value = data.Odometer }); sp.Add(new SqlParameter() { ParameterName = "@Heading", SqlDbType = SqlDbType.Int, Value = data.Heading }); sp.Add(new SqlParameter() { ParameterName = "@Speed", SqlDbType = SqlDbType.Int, Value = data.Speed }); cmd.Parameters.AddRange(sp.ToArray()); cmd.CommandType = CommandType.Text; var id = cmd.ExecuteScalar(); } catch (System.Data.SqlClient.SqlException ex) { string msg = "Insert Error:"; msg += ex.Message; AtrackMain.LogData(msg); throw new Exception(msg); } finally { conn.Close(); } }
public static void DbUpdate(GpsInfo data, string imei, string rawData) { string OwnerName = string.Empty; string Email = string.Empty; string RegNo = string.Empty; string mailSent = string.Empty; using (SqlConnection conn = new SqlConnection(GetConnectionString())) { try { using (SqlCommand cmd = new SqlCommand("SP_Insert_NewPointware", conn)) { List <SqlParameter> sp = new List <SqlParameter>() { new SqlParameter() { ParameterName = "@UNIT_ID", SqlDbType = SqlDbType.NVarChar, Value = "AT" + imei.Substring(11, 4) }, }; sp.Add(new SqlParameter() { ParameterName = "@LON", SqlDbType = SqlDbType.Float, Value = data.Longitude }); sp.Add(new SqlParameter() { ParameterName = "@LAT", SqlDbType = SqlDbType.Float, Value = data.Latitude }); sp.Add(new SqlParameter() { ParameterName = "@SPEED", SqlDbType = SqlDbType.Int, Value = data.Speed }); sp.Add(new SqlParameter() { ParameterName = "@DIRECTION", SqlDbType = SqlDbType.Int, Value = data.Heading }); sp.Add(new SqlParameter() { ParameterName = "@LOCAL_DATE_TIME", SqlDbType = SqlDbType.DateTime, Value = data.GPS_DateTime }); sp.Add(new SqlParameter() { ParameterName = "@DISTANCE", SqlDbType = SqlDbType.Float, Value = 0 }); sp.Add(new SqlParameter() { ParameterName = "@SERVER_TIME", SqlDbType = SqlDbType.VarChar, Value = DateTime.UtcNow }); sp.Add(new SqlParameter() { ParameterName = "@ATTRIBUTE1", SqlDbType = SqlDbType.VarChar, Value = data.EventType }); //SOS sp.Add(new SqlParameter() { ParameterName = "@ATTRIBUTE2", SqlDbType = SqlDbType.VarChar, Value = data.DriverID }); //immobilizer sp.Add(new SqlParameter() { ParameterName = "@CHARGING", SqlDbType = SqlDbType.Bit, Value = true }); //Charging sp.Add(new SqlParameter() { ParameterName = "@LOWBATT", SqlDbType = SqlDbType.Bit, Value = false }); //LOW BATTERY sp.Add(new SqlParameter() { ParameterName = "@IGNITION", SqlDbType = SqlDbType.Bit, Value = data.EventType == "IgnitionOn"? true: false }); //IGNITION sp.Add(new SqlParameter() { ParameterName = "@ANALOG_DATA", SqlDbType = SqlDbType.NVarChar, Value = "" }); //ANALOG_DATA sp.Add(new SqlParameter() { ParameterName = "@status", SqlDbType = SqlDbType.NVarChar, Value = data.EventType == "IdleOn" ? "ON" : "OFF" }); sp.Add(new SqlParameter() { ParameterName = "@reportID", SqlDbType = SqlDbType.Int, Value = data.ReportID }); sp.Add(new SqlParameter() { ParameterName = "@imei", SqlDbType = SqlDbType.NVarChar, Value = data.IMEI }); sp.Add(new SqlParameter() { ParameterName = "@eventValue", SqlDbType = SqlDbType.NVarChar, Value = data.EventType == "RFID" ? data.DriverID : data.EventType }); sp.Add(new SqlParameter() { ParameterName = "@validEvent", SqlDbType = SqlDbType.Bit, Value = data.ValidEvent }); sp.Add(new SqlParameter() { ParameterName = "@RAW_DATA", SqlDbType = SqlDbType.NVarChar, Value = rawData }); sp.Add(new SqlParameter() { ParameterName = "@ServerDateTime", SqlDbType = SqlDbType.DateTime, Value = DateTime.UtcNow }); cmd.Parameters.AddRange(sp.ToArray()); cmd.CommandType = CommandType.StoredProcedure; conn.Open(); cmd.ExecuteNonQuery(); } } catch (System.Data.SqlClient.SqlException ex) { string msg = "Insert Error:"; msg += ex.Message; throw new Exception(msg); } catch (Exception ex) { string msg = "Insert Error"; } finally { conn.Close(); } } }