Exemple #1
0
        public void SendToMongoDB(Location _Location)
        {
            //把資料送進 MongoDB
            IMongoClient              _client     = new MongoClient("mongodb://localhost");
            IMongoDatabase            _database   = _client.GetDatabase("NetConf2020");          //DataBase
            IMongoCollection <CarGPS> _collection = _database.GetCollection <CarGPS>("GPSData"); //Table  Collection

            UpdateResult result = _collection.UpdateOneAsync(
                Builders <CarGPS> .Filter.Eq(x => x.CarNo, "216-GH"),
                Builders <CarGPS> .Update.Push <Location>(x => x.locations, _Location)
                ).Result;

            //再新增
            if (result.ModifiedCount == 0)
            {
                CarGPS _CarGPS = new CarGPS()
                {
                    CarNo     = "123-ABC",
                    Time      = DateTime.UtcNow,
                    locations = new List <Location>()
                    {
                        _Location
                    }
                };
                _collection.InsertOneAsync(_CarGPS);
            }

            //向 Event Bus 發出一個 Event
            GPSTriggerEvent _ge = new GPSTriggerEvent()
            {
                //在這邊發出你的 Event Bus 事件
            };

            _eventBus.Publish(_ge);
        }
Exemple #2
0
        public async Task onReciveGPS(Location _Location)
        {
            try{
                //把資料送進 MongoDB
                IMongoClient              _client     = new MongoClient("mongodb://localhost");
                IMongoDatabase            _database   = _client.GetDatabase("NetConf2020");          //DataBase
                IMongoCollection <CarGPS> _collection = _database.GetCollection <CarGPS>("GPSData"); //Table  Collection

                UpdateResult result = _collection.UpdateOneAsync(
                    Builders <CarGPS> .Filter.Eq(x => x.CarNo, "123-GH"),
                    Builders <CarGPS> .Update.Push <Location>(x => x.locations, _Location) //每次第一筆更新不到很正常
                    ).Result;

                //再新增
                if (result.ModifiedCount == 0)
                {
                    CarGPS _CarGPS = new CarGPS()
                    {
                        GsmNo     = "0900123456",
                        CarNo     = "123-GH",
                        Time      = DateTime.UtcNow,
                        locations = new List <Location>()
                        {
                            _Location
                        }
                    };
                    _collection.InsertOneAsync(_CarGPS);
                }

                //向 Event Bus 發出一個 Event
                GPSTriggerEvent _ge = new GPSTriggerEvent()
                {
                    Event_id  = Guid.NewGuid().ToString(),
                    EventName = "車輛GPS更新",
                    timestamp = DateTime.Now,
                    version   = "v1.0",
                    CarNo     = "123-ABC",
                    location  = _Location
                };

                _eventBus.Publish(_ge);
            }
            catch (Exception ex)
            {
                throw new Exception();
            }
        }
Exemple #3
0
        static void Main(string[] args)
        {
            //載入模擬GPS的檔案
            string text = File.ReadAllText(@"car2.json");
            CarGPS item = JsonConvert.DeserializeObject <CarGPS>(text);

            //連結SignalR GPS資料
            var connection = new HubConnectionBuilder()
                             .WithUrl("http://localhost:6001/IMHub")
                             .ConfigureLogging(logging =>
            {
            })
                             .Build();

            connection.StartAsync().ContinueWith(task =>
            {
                if (task.IsFaulted)
                {
                    Console.WriteLine("There was an error opening the connection:{0}",
                                      task.Exception.GetBaseException());
                }
                else
                {
                    Console.WriteLine("Connected");
                }
            }).Wait();



            //每1秒鐘送出GPS訊號
            item.locations.ForEach(i =>
            {
                Console.WriteLine("經度 = {0},緯度 = {1}", i.Lon, i.Lat);
                Thread.Sleep(1000); //Delay 1秒
                connection.InvokeAsync("onReciveGPS", i).Wait();
            });



            Console.ReadLine();
        }