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); //发送端口允许 }
}//左键选择,暂时取消 private async void GridView_RightTapped(object sender, RightTappedRoutedEventArgs e) { var Info = (e.OriginalSource as FrameworkElement)?.DataContext as ViewDevModel; DeviceModel DataInfo = ModelConverter.ViewToData(Info); ContentDialog detail_dialog = new ContentDialog() { Title = DataInfo.Type + "设备详情:", Content = "设备地址:" + DataInfo.IPAddress + ":" + DataInfo.NetPort, PrimaryButtonText = "发送命令", SecondaryButtonText = "删除设备", CloseButtonText = "返回", FullSizeDesired = true, }; ContentDialogResult Reslut = await detail_dialog.ShowAsync(); if (Reslut == ContentDialogResult.Primary) { if (DataInfo.IPAddress == "Zigbee设备")//关闭串口服务 { WriteCmdByte(new byte[] { 0x02 }); //SendCmd(Convert.ToInt16("9003"), "0000+B"); } else { if (FanFlag == false) { SendCmd(Convert.ToInt16(DataInfo.NetPort), "0000+A"); FanFlag = true; } else { SendCmd(Convert.ToInt16(DataInfo.NetPort), "0000+B"); FanFlag = false; } } //SendCmd(DataInfo.NetPort, "这是一条测试信息/"); } if (Reslut == ContentDialogResult.Secondary) { if (DataInfo.IPAddress == "Zigbee设备")//关闭串口服务 { try { CancelReadTask(); CloseDevice(); ListAvailablePorts(); } catch (Exception) { } btnAddZigBee.IsEnabled = true; } DisposeLine(DataInfo.NetPort);//移除监听服务 for (int i = 0; i < Obj.Count; i++) { if (Obj[i].NetPort == DataInfo.NetPort) { Obj.Remove(Obj[i]); } } gridView.Items.Remove(Info); DevicesMethod.UpdateDevices(Obj); } }//右键选择