Exemplo n.º 1
0
        private void Engine_Room_combobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox me = sender as ComboBox;
            Int32 _erid;
            string _ername = (me.SelectedItem as ComboBoxItem).Content as string;
            //this.Selection_List.Items.Clear();
            this.Item_Collection.Clear();

            if (me.SelectedIndex != -1)
            {
                _erid = Convert.ToInt32((me.SelectedItem as ComboBoxItem).Tag);
                SecureDBContext db1=new SecureDBContext();
                var RTU =
                    from n in db1.GetTblControllerConfigQuery()
                    where n.ERID == _erid && (
                          n.ControlType == 3 ||
                          n.ControlType==6 ||
                          n.ControlType==7 ||
                          n.ControlType==8 ||
                          n.ControlType==9 || n.ControlType==10)
                    select n;

               
                LoadOperation<tblControllerConfig> lo_RTU =
                    db1.Load<tblControllerConfig>(RTU);

                lo_RTU.Completed += (o_RTU, e_RTU) =>
                    {
                        if (lo_RTU.Entities.Count() != 0)
                        {
                            // It is posible if this company has more than 1 RTU.
                            foreach (tblControllerConfig _controller in lo_RTU.Entities)
                            {
                                SecureDBContext db2 = new SecureDBContext();
                                var ItemConfig =
                                    from n in db2.GetTblItemConfigQuery()
                                    where n.ControlID == _controller.ControlID &&
                                          n.Type == "AI"
                                    select n;
                              
                                LoadOperation<tblItemConfig> lo_item =
                                    db2.Load<tblItemConfig>(ItemConfig);

                                lo_item.Completed += (o_item, e_item) =>
                                    {
                                        if (lo_item.Entities.Count() != 0)
                                        {
                                            foreach (tblItemConfig _item in lo_item.AllEntities)
                                            {
                                                if (this.Compare_Collection.Count != 0)
                                                {
                                                    Sensor snr =
                                                       ( from n in this.Compare_Collection
                                                        where n.ItemID == _item.ItemID
                                                         select n).FirstOrDefault() ;

                                                    if (snr==null)
                                                        this.Item_Collection.Add(
                                                                new Sensor(_ername,
                                                                           _controller.ControlID,
                                                                           _item.ItemID,
                                                                           _item.ItemName,
                                                                           _item.Unit));
                                                }
                                                else
                                                {
                                                    this.Item_Collection.Add(
                                                                   new Sensor(_ername,
                                                                              _controller.ControlID,
                                                                              _item.ItemID,
                                                                              _item.ItemName,
                                                                              _item.Unit));
                                                }
                                            }

                                            // Binding
                                            this.Selection_List.ItemsSource = this.Item_Collection;
                                           
                                        }
                                        //else { MessageBox.Show(_ername + "底下的" + _controller.ControlID + "並沒有可用的感知器!"); }
                                    };
                              //  this.DBContext.tblItemConfigs.Clear();

                            }
                        }
                        else { MessageBox.Show("找不到ER ID: " + _erid + " 的 RTU資料!"); }
                        
                    };
            //    this.DBContext.tblControllerConfigs.Clear();

            }
        }