/// <summary> /// Called when to start the simulation with a set point. /// </summary> /// <param name="blockAddress">The blockAddress.</param> /// <param name="temperatureSetPoint">The temperature set point.</param> /// <param name="humditySetPoint">The humdity set point.</param> /// <returns></returns> public StatusCode StartWithSetPoint(int blockAddress, double temperatureSetPoint, double humditySetPoint) { lock (m_lock) { BlockConfiguration controller = null; if (!m_blocks.TryGetValue(blockAddress, out controller)) { return(StatusCodes.BadNodeIdUnknown); } foreach (BlockProperty property in controller.Properties) { if (property.Name == "TemperatureSetPoint") { Write(blockAddress, property.Offset, temperatureSetPoint); } else if (property.Name == "HumiditySetPoint") { Write(blockAddress, property.Offset, humditySetPoint); } else if (property.Name == "State") { Write(blockAddress, property.Offset, (int)1); } } return(StatusCodes.Good); } }
/// <summary> /// Writes the tag value. /// </summary> /// <param name="blockAddress">The blockAddress.</param> /// <param name="tag">The tag.</param> /// <param name="value">The value.</param> /// <returns> /// True if the write was successful. /// </returns> public bool Write(int blockAddress, int tag, object value) { lock (m_lock) { if (blockAddress < 0 || tag < 0) { return(false); } if (blockAddress + tag > m_position - sizeof(int)) { return(false); } BlockConfiguration controller = null; if (!m_blocks.TryGetValue(blockAddress, out controller)) { return(false); } foreach (BlockProperty property in controller.Properties) { if (property.Offset == tag) { if (!property.Writeable) { return(false); } if (property.DataType == DataTypeIds.Double) { Write(blockAddress, tag, (double)value); return(true); } if (property.DataType == DataTypeIds.Int32) { Write(blockAddress, tag, (int)value); return(true); } } } return(false); } }
/// <summary> /// Reads the tag value. /// </summary> /// <param name="blockAddress">The blockAddress.</param> /// <param name="tag">The tag.</param> /// <returns>The value. null if no value exists.</returns> public object Read(int blockAddress, int tag) { lock (m_lock) { if (blockAddress < 0 || tag < 0) { return(null); } if (blockAddress + tag > m_position - sizeof(int)) { return(null); } BlockConfiguration controller = null; if (!m_blocks.TryGetValue(blockAddress, out controller)) { return(null); } foreach (BlockProperty property in controller.Properties) { if (property.Offset == tag) { if (property.DataType == DataTypeIds.Double) { return((double)BitConverter.ToSingle(m_registers, blockAddress + tag)); } if (property.DataType == DataTypeIds.Int32) { return(BitConverter.ToInt32(m_registers, blockAddress + tag)); } } } return(null); } }
/// <summary> /// Stops the specified object id. /// </summary> /// <param name="blockAddress">The blockAddress.</param> /// <returns></returns> public StatusCode Stop(int blockAddress) { lock (m_lock) { BlockConfiguration controller = null; if (!m_blocks.TryGetValue(blockAddress, out controller)) { return(StatusCodes.BadNodeIdUnknown); } foreach (BlockProperty property in controller.Properties) { if (property.Name == "State") { Write(blockAddress, property.Offset, (int)0); break; } } return(StatusCodes.Good); } }
/// <summary> /// Loads the configuration for the system. /// </summary> private void Load() { foreach (string resourceName in Assembly.GetExecutingAssembly().GetManifestResourceNames()) { if (resourceName.EndsWith(".SystemConfiguration.xml")) { using (Stream istrm = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) { XmlSerializer serializer = new XmlSerializer(typeof(Configuration)); m_configuration = (Configuration)serializer.Deserialize(istrm); } } } m_configuration = new Configuration(); m_configuration.Controllers = new ControllerConfiguration[1]; m_configuration.Controllers[0] = new ControllerConfiguration(); m_configuration.Controllers[0].Name = "Jemima"; m_configuration.Controllers[0].Type = 1; m_configuration.Controllers[0].Properties = new ControllerProperty[1]; m_configuration.Controllers[0].Properties[0] = new ControllerProperty(); m_configuration.Controllers[0].Properties[0] = new ControllerProperty(); m_configuration.Controllers[0].Properties[0].Name = "Jackey"; m_configuration.Controllers[0].Properties[0].DataType = "i=6"; m_configuration.Controllers[0].Properties[0].Range = null; m_configuration.Controllers[0].Properties[0].Writeable = true; m_configuration.Controllers[0].Properties[0].Value = "300"; ControllerConfiguration controller = m_configuration.Controllers[0]; int blockAddress = m_position; int offset = m_position - blockAddress; BlockConfiguration data = new BlockConfiguration() { Address = blockAddress, Name = controller.Name, Type = controller.Type, Properties = new List <BlockProperty>() }; if (controller.Properties != null) { for (int jj = 0; jj < controller.Properties.Length; jj++) { ControllerProperty property = controller.Properties[jj]; NodeId dataTypeId = NodeId.Parse(property.DataType); string value = property.Value; Range range = null; if (!String.IsNullOrEmpty(property.Range)) { try { NumericRange nr = NumericRange.Parse(property.Range); range = new Range() { High = nr.End, Low = nr.Begin }; } catch (Exception) { range = null; } } data.Properties.Add(new BlockProperty() { Offset = offset, Name = controller.Properties[jj].Name, DataType = dataTypeId, Writeable = controller.Properties[jj].Writeable, Range = range }); switch ((uint)dataTypeId.Identifier) { case DataTypes.Int32: { Write(blockAddress, offset, (int)TypeUtils.Cast(value, BuiltInType.Int32)); offset += 4; break; } case DataTypes.Double: { Write(blockAddress, offset, (double)TypeUtils.Cast(value, BuiltInType.Double)); offset += 4; break; } } m_position += offset; m_blocks[blockAddress] = data; } } }