private async void MainPage_Loaded(object sender, RoutedEventArgs e) { socket = new DatagramSocket(); socket.Control.MulticastOnly = true; socket.MessageReceived += Socket_MessageReceived; await socket.BindServiceNameAsync("9000"); #region 获取本地IP var hosts = NetworkInformation.GetHostNames(); var host = hosts.FirstOrDefault(h => { bool isIpaddr = (h.Type == Windows.Networking.HostNameType.Ipv4) || (h.Type == Windows.Networking.HostNameType.Ipv6); if (isIpaddr == false) { return(false); } IPInformation ipinfo = h.IPInformation; if (ipinfo.NetworkAdapter.IanaInterfaceType == 71 || ipinfo.NetworkAdapter.IanaInterfaceType == 6) { return(true); } return(false); }); if (host != null) { LocIp = host.DisplayName; txtLocIp.Text = "本机IP:" + host.DisplayName; //显示IP } #endregion #region 布局表格 if (await DevicesMethod.IsFirstUse() == true) { DevicesMethod.CreateLog();//设备文件丢失 } Obj = await DevicesMethod.GetDevices(); List <ViewDevModel> ObjView = new List <ViewDevModel>(); foreach (var item in Obj) { ObjView.Add(ModelConverter.DataToView(item)); } Controller.GridViewMethod.LayOutItem(gridView, ObjView); //布置控件 if (await IsFirstUse() == true) //队列文件丢失 { FileMethod.CreateTxt(); } #endregion foreach (var item in Obj)//布置已注册端口 { CreateLine(item.NetPort); } lblNetWorkName.Text = " " + InternetStatus.GetNetWorkName();//获取网络名称 //StatusBar S = StatusBar.GetForCurrentView(); //await S.HideAsync(); }
}//关闭设备 private async void BtnAddZigBee_Click(object sender, RoutedEventArgs e) { #region 配置串口 var selection = ConnectDevices.SelectedItems; if (selection.Count <= 0) { return; } DeviceInformation entry = (DeviceInformation)selection[0]; try { serialPort = await SerialDevice.FromIdAsync(entry.Id); if (serialPort == null) { return; } serialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000); serialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000); serialPort.BaudRate = 9600; serialPort.Parity = SerialParity.None; serialPort.StopBits = SerialStopBitCount.One; serialPort.DataBits = 8; serialPort.Handshake = SerialHandshake.None; // Create cancellation token object to close I/O operations when closing the device ReadCancellationTokenSource = new CancellationTokenSource(); Listen(); } catch (Exception) { } #endregion DeviceModel Device = new DeviceModel { NetPort = await DevicesMethod.GetPortNum() + 1, Type = "照明系统", IPAddress = "Zigbee设备" }; ViewDevModel DevObj = ModelConverter.DataToView(Device); Obj.Add(Device); DevicesMethod.UpdateDevices(Obj); gridView.Items.Add(DevObj); CreateLine(Device.NetPort);//绑定新通道 btnAddZigBee.IsEnabled = false; }
}//向云上传数据 #endregion #region 设备模型相关 private async void BtnAdd_Click(object sender, RoutedEventArgs e) { string[] data = cmbIp.SelectedItem.ToString().Trim().Split(Convert.ToChar("/")); DeviceModel Device = new DeviceModel { NetPort = await DevicesMethod.GetPortNum() + 1, Type = data[1], IPAddress = data[0] }; ViewDevModel DevObj = ModelConverter.DataToView(Device); Obj.Add(Device); DevicesMethod.UpdateDevices(Obj); gridView.Items.Add(DevObj); CreateLine(Device.NetPort); //绑定新通道 SendAllow(Device.NetPort); //发送端口允许 }