public RecordController(CarDbService carService)
 {
     _carService = carService;
 }
Exemple #2
0
        static void Main(string[] args)
        {
            //Task.Run(async () =>
            //{
            //    while (true)
            //    {
            //        // do the work in the loop
            //        //string newData = DateTime.Now.ToLongTimeString();

            //        //// update the UI on the UI thread
            //        //Console.WriteLine("tick tock");
            //        foreach(var _carDirty in _cars)
            //        {
            //            if(_carDirty.IsDirty)
            //            {
            //                Logger.Instance.Log(LogLevel.Information, $"dirty car found, db sync {_carDirty.CarId}");
            //                await _carDbService.SyncRoute(_carDirty);
            //                _carDirty.IsDirty = false;
            //            }
            //        }

            //        // don't run again for at least 200 milliseconds
            //        await Task.Delay(5000);
            //    }
            //});

            var dbSettings = new DBSettings()
            {
                ConnectionString = "mongodb://localhost:27017",
                DatabaseName     = "Shangqidb"
            };


            _carDbService = new CarDbService(Options.Create(dbSettings));

            IPAddress  ip           = IPAddress.Parse(IpStr);
            IPEndPoint ip_end_point = new IPEndPoint(IPAddress.Any, port);

            listener = new TcpListener(ip_end_point);
            listener.Start();
            Console.WriteLine("tcp server started");

            //异步接收 递归循环接收多个客户端
            listener.BeginAcceptTcpClient(new AsyncCallback(DoAcceptTcpclient), listener);



            while (true)
            {
                try
                {
                    //SEND COMMAND TO ROBOT
                    if (clients.Count > 0)
                    {
                        var model = RedisHelper.Instance.GetCacheItem <OutboundModel>("command").Result;
                        if (model != null)
                        {
                            Logger.Instance.Log(LogLevel.Information, $"sending command to robot ip: {model.CarName}");
                            var client = clients.FirstOrDefault(c => c.Client.RemoteEndPoint.ToString() == model.Ip);
                            //var client = clients.FirstOrDefault(); //todo remove this after local testing
                            if (client != null)
                            {
                                var stream = client.GetStream();

                                foreach (var obj in model.Data)
                                {
                                    var jsonStr = JsonConvert.SerializeObject(obj);


                                    var result = Encoding.ASCII.GetBytes(jsonStr);

                                    //TODO RESET CACHED COORDINATES

                                    Logger.Instance.Log(LogLevel.Information, $"send following command to robot: {model.CarName}, {jsonStr}");

                                    stream.Write(result, 0, result.Length);
                                    stream.Flush();
                                }
                            }


                            RedisHelper.Instance.ClearKey("command");
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("error:" + e.ToString());
                    break;
                }
            }
            Console.ReadKey();
        }
Exemple #3
0
 public RobotController(CarDbService carService)
 {
     _carService = carService;
 }