public void WriteProperty(uint instanceId, BacNetObject bacNetObject, BacNetEnums.BACNET_PROPERTY_ID propertyId, ArrayList valueList, WritePropertyDelegate callBack = null) { var apdu = new WriteProperty(bacNetObject, propertyId, valueList); apdu.InstanceId = instanceId; apdu.CallBack = callBack; var npdu = new BacNetIpNpdu(); npdu.ExpectingReply = true; IPEndPoint endPoint = null; foreach (BacNetRemoteDevice remoteDevice in BacNetDevice.Instance.Remote) { if (remoteDevice.InstanceNumber == instanceId) { npdu.Destination = remoteDevice.BacAddress; endPoint = remoteDevice.EndPoint; } } lock (_writePropertyPool) { if (_writePropertyPool.ContainsKey(apdu.InvokeId)) { _writePropertyPool[apdu.InvokeId].CallBack(_writePropertyPool[apdu.InvokeId].InstanceId, null, null); _writePropertyPool.Remove(apdu.InvokeId); } _writePropertyPool.Add(apdu.InvokeId, apdu); } BacNetDevice.Instance.Services.Execute(npdu, apdu, endPoint); }
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); }
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); }
public void WhoIs(UInt16 startAddress = UInt16.MinValue, UInt16 endAddress = UInt16.MaxValue, int timeOut = 1000) { var apdu = new WhoIs(startAddress, endAddress); var npdu = new BacNetIpNpdu(); npdu.Destination = new BacNetAddress(); npdu.Destination.Network = ByteConverter.GetBytes((ushort)65535); BacNetDevice.Instance.Services.Execute(npdu, apdu); //WaitForResponce(); }
public void SimpleAck(byte invokeId, byte serviceChoice) { var apdu = new SimpleAck() { InvokeId = invokeId, ServiceChoise = serviceChoice }; var npdu = new BacNetIpNpdu(); //npdu.Destination = new BacNetAddress(); //npdu.Destination.Network = ByteConverter.GetBytes((ushort)65535); BacNetDevice.Instance.Services.Execute(npdu, apdu); //WaitForResponce(); }
public void ReceivedSimpleAck(BacNetRawMessage msg) { var npdu = new BacNetIpNpdu(msg.Npdu); var apdu = new SimpleAck(msg.Apdu); //WritePropertyOk if (apdu.ServiceChoise == 15) { BacNetDevice.Instance.Services.Confirmed.WritePropertyCallBack(apdu.InvokeId, "Ok"); } }
public void SegmentAck(IPEndPoint endPoint, BacNetAddress source, byte invokeId, byte sequenceNumber, byte propWindowSize) { var apdu = new SegmentAck() { InvokeId = invokeId, SequenceNumber = sequenceNumber, PropWindowSize = propWindowSize }; var npdu = new BacNetIpNpdu(); npdu.Destination = source; //npdu.Destination = new BacNetAddress(); //npdu.Destination.Network = ByteConverter.GetBytes((ushort)65535); BacNetDevice.Instance.Services.Execute(npdu, apdu, endPoint); //WaitForResponce(); }
public void IAm() { var apdu = new IAm(); apdu.deviceObject = new BacNetObject {ObjectId = BacNetDevice.Instance.DeviceId, ObjectType = BacNetEnums.BACNET_OBJECT_TYPE.OBJECT_DEVICE}; apdu.MaxApduLength = new BacNetUInt {Value = 480}; apdu.SegmentationSupported = BacNetEnums.BACNET_SEGMENTATION.SEGMENTATION_NONE; apdu.VendorId = new BacNetUInt { Value = 500 }; var npdu = new BacNetIpNpdu(); npdu.Source = new BacNetAddress { Address = ByteConverter.GetBytes(BacNetDevice.Instance.DeviceId), Network = ByteConverter.GetBytes((ushort)60001), HopCount = 255}; BacNetDevice.Instance.Services.Execute(npdu, apdu); }
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); }
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>()); }
public BacNetObject CreateObject(uint instanceId, BacNetObject bacNetObject) { var npdu = new BacNetIpNpdu(); var apdu = new CreateObject(bacNetObject); IPEndPoint endPoint = null; foreach (BacNetRemoteDevice remoteDevice in BacNetDevice.Instance.Remote) { if (remoteDevice.InstanceNumber == instanceId) { npdu.Destination = remoteDevice.BacAddress; endPoint = remoteDevice.EndPoint; break; } } BacNetDevice.Instance.Services.Execute(npdu, apdu, endPoint); return(apdu.NewObject); }
public BacNetObject CreateObject(uint instanceId, BacNetObject bacNetObject) { var npdu = new BacNetIpNpdu(); var apdu = new CreateObject(bacNetObject); IPEndPoint endPoint = null; foreach (BacNetRemoteDevice remoteDevice in BacNetDevice.Instance.Remote) { if (remoteDevice.InstanceNumber == instanceId) { npdu.Destination = remoteDevice.BacAddress; endPoint = remoteDevice.EndPoint; break; } } BacNetDevice.Instance.Services.Execute(npdu, apdu, endPoint); return apdu.NewObject; }
private void ParseComplexAck(BacNetRawMessage msg, IPEndPoint endPoint) { if (msg.Apdu.Length > 2) { //Сегментированный ответ if ((msg.Apdu[0] >> 3 & 1) == 1) { /*BacNetRawMessage segmentedMsg = null; * foreach (object m in messagePool) * { * BacNetRawMessage ms = m as BacNetRawMessage; * //Если в пуле есть сообщение с таким же InvokeId как у поступившего используем его * if (ms != null && ms.Apdu[1] == msg.Apdu[1]) * segmentedMsg = ms; * } * if (segmentedMsg == null) segmentedMsg = msg;*/ messagePool.Add(msg); BacNetIpNpdu npdu = new BacNetIpNpdu(msg.Npdu); BacNetDevice.Instance.Services.Acknowledgement.SegmentAck(endPoint, npdu.Source, msg.Apdu[1], msg.Apdu[2], msg.Apdu[3]); //Если есть ещё сегменты if ((msg.Apdu[0] >> 2 & 1) == 1) { return; } msg = ReassembleMessage(messagePool, msg.Apdu[1]); } switch (msg.Apdu[2]) { case 12: BacNetDevice.Instance.Response.ReceivedReadPropertyAck(msg); break; case 14: BacNetDevice.Instance.Response.ReceivedReadPropertyMultipleAck(msg); break; } } }
public void DeletObject(uint instanceId, uint objectId) { foreach (BacNetRemoteDevice remoteDivice in BacNetDevice.Instance.Remote) { if (remoteDivice.InstanceNumber == instanceId) { foreach (var bacNetObject in remoteDivice.Objects) { if (bacNetObject.ObjectId == objectId) { var npdu = new BacNetIpNpdu(); npdu.Destination = remoteDivice.BacAddress; var apdu = new DeleteObject(bacNetObject); BacNetDevice.Instance.Services.Execute(npdu, apdu, remoteDivice.EndPoint); return; } } } } }
public void IAm() { var apdu = new IAm(); apdu.deviceObject = new BacNetObject { ObjectId = BacNetDevice.Instance.DeviceId, ObjectType = BacNetEnums.BACNET_OBJECT_TYPE.OBJECT_DEVICE }; apdu.MaxApduLength = new BacNetUInt { Value = 480 }; apdu.SegmentationSupported = BacNetEnums.BACNET_SEGMENTATION.SEGMENTATION_NONE; apdu.VendorId = new BacNetUInt { Value = 500 }; var npdu = new BacNetIpNpdu(); npdu.Source = new BacNetAddress { Address = ByteConverter.GetBytes(BacNetDevice.Instance.DeviceId), Network = ByteConverter.GetBytes((ushort)60001), HopCount = 255 }; BacNetDevice.Instance.Services.Execute(npdu, apdu); }
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); }
private void ParseComplexAck(BacNetRawMessage msg, IPEndPoint endPoint) { if (msg.Apdu.Length > 2) { //Сегментированный ответ if ((msg.Apdu[0] >> 3 & 1) == 1) { /*BacNetRawMessage segmentedMsg = null; foreach (object m in messagePool) { BacNetRawMessage ms = m as BacNetRawMessage; //Если в пуле есть сообщение с таким же InvokeId как у поступившего используем его if (ms != null && ms.Apdu[1] == msg.Apdu[1]) segmentedMsg = ms; } if (segmentedMsg == null) segmentedMsg = msg;*/ messagePool.Add(msg); BacNetIpNpdu npdu = new BacNetIpNpdu(msg.Npdu); BacNetDevice.Instance.Services.Acknowledgement.SegmentAck(endPoint, npdu.Source, msg.Apdu[1], msg.Apdu[2], msg.Apdu[3]); //Если есть ещё сегменты if ((msg.Apdu[0] >> 2 & 1) == 1) { return; } msg = ReassembleMessage(messagePool, msg.Apdu[1]); } switch (msg.Apdu[2]) { case 12: BacNetDevice.Instance.Response.ReceivedReadPropertyAck(msg); break; case 14: BacNetDevice.Instance.Response.ReceivedReadPropertyMultipleAck(msg); break; } } }
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); }
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; }
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); }
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; }
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); }
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>(); }