예제 #1
0
파일: Parser.cs 프로젝트: LowEQ/RealTimeMap
        public Juna ParserPayload(string json)
        {
            var data = JsonConvert.DeserializeObject <Train>(json);


            Juna junaItem = new Juna
            {
                trainId   = data.trainNumber,
                speed     = data.speed,
                longitude = data.location.coordinates[0],
                latitude  = data.location.coordinates[1]
            };


            return(junaItem);
        }
예제 #2
0
파일: Parser.cs 프로젝트: LowEQ/RealTimeMap
        public List <Juna> ParseJson(string json)
        {
            List <Juna> trainData = new List <Juna>();
            var         data      = JsonConvert.DeserializeObject <Train[]>(json);

            foreach (var item in data)
            {
                Juna junaItem = new Juna();
                junaItem.trainId   = item.trainNumber;
                junaItem.speed     = item.speed;
                junaItem.longitude = item.location.coordinates[0];
                junaItem.latitude  = item.location.coordinates[1];
                trainData.Add(junaItem);
            }
            return(trainData);
        }
예제 #3
0
 public static async Task UpdateList(Juna juna)
 {
     if (!data.Contains(juna))
     {
         data.Add(juna);
     }
     else
     {
         data.Where <Juna>(s => s.trainId == juna.trainId)
         .Select(s => {
             s.latitude  = juna.latitude;
             s.longitude = juna.longitude;
             s.speed     = juna.speed;
             return(s);
         });
     }
 }
예제 #4
0
        public async Task GetMQTTDataAsync()
        {
            Juna   data   = new Juna();
            Parser parser = new Parser();
            // Create a new MQTT client.
            var factory    = new MqttFactory();
            var mqttClient = factory.CreateMqttClient();
            var options    = new MqttClientOptionsBuilder()
                             .WithTcpServer("rata-mqtt.digitraffic.fi", 1883)
                             //.WithTls()//secure web socket connection from ws:// to wss://
                             .Build();


            mqttClient.UseConnectedHandler(async e =>
            {
                Console.WriteLine("### CONNECTED FROM SERVER ###");

                await mqttClient.SubscribeAsync(new TopicFilterBuilder().WithTopic("train-locations/#").Build());
            });
            //establishing connection
            await mqttClient.ConnectAsync(options, CancellationToken.None);

            mqttClient.UseApplicationMessageReceivedHandler(async e =>
            {
                //Console.WriteLine($"{Encoding.UTF8.GetString(e.ApplicationMessage.Payload)}");
                string strdata = Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
                Console.WriteLine(strdata);
                data = parser.ParserPayload(strdata);
                await TrainData.UpdateList(data);
                Console.WriteLine(data);
            });



            //Console.ReadKey();
        }
예제 #5
0
 public static async Task InsertList(Juna juna)
 {
     data.Add(juna);
 }