/// <summary> /// Add a new signal to component. /// Function called by Lua Script with 2 parameters: /// (string Signal name, string Signal type) /// Returns Nothing /// </summary> /// <param name="L">Lua State caller</param> /// <returns>Number of return parameters</returns> static int AddSignal(lua_State L) { //Get component owner lua_getglobal(L, "SmartComponent"); string compID = lua_tostring(L, -1).ToString(); SmartComponent myComponent = GetComponent(compID); if (myComponent != null) { string newSignalName = luaL_checklstring(L, 1).ToString(); string newSignalType = luaL_checklstring(L, 2).ToString(); IOSignalType signalType = IOSignalType.DigitalInput; switch (newSignalType.ToLower()) { case "analoginput": { signalType = IOSignalType.AnalogInput; break; } case "analogoutput": { signalType = IOSignalType.AnalogOutput; break; } case "digitalgroupinput": { signalType = IOSignalType.DigitalGroupInput; break; } case "digitalgroupoutput": { signalType = IOSignalType.DigitalGroupOutput; break; } case "digitalinput": { signalType = IOSignalType.DigitalInput; break; } case "digitaloutput": { signalType = IOSignalType.DigitalOutput; break; } } if (!myComponent.IOSignals.Contains(newSignalName)) { IOSignal ios = new IOSignal(newSignalName, signalType) { ReadOnly = true, }; myComponent.IOSignals.Add(ios); Logger.AddMessage("RSLuaScript: Lua Script adding Signal " + newSignalName + " to " + myComponent.Name, LogMessageSeverity.Information); } else { Logger.AddMessage("RSLuaScript: Lua Script want to add already existing Signal " + newSignalName + " to " + myComponent.Name + ". Check your lua script file.", LogMessageSeverity.Information); } myComponent.Properties["Status"].Value = "Signal Added"; } else { Logger.AddMessage("RSLuaScript: Lua Script adding Signal of unknown component. Closing it.", LogMessageSeverity.Error); lua_close(L); } return(0); // number of return parameters }
/// <summary> /// Update DIO list depends DIO_Number /// </summary> /// <param name="component">Component that owns signals. </param> /// <param name="oldCount">Old DIO count</param> private void UpdateIOCount(SmartComponent component, int oldCount, IO io) { String prefix = ""; IOSignalType iOSignalType = IOSignalType.DigitalInput; if (io == IO.Input) { prefix = "DI"; } else { prefix = "DO"; iOSignalType = IOSignalType.DigitalOutput; } int newIOCount = (int)component.Properties[prefix + "_Number"].Value; if (newIOCount > oldCount) { Array.Resize(ref bDIO_AddressIsValid[(int)io], newIOCount); for (int i = oldCount; i < newIOCount; i++) { string dioName = prefix + "_" + i.ToString(); if (!component.IOSignals.Contains(dioName)) { IOSignal ios = new IOSignal(dioName, iOSignalType) { ReadOnly = true, UIVisible = false }; component.IOSignals.Add(ios); } string dioAddress = prefix + "_Address_" + i.ToString(); if (!component.Properties.Contains(dioAddress)) { DynamicProperty idp = new DynamicProperty(dioAddress, "System.String") { Value = "M0.0", ReadOnly = false, UIVisible = true }; idp.Attributes["AutoApply"] = "true"; idp.Attributes["CustomValidation"] = "true"; component.Properties.Add(idp); bDIO_AddressIsValid[(int)io][i] = false; } } } else { for (int i = oldCount - 1; i >= newIOCount; i--) { string dioName = prefix + "_" + i.ToString(); if (component.IOSignals.Contains(dioName)) { component.IOSignals.Remove(dioName); } string dioAddress = prefix + "_Address_" + i.ToString(); if (component.Properties.Contains(dioAddress)) { component.Properties.Remove(dioAddress); } } Array.Resize(ref bDIO_AddressIsValid[(int)io], newIOCount); } }