Ejemplo n.º 1
0
        /// <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);
            }
        }