示例#1
0
        public void GetDevices()
        {
            try
            {
                //var bne = new BACnetNetworkEvents(this);


                //start BACnet
                BacnetClient.ProposedWindowSize = Yabe.Properties.Settings.Default.Segments_ProposedWindowSize;
                BacnetClient.Retries            = (int)Yabe.Properties.Settings.Default.DefaultRetries;
                BacnetClient.Timeout            = (int)Yabe.Properties.Settings.Default.DefaultTimeout;
                BacnetClient.MaxSegments        = BacnetClient.GetSegmentsCount(Yabe.Properties.Settings.Default.Segments_Max);
                if (Yabe.Properties.Settings.Default.YabeDeviceId >= 0) // If Yabe get a Device id
                {
                    if (BACnetGlobalNetwork.m_storage == null)
                    {
                        lock (BACnetGlobalNetwork.m_storage)
                        {
                            // Load descriptor from the embedded xml resource
                            BACnetGlobalNetwork.m_storage = DeviceStorage.Load("Yabe.YabeDeviceDescriptor.xml", (uint)Yabe.Properties.Settings.Default.YabeDeviceId);
                            // A fast way to change the PROP_OBJECT_LIST
                            Property Prop = Array.Find <Property>(BACnetGlobalNetwork.m_storage.Objects[0].Properties, p => p.Id == BacnetPropertyIds.PROP_OBJECT_LIST);
                            Prop.Value[0] = "OBJECT_DEVICE:" + Yabe.Properties.Settings.Default.YabeDeviceId.ToString();
                            // change PROP_FIRMWARE_REVISION
                            Prop          = Array.Find <Property>(BACnetGlobalNetwork.m_storage.Objects[0].Properties, p => p.Id == BacnetPropertyIds.PROP_FIRMWARE_REVISION);
                            Prop.Value[0] = this.GetType().Assembly.GetName().Version.ToString();
                            // change PROP_APPLICATION_SOFTWARE_VERSION
                            Prop          = Array.Find <Property>(BACnetGlobalNetwork.m_storage.Objects[0].Properties, p => p.Id == BacnetPropertyIds.PROP_APPLICATION_SOFTWARE_VERSION);
                            Prop.Value[0] = this.GetType().Assembly.GetName().Version.ToString();
                        }
                    }
                    BacnetClient.OnWhoIs += new BacnetClient.WhoIsHandler(OnWhoIs);    //maybe this can't be static...does anything need to be stored in it?
                    BacnetClient.OnReadPropertyRequest         += new BacnetClient.ReadPropertyRequestHandler(OnReadPropertyRequest);
                    BacnetClient.OnReadPropertyMultipleRequest += new BacnetClient.ReadPropertyMultipleRequestHandler(OnReadPropertyMultipleRequest);
                }
                else
                {
                    BacnetClient.OnWhoIs += new BacnetClient.WhoIsHandler(OnWhoIsIgnore);
                }
                BacnetClient.OnIam += new BacnetClient.IamHandler(OnIam);

                // Not sure if we need these; implement later
                //BacnetClient.OnCOVNotification += new BacnetClient.COVNotificationHandler(bne.OnCOVNotification);
                //BacnetClient.OnEventNotify += new BacnetClient.EventNotificationCallbackHandler(bne.OnEventNotify);

                BacnetClient.Start();


                Console.WriteLine("type:" + BacnetClient.Transport.Type);

                //start search
                if (BacnetClient.Transport.Type == BacnetAddressTypes.IP || BacnetClient.Transport.Type == BacnetAddressTypes.Ethernet ||
                    BacnetClient.Transport.Type == BacnetAddressTypes.IPV6 ||
                    (BacnetClient.Transport is BacnetMstpProtocolTransport && ((BacnetMstpProtocolTransport)BacnetClient.Transport).SourceAddress != -1) ||
                    BacnetClient.Transport.Type == BacnetAddressTypes.PTP)
                {
                    Console.WriteLine("Starting device search");
                    System.Threading.ThreadPool.QueueUserWorkItem((o) =>
                    {
                        for (int i = 0; i < BacnetClient.Retries; i++)
                        {
                            BacnetClient.WhoIs();
                            Console.WriteLine("Sent WhoIs"); //OK, So we do WhoIs, write this line, and then sleep
                            System.Threading.Thread.Sleep(BacnetClient.Timeout);
                        }
                    }, null);
                }

                //special MSTP auto discovery
                if (BacnetClient.Transport is BacnetMstpProtocolTransport)
                {
                    //not sure this is needed...
                    //((BacnetMstpProtocolTransport)BacnetClient.Transport).FrameRecieved += new BacnetMstpProtocolTransport.FrameRecievedHandler(bne.MSTP_FrameRecieved);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error getting devices:" + ex.Message + "\n" + ex.StackTrace);
                Instance.hspi.Log("BACnetDevice Exception in GetDevices " + ex.Message, 2);

                //m_devices.Remove(bacnetClient);
                //node.Remove();
                //MessageBox.Show(this, "Couldn't start Bacnet bacnetClientunication: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            //BacnetClient.WhoIs();
            //System.Threading.Thread.Sleep(BacnetClient.Timeout);

            //not actually waiting for this, just getting here right away
            Console.WriteLine("Done getting devices");
        }