90 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			90 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using MQTTnet;
 | |
| using MQTTnet.Protocol;
 | |
| using Newtonsoft.Json;
 | |
| using System.Text;
 | |
| using SimulatedTrackingDevice.DTOs; // Giữ nguyên nếu class DeviceMessage ở đây
 | |
| 
 | |
| namespace SimulatedTrackingDevice
 | |
| {
 | |
|     internal class Program
 | |
|     {
 | |
|         private static readonly string BrokerIp = "localhost";
 | |
|         private static readonly int BrokerPort = 1883;
 | |
|         private static readonly string Imei = "22222test2222";
 | |
| 
 | |
|         private static readonly string UpdatePvtTopic = "iot/devices/update_PVT";
 | |
|         private static readonly string ResponseTopic = $"iot/devices/update_PVT_response/{Imei}";
 | |
| 
 | |
|         static async Task Main(string[] args)
 | |
|         {
 | |
|             var factory = new MqttClientFactory();
 | |
|             var mqttClient = factory.CreateMqttClient();
 | |
| 
 | |
|             mqttClient.ApplicationMessageReceivedAsync += async e =>
 | |
|             {
 | |
|                 string topic = e.ApplicationMessage.Topic;
 | |
|                 string payload = Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
 | |
| 
 | |
|                 if (topic == ResponseTopic)
 | |
|                 {
 | |
|                     Console.WriteLine($"📩 Received response on topic '{topic}':");
 | |
|                     Console.WriteLine(payload);
 | |
|                 }
 | |
| 
 | |
|                 await Task.CompletedTask;
 | |
|             };
 | |
| 
 | |
|             var options = new MqttClientOptionsBuilder()
 | |
|                 .WithClientId($"SimDevice-{Guid.NewGuid()}")
 | |
|                 .WithCredentials("simdevice", "navis@123")
 | |
|                 .WithTcpServer(BrokerIp, BrokerPort)
 | |
|                 .WithCleanSession()
 | |
|                 .Build();
 | |
| 
 | |
|             await mqttClient.ConnectAsync(options);
 | |
| 
 | |
|             await mqttClient.SubscribeAsync(new MqttTopicFilterBuilder()
 | |
|                 .WithTopic(ResponseTopic)
 | |
|                 .WithAtLeastOnceQoS()
 | |
|                 .Build());
 | |
| 
 | |
|             Console.WriteLine($"✅ Subscribed to response topic: {ResponseTopic}");
 | |
| 
 | |
|             // Load mock data from file
 | |
|             string jsonFilePath = "mock_data.json";
 | |
|             if (!File.Exists(jsonFilePath))
 | |
|             {
 | |
|                 Console.WriteLine("❌ File 'mock_data.json' not found.");
 | |
|                 return;
 | |
|             }
 | |
| 
 | |
|             string jsonContent = await File.ReadAllTextAsync(jsonFilePath);
 | |
|             var message = JsonConvert.DeserializeObject<DeviceMessage>(jsonContent);
 | |
|             if (message == null)
 | |
|             {
 | |
|                 Console.WriteLine("❌ Failed to parse JSON data.");
 | |
|                 return;
 | |
|             }
 | |
| 
 | |
|             // Gửi bản tin update_pvt mỗi 10 giây
 | |
|             while (true)
 | |
|             {
 | |
|                 message.ReceivedTime = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss");
 | |
| 
 | |
|                 string jsonPayload = JsonConvert.SerializeObject(message);
 | |
|                 var mqttMessage = new MqttApplicationMessageBuilder()
 | |
|                     .WithTopic(UpdatePvtTopic)
 | |
|                     .WithPayload(jsonPayload)
 | |
|                     .WithQualityOfServiceLevel(MqttQualityOfServiceLevel.AtLeastOnce)
 | |
|                     .WithRetainFlag(false)
 | |
|                     .Build();
 | |
| 
 | |
|                 await mqttClient.PublishAsync(mqttMessage);
 | |
|                 Console.WriteLine($"📤 Sent PVT message to '{UpdatePvtTopic}'");
 | |
| 
 | |
|                 await Task.Delay(10000);
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| }
 |