예제 #1
0
        public void RpmE(uint instanceId, List <BacNetObject> objectList, RpmEDelegate callBack)
        {
            BacNetRemoteDevice remote = BacNetDevice.Instance.SearchRemote(BacNetRemoteDevice.Get(instanceId.ToString()));

            if (remote == null)
            {
                return;
            }

            var apdu = new ReadPropertyMultiple(objectList);

            apdu.CallBack   = callBack;
            apdu.InstanceId = instanceId;
            var npdu = new BacNetIpNpdu {
                ExpectingReply = true, Destination = remote.BacAddress
            };

            lock (_rpmPool)
            {
                if (_rpmPool.ContainsKey(apdu.InvokeId))
                {
                    _rpmPool[apdu.InvokeId].CallBack(_rpmPool[apdu.InvokeId].InstanceId, null);
                    _rpmPool.Remove(apdu.InvokeId);
                }
                _rpmPool.Add(apdu.InvokeId, apdu);
            }

            BacNetDevice.Instance.Services.Execute(npdu, apdu, remote.EndPoint);
        }
예제 #2
0
        internal BacNetRemoteDevice SearchRemote(BacNetRemoteDevice device)
        {
            if (LostDevices.ContainsKey(device.InstanceNumber))
            {
                if (DateTime.Now - LostDevices[device.InstanceNumber] < new TimeSpan(0, 0, 10))
                {
                    return(null);
                }
                LostDevices.Remove(device.InstanceNumber);
            }

            BacNetRemoteDevice rem = Instance.Remote.FirstOrDefault(s => s.InstanceNumber == device.InstanceNumber);

            if (rem == null)
            {
                Instance.Services.Unconfirmed.WhoIs((ushort)device.InstanceNumber, (ushort)device.InstanceNumber, 500);
                Thread.Sleep(500);
                rem = Instance.Remote.FirstOrDefault(s => s.InstanceNumber == device.InstanceNumber);
            }
            if (rem == null && !LostDevices.ContainsKey(device.InstanceNumber))
            {
                LostDevices.Add(device.InstanceNumber, DateTime.Now);
            }
            return(rem);
        }
예제 #3
0
        public bool?SubscribeCOV(string address, BacNetEnums.BACNET_PROPERTY_ID propId = BacNetEnums.BACNET_PROPERTY_ID.PROP_PRESENT_VALUE)
        {
            string[] addrArray = address.Split('.');
            if (addrArray.Length != 2)
            {
                _logger.Warn("Wrong address: " + address);
                return(null);
            }

            BacNetRemoteDevice remote = BacNetDevice.Instance.SearchRemote(BacNetRemoteDevice.Get(addrArray[0]));

            if (remote == null)
            {
                _logger.Warn("No such device in network. Device number: " + addrArray[0]);
                return(null);
            }

            BacNetObject tmpObj;

            try
            {
                tmpObj = new BacNetObject(addrArray[1]);
            }
            catch (Exception ex)
            {
                _logger.Warn(ex.Message);
                return(null);
            }

            BacNetObject obj = remote.Objects.FirstOrDefault(s => s.ObjectId == tmpObj.ObjectId && s.ObjectType == tmpObj.ObjectType);

            if (obj == null)
            {
                remote.Objects.Add(tmpObj);
                obj = tmpObj;
            }
            var apdu = new SubscribeCOV(obj)
            {
                ProccessId = new BacNetUInt(5556)
            };
            var npdu = new BacNetIpNpdu {
                ExpectingReply = true, Destination = remote.BacAddress
            };

            BacNetDevice.Instance.Waiter = apdu.InvokeId;
            BacNetDevice.Instance.Services.Execute(npdu, apdu, remote.EndPoint);
            ArrayList valueList = WaitForResponce(apdu.InvokeId) as ArrayList;

            /*BacNetProperty property = obj.Properties.FirstOrDefault(s => s.PropertyId.Value == (uint)propId);
             * if (property != null)
             *  property.Values = valueList ?? new ArrayList();
             * else
             * {
             *  property = new BacNetProperty { PropertyId = new BacNetUInt { Value = (uint)propId }, Values = valueList ?? new ArrayList() };
             *  obj.Properties.Add(property);
             * }
             * return property;*/
            return(true);
        }
예제 #4
0
파일: Form1.cs 프로젝트: zenglc/BACsharp
 private void whoIsButton_Click(object sender, EventArgs e)
 {
     _device.Services.Unconfirmed.WhoIs();
     Thread.Sleep(3000);
     foreach (var remoteDevice in _device.Remote)
     {
         BacNetRemoteDevice rm = remoteDevice as BacNetRemoteDevice;
         if (rm == null)
         {
             continue;
         }
         listBox1.Items.Add(rm.InstanceNumber);
     }
 }
예제 #5
0
        public void ReceivedIAm(BacNetRawMessage msg, IPEndPoint endPoint)
        {
            BacNetRemoteDevice newDevice = new BacNetRemoteDevice();

            newDevice.EndPoint = endPoint;

            BacNetIpNpdu npdu;
            IAm          apdu;

            try
            {
                npdu = new BacNetIpNpdu(msg.Npdu);
                apdu = new IAm(msg.Apdu);
            }
            catch (Exception ex)
            {
                _logger.WarnException("Received malformed I-am", ex);
                return;
            }

            if (npdu.Source != null)
            {
                newDevice.BacAddress = npdu.Source;
            }
            newDevice.MaxApduLength  = apdu.MaxApduLength;
            newDevice.InstanceNumber = apdu.deviceObject.ObjectId;
            newDevice.Segmentation   = apdu.SegmentationSupported;
            newDevice.VendorId       = apdu.VendorId;

            if (newDevice.InstanceNumber == BacNetDevice.Instance.DeviceId)
            {
                return;
            }

            BacNetRemoteDevice rem =
                BacNetDevice.Instance.Remote.FirstOrDefault(s => s.InstanceNumber == newDevice.InstanceNumber);

            if (rem != null)
            {
                BacNetDevice.Instance.Remote.Remove(rem);
            }

            BacNetDevice.Instance.Remote.Add(newDevice);
        }
예제 #6
0
        public List <BacNetObject> Rpm(uint instanceId, List <BacNetObject> objectList)
        {
            BacNetRemoteDevice remote = BacNetDevice.Instance.SearchRemote(BacNetRemoteDevice.Get(instanceId.ToString()));

            if (remote == null)
            {
                return(new List <BacNetObject>());
            }

            var apdu = new ReadPropertyMultiple(objectList);
            var npdu = new BacNetIpNpdu {
                ExpectingReply = true, Destination = remote.BacAddress
            };

            BacNetDevice.Instance.Waiter = apdu.InvokeId;
            BacNetDevice.Instance.Services.Execute(npdu, apdu, remote.EndPoint);
            objectList = WaitForResponce(apdu.InvokeId) as List <BacNetObject>;
            return(objectList ?? new List <BacNetObject>());
        }
예제 #7
0
        public ArrayList ReadProperty(UInt16 destinationAddress, BacNetObject bacNetObject, BacNetEnums.BACNET_PROPERTY_ID propertyId)
        {
            var apdu = new ReadProperty(bacNetObject, propertyId);
            var npdu = new BacNetIpNpdu();

            npdu.ExpectingReply = true;
            BacNetRemoteDevice remote = null;

            foreach (BacNetRemoteDevice remoteDevice in BacNetDevice.Instance.Remote)
            {
                if (remoteDevice.InstanceNumber == destinationAddress)
                {
                    remote = remoteDevice;
                }
            }
            if (remote != null)
            {
                npdu.Destination = remote.BacAddress;
                BacNetDevice.Instance.Services.Execute(npdu, apdu, remote.EndPoint);
                BacNetDevice.Instance.Waiter = apdu.InvokeId;
                return(WaitForResponce(apdu.InvokeId) as ArrayList);
            }
            return(null);
        }
예제 #8
0
        public BacNetProperty ReadProperty(string address, BacNetEnums.BACNET_PROPERTY_ID propId = BacNetEnums.BACNET_PROPERTY_ID.PROP_PRESENT_VALUE)
        {
            string[] addrArray = address.Split('.');
            if (addrArray.Length != 2)
            {
                _logger.Warn("Wrong address: " + address);
                return(null);
            }

            BacNetRemoteDevice remote = BacNetDevice.Instance.SearchRemote(BacNetRemoteDevice.Get(addrArray[0]));

            if (remote == null)
            {
                return(null);
            }

            BacNetObject tmpObj;

            try
            {
                tmpObj = new BacNetObject(addrArray[1]);
            }
            catch (Exception ex)
            {
                _logger.Warn(ex.Message);
                return(new BacNetProperty
                {
                    PropertyId = new BacNetUInt {
                        Value = (uint)propId
                    },
                    Values = new ArrayList {
                        new BacNetString("Error")
                    }
                });
                //return null;
            }

            BacNetObject obj = remote.Objects.FirstOrDefault(s => s.ObjectId == tmpObj.ObjectId && s.ObjectType == tmpObj.ObjectType);

            if (obj == null)
            {
                remote.Objects.Add(tmpObj);
                obj = tmpObj;
            }
            var apdu = new ReadProperty(obj, propId);
            var npdu = new BacNetIpNpdu {
                ExpectingReply = true, Destination = remote.BacAddress
            };

            BacNetDevice.Instance.Waiter = apdu.InvokeId;
            BacNetDevice.Instance.Services.Execute(npdu, apdu, remote.EndPoint);
            ArrayList valueList = WaitForResponce(apdu.InvokeId) as ArrayList;

            BacNetProperty property = obj.Properties.FirstOrDefault(s => s.PropertyId.Value == (uint)propId);

            if (property != null)
            {
                property.Values = valueList ?? new ArrayList();
            }
            else
            {
                property = new BacNetProperty {
                    PropertyId = new BacNetUInt {
                        Value = (uint)propId
                    }, Values = valueList ?? new ArrayList()
                };
                obj.Properties.Add(property);
            }
            return(property);
        }