static void ReadWriteExample() { BacnetValue Value; bool ret; // Read Present_Value property on the object ANALOG_INPUT:0 provided by the device 12345 // Scalar value only // ret = ReadScalarValue(20205, new BacnetObjectId(BacnetObjectTypes.OBJECT_ANALOG_VALUE, 3), BacnetPropertyIds.PROP_PRESENT_VALUE, out Value); // Sauter ret = ReadScalarValue(127001, new BacnetObjectId(BacnetObjectTypes.OBJECT_ANALOG_VALUE, 6923), BacnetPropertyIds.PROP_PRESENT_VALUE, out Value); // LM if (ret == true) { Debug.WriteLine("Read value : " + Value.Value.ToString()); // Write Present_Value property on the object ANALOG_OUTPUT:0 provided by the device 4000 BacnetValue newValue = new BacnetValue(Convert.ToSingle(Value.Value)); // expect it's a float //ret = WriteScalarValue(20205, new BacnetObjectId(BacnetObjectTypes.OBJECT_ANALOG_VALUE, 0), BacnetPropertyIds.PROP_PRESENT_VALUE, newValue); Debug.WriteLine("Write feedback : " + ret.ToString()); } else { Debug.WriteLine("Error somewhere !"); } }
public static string SerializeValue(BacnetValue value, BacnetApplicationTags type) { switch (type) { case BacnetApplicationTags.BACNET_APPLICATION_TAG_NULL: return(value.ToString()); // Modif FC case BacnetApplicationTags.BACNET_APPLICATION_TAG_REAL: return(((float)value.Value).ToString(CultureInfo.InvariantCulture)); case BacnetApplicationTags.BACNET_APPLICATION_TAG_DOUBLE: return(((double)value.Value).ToString(CultureInfo.InvariantCulture)); case BacnetApplicationTags.BACNET_APPLICATION_TAG_OCTET_STRING: return(Convert.ToBase64String((byte[])value.Value)); case BacnetApplicationTags.BACNET_APPLICATION_TAG_CONTEXT_SPECIFIC_DECODED: { return(value.Value is byte[]? Convert.ToBase64String((byte[])value.Value) : string.Join(";", ((BacnetValue[])value.Value) .Select(v => SerializeValue(v, v.Tag)))); } default: return(value.Value.ToString()); } }
/// <summary> /// Toggle Lighting State /// </summary> /// <param name="fiDeviceID"></param> /// <param name="fbToggleStatus"></param> private void ToggleLutronLight(int fiDeviceID, bool fbToggleStatus) { using (var loESDLutronEntities = new ESDLutronEntities()) { var loBACnetDeviceDetail = loESDLutronEntities.BACnetDevices .Where(x => x.device_id == fiDeviceID && x.object_instance == (int?)LutronObjectType.Lighting_State) .Select(x => x).FirstOrDefault(); if (loBACnetDeviceDetail != null && loBACnetDeviceDetail.object_instance != null) { BacnetAddress loBacnetAddress = new BacnetAddress(BacnetAddressTypes.IP, loBACnetDeviceDetail.network_id); loBacnetAddress.RoutedSource = new BacnetAddress(BacnetAddressTypes.IP, loBACnetDeviceDetail.routed_source, (ushort)loBACnetDeviceDetail.routed_net); IList <BacnetValue> loBacnetValueList; moBacnetClient.ReadPropertyRequest(loBacnetAddress, new BacnetObjectId(BacnetObjectTypes.OBJECT_BINARY_VALUE, (uint)loBACnetDeviceDetail.object_instance), BacnetPropertyIds.PROP_PRESENT_VALUE, out loBacnetValueList); BacnetValue loBacnetNewValue = new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_ENUMERATED, fbToggleStatus == true ? 1 : 0); BacnetValue[] loWriteValue = { loBacnetNewValue }; moBacnetClient.WritePropertyRequest(loBacnetAddress, new BacnetObjectId(BacnetObjectTypes.OBJECT_BINARY_VALUE, (uint)loBACnetDeviceDetail.object_instance), BacnetPropertyIds.PROP_PRESENT_VALUE, loWriteValue); //BacnetValue[] loNewBacnetValue = { new BacnetValue(Convert.ToSingle(111)) }; ////bacnet_client.WritePropertyRequest(loBacnetAddress, new BacnetObjectId(BacnetObjectTypes.OBJECT_BINARY_VALUE, (uint)loBACnetDeviceDetail.object_instance), BacnetPropertyIds.PROP_ACTION, loNewBacnetValue); ////bacnet_client.WritePropertyRequest(loBacnetAddress, new BacnetObjectId(BacnetObjectTypes.OBJECT_ANALOG_INPUT, 0), BacnetPropertyIds.PROP_PRESENT_VALUE, loNewBacnetValue); //bacnet_client.WritePropertyRequest(loBacnetAddress, new BacnetObjectId(BacnetObjectTypes.OBJECT_ANALOG_INPUT, 0), BacnetPropertyIds.PROP_PRESENT_VALUE, loNewBacnetValue); } } //bool ret = WriteScalarValue(1, new BacnetObjectId(BacnetObjectTypes.OBJECT_ANALOG_VALUE, 2), BacnetPropertyIds.PROP_PRESENT_VALUE, loNewValue); }
/*****************************************************************************************************/ static void ReadWriteExample() { Console.WriteLine("Attempt to read value from 12345"); BacnetValue Value; bool ret; // Read Present_Value property on the object ANALOG_INPUT:0 provided by the device 12345 // Scalar value only ret = ReadScalarValue(12345, new BacnetObjectId(BacnetObjectTypes.OBJECT_ANALOG_INPUT, 0), BacnetPropertyIds.PROP_PRESENT_VALUE, out Value); if (ret == true) { Console.WriteLine("Read value : " + Value.Value.ToString()); // Write Present_Value property on the object ANALOG_OUTPUT:0 provided by the device 4000 BacnetValue newValue = new BacnetValue(Convert.ToSingle(Value.Value)); // expect it's a float ret = WriteScalarValue(4000, new BacnetObjectId(BacnetObjectTypes.OBJECT_ANALOG_OUTPUT, 0), BacnetPropertyIds.PROP_PRESENT_VALUE, newValue); Console.WriteLine("Write feedback : " + ret.ToString()); } else { Console.WriteLine("Could not read from device 12345"); } }
public static LightStateEntity SetConfLightState(LightStateEntity lightStateEntity) { var bacnetDeviceFromDB = lutronEntities.BACnetDevices .Where(x => x.device_id == lightStateEntity.DeviceID && x.object_instance == (int?)LutronObjectType.Lighting_State) .Select(x => x).FirstOrDefault(); if (bacnetDeviceFromDB != null && bacnetDeviceFromDB.bacnet_device_id > 0) { BacnetAddress bacnetAddress = new BacnetAddress(BacnetAddressTypes.IP, bacnetDeviceFromDB.network_id); bacnetAddress.RoutedSource = new BacnetAddress(BacnetAddressTypes.IP, bacnetDeviceFromDB.routed_source, (ushort)bacnetDeviceFromDB.routed_net); BacnetValue newLightState = new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_ENUMERATED, lightStateEntity.LightState == true ? 1 : 0); BacnetValue[] writeNewLightState = { newLightState }; if (bacNetClient == null) { bacNetClient = BackNetClientInit.NewBackNetClient(); } bacNetClient.WritePropertyRequest(bacnetAddress, new BacnetObjectId(BacnetObjectTypes.OBJECT_BINARY_VALUE, (uint)LutronObjectType.Lighting_State), BacnetPropertyIds.PROP_PRESENT_VALUE, writeNewLightState); } Thread.Sleep(1000); return(BackNetReadAction.GetConfLightState(lightStateEntity.DeviceID)); }
public static LightLevelEntity SetConfLightLevel(LightLevelEntity lightLevelEntity) { var bacnetDeviceFromDB = lutronEntities.BACnetDevices .Where(x => x.device_id == lightLevelEntity.DeviceID && x.object_instance == (int?)LutronObjectType.Lighting_Level) .Select(x => x).FirstOrDefault(); if (bacnetDeviceFromDB != null && bacnetDeviceFromDB.bacnet_device_id > 0) { BacnetAddress bacnetAddress = new BacnetAddress(BacnetAddressTypes.IP, bacnetDeviceFromDB.network_id); bacnetAddress.RoutedSource = new BacnetAddress(BacnetAddressTypes.IP, bacnetDeviceFromDB.routed_source, (ushort)bacnetDeviceFromDB.routed_net); BacnetValue newLightLevel = new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_REAL, Convert.ToSingle(lightLevelEntity.LightLevel)); BacnetValue[] writeNewLightLevel = { newLightLevel }; if (bacNetClient == null) { bacNetClient = BackNetClientInit.NewBackNetClient(); } bacNetClient.WritePropertyRequest(bacnetAddress, new BacnetObjectId(BacnetObjectTypes.OBJECT_ANALOG_VALUE, (uint)LutronObjectType.Lighting_Level), BacnetPropertyIds.PROP_PRESENT_VALUE, writeNewLightLevel); } return(BackNetReadAction.GetConfLightLevel(lightLevelEntity.DeviceID)); }
private void AdaptationFarenheit() { BacnetObjectId b; BacnetValue bv; if (Application.CurrentCulture.ToString() != "en-US") { return; } for (int i = 0; i < 4; i++) { b = new BacnetObjectId(BacnetObjectTypes.OBJECT_ANALOG_VALUE, (uint)(i)); bv = BacnetActivity.GetBacObjectPresentValue(b); BacnetActivity.SetBacObjectPresentValue(b, new BacnetValue((float)Math.Round(TempDegre2Value((float)bv.Value)))); IList <BacnetValue> val = new BacnetValue[1] { new BacnetValue(64) }; BacnetActivity.m_storage.WriteProperty(b, BacnetPropertyIds.PROP_UNITS, 1, val, true); } for (int i = 0; i < 3; i++) { b = new BacnetObjectId(BacnetObjectTypes.OBJECT_ANALOG_INPUT, (uint)(i)); IList <BacnetValue> val = new BacnetValue[1] { new BacnetValue(64) }; BacnetActivity.m_storage.WriteProperty(b, BacnetPropertyIds.PROP_UNITS, 1, val, true); } }
public void AddBacnetObject(BaCSharpObject newObj) { BacnetValue v = new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_DEVICE_OBJECT_REFERENCE, new BacnetDeviceObjectReference(newObj.PROP_OBJECT_IDENTIFIER)); m_PROP_SUBORDINATE_LIST.Add(v); // Each object provided by the server must be added one by one to the DeviceObject Mydevice.AddBacnetObject(newObj); }
public override void Post_NewtonSoft_Json_Deserialization(DeviceObject device) { base.Post_NewtonSoft_Json_Deserialization(device); // In this copy the type become int64 for (int i = 0; i < 3; i++) { m_PROP_PRIORITY[i] = new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_UNSIGNED_INT, (uint)Convert.ToUInt16(m_PROP_PRIORITY[i].Value)); } }
// no test here if buffer is to small private void WriteEffectiveWeeklySchedule() { // Write Default Schedule First try { BacnetValue[] bv = new BacnetValue[1]; bv[0] = Property.DeserializeValue(TxtScheduleDefault.Text, ScheduleType); comm.WritePropertyRequest(adr, schedule_id, BacnetPropertyIds.PROP_SCHEDULE_DEFAULT, bv); } catch { } // Manual ASN.1/BER encoding EncodeBuffer b = comm.GetEncodeBuffer(0); ASN1.encode_opening_tag(b, 3); // Monday // Time // Value // Time // Value // Thusday // .... for (int i = 0; i < 7; i++) { ASN1.encode_opening_tag(b, 0); TreeNode T = Schedule.Nodes[i]; foreach (TreeNode entry in T.Nodes) { String[] s = entry.Text.Split('='); BacnetValue bdt = Property.DeserializeValue(s[0], BacnetApplicationTags.BACNET_APPLICATION_TAG_TIME); BacnetValue bval; if (s[1].ToLower().Contains("null")) { bval = new BacnetValue(null); } else { bval = Property.DeserializeValue(s[1], ScheduleType); } ASN1.bacapp_encode_application_data(b, bdt); ASN1.bacapp_encode_application_data(b, bval); } ASN1.encode_closing_tag(b, 0); } ASN1.encode_closing_tag(b, 3); Array.Resize <byte>(ref b.buffer, b.offset); byte[] InOutBuffer = b.buffer; comm.RawEncodedDecodedPropertyConfirmedRequest(adr, schedule_id, BacnetPropertyIds.PROP_WEEKLY_SCHEDULE, BacnetConfirmedServices.SERVICE_CONFIRMED_WRITE_PROPERTY, ref InOutBuffer); }
public void AnalogObjectEvent() { Last_PRESENT_VALUE = m_PROP_PRESENT_VALUE; for (int i = 0; i < 3; i++) { BacnetGenericTime stamp = new BacnetGenericTime(DateTime.Now, BacnetTimestampTags.TIME_STAMP_DATETIME); m_PROP_EVENT_TIME_STAMPS[i] = new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_TIMESTAMP, stamp); } Enable_Reporting(false); }
public ErrorCodes ReadProperty(BacnetObjectId object_id, BacnetPropertyIds property_id, uint array_index, out IList <BacnetValue> value) { value = new BacnetValue[0]; //wildcard device_id if (object_id.type == BacnetObjectTypes.OBJECT_DEVICE && object_id.instance >= System.IO.BACnet.Serialize.ASN1.BACNET_MAX_INSTANCE) { object_id.instance = DeviceId; } //overrides bool handled; ErrorCodes status; if (ReadOverride != null) { ReadOverride(object_id, property_id, array_index, out value, out status, out handled); if (handled) { return(status); } } //By thamersalek : find Object in storage Object Obj1 = FindObject(object_id); if (Obj1 == null) { return(ErrorCodes.UnKnownObject); } //Object found now find property Property p = FindProperty(object_id, property_id); if (p == null) { return(ErrorCodes.NotExist); } //get value ... check for array index if (array_index == 0) { value = new BacnetValue[] { new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_UNSIGNED_INT, (uint)p.BacnetValue.Count) }; } else if (array_index != System.IO.BACnet.Serialize.ASN1.BACNET_ARRAY_ALL) { value = new BacnetValue[] { p.BacnetValue[(int)array_index - 1] }; } else { value = p.BacnetValue; } return(ErrorCodes.Good); }
public IList <BacnetValue> FindPropValue(String propName) { IList <BacnetValue> propVal = null; // find first the property into the programmed methods // so that if a property exist in a class and programmed in // the heritage, the programmed one is the winner ! MethodInfo m = this.GetType().GetMethod("get2_" + propName); if (m != null) { propVal = (IList <BacnetValue>)m.Invoke(this, null); return(propVal); } // second find the property into the programmed property PropertyInfo p = this.GetType().GetProperty(propName); if (p != null) { object[] o = p.GetCustomAttributes(true); BacnetValue b; if (o.Length == 0) { b = new BacnetValue(p.GetValue(this, null)); return(new BacnetValue[] { b }); } else { object val = p.GetValue(this, null); IList <BacnetValue> ret = null; if (val != null) { try { ret = (IList <BacnetValue>)val; // the value is already IList< > } catch { ret = new BacnetValue[] { new BacnetValue((o[0] as BaCSharpTypeAttribute).BacnetNativeType, val) }; } } else { ret = new BacnetValue[] { new BacnetValue(null) } }; return(ret); } } return(propVal); }
public NotificationClass(int ObjId, String ObjName, String Description) : base(new BacnetObjectId(BacnetObjectTypes.OBJECT_NOTIFICATION_CLASS, (uint)ObjId), ObjName, Description) { for (int i = 0; i < 3; i++) { m_PROP_PRIORITY[i] = new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_UNSIGNED_INT, (uint)127); } m_PROP_ACK_REQUIRED.SetBit((byte)0, false); m_PROP_ACK_REQUIRED.SetBit((byte)1, false); m_PROP_ACK_REQUIRED.SetBit((byte)2, false); }
public override void Post_NewtonSoft_Json_Deserialization(DeviceObject device) { base.Post_NewtonSoft_Json_Deserialization(device); // basic int becom int64 for instance during serialization/deserialization for (int i = 0; i < 16; i++) { if (m_PROP_PRIORITY_ARRAY[i].Tag != BacnetApplicationTags.BACNET_APPLICATION_TAG_NULL) { m_PROP_PRIORITY_ARRAY[i] = new BacnetValue(m_PROP_PRIORITY_ARRAY[i].Tag, Convert.ChangeType(m_PROP_PRIORITY_ARRAY[i].Value, typeof(T))); } } }
private BacnetValue ReadScalarValue(BacnetAddress adr, BacnetObjectId oid, BacnetPropertyIds pid, byte invokeId = 0, uint arrayIndex = uint.MaxValue) { try { BacnetValue NoScalarValue = Bacnet_client.ReadPropertyRequest(adr, oid, pid, arrayIndex); return(NoScalarValue); } catch (Exception ex) { Log("Err:" + ex.Message); } return(new BacnetValue()); }
/// <summary> /// Load XML values into class /// </summary> /// <param name="path">Embedded or external file</param> /// <param name="Deviceid">Optional deviceId other than the one in the Xml file</param> /// <returns></returns> public static DeviceStorage Load(string path, uint?Deviceid = null) { Assembly _assembly; StreamReader _textStreamReader; _assembly = Assembly.GetExecutingAssembly(); try { // check if the xml file is an embedded resource _textStreamReader = new StreamReader(_assembly.GetManifestResourceStream(path)); } catch { // if not check the external file if (!System.IO.File.Exists(path)) { throw new Exception("No AppSettings found"); } _textStreamReader = new StreamReader(path); } System.Xml.Serialization.XmlSerializer s = new System.Xml.Serialization.XmlSerializer(typeof(DeviceStorage)); using (_textStreamReader) { DeviceStorage ret = (DeviceStorage)s.Deserialize(_textStreamReader); //set device_id Object obj = ret.FindObject(BacnetObjectTypes.OBJECT_DEVICE); if (obj != null) { ret.DeviceId = obj.Instance; } // use the deviceId in the Xml file or another one if (Deviceid.HasValue) { ret.DeviceId = Deviceid.Value; if (obj != null) { // change the value obj.Instance = Deviceid.Value; IList <BacnetValue> val = new BacnetValue[1] { new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_OBJECT_ID, "OBJECT_DEVICE:" + Deviceid.Value.ToString()) }; ret.WriteProperty(new BacnetObjectId(BacnetObjectTypes.OBJECT_DEVICE, System.IO.BACnet.Serialize.ASN1.BACNET_MAX_INSTANCE), BacnetPropertyIds.PROP_OBJECT_IDENTIFIER, 1, val, true); } } return(ret); } }
public ErrorCodes ReadProperty(BacnetObjectId objectId, BacnetPropertyIds propertyId, uint arrayIndex, out IList <BacnetValue> value) { value = new BacnetValue[0]; //wildcard device_id if (objectId.type == BacnetObjectTypes.OBJECT_DEVICE && objectId.instance >= Serialize.ASN1.BACNET_MAX_INSTANCE) { objectId.instance = DeviceId; } //overrides if (ReadOverride != null) { ErrorCodes status; bool handled; ReadOverride(objectId, propertyId, arrayIndex, out value, out status, out handled); if (handled) { return(status); } } //find in storage var p = FindProperty(objectId, propertyId); if (p == null) { return(ErrorCodes.NotExist); } //get value ... check for array index if (arrayIndex == 0) { value = new[] { new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_UNSIGNED_INT, (uint)p.BacnetValue.Count) }; } else if (arrayIndex != Serialize.ASN1.BACNET_ARRAY_ALL) { value = new[] { p.BacnetValue[(int)arrayIndex - 1] }; } else { value = p.BacnetValue; } return(ErrorCodes.Good); }
public ErrorCodes ReadPropertyValue(BacnetPropertyReference PropRef, out IList <BacnetValue> propVal) { propVal = null; try { string PropName = PropRef.ToString(); if (PropName[0] != 'P') { PropName = "PROP_" + PropName; // private property, not in the Enum list } propVal = FindPropValue(PropName); if (propVal == null) { return(ErrorCodes.NotExist); } // number of elements required if (PropRef.propertyArrayIndex == 0) { propVal = new BacnetValue[] { new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_UNSIGNED_INT, (uint)propVal.Count) }; return(ErrorCodes.Good); } // only a particular element else if (PropRef.propertyArrayIndex != System.IO.BACnet.Serialize.ASN1.BACNET_ARRAY_ALL) { if ((int)PropRef.propertyArrayIndex <= (uint)propVal.Count) { propVal = new BacnetValue[] { propVal[(int)PropRef.propertyArrayIndex - 1] } } ; else { return(ErrorCodes.IndexNotExist); } } return(ErrorCodes.Good); } catch { return(ErrorCodes.GenericError); } }
public static void SetBacObjectPresentValue(BacnetObjectId id, BacnetValue bv) { // On est sur des valeurs simples, la comparaison est possible ici sans problème if (GetBacObjectPresentValue(id).Value.ToString() == bv.Value.ToString()) { return; } // L'index 0 c'est le nombre de valeurs associées à la propriété // L'index 1 pour la première valeur // L'index System.IO.BACnet.Serialize.ASN1.BACNET_ARRAY_ALL pour tout le tableau IList <BacnetValue> val = new BacnetValue[1] { bv }; m_storage.WriteProperty(id, BacnetPropertyIds.PROP_PRESENT_VALUE, 1, val, true); }
public void WritePropertyValue(BacnetObjectId object_id, BacnetPropertyIds property_id, int value) { IList <BacnetValue> read_values; //get existing type if (ReadProperty(object_id, property_id, System.IO.BACnet.Serialize.ASN1.BACNET_ARRAY_ALL, out read_values) != ErrorCodes.Good) { return; } if (read_values == null || read_values.Count == 0) { return; } //write BacnetValue[] write_values = new BacnetValue[] { new BacnetValue(read_values[0].Tag, Convert.ChangeType(value, read_values[0].Value.GetType())) }; WriteProperty(object_id, property_id, System.IO.BACnet.Serialize.ASN1.BACNET_ARRAY_ALL, write_values); }
/// <summary> /// Set Lighting Level as per trackbar value between 1 to 100 /// </summary> /// <param name="fiDeviceID"></param> private void ChangeLutronLightLevel(Int32 fiDeviceID) { using (var loESDLutronEntities = new ESDLutronEntities()) { var loBACnetDeviceDetail = loESDLutronEntities.BACnetDevices .Where(x => x.device_id == fiDeviceID && x.object_instance == (int?)LutronObjectType.Lighting_Level) .Select(x => x).FirstOrDefault(); BacnetAddress loBacnetAddress; loBacnetAddress = new BacnetAddress(BacnetAddressTypes.IP, loBACnetDeviceDetail.network_id); loBacnetAddress.RoutedSource = new BacnetAddress(BacnetAddressTypes.IP, loBACnetDeviceDetail.routed_source, (ushort)loBACnetDeviceDetail.routed_net); BacnetValue loNewBacnetValue = new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_REAL, Convert.ToSingle(trackBarLightingLevel.Value)); BacnetValue[] loWriteNewBacnetValueValue = { loNewBacnetValue }; moBacnetClient.WritePropertyRequest(loBacnetAddress, new BacnetObjectId(BacnetObjectTypes.OBJECT_ANALOG_VALUE, (uint)loBACnetDeviceDetail.object_instance), BacnetPropertyIds.PROP_PRESENT_VALUE, loWriteNewBacnetValueValue); } }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { _logger.LogInformation("Worker has started."); while (!stoppingToken.IsCancellationRequested) { startActivity(); _logger.LogInformation("Started"); BacnetObjectId OBJECT_ANALOG_VALUE_0 = new BacnetObjectId(BacnetObjectTypes.OBJECT_ANALOG_VALUE, 0); BacnetObjectId OBJECT_ANALOG_INPUT_0 = new BacnetObjectId(BacnetObjectTypes.OBJECT_ANALOG_INPUT, 0); double count = 0; for (; ;) { lock (m_storage) // read and write callback are fired in a separated thread, so multiple access needs protection { // Read the Present Value IList <BacnetValue> valtoread; // index 0 : number of values in the array // index 1 : first value m_storage.ReadProperty(OBJECT_ANALOG_VALUE_0, BacnetPropertyIds.PROP_PRESENT_VALUE, 1, out valtoread); // Get the first ... and here the only element double coef = Convert.ToDouble(valtoread[0].Value); float sin = (float)(coef * Math.Sin(count)); // Write the Present Value IList <BacnetValue> valtowrite = new BacnetValue[1] { new BacnetValue(sin) }; m_storage.WriteProperty(OBJECT_ANALOG_INPUT_0, BacnetPropertyIds.PROP_PRESENT_VALUE, 1, valtowrite, true); } await Task.Delay(1000, stoppingToken); count += 0.1; } //_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); //await Task.Delay(1000, stoppingToken); } }
/*****************************************************************************************************/ static void Main(string[] args) { Trace.Listeners.Add(new ConsoleTraceListener()); try { StartActivity(); Console.WriteLine("Started"); BacnetObjectId OBJECT_ANALOG_VALUE_0 = new BacnetObjectId(BacnetObjectTypes.OBJECT_ANALOG_VALUE, 0); BacnetObjectId OBJECT_ANALOG_INPUT_0 = new BacnetObjectId(BacnetObjectTypes.OBJECT_ANALOG_INPUT, 0); Console.WriteLine(OBJECT_ANALOG_VALUE_0.Type + OBJECT_ANALOG_VALUE_0.Instance); double count = 0; for (; ;) { lock (m_storage) // read and write callback are fired in a separated thread, so multiple access needs protection { // Read the Present Value IList <BacnetValue> valtoread; // index 0 : number of values in the array // index 1 : first value m_storage.ReadProperty(OBJECT_ANALOG_VALUE_0, BacnetPropertyIds.PROP_PRESENT_VALUE, 1, out valtoread); // Get the first ... and here the only element double coef = Convert.ToDouble(valtoread[0].Value); // Write the Present Value float sin = (float)(coef * Math.Sin(count)); IList <BacnetValue> valtowrite = new BacnetValue[1] { new BacnetValue(sin) }; Console.WriteLine(valtowrite.ElementAt(0).Value); m_storage.WriteProperty(OBJECT_ANALOG_INPUT_0, BacnetPropertyIds.PROP_PRESENT_VALUE, 1, valtowrite, true); } Thread.Sleep(1000); count += 0.1; } } catch (Exception e) { Console.WriteLine(e.Message); }; }
/// <summary> /// Temporary method /// </summary> /// <param name="fiDeviceID"></param> /// <param name="foBacnetObjectId"></param> /// <param name="foBacnetPropertyIds"></param> /// <param name="foBacnetValue"></param> /// <returns></returns> static bool WriteScalarValue(int fiDeviceID, BacnetObjectId foBacnetObjectId, BacnetPropertyIds foBacnetPropertyIds, BacnetValue foBacnetValue) { using (var loESDLutronEntities = new ESDLutronEntities()) { var loBACnetDeviceDetail = loESDLutronEntities.BACnetDevices .Where(x => x.device_id == fiDeviceID && x.object_instance == (int?)LutronObjectType.Lighting_State) .Select(x => x).FirstOrDefault(); BacnetAddress adr; //// Looking for the device adr = new BacnetAddress(BacnetAddressTypes.IP, loBACnetDeviceDetail.network_id); if (adr == null) { return(false); // not found } adr.RoutedSource = new BacnetAddress(BacnetAddressTypes.IP, loBACnetDeviceDetail.routed_source, (ushort)loBACnetDeviceDetail.routed_net); //// Property Write IList <BacnetValue> NoScalarValue; moBacnetClient.ReadPropertyRequest(adr, foBacnetObjectId, foBacnetPropertyIds, out NoScalarValue); //var loWirteList = NoScalarValue.ToList(); //loWirteList[0].Value = 5; //NoScalarValue[0].Value = 5; //List<BacnetValue> loWriteValue = new List<BacnetValue>(); //loWriteValue.Add(new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_REAL, 5)); BacnetValue loBacnetNewValue = new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_REAL, Convert.ToSingle(foBacnetValue.Value)); BacnetValue[] loWriteValue = { loBacnetNewValue }; moBacnetClient.WritePropertyRequest(adr, foBacnetObjectId, foBacnetPropertyIds, loWriteValue); return(true); } }
/// <summary> /// Change light level for floor (AV) /// </summary> /// <param name="fiFloorID"></param> private void ChangeLutronLightLevel(Int32 fiFloorID) { using (var loESDLutronEntities = new ESDLutronEntities()) { //var loBACnetDeviceDetail = loESDLutronEntities.BACnetDevices // .Where(x => x.device_id == fiDeviceID // && x.object_type == LutronFloorObjectType.Lighting_Level) // .Select(x => x).FirstOrDefault(); var loBACnetDeviceDetail = (from DM in loESDLutronEntities.BACnetDeviceMappings join D in loESDLutronEntities.BACnetDevices on DM.device_id equals D.device_id where DM.floor_id == fiFloorID && D.object_type.ToUpper() == LutronFloorObjectType.Lighting_Level && D.object_instance != 7 //// 7 is for alarm event select new { network_id = D.network_id, device_id = D.device_id, object_instance = D.object_instance, object_name = D.object_name, routed_source = D.routed_source, routed_net = D.routed_net }).Distinct().ToList(); if (loBACnetDeviceDetail != null && loBACnetDeviceDetail.Count > 0) { foreach (var loData in loBACnetDeviceDetail) { BacnetAddress loBacnetAddress; loBacnetAddress = new BacnetAddress(BacnetAddressTypes.IP, loData.network_id); loBacnetAddress.RoutedSource = new BacnetAddress(BacnetAddressTypes.IP, loData.routed_source, (ushort)loData.routed_net); BacnetValue loNewBacnetValue = new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_REAL, Convert.ToSingle(trbkLightSense.Value)); BacnetValue[] loWriteNewBacnetValueValue = { loNewBacnetValue }; moBacnetClient.WritePropertyRequest(loBacnetAddress, new BacnetObjectId(BacnetObjectTypes.OBJECT_ANALOG_VALUE, (uint)loData.object_instance), BacnetPropertyIds.PROP_PRESENT_VALUE, loWriteNewBacnetValueValue); } } } }
/*****************************************************************************************************/ static bool ReadScalarValue(int device_id, BacnetObjectId BacnetObjet, BacnetPropertyIds Propriete, out BacnetValue Value) { BacnetAddress adr; IList <BacnetValue> NoScalarValue; Value = new BacnetValue(null); // Looking for the device adr = DeviceAddr((uint)device_id); if (adr == null) { return(false); // not found } // Property Read if (bacnet_client.ReadPropertyRequest(adr, BacnetObjet, Propriete, out NoScalarValue) == false) { return(false); } Value = NoScalarValue[0]; return(true); }
public static string SerializeValue(BacnetValue value, BacnetApplicationTags type) { switch (type) { case BacnetApplicationTags.BACNET_APPLICATION_TAG_NULL: return(value.ToString()); // Modif FC case BacnetApplicationTags.BACNET_APPLICATION_TAG_REAL: return(((float)value.Value).ToString(System.Globalization.CultureInfo.InvariantCulture)); case BacnetApplicationTags.BACNET_APPLICATION_TAG_DOUBLE: return(((double)value.Value).ToString(System.Globalization.CultureInfo.InvariantCulture)); case BacnetApplicationTags.BACNET_APPLICATION_TAG_OCTET_STRING: return(Convert.ToBase64String((byte[])value.Value)); case BacnetApplicationTags.BACNET_APPLICATION_TAG_CONTEXT_SPECIFIC_DECODED: { if (value.Value is byte[]) { return(Convert.ToBase64String((byte[])value.Value)); } else { string ret = ""; BacnetValue[] arr = (BacnetValue[])value.Value; foreach (BacnetValue v in arr) { ret += ";" + SerializeValue(v, v.Tag); } return(ret.Length > 0 ? ret.Substring(1) : ""); } } default: return(value.Value.ToString()); } }
// Verify if Time is OK and if Value is in the right format private bool Valid_Entry(string e) { String[] s = e.Split('='); if (s.Length != 2) { return(false); } try { DateTime dt = Convert.ToDateTime("01/01/2001 " + s[0]); if (s[1].ToLower().Contains("null")) { return(true); } BacnetValue bv = Property.DeserializeValue(s[1], ScheduleType); return(true); } catch { return(false); } }
static void client_recievedMessage(object sender, MqttMsgPublishEventArgs e) { bool ret; // Handle message received var message = System.Text.Encoding.Default.GetString(e.Message); System.Console.WriteLine("Message received: " + message); // Write Present_Value property on the object ANALOG_OUTPUT:NAE_Object_Pin provided by the device NAE_Device_Id BacnetValue newValue = new BacnetValue(Convert.ToSingle(message)); // expect it's a float try { ret = WriteScalarValue(NAE_Device_Id, new BacnetObjectId(BacnetObjectTypes.OBJECT_ANALOG_VALUE, NAE_Object_Pin), BacnetPropertyIds.PROP_PRESENT_VALUE, newValue); Console.WriteLine("Write feedback : " + ret.ToString()); } catch (Exception) { Console.WriteLine("Error somewhere !"); } }
public bool WriteScalarValue(int deviceId, BacnetObjectId bacnetObjet, BacnetPropertyIds property, BacnetValue value) { lock (this) { if (!WriteProtectionEnabled || CurrentNumberOfWrites < Settings.Default.MaxNumberOfWritesPer24h) { // Looking for the device var adr = DeviceAddr((uint) deviceId); if (adr == null) return false; // not found Logger.Debug($"WriteScalarValue(): {bacnetObjet.Instance} = {value}"); bool retValue = _bacnetClient.WritePropertyRequest(adr, bacnetObjet, property, new[] {value}); CurrentNumberOfWrites++; ValuesChanged?.Invoke(); return retValue; } Logger.Error( $"WriteScalarValue(): Number of Writes per 24h exceeded. Change the number MaxNumberOfWritesPer24h if requried."); return false; } }
public bool ReadScalarValue(int deviceId, BacnetObjectId bacnetObjet, BacnetPropertyIds property, out BacnetValue value) { lock (this) { IList<BacnetValue> noScalarValue; value = new BacnetValue(null); // Looking for the device var adr = DeviceAddr((uint) deviceId); if (adr == null) return false; // not found // Property Read if (_bacnetClient.ReadPropertyRequest(adr, bacnetObjet, property, out noScalarValue) == false) return false; value = noScalarValue[0]; return true; } }
public bool WriteValue(IItemValue item, object newItemValue) { BacnetValue val = new BacnetValue(item.Tag, item.ConvertValueBack(newItemValue)); return WriteScalarValue(Settings.Default.BacnetMasterId, item.BacnetObjectId, BacnetPropertyIds.PROP_PRESENT_VALUE, val); }
public async void ReadValue() { try { BacnetValue val = new BacnetValue(); await Task.Delay(1); _client.ReadScalarValue(1, new BacnetObjectId((BacnetObjectTypes) Enum.Parse(typeof (BacnetObjectTypes), BacnetType), BacnetAddress), BacnetPropertyIds.PROP_PRESENT_VALUE, out val); Result = val.Value; } catch (Exception ex) { Logger.Error($"ReadAllValues() has thrown an exception: {ex.Message}"); MessageBox.Show(ex.Message, "Error"); } }
/*****************************************************************************************************/ static bool ReadScalarValue(int device_id, BacnetObjectId BacnetObjet, BacnetPropertyIds Propriete, out BacnetValue Value) { BacnetAddress adr; IList<BacnetValue> NoScalarValue; Value = new BacnetValue(null); // Looking for the device adr = DeviceAddr((uint)device_id); if (adr == null) return false; // not found // Property Read if (bacnet_client.ReadPropertyRequest(adr, BacnetObjet, Propriete, out NoScalarValue) == false) return false; Value = NoScalarValue[0]; return true; }
/*****************************************************************************************************/ static bool WriteScalarValue(int device_id, BacnetObjectId BacnetObjet, BacnetPropertyIds Propriete, BacnetValue Value) { BacnetAddress adr; // Looking for the device adr = DeviceAddr((uint)device_id); if (adr == null) return false; // not found // Property Write BacnetValue[] NoScalarValue = { Value }; if (bacnet_client.WritePropertyRequest(adr, BacnetObjet, Propriete, NoScalarValue) == false) return false; return true; }