Пример #1
0
        public override async Task OnStartAsync(StartKind startKind, IActivatedEventArgs args)
        {
            // long-running startup tasks go here
            await Task.Delay(3000);

            //初始化请求消息
            string message = JsonOperation.GetInstance().PraseJson(2, "SYN", 25.0, 29.0, "Medium", "Cooling");
            //建立TCP连接,异步
            await TcpClient.GetInstance().Start();

            //向服务器请求链接
            await TcpClient.GetInstance().SendToTcpServer(message);

            //等待Ack
            while (true)
            {
                string JsonString = await TcpClient.GetInstance().WaitTcpServer();

                //Debug.WriteLine(JsonOperation.GetInstance().InitSlaveFromJson(JsonString));
                //如果收到的消息是ack且能够成功初始化就跳出循环
                if (JsonOperation.GetInstance().InitSlaveFromJson(JsonString))
                {
                    //收到初始化消息时候,约定向中央空调再发送一条状态请求消息
                    message = JsonOperation.GetInstance().PraseJson(2, "req", 25.0, 29.0, "Medium", "Cooling");
                    await TcpClient.GetInstance().SendToTcpServer(message);

                    //启动定期请求线程,参数为间隔时间
                    Threading.CreatePeriodicRequest(5000);
                    break;
                }
            }
            NavigationService.Navigate(typeof(Views.MainPage));
            await Task.CompletedTask;
        }