Пример #1
0
        private void AddProperty(BacnetPropertyIds id, BacnetPropertyValue val)
        {
            //var id = BacnetPropertyIds.PROP_OBJECT_IDENTIFIER;
            //var val = new BacnetValue(object_id);



            if (!BACnetObject.SupportedPropertyIds.Contains(id))
            {
                return;
            }


            var prop = new BACnetProperty(this, id, val);

            addProp(id, prop);
            //if id == BacnetPropertyIds.



            //if (BACnetObject.RequiredPropertyIds.Contains(id))        //don't keep required properties separate anymore.....
            //    RequiredProperties[id] = prop;      //TODO: could put option to not overwrite, I guess...
            //else
            //{
            //var kvp = new KeyValuePair<BacnetPropertyIds, BACnetProperty>(id, prop);
            //BacnetProperties.Add(kvp);
        }
Пример #2
0
        public void ReadPropertyMultiple(BacnetObjectId object_id, ICollection <BacnetPropertyReference> properties, out IList <BacnetPropertyValue> values)
        {
            BacnetPropertyValue[] values_ret = new BacnetPropertyValue[properties.Count];

            int count = 0;

            foreach (BacnetPropertyReference entry in properties)
            {
                BacnetPropertyValue new_entry = new BacnetPropertyValue();
                new_entry.property = entry;

                ErrorCodes error = ReadProperty(object_id, (BacnetPropertyIds)entry.propertyIdentifier, entry.propertyArrayIndex, out new_entry.value);
                if (error == ErrorCodes.UnKnownObject)
                {
                    new_entry.value = new BacnetValue[] { new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_ERROR, new BacnetError(BacnetErrorClasses.ERROR_CLASS_OBJECT, BacnetErrorCodes.ERROR_CODE_UNKNOWN_OBJECT)) }
                }
                ;
                else if (error == ErrorCodes.NotExist)
                {
                    new_entry.value = new BacnetValue[] { new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_ERROR, new BacnetError(BacnetErrorClasses.ERROR_CLASS_PROPERTY, BacnetErrorCodes.ERROR_CODE_UNKNOWN_PROPERTY)) }
                }
                ;

                values_ret[count] = new_entry;

                count++;
            }

            values = values_ret;
        }
Пример #3
0
        public bool ReadPropertyAll(BacnetObjectId objectId, out IList <BacnetPropertyValue> values)
        {
            //find
            var obj = FindObject(objectId);

            if (obj == null)
            {
                values = null;
                return(false);
            }

            //build
            var propertyValues = new BacnetPropertyValue[obj.Properties.Length];

            for (var i = 0; i < obj.Properties.Length; i++)
            {
                var newEntry = new BacnetPropertyValue
                {
                    property = new BacnetPropertyReference((uint)obj.Properties[i].Id, Serialize.ASN1.BACNET_ARRAY_ALL)
                };

                if (ReadProperty(objectId, obj.Properties[i].Id, Serialize.ASN1.BACNET_ARRAY_ALL, out newEntry.value) != ErrorCodes.Good)
                {
                    var bacnetError = new BacnetError(BacnetErrorClasses.ERROR_CLASS_OBJECT, BacnetErrorCodes.ERROR_CODE_UNKNOWN_PROPERTY);
                    newEntry.value = new[] { new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_ERROR, bacnetError) };
                }

                propertyValues[i] = newEntry;
            }

            values = propertyValues;
            return(true);
        }
Пример #4
0
        public void ReadPropertyMultiple(BacnetObjectId objectId, ICollection <BacnetPropertyReference> properties, out IList <BacnetPropertyValue> values)
        {
            var valuesRet = new List <BacnetPropertyValue>();

            foreach (var entry in properties)
            {
                var newEntry = new BacnetPropertyValue {
                    property = entry
                };

                switch (ReadProperty(objectId, (BacnetPropertyIds)entry.propertyIdentifier, entry.propertyArrayIndex, out newEntry.value))
                {
                case ErrorCodes.UnknownObject:
                    newEntry.value = new[]
                    {
                        new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_ERROR,
                                        new BacnetError(BacnetErrorClasses.ERROR_CLASS_OBJECT, BacnetErrorCodes.ERROR_CODE_UNKNOWN_OBJECT))
                    };
                    break;

                case ErrorCodes.NotExist:
                    newEntry.value = new[]
                    {
                        new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_ERROR,
                                        new BacnetError(BacnetErrorClasses.ERROR_CLASS_PROPERTY, BacnetErrorCodes.ERROR_CODE_UNKNOWN_PROPERTY))
                    };
                    break;
                }

                valuesRet.Add(newEntry);
            }

            values = valuesRet;
        }
Пример #5
0
        public bool ReadPropertyAll(BacnetObjectId object_id, out IList <BacnetPropertyValue> values)
        {
            values = null;

            //find
            Object obj = FindObject(object_id);

            if (obj == null)
            {
                return(false);
            }

            //build
            ErrorCodes[]          ret     = new ErrorCodes[obj.Properties.Length];
            BacnetPropertyValue[] _values = new BacnetPropertyValue[obj.Properties.Length];
            for (int i = 0; i < obj.Properties.Length; i++)
            {
                BacnetPropertyValue new_entry = new BacnetPropertyValue();
                new_entry.property = new BacnetPropertyReference((uint)obj.Properties[i].Id, System.IO.BACnet.Serialize.ASN1.BACNET_ARRAY_ALL);
                if (ReadProperty(object_id, obj.Properties[i].Id, System.IO.BACnet.Serialize.ASN1.BACNET_ARRAY_ALL, out new_entry.value) != ErrorCodes.Good)
                {
                    new_entry.value = new BacnetValue[] { new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_ERROR, new BacnetError(BacnetErrorClasses.ERROR_CLASS_OBJECT, BacnetErrorCodes.ERROR_CODE_UNKNOWN_PROPERTY)) }
                }
                ;
                _values[i] = new_entry;
            }
            values = _values;

            return(true);
        }
Пример #6
0
        private void SetName()      //don't call in the middle of FetchProperties...        actually, now it's fine because of bypass refresh
        {
            try
            {
                BacnetPropertyValue namePropVal = (BacnetPropertyValue)GetBacnetProperty(BacnetPropertyIds.PROP_OBJECT_NAME, true).BacnetPropertyValue;
                //careful - don't call GetBacnetProperty internally, or else it will keep trying to refresh properties since AllPropertiesFetched is still false

                Name = namePropVal.value[0].ToString();   //BacnetValue.Value.ToString();
            }
            catch (Exception ex)
            {
                //leave it alone?
            }
        }
        /*****************************************************************************************************/
        static void handler_OnCOVManagementNotify(BaCSharpObject sender, BacnetPropertyIds propId)
        {
            System.Threading.ThreadPool.QueueUserWorkItem((o) =>
            {
                lock (device)
                {
                    //remove old leftovers
                    SubscriptionManager.RemoveOldSubscriptions();

                    //find subscription
                    List <Subscription> subs = SubscriptionManager.GetSubscriptionsForObject(sender.PROP_OBJECT_IDENTIFIER);

                    if (subs == null)
                    {
                        return;             // nobody
                    }
                    //Read the property
                    IList <BacnetValue> value;
                    BacnetPropertyReference br = new BacnetPropertyReference((uint)propId, (uint)System.IO.BACnet.Serialize.ASN1.BACNET_ARRAY_ALL);
                    ErrorCodes error           = sender.ReadPropertyValue(br, out value);

                    List <BacnetPropertyValue> values = new List <BacnetPropertyValue>();
                    BacnetPropertyValue tmp           = new BacnetPropertyValue();
                    tmp.value    = value;
                    tmp.property = br;
                    values.Add(tmp);

                    //send to all
                    foreach (Subscription sub in subs)
                    {
                        if (sub.monitoredProperty.propertyIdentifier == (uint)BacnetPropertyIds.PROP_ALL || sub.monitoredProperty.propertyIdentifier == (uint)propId)
                        {
                            tmp.property = sub.monitoredProperty;
                            if (!sub.reciever.Notify(sub.reciever_address, sub.subscriberProcessIdentifier, deviceId, sub.monitoredObjectIdentifier, (uint)sub.GetTimeRemaining(), sub.issueConfirmedNotifications, values))
                            {
                                SubscriptionManager.RemoveReceiver(sub.reciever_address);
                            }
                        }
                    }
                }
            }, null);
        }
Пример #8
0
        public bool ReadValue()
        {
            var comm        = BacnetObject.BacnetDevice.BacnetNetwork.BacnetClient;
            var adr         = BacnetObject.BacnetDevice.BacnetAddress;
            var object_id   = BacnetObject.BacnetObjectId;
            var property_id = BacnetPropertyId;
            var array_index = arrayIndex;


            BacnetPropertyValue new_entry = new BacnetPropertyValue();

            new_entry.property = new BacnetPropertyReference((uint)property_id); //, array_index);   //don't need this.  Already have.
            IList <BacnetValue> value;

            try
            {
                if (!comm.ReadPropertyRequest(adr, object_id, property_id, out value)) //, 0, array_index))      //TODO: do we ever need array index?
                {
                    return(false);                                                     //ignore
                }
            }
            catch
            {
                return(false);         //ignore
            }
            new_entry.value = value;


            this.BacnetPropertyValue = new_entry;


            //if (value.Count == 1)
            //    this.BacnetValue = value[0];    //this should never be called on object groups or structured views, so will only contain one value....
            //else
            //    this.BacnetValues = value;  //otherwise leave value as Null, since we can't handle multiples here...

            //this.BacnetValues = value;      //will this list always only contain one?  What about possible properties?

            //new_entry.value = value;
            //values.Add(new_entry);
            return(true);
        }
Пример #9
0
        private async void SendTelemtry(BacnetPropertyValue value, BacnetObjectId monitoredObjectIdentifier)
        {
            var l_telemetry = new TelemetryMessage()
            {
                GatewaySource       = "WinBerry3",
                MessageSource       = monitoredObjectIdentifier.instance.ToString(),
                MessageType         = monitoredObjectIdentifier.Type.ToString(),
                Value               = value.value[0].Value.ToString(),
                GatewayTimeStampUtc = DateTime.Now.ToUniversalTime()
            };

            var l_outBoundMessage = new Message(Serialize(l_telemetry));

            if (m_azureDeviceClient != null)
            {
                await m_azureDeviceClient.SendEventAsync(l_outBoundMessage);
            }

            Debug.WriteLine($"Sent telemetry data to IoT Suite \t Object{monitoredObjectIdentifier.Instance} \t Data={value.value[0].Value}");
        }
Пример #10
0
        public void ReadPropertyMultiple(BacnetObjectId object_id, ICollection <BacnetPropertyReference> properties, out IList <BacnetPropertyValue> values)
        {
            var valuesRet = new List <BacnetPropertyValue>();

            foreach (var entry in properties)
            {
                var newEntry = new BacnetPropertyValue {
                    property = entry
                };
                if (ReadProperty(object_id, (BacnetPropertyIds)entry.propertyIdentifier, entry.propertyArrayIndex, out newEntry.value) != ErrorCodes.Good)
                {
                    var bacnetError = new BacnetError(BacnetErrorClasses.ERROR_CLASS_OBJECT, BacnetErrorCodes.ERROR_CODE_UNKNOWN_PROPERTY);
                    newEntry.value = new[] { new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_ERROR, bacnetError) };
                }

                valuesRet.Add(newEntry);
            }

            values = valuesRet;
        }
Пример #11
0
        public bool ReadPropertyMultiple(BacnetClient sender, BacnetAddress adr, IList <BacnetPropertyReference> properties, out IList <BacnetPropertyValue> values)
        {
            values = new BacnetPropertyValue[properties.Count];

            int count = 0;

            foreach (BacnetPropertyReference entry in properties)
            {
                BacnetPropertyValue new_entry = new BacnetPropertyValue();
                new_entry.property = entry;
                if (ReadPropertyValue(sender, adr, entry, out new_entry.value) != ErrorCodes.Good)
                {
                    new_entry.value = new BacnetValue[] { new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_ERROR, new BacnetError(BacnetErrorClasses.ERROR_CLASS_OBJECT, BacnetErrorCodes.ERROR_CODE_UNKNOWN_PROPERTY)) }
                }
                ;
                values[count] = new_entry;
                count++;
            }

            return(true);
        }
Пример #12
0
        public void FetchRequiredProperties()           //honesty could just fetch all properties and then add them in appropriately...
        {
            var object_id = this.BacnetObjectId;



            BacnetPropertyValue new_entry = new BacnetPropertyValue();

            new_entry.property = new BacnetPropertyReference((uint)BacnetPropertyIds.PROP_OBJECT_IDENTIFIER); //, System.IO.BACnet.Serialize.ASN1.BACNET_ARRAY_ALL);
            new_entry.value    = new BacnetValue[] { new BacnetValue(object_id) };
            AddProperty(BacnetPropertyIds.PROP_OBJECT_IDENTIFIER, new_entry);


            //AddProperty(BacnetPropertyIds.PROP_OBJECT_IDENTIFIER, new BacnetValue(object_id));



            // ReadProperty(comm, adr, object_id, BacnetPropertyIds.PROP_OBJECT_TYPE, ref values);
            // No need to query it, known value
            new_entry          = new BacnetPropertyValue();
            new_entry.property = new BacnetPropertyReference((uint)BacnetPropertyIds.PROP_OBJECT_TYPE, System.IO.BACnet.Serialize.ASN1.BACNET_ARRAY_ALL);
            new_entry.value    = new BacnetValue[] { new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_ENUMERATED, (uint)object_id.type) };
            //values.Add(new_entry);
            AddProperty(BacnetPropertyIds.PROP_OBJECT_TYPE, new_entry);

            //AddProperty(BacnetPropertyIds.PROP_OBJECT_TYPE, new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_ENUMERATED, (uint)object_id.type));



            // We do not know the value here
            //ReadProperty(comm, adr, object_id, BacnetPropertyIds.PROP_OBJECT_NAME, ref values);

            AddProperty(BacnetPropertyIds.PROP_OBJECT_NAME);    //not known, so it will get.


            SetName();

            //Name = RequiredProperties[BacnetPropertyIds.PROP_OBJECT_NAME].BacnetValue.Value.ToString();
        }
Пример #13
0
        private static void m_storage_ChangeOfValue(DeviceStorage sender, BacnetObjectId object_id, BacnetPropertyIds property_id, uint array_index, IList <BacnetValue> value)
        {
            System.Threading.ThreadPool.QueueUserWorkItem((o) =>
            {
                lock (m_lockObject)
                {
                    //remove old leftovers
                    RemoveOldSubscriptions();

                    //find subscription
                    if (!m_subscriptions.ContainsKey(object_id))
                    {
                        return;
                    }
                    List <Subscription> subs = m_subscriptions[object_id];

                    //convert
                    List <BacnetPropertyValue> values = new List <BacnetPropertyValue>();
                    BacnetPropertyValue tmp           = new BacnetPropertyValue();
                    tmp.property = new BacnetPropertyReference((uint)property_id, array_index);
                    tmp.value    = value;
                    values.Add(tmp);

                    //send to all
                    foreach (Subscription sub in subs)
                    {
                        if (sub.monitoredProperty.propertyIdentifier == (uint)BacnetPropertyIds.PROP_ALL || sub.monitoredProperty.propertyIdentifier == (uint)property_id)
                        {
                            //send notify
                            if (!sub.reciever.Notify(sub.reciever_address, sub.subscriberProcessIdentifier, m_storage.DeviceId, sub.monitoredObjectIdentifier, (uint)sub.GetTimeRemaining(), sub.issueConfirmedNotifications, values))
                            {
                                Trace.TraceError("Couldn't send notify");
                            }
                        }
                    }
                }
            }, null);
        }
Пример #14
0
        private static void OnSubscribeCOVProperty(BacnetClient sender, BacnetAddress adr, byte invoke_id, uint subscriberProcessIdentifier, BacnetObjectId monitoredObjectIdentifier, BacnetPropertyReference monitoredProperty, bool cancellationRequest, bool issueConfirmedNotifications, uint lifetime, float covIncrement, BacnetMaxSegments max_segments)
        {
            lock (m_lockObject)
            {
                try
                {
                    //create
                    Subscription sub = HandleSubscriptionRequest(sender, adr, invoke_id, subscriberProcessIdentifier, monitoredObjectIdentifier, (uint)BacnetPropertyIds.PROP_ALL, cancellationRequest, issueConfirmedNotifications, lifetime, covIncrement);

                    //send confirm
                    sender.SimpleAckResponse(adr, BacnetConfirmedServices.SERVICE_CONFIRMED_SUBSCRIBE_COV_PROPERTY, invoke_id);

                    //also send first values
                    if (!cancellationRequest)
                    {
                        System.Threading.ThreadPool.QueueUserWorkItem((o) =>
                        {
                            IList <BacnetValue> _values;
                            m_storage.ReadProperty(sub.monitoredObjectIdentifier, (BacnetPropertyIds)sub.monitoredProperty.propertyIdentifier, sub.monitoredProperty.propertyArrayIndex, out _values);
                            List <BacnetPropertyValue> values = new List <BacnetPropertyValue>();
                            BacnetPropertyValue tmp           = new BacnetPropertyValue();
                            tmp.property = sub.monitoredProperty;
                            tmp.value    = _values;
                            values.Add(tmp);
                            if (!sender.Notify(adr, sub.subscriberProcessIdentifier, m_storage.DeviceId, sub.monitoredObjectIdentifier, (uint)sub.GetTimeRemaining(), sub.issueConfirmedNotifications, values))
                            {
                                Trace.TraceError("Couldn't send notify");
                            }
                        }, null);
                    }
                }
                catch (Exception)
                {
                    sender.ErrorResponse(adr, BacnetConfirmedServices.SERVICE_CONFIRMED_SUBSCRIBE_COV_PROPERTY, invoke_id, BacnetErrorClasses.ERROR_CLASS_DEVICE, BacnetErrorCodes.ERROR_CODE_OTHER);
                }
            }
        }
        /*****************************************************************************************************/
        static void handler_OnSubscribeCOVProperty(BacnetClient sender, BacnetAddress adr, byte invoke_id, uint subscriberProcessIdentifier, BacnetObjectId monitoredObjectIdentifier, BacnetPropertyReference monitoredProperty, bool cancellationRequest, bool issueConfirmedNotifications, uint lifetime, float covIncrement, BacnetMaxSegments max_segments)
        {
            lock (device)
            {
                BaCSharpObject bacobj = device.FindBacnetObject(monitoredObjectIdentifier);
                if (bacobj != null)
                {
                    //create
                    Subscription sub = SubscriptionManager.HandleSubscriptionRequest(sender, adr, invoke_id, subscriberProcessIdentifier, monitoredObjectIdentifier, monitoredProperty.propertyIdentifier, cancellationRequest, issueConfirmedNotifications, lifetime, covIncrement);

                    //send confirm
                    sender.SimpleAckResponse(adr, BacnetConfirmedServices.SERVICE_CONFIRMED_SUBSCRIBE_COV_PROPERTY, invoke_id);

                    //also send first values
                    if (!cancellationRequest)
                    {
                        System.Threading.ThreadPool.QueueUserWorkItem((o) =>
                        {
                            IList <BacnetValue> _values;
                            bacobj.ReadPropertyValue(sender, adr, monitoredProperty, out _values);

                            List <BacnetPropertyValue> values = new List <BacnetPropertyValue>();
                            BacnetPropertyValue tmp           = new BacnetPropertyValue();
                            tmp.property = sub.monitoredProperty;
                            tmp.value    = _values;
                            values.Add(tmp);

                            sender.Notify(adr, sub.subscriberProcessIdentifier, deviceId, sub.monitoredObjectIdentifier, (uint)sub.GetTimeRemaining(), sub.issueConfirmedNotifications, values);
                        }, null);
                    }
                }
                else
                {
                    sender.ErrorResponse(adr, BacnetConfirmedServices.SERVICE_CONFIRMED_SUBSCRIBE_COV_PROPERTY, invoke_id, BacnetErrorClasses.ERROR_CLASS_DEVICE, BacnetErrorCodes.ERROR_CODE_OTHER);
                }
            }
        }
        // Copy the Present value into each reference properties value
        protected virtual void DoDispatchValue()
        {
            if ((m_PROP_LIST_OF_OBJECT_PROPERTY_REFERENCES.references == null) || (Mydevice == null))
            {
                return;
            }

            foreach (object obj in m_PROP_LIST_OF_OBJECT_PROPERTY_REFERENCES.references)
            {
                BacnetDeviceObjectPropertyReference reference = (BacnetDeviceObjectPropertyReference)obj;

                // reference.deviceIndentifier.type is not set to OBJECT_DEVICE for local object reference
                if (reference.deviceIndentifier.type != BacnetObjectTypes.OBJECT_DEVICE) // local object
                {
                    BaCSharpObject bcs = Mydevice.FindBacnetObject(reference.objectIdentifier);
                    if (bcs != null)
                    {
                        BacnetPropertyValue value = new BacnetPropertyValue();

                        if (m_PROP_PRIORITY == 0)
                        {
                            value.priority = (byte)16;
                        }
                        else
                        {
                            value.priority = (byte)m_PROP_PRIORITY;
                        }

                        value.property = new BacnetPropertyReference((uint)reference.propertyIdentifier, reference.arrayIndex);

                        value.value = new BacnetValue[] { new BacnetValue(m_PROP_PRESENT_VALUE) };

                        bcs.WritePropertyValue(value, false);
                    }
                }
                else
                {
                    KeyValuePair <BacnetClient, BacnetAddress>?recipient = null;

                    try
                    {
                        // SuroundingDevices is updated with Iam messages
                        recipient = Mydevice.SuroundingDevices[reference.deviceIndentifier.instance];
                    }
                    catch { }
                    if (recipient == null)
                    {
                        return;
                    }

                    BacnetValue[] value = new BacnetValue[] { new BacnetValue(m_PROP_PRESENT_VALUE) };
                    uint          wp    = m_PROP_PRIORITY;
                    System.Threading.ThreadPool.QueueUserWorkItem((o) =>
                    {
                        recipient.Value.Key.WritePriority = wp;
                        recipient.Value.Key.BeginWritePropertyRequest(recipient.Value.Value, reference.objectIdentifier, (BacnetPropertyIds)reference.propertyIdentifier, value, false);
                    }
                                                                  , null);
                }
            }
        }
Пример #17
0
        private static void OnWritePropertyRequest(BacnetClient sender, BacnetAddress adr, byte invoke_id, BacnetObjectId object_id, BacnetPropertyValue value, BacnetMaxSegments max_segments)
        {
            lock (m_lockObject)
            {
                try
                {
                    // Modif FC

                    DeviceStorage.ErrorCodes code = m_storage.WriteCommandableProperty(object_id, (BacnetPropertyIds)value.property.propertyIdentifier, value.value[0], value.priority);

                    if (code == DeviceStorage.ErrorCodes.NotForMe)
                    {
                        code = m_storage.WriteProperty(object_id, (BacnetPropertyIds)value.property.propertyIdentifier, value.property.propertyArrayIndex, value.value);
                    }

                    if (code == DeviceStorage.ErrorCodes.Good)
                    {
                        sender.SimpleAckResponse(adr, BacnetConfirmedServices.SERVICE_CONFIRMED_WRITE_PROPERTY, invoke_id);
                    }
                    else
                    if (code == DeviceStorage.ErrorCodes.WriteAccessDenied)
                    {
                        sender.ErrorResponse(adr, BacnetConfirmedServices.SERVICE_CONFIRMED_WRITE_PROPERTY, invoke_id, BacnetErrorClasses.ERROR_CLASS_DEVICE, BacnetErrorCodes.ERROR_CODE_WRITE_ACCESS_DENIED);
                    }
                    else
                    {
                        sender.ErrorResponse(adr, BacnetConfirmedServices.SERVICE_CONFIRMED_WRITE_PROPERTY, invoke_id, BacnetErrorClasses.ERROR_CLASS_DEVICE, BacnetErrorCodes.ERROR_CODE_OTHER);
                    }
                }
                catch (Exception)
                {
                    sender.ErrorResponse(adr, BacnetConfirmedServices.SERVICE_CONFIRMED_WRITE_PROPERTY, invoke_id, BacnetErrorClasses.ERROR_CLASS_DEVICE, BacnetErrorCodes.ERROR_CODE_OTHER);
                }
            }
        }
Пример #18
0
        private void AddProperty(BacnetPropertyValue val)
        {
            var id = (BacnetPropertyIds)val.property.propertyIdentifier;

            AddProperty(id, val);
        }
Пример #19
0
        /*****************************************************************************************************/
        static void handler_OnWritePropertyRequest(BacnetClient sender, BacnetAddress adr, byte invoke_id, BacnetObjectId object_id, BacnetPropertyValue value, BacnetMaxSegments max_segments)
        {
            // only OBJECT_ANALOG_VALUE:0.PROP_PRESENT_VALUE could be write in this sample code
            if ((object_id.type != BacnetObjectTypes.OBJECT_ANALOG_VALUE) || (object_id.instance != 0) || ((BacnetPropertyIds)value.property.propertyIdentifier != BacnetPropertyIds.PROP_PRESENT_VALUE))
            {
                sender.ErrorResponse(adr, BacnetConfirmedServices.SERVICE_CONFIRMED_WRITE_PROPERTY, invoke_id, BacnetErrorClasses.ERROR_CLASS_DEVICE, BacnetErrorCodes.ERROR_CODE_WRITE_ACCESS_DENIED);
                return;
            }

            lock (m_storage)
            {
                try
                {
                    DeviceStorage.ErrorCodes code = m_storage.WriteCommandableProperty(object_id, (BacnetPropertyIds)value.property.propertyIdentifier, value.value[0], value.priority);
                    if (code == DeviceStorage.ErrorCodes.NotForMe)
                    {
                        code = m_storage.WriteProperty(object_id, (BacnetPropertyIds)value.property.propertyIdentifier, value.property.propertyArrayIndex, value.value);
                    }

                    if (code == DeviceStorage.ErrorCodes.Good)
                    {
                        sender.SimpleAckResponse(adr, BacnetConfirmedServices.SERVICE_CONFIRMED_WRITE_PROPERTY, invoke_id);
                    }
                    else
                    {
                        sender.ErrorResponse(adr, BacnetConfirmedServices.SERVICE_CONFIRMED_WRITE_PROPERTY, invoke_id, BacnetErrorClasses.ERROR_CLASS_DEVICE, BacnetErrorCodes.ERROR_CODE_OTHER);
                    }
                }
                catch (Exception)
                {
                    sender.ErrorResponse(adr, BacnetConfirmedServices.SERVICE_CONFIRMED_WRITE_PROPERTY, invoke_id, BacnetErrorClasses.ERROR_CLASS_DEVICE, BacnetErrorCodes.ERROR_CODE_OTHER);
                }
            }
        }
        private void btnSchedule_Click(object sender, EventArgs e)
        {
            Int32  liDeviceID        = Convert.ToInt32(ddlScheduleDevice.SelectedValue);
            Int32  liObjectID        = Convert.ToInt32(Convert.ToString(ddlScheduleObject.SelectedValue).Split('|')[1]);
            string lsNewPresentvalue = Convert.ToString(txtScheduleValue.Text);
            Int32  liDaySelected     = Convert.ToInt32(ddlScheduleDay.SelectedValue);

            if (liDeviceID > -1 && liObjectID > -1 && !string.IsNullOrEmpty(lsNewPresentvalue) &&
                !string.IsNullOrEmpty(txtScheduleHours.Text) &&
                !string.IsNullOrEmpty(txtScheduleMinutes.Text) &&
                !string.IsNullOrEmpty(txtScheduleSeconds.Text))
            {
                using (var loESDLutronEntities = new ESDLutronEntities())
                {
                    var loBACnetDeviceDetail = loESDLutronEntities.BACnetDevices
                                               .Where(x => x.device_id == liDeviceID &&
                                                      x.object_type.ToUpper() == LutronFloorObjectType.Device)
                                               .Select(x => x).FirstOrDefault();


                    int?liTopInstanceID = loESDLutronEntities.BACnetDevices
                                          .Where(x => x.device_id == liDeviceID &&
                                                 x.object_type.ToUpper() == LutronFloorObjectType.SCHEDULE)
                                          .Select(x => x.object_instance).OrderByDescending(x => x.Value).FirstOrDefault();


                    BacnetAddress loBacnetAddress;
                    loBacnetAddress = new BacnetAddress(BacnetAddressTypes.IP, loBACnetDeviceDetail.network_id);
                    loBacnetAddress.RoutedSource = new BacnetAddress(BacnetAddressTypes.IP, loBACnetDeviceDetail.routed_source, (ushort)loBACnetDeviceDetail.routed_net);

                    //var deviceObjId = new BacnetObjectId(BacnetObjectTypes.OBJECT_DEVICE, (uint)liDeviceID);
                    //var objectIdList = await moBacnetClient.ReadPropertyAsync(loBacnetAddress, deviceObjId, BacnetPropertyIds.PROP_OBJECT_LIST);

                    //IList<BacnetValue> loDeviceObject;
                    //moBacnetClient.ReadPropertyRequest(loBacnetAddress, new BacnetObjectId(BacnetObjectTypes.OBJECT_DEVICE, (uint)liDeviceID), BacnetPropertyIds.PROP_OBJECT_LIST, out loDeviceObject);

                    //IList<BacnetValue> loScheduleValues;
                    //moBacnetClient.ReadPropertyRequest(loBacnetAddress, new BacnetObjectId(BacnetObjectTypes.OBJECT_SCHEDULE, (uint)11), BacnetPropertyIds.PROP_LIST_OF_OBJECT_PROPERTY_REFERENCES, out loScheduleValues);


                    //IList<BacnetValue> loWeekValues;
                    //moBacnetClient.ReadPropertyRequest(loBacnetAddress, new BacnetObjectId(BacnetObjectTypes.OBJECT_SCHEDULE, (uint)11), BacnetPropertyIds.PROP_WEEKLY_SCHEDULE, out loWeekValues);



                    //IList<BacnetValue> loObjectName;
                    //moBacnetClient.ReadPropertyRequest(loBacnetAddress, new BacnetObjectId(BacnetObjectTypes.OBJECT_SCHEDULE, (uint)11), BacnetPropertyIds.PROP_OBJECT_NAME, out loObjectName);


                    //var loData = values
                    //              .Where(x => ((BacnetObjectId)values[0].Value).Type == BacnetObjectTypes.OBJECT_SCHEDULE)
                    //              .Select(x => x.Value.GetType().GetProperty("Instance"))


                    IList <BacnetValue> loScheduleValues;
                    moBacnetClient.ReadPropertyRequest(loBacnetAddress, new BacnetObjectId(BacnetObjectTypes.OBJECT_SCHEDULE, (uint)2), BacnetPropertyIds.PROP_EXCEPTION_SCHEDULE, out loScheduleValues);



                    ICollection <BacnetPropertyValue> loBacnetPropertyValueList = new List <BacnetPropertyValue>();

                    BacnetPropertyValue loNewPropertyValue = new BacnetPropertyValue();
                    List <BacnetValue>  loBacnetValue      = new List <BacnetValue>();


                    #region set schedule

                    ////    Create new instance id based on largest available
                    //if(liTopInstanceID)
                    liTopInstanceID = liTopInstanceID != null ? liTopInstanceID + 1 : 1;

                    //// Set schedule object name
                    loBacnetValue      = new List <BacnetValue>();
                    loNewPropertyValue = new BacnetPropertyValue();
                    loBacnetValue.Add(new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_CHARACTER_STRING, "Schedule" + " " + liTopInstanceID));

                    loNewPropertyValue.value    = loBacnetValue;
                    loNewPropertyValue.property = new BacnetPropertyReference((uint)BacnetPropertyIds.PROP_OBJECT_NAME, ASN1.BACNET_ARRAY_ALL);

                    loBacnetPropertyValueList.Add(loNewPropertyValue);


                    //// Set effective period for schedule object
                    loBacnetValue      = new List <BacnetValue>();
                    loNewPropertyValue = new BacnetPropertyValue();
                    loBacnetValue.Add(new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_DATE, DateTime.Today.AddDays(-1)));
                    loBacnetValue.Add(new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_DATE, DateTime.Today.AddMonths(1)));

                    loNewPropertyValue.value    = loBacnetValue;
                    loNewPropertyValue.property = new BacnetPropertyReference((uint)BacnetPropertyIds.PROP_EFFECTIVE_PERIOD, ASN1.BACNET_ARRAY_ALL);

                    loBacnetPropertyValueList.Add(loNewPropertyValue);
                    #endregion


                    //// Set object reference to update it's value with schedule object
                    loBacnetValue      = new List <BacnetValue>();
                    loNewPropertyValue = new BacnetPropertyValue();

                    BacnetDeviceObjectPropertyReference loPropertyReference = new BacnetDeviceObjectPropertyReference();


                    //BACnetCalendarEntry loBACnetCalendarEntry = new BACnetCalendarEntry();


                    //// Add AV 1 present value change
                    loPropertyReference.ArrayIndex = -1;
                    loPropertyReference.DeviceId   = new BacnetObjectId(BacnetObjectTypes.OBJECT_DEVICE, (uint)liDeviceID);
                    loPropertyReference.ObjectId   = new BacnetObjectId(BacnetObjectTypes.OBJECT_ANALOG_VALUE, (uint)liObjectID);
                    loPropertyReference.PropertyId = BacnetPropertyIds.PROP_PRESENT_VALUE;

                    loBacnetValue.Add(new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_OBJECT_PROPERTY_REFERENCE,
                                                      loPropertyReference));

                    // Add AV 2 present value change
                    loPropertyReference            = new BacnetDeviceObjectPropertyReference();
                    loPropertyReference.ArrayIndex = -1;
                    loPropertyReference.DeviceId   = new BacnetObjectId(BacnetObjectTypes.OBJECT_DEVICE, (uint)liDeviceID);
                    loPropertyReference.ObjectId   = new BacnetObjectId(BacnetObjectTypes.OBJECT_ANALOG_VALUE, (uint)2);
                    loPropertyReference.PropertyId = BacnetPropertyIds.PROP_PRESENT_VALUE;

                    loBacnetValue.Add(new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_OBJECT_PROPERTY_REFERENCE,
                                                      loPropertyReference));

                    loNewPropertyValue.value    = loBacnetValue;
                    loNewPropertyValue.property = new BacnetPropertyReference((uint)BacnetPropertyIds.PROP_LIST_OF_OBJECT_PROPERTY_REFERENCES, ASN1.BACNET_ARRAY_ALL);

                    loBacnetPropertyValueList.Add(loNewPropertyValue);



                    //// Add weekly schedule for object
                    loBacnetValue      = new List <BacnetValue>();
                    loNewPropertyValue = new BacnetPropertyValue();

                    //// setup weekday to add schedule (0 = Monday, 1 = Tuesday ....., 6 = Sunday)
                    BacnetWeeklySchedule loBacnetWeeklySchedule = new BacnetWeeklySchedule();
                    loBacnetWeeklySchedule.days[liDaySelected] = new List <DaySchedule>();

                    //// Schedule to update AV 1,2 at specified time
                    loBacnetWeeklySchedule.days[liDaySelected].Add(
                        new DaySchedule(new DateTime(1, 1, 1, Convert.ToInt32(txtScheduleHours.Text),
                                                     Convert.ToInt32(txtScheduleMinutes.Text), Convert.ToInt32(txtScheduleSeconds.Text)),
                                        Convert.ToSingle((new Random()).Next(100, 999))));

                    //// Schedule to update AV 1,2 after 5 mins of schedule
                    loBacnetWeeklySchedule.days[liDaySelected].Add(new DaySchedule(DateTime.Now.AddMinutes(1), Convert.ToSingle(lsNewPresentvalue)));


                    loBacnetValue.Add(new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_WEEKLY_SCHEDULE, loBacnetWeeklySchedule));

                    loNewPropertyValue.value    = loBacnetValue;
                    loNewPropertyValue.property = new BacnetPropertyReference((uint)BacnetPropertyIds.PROP_WEEKLY_SCHEDULE, ASN1.BACNET_ARRAY_ALL);

                    loBacnetPropertyValueList.Add(loNewPropertyValue);



                    //// Add exception schedule
                    //loBacnetValue = new List<BacnetValue>();
                    //loNewPropertyValue = new BacnetPropertyValue();

                    //loBacnetValue.Add(new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_TIME, new DateTime(2017,9,11,15,10,0)));
                    //loBacnetValue.Add(new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_REAL, 333));
                    //loBacnetValue.Add(new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_DATE, DateTime.Today.Date));


                    //loNewPropertyValue.value = loBacnetValue;
                    //loNewPropertyValue.property = new BacnetPropertyReference((uint)BacnetPropertyIds.PROP_EXCEPTION_SCHEDULE, ASN1.BACNET_ARRAY_ALL);
                    //loBacnetPropertyValueList.Add(loNewPropertyValue);



                    loBacnetValue      = new List <BacnetValue>();
                    loNewPropertyValue = new BacnetPropertyValue();

                    BacnetWeeklyExceptionSchedule loBacnetWeeklyExceptionSchedule = new BacnetWeeklyExceptionSchedule();

                    List <ExceptionScheduleTimeValue>[] loExceptionSchedulTimeValue = new List <ExceptionScheduleTimeValue> [1];
                    loExceptionSchedulTimeValue[0] = new List <ExceptionScheduleTimeValue>();
                    loExceptionSchedulTimeValue[0].Add(new ExceptionScheduleTimeValue(DateTime.Now.AddMinutes(2), Convert.ToSingle(111)));


                    loBacnetWeeklyExceptionSchedule.loExceptionScheduleArray    = new List <ExceptionScheduleArray> [1];
                    loBacnetWeeklyExceptionSchedule.loExceptionScheduleArray[0] = new List <ExceptionScheduleArray>();
                    loBacnetWeeklyExceptionSchedule.loExceptionScheduleArray[0].Add(new ExceptionScheduleArray(new DateTime(1, 1, 1, 18, 30, 1), loExceptionSchedulTimeValue));

                    loBacnetValue.Add(new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_SPECIAL_EVENT, loBacnetWeeklyExceptionSchedule));

                    loNewPropertyValue.value    = loBacnetValue;
                    loNewPropertyValue.property = new BacnetPropertyReference((uint)BacnetPropertyIds.PROP_EXCEPTION_SCHEDULE, ASN1.BACNET_ARRAY_ALL);

                    loBacnetPropertyValueList.Add(loNewPropertyValue);

                    moBacnetClient.CreateObjectRequest(loBacnetAddress, new BacnetObjectId(BacnetObjectTypes.OBJECT_SCHEDULE, (uint)liTopInstanceID), loBacnetPropertyValueList);
                }
            }
            else
            {
                if (liDeviceID < 0)
                {
                    MessageBox.Show("Please select device");
                }
                else if (liObjectID < 0)
                {
                    MessageBox.Show("Please select object");
                }
                else
                {
                    MessageBox.Show("Schedule is not setup properly, please review.");
                }
            }
        }
Пример #21
0
        private static void OnWritePropertyRequest(BacnetClient sender, BacnetAddress adr, byte invoke_id, BacnetObjectId object_id, BacnetPropertyValue value, BacnetMaxSegments max_segments)
        {
            BacnetPropertyIds PropId = (BacnetPropertyIds)value.property.propertyIdentifier;

            bool AllowWrite =
                (object_id.Equals("OBJECT_ANALOG_VALUE:0") && (PropId == BacnetPropertyIds.PROP_OUT_OF_SERVICE)) ||
                (object_id.Equals("OBJECT_ANALOG_VALUE:0") && (PropId == BacnetPropertyIds.PROP_PRESENT_VALUE)) ||
                (object_id.Equals("OBJECT_ANALOG_VALUE:1") && (PropId == BacnetPropertyIds.PROP_PRESENT_VALUE)) ||
                (object_id.Equals("OBJECT_ANALOG_VALUE:2") && (PropId == BacnetPropertyIds.PROP_PRESENT_VALUE)) ||
                (object_id.Equals("OBJECT_ANALOG_VALUE:3") && (PropId == BacnetPropertyIds.PROP_PRESENT_VALUE)) ||
                (object_id.Equals("OBJECT_CHARACTERSTRING_VALUE:1") && (PropId == BacnetPropertyIds.PROP_PRESENT_VALUE)) ||
                (object_id.Equals("OBJECT_CHARACTERSTRING_VALUE:2") && (PropId == BacnetPropertyIds.PROP_PRESENT_VALUE)) ||
                (object_id.Equals("OBJECT_CHARACTERSTRING_VALUE:3") && (PropId == BacnetPropertyIds.PROP_PRESENT_VALUE)) ||
                (object_id.Equals("OBJECT_MULTI_STATE_VALUE:0") && (PropId == BacnetPropertyIds.PROP_PRESENT_VALUE));

            if (AllowWrite == false)
            {
                sender.ErrorResponse(adr, BacnetConfirmedServices.SERVICE_CONFIRMED_WRITE_PROPERTY, invoke_id, BacnetErrorClasses.ERROR_CLASS_DEVICE, BacnetErrorCodes.ERROR_CODE_WRITE_ACCESS_DENIED);
                return;
            }

            lock (m_lockObject)
            {
                try
                {
                    // Modif FC

                    DeviceStorage.ErrorCodes code = m_storage.WriteCommandableProperty(object_id, (BacnetPropertyIds)value.property.propertyIdentifier, value.value[0], value.priority);

                    if (code == DeviceStorage.ErrorCodes.NotForMe)
                    {
                        code = m_storage.WriteProperty(object_id, (BacnetPropertyIds)value.property.propertyIdentifier, value.property.propertyArrayIndex, value.value);
                    }

                    if (code == DeviceStorage.ErrorCodes.Good)
                    {
                        sender.SimpleAckResponse(adr, BacnetConfirmedServices.SERVICE_CONFIRMED_WRITE_PROPERTY, invoke_id);
                    }
                    else
                    if (code == DeviceStorage.ErrorCodes.WriteAccessDenied)
                    {
                        sender.ErrorResponse(adr, BacnetConfirmedServices.SERVICE_CONFIRMED_WRITE_PROPERTY, invoke_id, BacnetErrorClasses.ERROR_CLASS_DEVICE, BacnetErrorCodes.ERROR_CODE_WRITE_ACCESS_DENIED);
                    }
                    else
                    {
                        sender.ErrorResponse(adr, BacnetConfirmedServices.SERVICE_CONFIRMED_WRITE_PROPERTY, invoke_id, BacnetErrorClasses.ERROR_CLASS_DEVICE, BacnetErrorCodes.ERROR_CODE_OTHER);
                    }
                }
                catch (Exception)
                {
                    sender.ErrorResponse(adr, BacnetConfirmedServices.SERVICE_CONFIRMED_WRITE_PROPERTY, invoke_id, BacnetErrorClasses.ERROR_CLASS_DEVICE, BacnetErrorCodes.ERROR_CODE_OTHER);
                }
            }
        }
Пример #22
0
        // Ici les remplacement de la lecture de quelques élements
        private static void m_storage_ReadOverride(BacnetObjectId object_id, BacnetPropertyIds property_id, uint array_index, out IList <BacnetValue> value, out DeviceStorage.ErrorCodes status, out bool handled)
        {
            handled = true;
            value   = new BacnetValue[0];
            status  = DeviceStorage.ErrorCodes.Good;


            if (object_id.type == BacnetObjectTypes.OBJECT_DEVICE && property_id == BacnetPropertyIds.PROP_OBJECT_LIST)
            {
                if (array_index == 0)
                {
                    //object list count
                    value = new BacnetValue[] { new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_UNSIGNED_INT, (uint)m_storage.Objects.Length) };
                }
                else if (array_index != System.IO.BACnet.Serialize.ASN1.BACNET_ARRAY_ALL)
                {
                    //object list index
                    value = new BacnetValue[] { new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_OBJECT_ID, new BacnetObjectId(m_storage.Objects[array_index - 1].Type, m_storage.Objects[array_index - 1].Instance)) };
                }
                else
                {
                    //object list whole
                    BacnetValue[] list = new BacnetValue[m_storage.Objects.Length];
                    for (int i = 0; i < list.Length; i++)
                    {
                        list[i].Tag   = BacnetApplicationTags.BACNET_APPLICATION_TAG_OBJECT_ID;
                        list[i].Value = new BacnetObjectId(m_storage.Objects[i].Type, m_storage.Objects[i].Instance);
                    }
                    value = list;
                }
            }
            else if (object_id.type == BacnetObjectTypes.OBJECT_DEVICE && object_id.instance == m_storage.DeviceId && property_id == BacnetPropertyIds.PROP_PROTOCOL_OBJECT_TYPES_SUPPORTED)
            {
                BacnetValue v = new BacnetValue();
                v.Tag = BacnetApplicationTags.BACNET_APPLICATION_TAG_BIT_STRING;
                BacnetBitString b = new BacnetBitString();
                b.SetBit((byte)BacnetObjectTypes.MAX_ASHRAE_OBJECT_TYPE, false); //set all false
                b.SetBit((byte)BacnetObjectTypes.OBJECT_ANALOG_INPUT, true);
                b.SetBit((byte)BacnetObjectTypes.OBJECT_DEVICE, true);
                b.SetBit((byte)BacnetObjectTypes.OBJECT_ANALOG_VALUE, true);
                b.SetBit((byte)BacnetObjectTypes.OBJECT_CHARACTERSTRING_VALUE, true);
                b.SetBit((byte)BacnetObjectTypes.OBJECT_MULTI_STATE_VALUE, true);
                b.SetBit((byte)BacnetObjectTypes.OBJECT_BINARY_VALUE, true);
                v.Value = b;
                value   = new BacnetValue[] { v };
            }
            else if (object_id.type == BacnetObjectTypes.OBJECT_DEVICE && object_id.instance == m_storage.DeviceId && property_id == BacnetPropertyIds.PROP_PROTOCOL_SERVICES_SUPPORTED)
            {
                BacnetValue v = new BacnetValue();
                v.Tag = BacnetApplicationTags.BACNET_APPLICATION_TAG_BIT_STRING;
                BacnetBitString b = new BacnetBitString();
                b.SetBit((byte)BacnetServicesSupported.MAX_BACNET_SERVICES_SUPPORTED, false); //set all false
                b.SetBit((byte)BacnetServicesSupported.SERVICE_SUPPORTED_SUBSCRIBE_COV, true);

                b.SetBit((byte)BacnetServicesSupported.SERVICE_SUPPORTED_I_AM, true);
                b.SetBit((byte)BacnetServicesSupported.SERVICE_SUPPORTED_WHO_IS, true);
                b.SetBit((byte)BacnetServicesSupported.SERVICE_SUPPORTED_READ_PROP_MULTIPLE, true);
                b.SetBit((byte)BacnetServicesSupported.SERVICE_SUPPORTED_READ_PROPERTY, true);
                b.SetBit((byte)BacnetServicesSupported.SERVICE_SUPPORTED_WRITE_PROPERTY, true);
                b.SetBit((byte)BacnetServicesSupported.SERVICE_SUPPORTED_CONFIRMED_COV_NOTIFICATION, true);
                b.SetBit((byte)BacnetServicesSupported.SERVICE_SUPPORTED_UNCONFIRMED_COV_NOTIFICATION, true);
                b.SetBit((byte)BacnetServicesSupported.SERVICE_SUPPORTED_SUBSCRIBE_COV_PROPERTY, true);
                b.SetBit((byte)BacnetServicesSupported.SERVICE_SUPPORTED_REINITIALIZE_DEVICE, true);
                b.SetBit((byte)BacnetServicesSupported.SERVICE_SUPPORTED_DEVICE_COMMUNICATION_CONTROL, true);
                b.SetBit((byte)BacnetServicesSupported.SERVICE_SUPPORTED_TIME_SYNCHRONIZATION, true);
                b.SetBit((byte)BacnetServicesSupported.SERVICE_SUPPORTED_UTC_TIME_SYNCHRONIZATION, true);
                v.Value = b;
                value   = new BacnetValue[] { v };
            }
            else if (object_id.type == BacnetObjectTypes.OBJECT_DEVICE && object_id.instance == m_storage.DeviceId && property_id == BacnetPropertyIds.PROP_SEGMENTATION_SUPPORTED)
            {
                BacnetValue v = new BacnetValue();
                v.Tag   = BacnetApplicationTags.BACNET_APPLICATION_TAG_ENUMERATED;
                v.Value = (uint)BacnetSegmentations.SEGMENTATION_BOTH;
                value   = new BacnetValue[] { v };
            }
            else if (object_id.type == BacnetObjectTypes.OBJECT_DEVICE && object_id.instance == m_storage.DeviceId && property_id == BacnetPropertyIds.PROP_SYSTEM_STATUS)
            {
                BacnetValue v = new BacnetValue();
                v.Tag   = BacnetApplicationTags.BACNET_APPLICATION_TAG_ENUMERATED;
                v.Value = (uint)BacnetDeviceStatus.OPERATIONAL;      //can we be in any other mode I wonder?
                value   = new BacnetValue[] { v };
            }
            else if (object_id.type == BacnetObjectTypes.OBJECT_DEVICE && object_id.instance == m_storage.DeviceId && property_id == BacnetPropertyIds.PROP_ACTIVE_COV_SUBSCRIPTIONS)
            {
                List <BacnetValue> list = new List <BacnetValue>();
                foreach (KeyValuePair <BacnetObjectId, List <Subscription> > entry in m_subscriptions)
                {
                    foreach (Subscription sub in entry.Value)
                    {
                        //encode
                        System.IO.BACnet.Serialize.EncodeBuffer buffer = new System.IO.BACnet.Serialize.EncodeBuffer();
                        BacnetCOVSubscription cov = new BacnetCOVSubscription();
                        cov.Recipient = sub.reciever_address;
                        cov.subscriptionProcessIdentifier = sub.subscriberProcessIdentifier;
                        cov.monitoredObjectIdentifier     = sub.monitoredObjectIdentifier;
                        cov.monitoredProperty             = sub.monitoredProperty;
                        cov.IssueConfirmedNotifications   = sub.issueConfirmedNotifications;
                        cov.TimeRemaining = (uint)sub.lifetime - (uint)(DateTime.Now - sub.start).TotalMinutes;
                        cov.COVIncrement  = sub.covIncrement;
                        System.IO.BACnet.Serialize.ASN1.encode_cov_subscription(buffer, cov);

                        //add
                        BacnetValue v = new BacnetValue();
                        v.Tag   = BacnetApplicationTags.BACNET_APPLICATION_TAG_COV_SUBSCRIPTION;
                        v.Value = buffer.ToArray();
                        list.Add(v);
                    }
                }
                value = list;
            }
            else if (object_id.type == BacnetObjectTypes.OBJECT_OCTETSTRING_VALUE && object_id.instance == 0 && property_id == BacnetPropertyIds.PROP_PRESENT_VALUE)
            {
                //this is our huge blob
                BacnetValue v = new BacnetValue();
                v.Tag = BacnetApplicationTags.BACNET_APPLICATION_TAG_OCTET_STRING;
                byte[] blob = new byte[2000];
                for (int i = 0; i < blob.Length; i++)
                {
                    blob[i] = (i % 2 == 0) ? (byte)'A' : (byte)'B';
                }
                v.Value = blob;
                value   = new BacnetValue[] { v };
            }
            else if (object_id.type == BacnetObjectTypes.OBJECT_GROUP && property_id == BacnetPropertyIds.PROP_PRESENT_VALUE)
            {
                //get property list
                IList <BacnetValue> properties;
                if (m_storage.ReadProperty(object_id, BacnetPropertyIds.PROP_LIST_OF_GROUP_MEMBERS, System.IO.BACnet.Serialize.ASN1.BACNET_ARRAY_ALL, out properties) != DeviceStorage.ErrorCodes.Good)
                {
                    value = new BacnetValue[] { new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_ERROR, new BacnetError(BacnetErrorClasses.ERROR_CLASS_DEVICE, BacnetErrorCodes.ERROR_CODE_INTERNAL_ERROR)) };
                }
                else
                {
                    List <BacnetValue> _value = new List <BacnetValue>();
                    foreach (BacnetValue p in properties)
                    {
                        if (p.Value is BacnetReadAccessSpecification)
                        {
                            BacnetReadAccessSpecification prop   = (BacnetReadAccessSpecification)p.Value;
                            BacnetReadAccessResult        result = new BacnetReadAccessResult();
                            result.objectIdentifier = prop.objectIdentifier;
                            List <BacnetPropertyValue> result_values = new List <BacnetPropertyValue>();
                            foreach (BacnetPropertyReference r in prop.propertyReferences)
                            {
                                BacnetPropertyValue prop_value = new BacnetPropertyValue();
                                prop_value.property = r;
                                if (m_storage.ReadProperty(prop.objectIdentifier, (BacnetPropertyIds)r.propertyIdentifier, r.propertyArrayIndex, out prop_value.value) != DeviceStorage.ErrorCodes.Good)
                                {
                                    prop_value.value = new BacnetValue[] { new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_ERROR, new BacnetError(BacnetErrorClasses.ERROR_CLASS_DEVICE, BacnetErrorCodes.ERROR_CODE_INTERNAL_ERROR)) };
                                }
                                result_values.Add(prop_value);
                            }
                            result.values = result_values;
                            _value.Add(new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_READ_ACCESS_RESULT, result));
                        }
                    }
                    value = _value;
                }
            }
            else
            {
                handled = false;
            }
        }
Пример #23
0
 public ErrorCodes WritePropertyValue(BacnetClient sender, BacnetAddress adr, BacnetPropertyValue value, bool writeFromNetwork)
 {
     this.sender = sender;
     return(WritePropertyValue(value, writeFromNetwork));
 }
Пример #24
0
        private void ParseValue()
        {
            if (this.BacnetPropertyValue == null)
            {
                return;
            }

            BacnetPropertyValue p_value = (BacnetPropertyValue)this.BacnetPropertyValue;

            object value = null;

            BacnetValue[] b_values = null;
            if (p_value.value != null)
            {
                b_values = new BacnetValue[p_value.value.Count];

                p_value.value.CopyTo(b_values, 0);
                if (b_values.Length > 1)
                {
                    object[] arr = new object[b_values.Length];
                    for (int j = 0; j < arr.Length; j++)
                    {
                        arr[j] = b_values[j].Value;
                    }
                    value = arr;
                }
                else if (b_values.Length == 1)
                {
                    value = b_values[0].Value;
                }
            }
            else
            {
                b_values = new BacnetValue[0];
            }


            var propName = GetNiceName((BacnetPropertyIds)p_value.property.propertyIdentifier);
            var propRef  = p_value.property;
            //var readOnly = false;   //may change...
            var appTag = b_values.Length > 0 ? b_values[0].Tag : (BacnetApplicationTags?)null;


            // Modif FC
            CustomProperty cp;

            Type    propType = null;
            Boolean readOnly;

            switch ((BacnetPropertyIds)p_value.property.propertyIdentifier)
            {
            // PROP_RELINQUISH_DEFAULT can be write to null value
            case BacnetPropertyIds.PROP_PRESENT_VALUE:
                // change to the related nullable type
                Type t = null;
                try
                {
                    t = value.GetType();
                    t = Type.GetType("System.Nullable`1[" + value.GetType().FullName + "]");
                }
                catch { }

                propType = t != null ? t : typeof(string);
                readOnly = false;       //for now - in future, some properties may be
                break;

            default:

                propType = value != null?value.GetType() : typeof(string);

                readOnly = false;
                break;
            }

            this.CustomProperty = new Utilities.CustomProperty(propName, value, propType, readOnly, "", appTag, null, propRef);

            this.PropertyDescriptor = new BACnetCustomPropertyDescriptor(ref this.CustomProperty, new Attribute[] { });
            //this.


            //want to put in logic here so that priority array gets read as a string.  Look at how property html is generated
        }
        /*****************************************************************************************************/
        static void handler_OnWritePropertyRequest(BacnetClient sender, BacnetAddress adr, byte invoke_id, BacnetObjectId object_id, BacnetPropertyValue value, BacnetMaxSegments max_segments)
        {
            lock (device)
            {
                BaCSharpObject bacobj = device.FindBacnetObject(object_id);
                if (bacobj != null)
                {
                    ErrorCodes error = bacobj.WritePropertyValue(sender, adr, value, true);
                    if (error == ErrorCodes.Good)
                    {
                        sender.SimpleAckResponse(adr, BacnetConfirmedServices.SERVICE_CONFIRMED_WRITE_PROPERTY, invoke_id);
                    }
                    else
                    {
                        BacnetErrorCodes bacEr = BacnetErrorCodes.ERROR_CODE_OTHER;
                        if (error == ErrorCodes.WriteAccessDenied)
                        {
                            bacEr = BacnetErrorCodes.ERROR_CODE_WRITE_ACCESS_DENIED;
                        }
                        if (error == ErrorCodes.OutOfRange)
                        {
                            bacEr = BacnetErrorCodes.ERROR_CODE_VALUE_OUT_OF_RANGE;
                        }

                        sender.ErrorResponse(adr, BacnetConfirmedServices.SERVICE_CONFIRMED_WRITE_PROPERTY, invoke_id, BacnetErrorClasses.ERROR_CLASS_DEVICE, bacEr);
                    }
                }
                else
                {
                    sender.ErrorResponse(adr, BacnetConfirmedServices.SERVICE_CONFIRMED_READ_PROPERTY, invoke_id, BacnetErrorClasses.ERROR_CLASS_OBJECT, BacnetErrorCodes.ERROR_CODE_UNKNOWN_OBJECT);
                }
            }
        }
Пример #26
0
        public ErrorCodes WritePropertyValue(BacnetPropertyValue value, bool writeFromNetwork)
        {
            // First try to find the set2_ method in the class code
            string lookup = $"set2_{value.property.ToString()}";

            MethodInfo m;

            if (!_writePropertyMethods.TryGetValue(lookup, out m))
            {
                m = GetType().GetMethod(lookup);

                _writePropertyMethods.TryAdd(lookup, m);
            }

            try
            {
                if (m != null)
                {
                    // Yes Invoke
                    ErrorCode_PropertyWrite = ErrorCodes.Good;
                    m.Invoke(this, new object[] { value.value, value.priority });

                    if (ErrorCode_PropertyWrite == ErrorCodes.Good)
                    {
                        if (OnWriteNotify != null)
                        {
                            OnWriteNotify(this, (BacnetPropertyIds)value.property.propertyIdentifier);
                        }
                        if (OnExternalCOVNotify != null)
                        {
                            OnExternalCOVNotify(this, (BacnetPropertyIds)value.property.propertyIdentifier);
                        }
                    }

                    return(ErrorCode_PropertyWrite);
                }
            }
            catch
            {
                return(ErrorCodes.GenericError);
            }


            // Second, if not found, try to find in the programmed properties list
            PropertyInfo p;

            if (!_writePropertyProperties.TryGetValue(value.property.ToString(), out p))
            {
                p = GetType().GetProperty(value.property.ToString());

                _writePropertyProperties.TryAdd(lookup, p);
            }

            if (p != null)
            {
                if (p.GetSetMethod() == null)
                {
                    return(ErrorCodes.WriteAccessDenied);
                }

                try
                {
                    // since Property cannot return error, this member could be set if a problem occure
                    // or an Exception can be throw
                    ErrorCode_PropertyWrite = ErrorCodes.Good;

                    try
                    {
                        if (value.value.Count == 1)
                        {
                            try
                            {
                                p.SetValue(this, value.value[0].Value, null);   // The value is not a List< >
                            }
                            catch { }
                        }
                        else
                        {
                            p.SetValue(this, value.value, null);    // The value is a List <  >
                        }
                    }
                    catch
                    {
                        p.SetValue(this, value.value[0].Value, null); // The value is not a List< > but a List<> was given
                    }


                    if (ErrorCode_PropertyWrite == ErrorCodes.Good)
                    {
                        if (OnWriteNotify != null)
                        {
                            OnWriteNotify(this, (BacnetPropertyIds)value.property.propertyIdentifier);
                        }
                        if (OnExternalCOVNotify != null)
                        {
                            OnExternalCOVNotify(this, (BacnetPropertyIds)value.property.propertyIdentifier);
                        }
                    }

                    return(ErrorCode_PropertyWrite);
                }
                catch
                {
                    return(ErrorCodes.GenericError);
                }
            }

            return(ErrorCodes.NotExist);
        }
Пример #27
0
 public BACnetProperty(BACnetObject bno, BacnetPropertyIds property_id, BacnetPropertyValue property_value) //, uint array_index = System.IO.BACnet.Serialize.ASN1.BACNET_ARRAY_ALL) //this(bno, property_id, array_index)
 {
     Initialize(bno, property_id);
     this.BacnetPropertyValue = property_value;
     ParseValue();
 }