Example #1
0
        /// <summary>
        /// Выполнить действия после добавления КП на линию связи
        /// </summary>
        public override void OnAddedToCommLine()
        {
            // загрузка шаблона устройства
            deviceModel  = null;
            elemGroupCnt = 0;
            floatSignals = new HashSet <int>();
            string fileName = ReqParams.CmdLine.Trim();

            if (fileName == "")
            {
                WriteToLog(string.Format(Localization.UseRussian ?
                                         "{0} Ошибка: Не задан шаблон устройства для {1}" :
                                         "{0} Error: Template is undefined for the {1}", CommUtils.GetNowDT(), Caption));
            }
            else
            {
                Dictionary <string, Modbus.DeviceModel> templates = GetTemplates();
                if (templates.ContainsKey(fileName))
                {
                    // создание модели устройства на основе модели, загруженной ранее
                    Modbus.DeviceModel template = templates[fileName];
                    if (template != null)
                    {
                        deviceModel = new Modbus.DeviceModel();
                        deviceModel.CopyFrom(template);
                        elemGroupCnt = deviceModel.ElemGroups.Count;
                    }
                }
                else
                {
                    WriteToLog(string.Format(Localization.UseRussian ?
                                             "{0} Загрузка шаблона устройства из файла {1}" :
                                             "{0} Load device template from file {1}", CommUtils.GetNowDT(), fileName));
                    Modbus.DeviceModel template = new Modbus.DeviceModel();
                    string             errMsg;

                    if (template.LoadTemplate(AppDirs.ConfigDir + fileName, out errMsg))
                    {
                        deviceModel  = template;
                        elemGroupCnt = deviceModel.ElemGroups.Count;
                        templates.Add(fileName, template);
                    }
                    else
                    {
                        WriteToLog(errMsg);
                        templates.Add(fileName, null);
                    }
                }
            }

            // инициализация тегов КП на основе модели устройства
            if (elemGroupCnt > 0)
            {
                List <TagGroup> tagGroups = new List <TagGroup>();
                int             tagInd    = 0;

                foreach (Modbus.ElemGroup elemGroup in deviceModel.ElemGroups)
                {
                    TagGroup tagGroup = new TagGroup(elemGroup.Name);
                    tagGroups.Add(tagGroup);
                    elemGroup.StartKPTagInd = tagInd;

                    foreach (Modbus.Elem elem in elemGroup.Elems)
                    {
                        int signal = ++tagInd;
                        tagGroup.KPTags.Add(new KPTag(signal, elem.Name));
                        if (elem.ElemType == Modbus.ElemTypes.Float)
                        {
                            floatSignals.Add(signal);
                        }
                    }
                }

                InitKPTags(tagGroups);
                CanSendCmd = deviceModel.Cmds.Count > 0;
            }
        }
Example #2
0
        /// <summary>
        /// Выполнить действия после добавления КП на линию связи
        /// </summary>
        public override void OnAddedToCommLine()
        {
            // загрузка шаблона устройства
            deviceModel  = null;
            elemGroupCnt = 0;
            floatSignals = new HashSet <int>();
            string fileName = KPReqParams.CmdLine.Trim();

            if (fileName == "")
            {
                WriteToLog((Localization.UseRussian ? "Не задан шаблон устройства для " :
                            "Template is undefined for the ") + Caption);
            }
            else
            {
                Dictionary <string, Modbus.DeviceModel> templates = GetTemplates();
                if (templates.ContainsKey(fileName))
                {
                    // создание модели устройства на основе модели, загруженной ранее
                    Modbus.DeviceModel template = templates[fileName];
                    if (template != null)
                    {
                        deviceModel = new Modbus.DeviceModel();
                        deviceModel.CopyFrom(template);
                        elemGroupCnt = deviceModel.ElemGroups.Count;
                    }
                }
                else
                {
                    WriteToLog((Localization.UseRussian ? "Загрузка шаблона устройства из файла " :
                                "Load device template from file ") + fileName);
                    Modbus.DeviceModel template = new Modbus.DeviceModel();
                    string             errMsg;

                    if (template.LoadTemplate(ConfigDir + fileName, out errMsg))
                    {
                        deviceModel  = template;
                        elemGroupCnt = deviceModel.ElemGroups.Count;
                        templates.Add(fileName, template);
                    }
                    else
                    {
                        WriteToLog(errMsg);
                        templates.Add(fileName, null);
                    }
                }
            }

            // инициализация параметров КП на основе модели устройства
            if (elemGroupCnt > 0)
            {
                ParamGroup[] paramGroups   = new ParamGroup[elemGroupCnt];
                int          paramInd      = 0;
                int          paramGroupInd = 0;

                foreach (Modbus.ElemGroup elemGroup in deviceModel.ElemGroups)
                {
                    int        elemCnt    = elemGroup.Elems.Count;
                    ParamGroup paramGroup = new ParamGroup(elemGroup.Name, elemCnt);
                    paramGroups[paramGroupInd++] = paramGroup;
                    elemGroup.StartParamInd      = paramInd;

                    for (int i = 0; i < elemCnt; i++)
                    {
                        int         signal = ++paramInd;
                        Modbus.Elem elem   = elemGroup.Elems[i];

                        Param param = new Param(signal, elem.Name);
                        paramGroup.KPParams[i] = param;

                        if (elem.ElemType == Modbus.ElemTypes.Float)
                        {
                            floatSignals.Add(signal);
                        }
                    }
                }

                InitArrays(paramInd, elemGroupCnt);
                for (int i = 0; i < elemGroupCnt; i++)
                {
                    ParamGroups[i] = paramGroups[i];
                }
                CopyParamsFromGroups();
                CanSendCmd = deviceModel.Cmds.Count > 0;
            }
        }