Exemple #1
0
        private void Build(IModelContext context, ICodeWriter w, IResourceBuilder resBuilder)
        {
            w.Write("//------------------------------------------------------------------------------");
            w.Write("// <auto-generated>");
            w.Write("//     This code was generated by a tool.");
            w.Write("//");
            w.Write("//     Changes to this file may cause incorrect behavior and will be lost if");
            w.Write("//     the code is regenerated.");
            w.Write("// </auto-generated>");
            w.Write("//------------------------------------------------------------------------------");
            w.Write();
            var resourcesName          = Path.GetFileNameWithoutExtension(Path.GetFileName(ResFile));
            Func <string, string>  res = s => string.Format("{0}.{1}", resourcesName, resBuilder.Add(s));
            Func <double?, string> num = d => d.HasValue ? d.Value.ToString(CultureInfo.InvariantCulture) : "null";

            using (w.Block("namespace {0}", NameSpace))
            {
                var ioDevices = context.Get <IoDevice>().OrderBy(d => d.Name).ToArray();

                #region IoDevices

                foreach (var ioDevice in ioDevices)
                {
                    using (w.Block("public sealed partial class {0} : PHmiClient.Tags.IoDeviceBase", ioDevice.Name))
                    {
                        var digitalTags = ioDevice.DigTags.OrderBy(t => t.Name).ToArray();
                        var numericTags = ioDevice.NumTags.OrderBy(t => t.Name).ToArray();
                        using (w.Block("public {0}() : base({1}, \"{0}\")", ioDevice.Name, ioDevice.Id))
                        {
                            foreach (var t in digitalTags)
                            {
                                w.Write("{0} = AddDigitalTag({1}, \"{0}\", () => {2});",
                                        t.Name, t.Id, res(t.Description));
                            }
                            foreach (var t in numericTags)
                            {
                                w.Write("{0} = AddNumericTag({1}, \"{0}\", () => {2}, () => {3}, () => {4}, {5}, {6});",
                                        t.Name, t.Id, res(t.Description), res(t.Format),
                                        res(t.EngUnit), num(t.EngMinDb), num(t.EngMaxDb));
                            }
                        }
                        foreach (var t in digitalTags)
                        {
                            w.Write();
                            w.Write("public PHmiClient.Tags.IDigitalTag {0} {{ get; private set; }}", t.Name);
                        }
                        foreach (var t in numericTags)
                        {
                            w.Write();
                            w.Write("public PHmiClient.Tags.INumericTag {0} {{ get; private set; }}", t.Name);
                        }
                    }
                    w.Write();
                }

                #endregion

                var alarmCategories = context.Get <AlarmCategory>().OrderBy(c => c.Name).ToArray();

                #region AlarmCategories

                foreach (var category in alarmCategories)
                {
                    using (w.Block("public sealed partial class {0} : PHmiClient.Alarms.AlarmCategoryBase", category.Name))
                    {
                        using (w.Block("public {0}() : base({1}, \"{0}\", () => {2})", category.Name, category.Id, res(category.Description)))
                        {
                            foreach (var alarmTag in category.AlarmTags.ToArray())
                            {
                                w.Write("AddAlarmInfo({0}, () => {1}, () => {2});", alarmTag.Id, res(alarmTag.Location), res(alarmTag.Description));
                            }
                        }
                    }
                    w.Write();
                }

                #endregion

                var trendsCategories = context.Get <TrendCategory>().OrderBy(c => c.Name).ToArray();

                #region TrendsCategories

                foreach (var category in trendsCategories)
                {
                    using (w.Block("public sealed partial class {0} : PHmiClient.Trends.TrendsCategoryBase", category.Name))
                    {
                        var trendTags = category.TrendTags.OrderBy(t => t.Name).ToArray();
                        using (w.Block("public {0}() : base({1}, \"{0}\", {2})", category.Name, category.Id, category.PeriodDb))
                        {
                            foreach (var t in trendTags)
                            {
                                w.Write("{0} = AddTrendTag({1}, \"{0}\", () => {2}, () => {3}, () => {4}, {5}, {6});",
                                        t.Name, t.Id, res(t.Description), res(t.NumTag.Format),
                                        res(t.NumTag.EngUnit), num(t.NumTag.EngMinDb), num(t.NumTag.EngMaxDb));
                            }
                        }

                        foreach (var t in trendTags)
                        {
                            w.Write();
                            w.Write("public PHmiClient.Trends.ITrendTag {0} {{ get; private set; }}", t.Name);
                        }
                    }
                    w.Write();
                }

                #endregion

                var logs = context.Get <Log>().OrderBy(l => l.Name).ToArray();

                var settings = context.Get <Settings>().Single();
                using (w.Block("public sealed partial class PHmi : PHmiClient.PHmiSystem.PHmiBase"))
                {
                    using (w.Block("public PHmi() : this(\"{0}\")", settings.Server))
                    {
                    }
                    w.Write();
                    using (w.Block("public PHmi(string server) : this(server, \"{0}\")", settings.Guid))
                    {
                    }
                    w.Write();
                    using (w.Block("public PHmi(string server, string guid) : base(server, guid)"))
                    {
                        foreach (var ioDevice in ioDevices)
                        {
                            w.Write("{0} = AddIoDevice(new {0}());", ioDevice.Name);
                        }
                        foreach (var category in alarmCategories)
                        {
                            w.Write("{0} = AddAlarmCategory(new {0}());", category.Name);
                        }
                        foreach (var category in trendsCategories)
                        {
                            w.Write("{0} = AddTrendsCategory(new {0}());", category.Name);
                        }
                        foreach (var log in logs)
                        {
                            w.Write("{0} = AddLog({1}, \"{0}\");", log.Name, log.Id);
                        }
                    }
                    foreach (var ioDevice in ioDevices)
                    {
                        w.Write();
                        w.Write("public {0} {0} {{ get; private set; }}", ioDevice.Name);
                    }
                    foreach (var category in alarmCategories)
                    {
                        w.Write();
                        w.Write("public {0} {0} {{ get; private set; }}", category.Name);
                    }
                    foreach (var category in trendsCategories)
                    {
                        w.Write();
                        w.Write("public {0} {0} {{ get; private set; }}", category.Name);
                    }
                    foreach (var log in logs)
                    {
                        w.Write();
                        w.Write("public PHmiClient.Logs.LogAbstract {0} {{ get; private set; }}", log.Name);
                    }
                }
            }
        }