📘 C# Blog Series – Blog 52 **“C# REST API + GPS Tracking — Device Data को API के Through कैसे Serve करें?

📘 C# Blog Series – Blog 52

**“C# REST API + GPS Tracking — Device Data को API के Through कैसे Serve करें?






(Real-Life Example: Live Location, History, Alerts, Device List APIs)”**

WebSocket से live updates मिलते हैं,
लेकिन REST APIs के बिना कोई भी GPS platform complete नहीं होता।

Mobile App, Web Dashboard, Reports, Admin Panel —
सब REST APIs से ही data लेते हैं।

आज हम सीखेंगे:

✔ REST API क्या है
✔ ASP.NET Core Web API basics
✔ GPS tracking के real API endpoints
✔ GET / POST / PUT / DELETE
✔ DTOs & clean responses
✔ Security basics (token)
✔ Performance tips


🟦 1. REST API क्या होता है? (Simple Definition)

REST API =
HTTP के ज़रिये data को structured तरीके से provide करना

Client (App/Web) → HTTP Request
Server (API) → JSON Response

Example:

GET /api/devices

Response:

[ { "imei": "8611...", "lat": 26.85, "lon": 80.94 } ]

🟢 2. GPS System में REST API कहाँ use होती है?

✔ Device list दिखाने के लिए
✔ Current location fetch
✔ Route history
✔ Alerts history
✔ Reports (daily/weekly)
✔ Settings save
✔ User management

Live movement → WebSocket
Static / On-demand data → REST API


3. ASP.NET Core Web API Project Setup

Create project:

dotnet new webapi -n GpsApi

Folder structure (recommended):

Controllers/ Services/ Models/ DTOs/ Repositories/

🌟 4. GPSDevice Model (Simple)

public class GPSDevice { public string IMEI { get; set; } public double Latitude { get; set; } public double Longitude { get; set; } public int Speed { get; set; } public int Battery { get; set; } public DateTime LastUpdated { get; set; } }

🟠 5. Device Controller (Core API)

[ApiController] [Route("api/devices")] public class DevicesController : ControllerBase { private readonly IDeviceService _service; public DevicesController(IDeviceService service) { _service = service; } }

🔵 6. GET: All Devices List

[HttpGet] public IActionResult GetAll() { var devices = _service.GetAllDevices(); return Ok(devices); }

API:

GET /api/devices

Used in:
✔ Dashboard list
✔ Admin panel


🟣 7. GET: Single Device Live Location

[HttpGet("{imei}")] public IActionResult GetByImei(string imei) { var device = _service.GetByImei(imei); if(device == null) return NotFound(); return Ok(device); }

API:

GET /api/devices/861128068064267

Used in:
✔ Map load
✔ Device detail page


🔥 8. GET: Device Route History

[HttpGet("{imei}/history")] public IActionResult GetHistory(string imei, [FromQuery] DateTime from, [FromQuery] DateTime to) { var data = _service.GetHistory(imei, from, to); return Ok(data); }

API:

GET /api/devices/8611/history?from=2025-12-01&to=2025-12-02

Used in:
✔ Playback
✔ Reports


🧠 9. POST: Save Device Settings

[HttpPost("{imei}/settings")] public IActionResult SaveSettings(string imei, DeviceSettingsDto dto) { _service.SaveSettings(imei, dto); return Ok(new { status = "saved" }); }

Used in:
✔ Geo-fence
✔ Speed limit
✔ Alerts config


📌 10. DTOs (Best Practice)

Never expose DB models directly.

public class DeviceDto { public string IMEI { get; set; } public double Lat { get; set; } public double Lon { get; set; } public int Speed { get; set; } }

Return lightweight data → faster API.


🛡 11. API Security (Basic Token Example)

[Authorize] [HttpGet] public IActionResult GetAll() { ... }

Client sends:

Authorization: Bearer TOKEN

GPS APIs without auth = dangerous.


12. Performance Tips (GPS APIs)

✔ Always use async

public async Task<IActionResult> GetAll()

✔ Index IMEI column
✔ Cache device list in memory
✔ Don’t join heavy tables
✔ Paginate history data
✔ Use DTOs only


🧪 13. API + WebSocket Together (Perfect Combo)

Pattern used by all GPS companies:

  • REST API → initial data load

  • WebSocket → live movement

Flow:

Page LoadGET /api/devices Live Update → WebSocket push

⚠ Common Mistakes (Avoid)**

❌ Returning huge JSON
❌ Blocking DB calls
❌ No pagination
❌ No authentication
❌ Using entity models directly
❌ Mixing business logic in controller


🔚 Conclusion

REST API आपके GPS platform को बनाता है:

✔ Structured
✔ Scalable
✔ Secure
✔ App-friendly
✔ Report-ready
✔ Industry standard

GPS Tracking =
TCP (device) + WebSocket (live) + REST API (data)
तीनों मिलकर complete system बनाते हैं।

Post a Comment

0 Comments

Translate

Close Menu