コード例 #1
0
        protected void GenerateApplicationIds(ModelContainer mc)
        {
            StringBuilder sb = new StringBuilder();

            //mc.Model.Nodes.ForEach(n => page.Nodes.Add(n.Id));
            //page.Nodes.Add("CNT");

            foreach (var id in Enum.GetValues <Nodes.ApplicationId>())
            {
                sb.AppendLine(id.ToString() + "=" + (ushort)id + ",");
            }
            sb.AppendLine("CNT,");
            this.WriteCommonFile("applicationIds", sb);
            LOG.LogInformation("Successfully created appids");
            return;
        }
コード例 #2
0
 private void GenerateJSONDescription(ModelContainer mc)
 {
     foreach (Node node in mc.Model.Nodes)
     {
         List <object> list = new List <object>();
         foreach (var app in node.Applications)
         {
             app.AddJSONDescriptionToList(list);
         }
         string directory = Path.Combine(this.options.BasePath, node.NodeName);
         Directory.CreateDirectory(directory);
         string path = Path.Combine(directory, "app_descriptor.json");
         File.WriteAllText(path, JsonSerializer.Serialize <object>(list));
     }
     LOG.LogInformation("Successfully created all node specific .json files");
 }
コード例 #3
0
ファイル: Program.cs プロジェクト: klaus-liebler/sensact
        static void Main(string[] args)
        {
            var logger = LogManager.GetCurrentClassLogger();

            try
            {
                var config = new ConfigurationBuilder()
                             .SetBasePath(System.IO.Directory.GetCurrentDirectory()) //From NuGet Package Microsoft.Extensions.Configuration.Json
                             .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                             .Build();


                var servicesProvider = BuildServiceProvider(config);
                using (servicesProvider as IDisposable)
                {
                    var generator = servicesProvider.GetRequiredService <SourceCodeGenerator>();
                    var model     = servicesProvider.GetRequiredService <Model>();

                    ModelContainer mc = new ModelContainer()
                    {
                        Model = model
                    };
                    if (!generator.CheckAndPrepare(mc))
                    {
                        Console.WriteLine("See errors and then press any key to exit");
                        Console.ReadKey();
                        return;
                    }
                    generator.DeleteBaseDirectory();
                    generator.generateAll(mc);
                    Console.WriteLine("Press ANY key to exit");
                    Console.ReadKey();
                }
            }
            catch (Exception ex)
            {
                // NLog: catch any exception and log it.
                logger.Error(ex, "Stopped program because of exception");
                throw;
            }
            finally
            {
                // Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)
                LogManager.Shutdown();
            }
        }
コード例 #4
0
        internal static void GenerateAppIds_h(ModelContainer mc)
        {
            HC_APPIDS_H page = new HC_APPIDS_H()
            {
                version = "1.0"
            };

            mc.Model.Nodes.ForEach(n => page.Nodes.Add(n.Id));
            page.Nodes.Add("CNT");

            foreach (var kv in mc.predefinedIndices)
            {
                page.AppIds.Add(kv.Key + "=" + kv.Value);
            }
            page.AppIds.Add("CNT");
            String pageContent = page.TransformText();

            File.WriteAllText(GetGeneratedPathForFile("appids.h"), pageContent);
            LOG.InfoFormat("Successfully created appids.h");
            return;
        }
コード例 #5
0
        internal static void GenerateApplication_H_and_CPP(ModelContainer model)
        {
            APPLICATION_H h = new APPLICATION_H()
            {
                version        = "1.0",
                CommandHeaders = HeaderForCommands(),
            };
            APPLICATION_CPP cpp = new APPLICATION_CPP()
            {
                version = "1.0",
                CommandParseImplementation        = ImplementationForCmdParse(),
                CommandEmptyHandlerImplementation = EmptyImplementationForCmdHandler(),
                CommandCreateImplementation       = ImplementationForCmdSend(),
            };

            string pageContent = h.TransformText();

            File.WriteAllText(GetGeneratedPathForFile("cApplication.h"), pageContent);
            LOG.InfoFormat("Successfully created cApplication.h");
            pageContent = cpp.TransformText();
            File.WriteAllText(GetGeneratedPathForFile("cApplication.cpp"), pageContent);
            LOG.InfoFormat("Successfully created cApplication.cpp");
            return;
        }
コード例 #6
0
 public abstract string GenerateInitializer(ModelContainer m);
コード例 #7
0
        protected string EventInitializer(string prefix, ICollection <EventType> evts, ModelContainer m)
        {
            //eEventType PUSHB_EG_WOZ_G0S0_BusEvent[2] = {eEventType::PRESSED, eEventType::RELEASED, eEventType::END_OF_EVENTS};
            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("eEventType {0}{1}_{2}Events", (evts == null || evts.Count == 0) ? "*" : "", ApplicationId, prefix);
            if (evts != null && evts.Count > 0)
            {
                sb.Append("[" + evts.Count + "]={");
                bool first = true;
                foreach (EventType evt in evts)
                {
                    if (!first)
                    {
                        sb.AppendLine("," + Environment.NewLine);
                    }
                    sb.AppendFormat("fEvent::{0}", evt);
                    first = false;
                }
                sb.Append("}");
            }
            else
            {
                sb.Append("=0");
            }
            sb.AppendLine(";");
            return(sb.ToString());
        }
コード例 #8
0
        protected string CommandInitializer(string collectionName, ICollection <Command> cmds, ModelContainer m)
        {
            StringBuilder payloads = new StringBuilder();
            StringBuilder sb       = new StringBuilder();

            sb.AppendFormat("const Command {0}{1}_{2}", (cmds == null || cmds.Count == 0) ? "*const " : "", ApplicationId, collectionName);
            if (cmds != null && cmds.Count > 0)
            {
                sb.Append("[" + cmds.Count + "]={");
                int cmdIndex = 0;
                foreach (Command cmd in cmds)
                {
                    sb.Append("{eApplicationID::" + cmd.TargetAppId + ", eCommandType::" + cmd.CommandType + ", ");
                    if (cmd.Payload != null && cmd.Payload.Length > 0)
                    {
                        string nameOfArray = string.Format("{0}_{1}_{2}", ApplicationId, collectionName, cmdIndex);
                        payloads.Append(BuildUint8Array(nameOfArray, cmd.Payload));
                        sb.Append(nameOfArray + ", " + cmd.Payload.Length + "}, ");
                    }
                    else
                    {
                        sb.Append("0, 0, },");
                    }
                    cmdIndex++;
                }

                sb.Append("}");
            }
            else
            {
                sb.Append("=0");
            }
            sb.AppendLine(";");

            payloads.Append(sb);
            return(payloads.ToString());
        }
コード例 #9
0
        public bool CheckAndPrepare(ModelContainer mc)
        {
            HashSet <ushort> predefinedAppIds = Enum.GetValues <Nodes.ApplicationId>().Select(x => (ushort)x).ToHashSet();

            mc.NextFreeIndex = predefinedAppIds.Where(x => x != (ushort)Nodes.ApplicationId.NO_APPLICATION).Max();
            mc.NextFreeIndex++;
            SensactApplicationContainer masterApp = new SensactApplicationContainer(null, new Applications.MasterApplication());

            mc.id2app[masterApp.Application.ApplicationId] = masterApp;

            foreach (Node n in mc.Model.Nodes)
            {
                HashSet <string> usedInputPins  = new HashSet <string>();
                HashSet <string> usedOutputPins = new HashSet <string>();

                foreach (SensactApplication app in n.Applications)
                {
                    if (app.ApplicationName == null)
                    {
                        LOG.LogError("An App in Node {0} has no name", n.NodeName);
                        return(false);
                    }
                    if (app.ApplicationId == 0 && app.ApplicationName != null)
                    {
                        app.SetApplicationId_BeCareful(mc.NextFreeIndex++);
                    }

                    if (mc.id2app.ContainsKey(app.ApplicationId))
                    {
                        LOG.LogError("AppId {0} is defined at least two times in node applications", app.ApplicationId);
                        return(false);
                    }
                    if (!app.HasValidAppId())
                    {
                        LOG.LogWarning("AppId {0} with name {1} in node {2} does not fulfill the regex pattern {3} for application name. This is ok, but maybe it is an error...", app.ApplicationId, app.ApplicationName, n.NodeName, app.AppNameRegex);
                        app.HasValidAppId();
                    }
                    string errorMessage = app.CheckAndAddUsedPins(usedInputPins, usedOutputPins);
                    if (errorMessage != null)
                    {
                        LOG.LogError("AppId {0} uses pins that have been reserved by another app:\n{1}", app.ApplicationId, errorMessage);
                        return(false);
                    }
                    SensactApplicationContainer cont = new SensactApplicationContainer(n, app);
                    mc.id2app[cont.Application.ApplicationId] = cont;
                }

                //Sanity: Check, whether node application has already been added
                if (mc.id2app.ContainsKey(n.NodeId))
                {
                    //Es wurde eine Application mit der ID der Node angelegt
                    if (!mc.id2app[n.NodeId].Application.ApplicationName.Equals(n.NodeName))
                    {
                        //mit dem Namen gibt es ein Problem
                        LOG.LogError("There is a predefined node app for node {0}, but its name is not correct:", n.NodeName);
                        return(false);
                    }
                }
                else
                {
                    SensactApplicationContainer nodeAppContainer = new SensactApplicationContainer(n, new SensactNodeApplication(n.NodeId, n.NodeName));
                    n.Applications.Add(nodeAppContainer.Application);
                    mc.id2app.Add(nodeAppContainer.Application.ApplicationId, nodeAppContainer);
                }
            }

            //Find out which events should be fired by each application because there is a listener for the event
            //distinguish between local events and bus events
            foreach (Node n in mc.Model.Nodes)
            {
                foreach (SensactApplication app in n.Applications)
                {
                    HashSet <Event>             evts    = app.IReactOnTheseEvents();
                    SensactApplicationContainer appCont = mc.id2app[app.ApplicationId];
                    foreach (Event evt in evts)
                    {
                        //Kann die SourceApp dieses Event überhaupt erzeugen?
                        SensactApplicationContainer source;
                        if (!mc.id2app.TryGetValue(evt.SourceAppId, out source))
                        {
                            LOG.LogError("AppId {0} listens to event from AppId {1}, but this app does not exist.", app.ApplicationId, evt.SourceAppId);
                            return(false);
                        }
                        else
                        {
                            if (!source.Application.ICanSendTheseEvents().Contains(evt.EventType))
                            {
                                LOG.LogError("AppId {0} listens to event {1} from AppId {2}, but this app cannot produce such events.", app.ApplicationId, evt.EventType, evt.SourceAppId);
                                return(false);
                            }
                        }
                        if (appCont.Node == source.Node)
                        {
                            //source und destination leben in der selben node
                            HashSet <EventType> set = null;
                            if (!mc.id2localEvents.TryGetValue(evt.SourceAppId, out set))
                            {
                                set = new HashSet <EventType>();
                                mc.id2localEvents[evt.SourceAppId] = set;
                            }
                            set.Add(evt.EventType);
                        }
                        else
                        {
                            HashSet <EventType> set = null;
                            if (!mc.id2busEvents.TryGetValue(evt.SourceAppId, out set))
                            {
                                set = new HashSet <EventType>();
                                mc.id2busEvents[evt.SourceAppId] = set;
                            }
                            set.Add(evt.EventType);
                        }
                    }
                    HashSet <Command> cmds = app.ISendTheseCommands();
                    foreach (Command cmd in cmds)
                    {
                        SensactApplicationContainer target;
                        if (!mc.id2app.TryGetValue(cmd.TargetAppId, out target))
                        {
                            if (cmd.TargetAppId != (ushort)Nodes.ApplicationId.NO_APPLICATION)
                            {
                                LOG.LogError("AppId {0} sends command to AppId {1}, but this app does not exist.", app.ApplicationId, cmd.TargetAppId);
                                return(false);
                            }
                        }
                        else
                        {
                            if (!target.Application.ICanReactOnTheseCommands().Contains(cmd.CommandType))
                            {
                                LOG.LogError("AppId {0} sends command {1} to AppId {2}, but this app cannot react on this command.", app.ApplicationId, cmd.CommandType, cmd.TargetAppId);
                                return(false);
                            }
                        }
                    }
                }
            }

            return(true);
        }
コード例 #10
0
        public static bool CheckAndPrepare(ModelContainer mc)
        {
            //preFill predefined indices
            mc.PrefillPredefinedIndices(Enum.GetValues(typeof(ID)));

            HashSet <string>            alreadyDefinedAppIds = new HashSet <string>();
            SensactApplicationContainer masterApp            = new SensactApplicationContainer()
            {
                Application = new Applications.MasterApplication(), Index = mc.GetIndex("MASTER"), Node = null
            };

            mc.id2app[masterApp.Application.ApplicationId] = masterApp;
            mc.index2app[0] = masterApp;
            foreach (Node n in mc.Model.Nodes)
            {
                n.Applications.Add(new SensactNodeApplication
                {
                    ApplicationId = n.Id,
                });
                HashSet <string> usedInputPins  = new HashSet <string>();
                HashSet <string> usedOutputPins = new HashSet <string>();
                foreach (SensactApplication app in n.Applications)
                {
                    if (alreadyDefinedAppIds.Contains(app.ApplicationId))
                    {
                        LOG.ErrorFormat("AppId {0} is defined at least two times in node applications", app.ApplicationId);
                        return(false);
                    }
                    if (!app.HasValidAppId())
                    {
                        LOG.WarnFormat("AppId {0} does not fulfill the recommendations for application name. This is ok, but maybe it is an error...", app.ApplicationId, n.Id);
                        app.HasValidAppId();
                    }
                    string errorMessage = app.CheckAndAddUsedPins(usedInputPins, usedOutputPins);
                    if (errorMessage != null)
                    {
                        LOG.ErrorFormat("AppId {0} uses pins that have been reserved by another app:\n{1}", app.ApplicationId, errorMessage);
                        return(false);
                    }
                    alreadyDefinedAppIds.Add(app.ApplicationId);
                    SensactApplicationContainer cont = new SensactApplicationContainer
                    {
                        Application = app,
                        Index       = mc.GetIndex(app.ApplicationId),
                        Node        = n,
                    };

                    mc.id2app[cont.Application.ApplicationId] = cont;
                    mc.index2app[cont.Index] = cont;
                }
            }

            //Find out which events should be fired by each application because there is a listener for the event
            //distinguish between local events and bus events
            foreach (Node n in mc.Model.Nodes)
            {
                foreach (SensactApplication app in n.Applications)
                {
                    HashSet <Event>             evts    = app.IReactOnTheseEvents();
                    SensactApplicationContainer appCont = mc.id2app[app.ApplicationId];
                    foreach (Event evt in evts)
                    {
                        //Kann die SourceApp dieses Event überhaupt erzeugen?
                        SensactApplicationContainer source;
                        if (!mc.id2app.TryGetValue(evt.SourceAppId, out source))
                        {
                            LOG.ErrorFormat("AppId {0} listens to event from AppId {1}, but this app does not exist.", app.ApplicationId, evt.SourceAppId);
                            return(false);
                        }
                        else
                        {
                            if (!source.Application.ICanSendTheseEvents().Contains(evt.EventType))
                            {
                                LOG.ErrorFormat("AppId {0} listens to event {1} from AppId {2}, but this app cannot produce such events.", app.ApplicationId, evt.EventType, evt.SourceAppId);
                                return(false);
                            }
                        }
                        if (appCont.Node == source.Node)
                        {
                            //source und destination leben in der selben node
                            HashSet <EventType> set = null;
                            if (!mc.id2localEvents.TryGetValue(evt.SourceAppId, out set))
                            {
                                set = new HashSet <EventType>();
                                mc.id2localEvents[evt.SourceAppId] = set;
                            }
                            set.Add(evt.EventType);
                        }
                        else
                        {
                            HashSet <EventType> set = null;
                            if (!mc.id2busEvents.TryGetValue(evt.SourceAppId, out set))
                            {
                                set = new HashSet <EventType>();
                                mc.id2busEvents[evt.SourceAppId] = set;
                            }
                            set.Add(evt.EventType);
                        }
                    }
                    HashSet <Command> cmds = app.ISendTheseCommands();
                    foreach (Command cmd in cmds)
                    {
                        SensactApplicationContainer target;
                        if (!mc.id2app.TryGetValue(cmd.TargetAppId, out target))
                        {
                            if (cmd.TargetAppId != ID.NO_APPLICATION.ToString())
                            {
                                LOG.ErrorFormat("AppId {0} sends command to AppId {1}, but this app does not exist.", app.ApplicationId, cmd.TargetAppId);
                                return(false);
                            }
                        }
                        else
                        {
                            if (!target.Application.ICanReactOnTheseCommands().Contains(cmd.CommandType))
                            {
                                LOG.ErrorFormat("AppId {0} sends command {1} to AppId {2}, but this app cannot react on this command.", app.ApplicationId, cmd.CommandType, cmd.TargetAppId);
                                return(false);
                            }
                        }
                    }
                }
            }

            return(true);
        }
コード例 #11
0
        protected string ResourcesInitializer(string collectionName, ICollection <ushort> cmds, ModelContainer m)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("uint16_t {0}{1}_{2}", (cmds == null || cmds.Count == 0) ? "*" : "", ApplicationId, collectionName);
            if (cmds != null && cmds.Count > 0)
            {
                sb.Append("[" + cmds.Count + "]={");
                foreach (ushort cmd in cmds)
                {
                    sb.Append(Convert.ToString(cmd) + ",");
                }
                sb.Append("}");
            }
            else
            {
                sb.Append("=0");
            }
            sb.AppendLine(";");
            return(sb.ToString());
        }