string GetPacket(TrackerPacket point, int isResend) { decimal Alarm = 0; point.GetInput("ALARM", out Alarm); int timestamp, packettimestamp, servertimestamp; packettimestamp = point.m_Time; DateTime dt = CTime.GetTime(Convert.ToInt64(packettimestamp)); servertimestamp = Convert.ToInt32(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds); if (isResend > 0) { timestamp = servertimestamp; } else { timestamp = packettimestamp; } string str = "{\"deviceId\": \"" + m_Tracker.m_IMEI + "\", " + "\"latitude\": \"" + point.m_fLat + "\", " + "\"longitude\": \"" + point.m_fLng + "\", " + "\"timeStamp\": \"" + timestamp + "\", " + "\"speed\": \"" + CObject.Float2XML(point.m_Speed / 3.6) + "\", " + "\"sos\": \"" + ((Alarm != 0) ? "True": "False") + "\", " + "\"altitude\": \"" + point.m_Alt + "\", " + "\"vehicleNo\": \"" + m_Tracker.m_strName + "\", " + "\"isValid\": \"" + (point.IsFixed(false) ? "True" : "False") + "\", " + "\"packetTimeStamp\": \"" + packettimestamp + "\"," + "\"serverTimeStamp\": \"" + servertimestamp + "\"" + "}"; return(str); }
////////////////////////////////////////////////////////////////////// public bool PushPacketWithID(TrackerPacket packet) { try { CTracker tracker = ConfigMgr.inst().GetTrackerByID((int)packet.m_ID); if (tracker != null) { if (packet.IsValid()) { lock (m_Queue) m_Queue.Enqueue(packet); if (packet.IsFixed(true)) { CurPosMgr.inst().AddPoint(packet); } LastPointMgr.inst().AddPoint(packet); } return(true); } } catch (Exception ex) { m_Logger.Push(LogLevel.ERROR, 0, "PushPacket:" + ex.ToString()); } return(false); }
string GetPacket(TrackerPacket point) { decimal Alarm = 0; point.GetInput("ALARM", out Alarm); string str = "{\"deviceId\": \"" + m_Tracker.m_IMEI + "\", " + "\"latitude\": \"" + point.m_fLat + "\", " + "\"longitude\": \"" + point.m_fLng + "\", " + "\"timeStamp\": \"" + point.m_Time + "\", " + "\"speed\": \"" + CObject.Float2XML(point.m_Speed / 3.6) + "\", " + "\"sos\": \"" + ((Alarm != 0) ? "True": "False") + "\", " + "\"altitude\": \"" + point.m_Alt + "\", " + "\"vehicleNo\": \"" + m_Tracker.m_strName + "\", " + "\"isValid\": \"" + (point.IsFixed(false) ? "True" : "False") + "\"}"; return(str); }
// У$DIMTS,null,null,null,357852031069696,DL1PC4716,31.12.2013,06:54:11,28.5271550,null,77.2807010,null,12.5,1,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null#Ф string GetPacket(TrackerPacket point) { DateTime dt = CTime.GetTime(point.m_Time); decimal odo = 0, pwr = 0, acc = 0; point.GetInput("ODO", out odo); point.GetInput("ACC", out acc); point.GetInput("PWR", out pwr); string res = "$DIMTS,null,null,null"; res += "," + m_Tracker.m_IMEI; res += "," + m_Tracker.m_strName; res += "," + dt.ToString("dd.MM.yyyy"); res += "," + dt.ToString("HH:mm:ss"); res += "," + CObject.Float2XML(Math.Abs(point.m_fLat)); res += "," + ((point.m_fLat >= 0) ? "N" : "S"); res += "," + CObject.Float2XML(Math.Abs(point.m_fLng)); res += "," + ((point.m_fLng >= 0) ? "E" : "W"); res += "," + point.m_Speed; res += "," + (point.IsFixed(false) ? "1" : "0"); res += "," + point.m_SatteliteCount; res += ",null";// + 1; //DOP res += "," + point.m_Direction; res += "," + point.m_Alt; res += ",null"; // + 0; res += "," + Math.Round(odo); //ODO res += ",null"; //IGN res += ",null"; // + CObject.Float2XML((double)acc / 1000.0); //Batt res += ",null"; // + CObject.Float2XML((double)pwr / 1000.0); //POWER res += ",null"; // + "00000000"; //Digitalinputvalues res += ",null"; //NetworkOperatorName res += ",null"; // + 15; //GSMStrength res += ",null"; // + 0; //CellID res += ",null"; // + 0; //LAC res += ",null"; // + 1; //SeqNo res += ",null"; // + 0; //AvgSpd res += ",null"; // + 0; //MinSpd res += "#"; m_Logger.Push(LogLevel.INFO, 0, res); return(res); }
byte[] GetPacket(TrackerPacket point) { int i = 0; byte[] buf = new byte[45 + 20]; for (int j = 0; j < buf.Length; j++) { buf[j] = 0; } buf[i++] = 0x29; buf[i++] = 0x29; buf[i++] = 0x80; i += 2; // length uint imei = (uint)(m_Tracker.m_IMEI - 14100000000); for (int k = 0; k < 4; k++) { buf[i + 3 - k] = (byte)(imei % 100); if (k != 2) { buf[i + 3 - k] |= 0x80; } imei = imei / 100; } i += 4; DateTime dt = CTime.GetTime(point.m_Time + 3600 * 2); buf[i++] = BCDConverter.GetBytes(dt.Year - 2000, 1)[0]; buf[i++] = BCDConverter.GetBytes(dt.Month, 1)[0]; buf[i++] = BCDConverter.GetBytes(dt.Day, 1)[0]; buf[i++] = BCDConverter.GetBytes(dt.Hour, 1)[0]; buf[i++] = BCDConverter.GetBytes(dt.Minute, 1)[0]; buf[i++] = BCDConverter.GetBytes(dt.Second, 1)[0]; GetCoord(point.m_fLat).CopyTo(buf, i); i += 4; GetCoord(point.m_fLng).CopyTo(buf, i); i += 4; BCDConverter.GetBytes(point.m_Speed, 2).CopyTo(buf, i); i += 2; BCDConverter.GetBytes(point.m_Direction, 2).CopyTo(buf, i); i += 2; decimal odo = 0, ignition = 0, oilcut = 0, pwr = 12000; decimal din1 = 0, din2 = 0, din3 = 0, din4 = 0; point.GetInput("DIN6", out ignition); point.GetInput("DIN7", out oilcut); point.GetInput("PWR", out pwr); point.GetInput("ODO", out odo); point.GetInput("DIN1", out din1); point.GetInput("DIN2", out din2); point.GetInput("DIN3", out din3); point.GetInput("DIN4", out din4); BIConverter.GetBytes((int)odo).CopyTo(buf, i); buf[i++] = (byte)(((pwr == 0) ? 16 : (pwr > 3000) ? 24 : 8) + ((point.IsFixed(false) ? 224 : 0))); i += 3; buf[i++] = (byte)(1 + ((oilcut > 0) ? 0 : 4) + ((din1 > 0) ? 0 : 8) + ((din2 > 0) ? 0 : 16) + ((din3 > 0) ? 0 : 32) + ((din4 > 0) ? 0 : 64) + ((ignition > 0) ? 0 : 128)); //ign, pwr, oil cut buf[i++] = 0xFC; //alarms buf[i++] = 0x36; // 32;//tcp buf[i++] = 0x00; buf[i++] = 0x00; buf[i++] = 0x78; buf[i++] = 0x00; buf[i++] = 0x00; buf[i++] = 0x00; buf[i++] = 0x00; buf[i++] = 0x3C; buf[i++] = 0x00; decimal fuel = 0; if (point.GetInput("FUEL", out fuel)) { string data = fuel.ToString("000.00").Replace(",", "."); BIConverter.GetBytes((ushort)(data.Length + 2)).CopyTo(buf, i); i += 2; BIConverter.GetBytes((ushort)0x0023).CopyTo(buf, i); i += 2; Encoding.ASCII.GetBytes(data).CopyTo(buf, i); i += data.Length; } if (point.GetInput("AIN5", out fuel)) { BIConverter.GetBytes((ushort)4).CopyTo(buf, i); i += 2; BIConverter.GetBytes((ushort)0x002D).CopyTo(buf, i); i += 2; BIConverter.GetBytes((ushort)fuel).CopyTo(buf, i); i += 2; } BIConverter.GetBytes((ushort)(i - 3)).CopyTo(buf, 3); buf[i++] = GetCRC(buf, 0, i); buf[i++] = 0x0D; byte[] res = new byte[i]; string str = "<<"; for (int j = 0; j < i; j++) { res[j] = buf[j]; str += " " + buf[j].ToString("X2"); } m_Logger.Push(LogLevel.INFO, 0, str); return(res); }
////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// void FlushData(object obj) //записываем данные в базу данных { if (m_bFlushing) { return; } m_bFlushing = true; //записать в базу все точки lock (m_Queue) if (m_Queue.Count > 100000) { m_Queue.Clear(); m_Logger.Push(LogLevel.ERROR, 0, "Points queue purged"); } using (CDatabase db = Configuration.inst().GetDB()) { if (!db.IsConnected()) { m_bFlushing = false; m_Logger.Push(LogLevel.ERROR, 0, "Cant connect to db"); return; } if (m_Queue.Count > 0) { try { using (MySqlCommand cmdUpdate = new MySqlCommand("UPDATE curpos SET Time=?Time, Lat=?Lat, Lng=?Lng, Status=?Status, Speed=?Speed, Alt=?Alt, IO=?IO, GSMInfo=?GSMInfo WHERE TrackerID = ?ID AND Time <= ?Time", db.connection)) using (MySqlCommand cmdUpdate1 = new MySqlCommand("UPDATE lastpoint SET Time=?Time, Lat=?Lat, Lng=?Lng, Status=?Status, Speed=?Speed, Alt=?Alt, IO=?IO, GSMInfo=?GSMInfo WHERE TrackerID = ?ID AND Time <= ?Time", db.connection)) using (MySqlCommand cmdInsert = new MySqlCommand("INSERT INTO points(TrackerID, Time, Lat, Lng, Status, Speed, Alt, IO, GSMInfo,ForwardTime,ReceivedTime) VALUES(?ID, ?Time, ?Lat, ?Lng, ?Status, ?Speed, ?Alt, ?IO, ?GSMInfo,0," + CTime.GetTime(DateTime.UtcNow) + ") ON DUPLICATE KEY UPDATE TrackerID=TrackerID", db.connection)) { cmdUpdate.Parameters.Add("?ID", MySqlDbType.Int32); cmdInsert.Parameters.Add("?ID", MySqlDbType.Int32); cmdUpdate.Parameters.Add("?Time", MySqlDbType.Int32); cmdInsert.Parameters.Add("?Time", MySqlDbType.Int32); cmdUpdate.Parameters.Add("?Lat", MySqlDbType.Float); cmdInsert.Parameters.Add("?Lat", MySqlDbType.Float); cmdUpdate.Parameters.Add("?Lng", MySqlDbType.Float); cmdInsert.Parameters.Add("?Lng", MySqlDbType.Float); cmdUpdate.Parameters.Add("?Alt", MySqlDbType.Int32); cmdInsert.Parameters.Add("?Alt", MySqlDbType.Int32); cmdUpdate.Parameters.Add("?Speed", MySqlDbType.Int32); cmdInsert.Parameters.Add("?Speed", MySqlDbType.Int32); cmdUpdate.Parameters.Add("?Status", MySqlDbType.Int32); cmdInsert.Parameters.Add("?Status", MySqlDbType.Int32); cmdUpdate.Parameters.Add("?IO", MySqlDbType.Blob); cmdInsert.Parameters.Add("?IO", MySqlDbType.Blob); cmdUpdate.Parameters.Add("?GSMInfo", MySqlDbType.UInt64); cmdInsert.Parameters.Add("?GSMInfo", MySqlDbType.UInt64); cmdUpdate1.Parameters.Add("?ID", MySqlDbType.Int32); cmdUpdate1.Parameters.Add("?Time", MySqlDbType.Int32); cmdUpdate1.Parameters.Add("?Lat", MySqlDbType.Float); cmdUpdate1.Parameters.Add("?Lng", MySqlDbType.Float); cmdUpdate1.Parameters.Add("?Alt", MySqlDbType.Int32); cmdUpdate1.Parameters.Add("?Speed", MySqlDbType.Int32); cmdUpdate1.Parameters.Add("?Status", MySqlDbType.Int32); cmdUpdate1.Parameters.Add("?IO", MySqlDbType.Blob); cmdUpdate1.Parameters.Add("?GSMInfo", MySqlDbType.UInt64); using (MySqlTransaction trans = db.connection.BeginTransaction()) { while (true) { lock (m_Queue) if (m_Queue.Count > 0) { TrackerPacket point = m_Queue.Dequeue(); if (point.IsFixed(true) && !point.Write2DB(cmdUpdate)) { break; } if (!point.Write2DB(cmdInsert)) { break; } if (!point.Write2DB(cmdUpdate1)) { break; } } else { break; } } trans.Commit(); } } } catch (Exception e) { m_Logger.Push(LogLevel.ERROR, 0, "Points " + e.ToString()); } } //online режим try { using (MySqlTransaction trans = db.connection.BeginTransaction()) { using (MySqlCommand cmd1 = new MySqlCommand("UPDATE curpos SET online = 0", db.connection)) cmd1.ExecuteNonQuery(); using (MySqlCommand cmd = new MySqlCommand("UPDATE curpos SET online = 1 WHERE TrackerID = ?ID", db.connection)) { cmd.Parameters.Add("?ID", MySqlDbType.Int32); lock (m_SessionTrackers) foreach (KeyValuePair <uint, long> value in m_SessionTrackers) { cmd.Parameters["?ID"].Value = value.Value; cmd.ExecuteNonQuery(); } } trans.Commit(); } } catch (Exception e) { m_Logger.Push(LogLevel.ERROR, 0, "Events " + e.ToString()); } } GC.Collect(); m_bFlushing = false; }