/// <summary> /// Sets the configuration according to the controls. /// </summary> private void ControlsToConfig() { // active extensions config.ExtensionCodes.Clear(); foreach (ExtentionItem item in lbActiveExt.Items) { config.ExtensionCodes.Add(item.ExtentionCode); } // file associations config.FileAssociations.Clear(); foreach (ListViewItem item in lvAssoc.Items) { config.FileAssociations[item.SubItems[0].Text] = item.SubItems[1].Text; } // channel numbering ChannelNumberingOptions options = config.ChannelNumberingOptions; options.Multiplicity = Convert.ToInt32(numMultiplicity.Value); options.Shift = Convert.ToInt32(numShift.Value); options.Gap = Convert.ToInt32(numGap.Value); options.PrependDeviceName = chkPrependDeviceName.Checked; }
/// <summary> /// Calculates a start channel number. /// </summary> private bool CalcStartCnlNum(int cnlCnt, out int startCnlNum) { ChannelNumberingOptions options = adminContext.AppConfig.ChannelNumberingOptions; startCnlNum = options.Multiplicity + options.Shift; int prevCnlNum = 0; foreach (int cnlNum in project.ConfigDatabase.CnlTable.EnumerateKeys()) { if (prevCnlNum < startCnlNum && startCnlNum <= cnlNum) { if (startCnlNum + cnlCnt + options.Gap <= cnlNum) { return(true); } else { startCnlNum += options.Multiplicity; } } prevCnlNum = cnlNum; } return(startCnlNum <= ushort.MaxValue); }
/// <summary> /// Sets the controls according to the configuration. /// </summary> private void ConfigToControls() { // active extensions try { lbActiveExt.BeginUpdate(); lbActiveExt.Items.Clear(); foreach (string extentionCode in config.ExtensionCodes) { lbActiveExt.Items.Add(new ExtentionItem { IsInitialized = false, ExtentionCode = extentionCode, FileName = Path.Combine(appData.AppDirs.LibDir, extentionCode + ".dll") }); } if (lbActiveExt.Items.Count > 0) { lbActiveExt.SelectedIndex = 0; } } finally { lbActiveExt.EndUpdate(); } // file associations try { lvAssoc.BeginUpdate(); lvAssoc.Items.Clear(); foreach (KeyValuePair <string, string> pair in config.FileAssociations) { lvAssoc.Items.Add(CreateAssociationItem(pair.Key, pair.Value)); } if (lvAssoc.Items.Count > 0) { lvAssoc.Items[0].Selected = true; } } finally { lvAssoc.EndUpdate(); } // channel numbering ChannelNumberingOptions options = config.ChannelNumberingOptions; numMultiplicity.SetValue(options.Multiplicity); numShift.SetValue(options.Shift); numGap.SetValue(options.Gap); chkPrependDeviceName.Checked = options.PrependDeviceName; }
/// <summary> /// Builds the channels. /// </summary> private void BuildCnls(DeviceConfigEntry entry, ControlModel controlModel, ChannelNumberingOptions options, int?objNum, int deviceNum, ref int cnlNum) { string namePrefix = options.PrependDeviceName ? entry.DeviceEntity.Name + " - " : ""; ControlMeta controlMeta = controlModel.Meta; int? formatID = GetFormatID(controlMeta); int? quantityID = GetQuantityID(controlMeta); int? unitID = GetUnitID(controlMeta); // control value entry.Cnls.Add(new Cnl { CnlNum = cnlNum++, Active = true, Name = namePrefix + controlMeta.Name + " Last", CnlTypeID = CnlTypeID.Input, ObjNum = objNum, DeviceNum = deviceNum, TagCode = controlModel.Code, FormatID = formatID, QuantityID = quantityID, UnitID = unitID }); // control error entry.Cnls.Add(new Cnl { CnlNum = cnlNum++, Active = true, Name = namePrefix + controlMeta.Name + " Error", CnlTypeID = CnlTypeID.Input, ObjNum = objNum, DeviceNum = deviceNum, TagCode = controlModel.Code + "_error" }); // calculated channel entry.Cnls.Add(new Cnl { CnlNum = cnlNum++, Active = true, Name = namePrefix + controlMeta.Name, CnlTypeID = controlMeta.ReadOnly ? CnlTypeID.Calculated : CnlTypeID.CalculatedOutput, ObjNum = objNum, DeviceNum = deviceNum, TagCode = controlModel.Code, FormulaEnabled = true, InFormula = "WB_CalcControl()", FormatID = formatID, QuantityID = quantityID, UnitID = unitID, EventMask = ControlEventMask }); }
/// <summary> /// Gets a starting channel number for a next device. /// </summary> public static int AdjustCnlNum(ChannelNumberingOptions options, int nextCnlNum) { ArgumentNullException.ThrowIfNull(options, nameof(options)); int cnlNum = options.Gap > 0 ? nextCnlNum + options.Gap : nextCnlNum; if (options.Multiplicity > 1) { int remainder = cnlNum % options.Multiplicity; if (remainder > 0) { cnlNum = cnlNum - remainder + options.Multiplicity; } return(cnlNum + options.Shift); } else { return(cnlNum); } }
/// <summary> /// Builds a project configuration. /// </summary> public void Build(List <DeviceModel> selectedDevices, int commLineNum, int startDeviceNum, int startCnlNum, int?objNum) { ArgumentNullException.ThrowIfNull(selectedDevices, nameof(selectedDevices)); try { ChannelNumberingOptions options = adminContext.AppConfig.ChannelNumberingOptions; int deviceNum = startDeviceNum; int cnlNum = startCnlNum; int?deviceTypeID = project.ConfigDatabase.DevTypeTable.Enumerate() .Where(devType => devType.Driver == MqttDriverName).FirstOrDefault()?.DevTypeID; if (deviceTypeID == null) { logHelper.WriteError(string.Format(Locale.IsRussian ? "Драйвер {0} не найден в таблице Типы устройств" : "Driver {0} not found in the Device Types table", MqttDriverName)); return; } foreach (DeviceModel deviceModel in selectedDevices) { bool deviceIgnored = true; if (deviceModel.Controls.Count > 0) { DeviceConfigEntry entry = new(); // device entity Device deviceEntity = entry.DeviceEntity; deviceEntity.DeviceNum = deviceNum; deviceEntity.Name = deviceModel.Meta.Name; deviceEntity.Code = deviceModel.Code; deviceEntity.DevTypeID = deviceTypeID; deviceEntity.CommLineNum = commLineNum; // device configuration DeviceConfig deviceConfig = entry.DeviceConfig; deviceConfig.DeviceNum = deviceNum; deviceConfig.Name = deviceModel.Meta.Name; deviceConfig.Driver = MqttDriverName; // channels and subscriptions BuildCnls(entry, deviceModel, objNum, deviceNum, ref cnlNum); BuildMqttDeviceConfig(entry, deviceModel); foreach (ControlModel controlModel in deviceModel.Controls) { BuildCnls(entry, controlModel, options, objNum, deviceNum, ref cnlNum); BuildMqttDeviceConfig(entry, controlModel); } if (entry.Cnls.Count > 0) { logHelper.WriteMessage(string.Format(Locale.IsRussian ? "Устройство [{0}] {1}" : "Device [{0}] {1}", deviceNum, deviceModel.Meta.Name)); logHelper.WriteMessage(string.Format(Locale.IsRussian ? "Каналы: {0}" : "Channels: {0}", entry.Cnls.Select(cnl => cnl.CnlNum).ToArray().ToRangeString())); DeviceConfigs.Add(entry); deviceIgnored = false; } deviceNum++; } if (deviceIgnored) { logHelper.WriteWarning(string.Format(Locale.IsRussian ? "Устройство {0} игнорируется" : "Device {0} ignored", deviceModel.Meta.Name)); } logHelper.WriteLine(); cnlNum = AdjustCnlNum(options, cnlNum); } BuildResult = DeviceConfigs.Count > 0; } catch (Exception ex) { logHelper.WriteError(ex.Message); } }