Exemplo n.º 1
0
        public static void EnsureDatabaseCreated(IServiceProvider services)
        {
            var context         = services.GetRequiredService <AutomaticaContext>();
            var visuInitFactory = services.GetRequiredService <IVisualisationFactory>();
            var config          = services.GetRequiredService <IConfiguration>();

            context.Database.Migrate();

            bool dbCreated = !context.BoardTypes.Any();

            if (dbCreated)
            {
                context.RuleInterfaceDirections.Add(new EF.Models.RuleInterfaceDirection()
                {
                    ObjId       = 1,
                    Name        = "Input",
                    Description = "Input",
                    Key         = "I"
                });
                context.RuleInterfaceDirections.Add(new EF.Models.RuleInterfaceDirection()
                {
                    ObjId       = 2,
                    Name        = "Output",
                    Description = "Output",
                    Key         = "O"
                });
                context.RuleInterfaceDirections.Add(new EF.Models.RuleInterfaceDirection()
                {
                    ObjId       = 3,
                    Name        = "Parameter",
                    Description = "Parameter",
                    Key         = "P"
                });

                context.RulePageTypes.Add(new RulePageType()
                {
                    ObjId       = 1,
                    Name        = "Rules",
                    Description = "Rules",
                    Key         = "rules"
                });
                context.VisuPageTypes.Add(new VisuPageType()
                {
                    ObjId       = 1,
                    Name        = "PC",
                    Description = "PC",
                    Key         = "pc"
                });
                context.VisuPageTypes.Add(new VisuPageType()
                {
                    ObjId       = 2,
                    Name        = "Mobile",
                    Description = "Mobile",
                    Key         = "mobile"
                });
                context.SaveChanges();

                context.Slaves.Add(new Slave()
                {
                    ObjId       = new Guid(ServerInfo.SelfSlaveId),
                    Name        = "local",
                    Description = "this is me",
                    ClientId    = "",
                    ClientKey   = ""
                });


                context.Settings.Add(new Setting
                {
                    ObjId     = 1,
                    ValueKey  = "ConfigVersion",
                    Type      = (long)PropertyTemplateType.Numeric,
                    Value     = 0,
                    Group     = "ConfigVersion",
                    IsVisible = false,
                    Order     = 10
                });
                context.SaveChanges();
            }

            var lat = context.Settings.SingleOrDefault(a => a.ValueKey == "Latitude");

            if (lat == null)
            {
                context.Settings.Add(new Setting
                {
                    ValueKey  = "Latitude",
                    Type      = (long)PropertyTemplateType.Numeric,
                    Value     = 0,
                    Group     = "SERVER.SETTINGS",
                    IsVisible = true,
                    Order     = 10
                });

                context.Settings.Add(new Setting
                {
                    ValueKey  = "Longitude",
                    Type      = (long)PropertyTemplateType.Numeric,
                    Value     = 0,
                    Group     = "SERVER.SETTINGS",
                    IsVisible = true,
                    Order     = 11
                });
            }
            else
            {
                var longi = context.Settings.SingleOrDefault(a => a.ValueKey == "Longitude");

                if (lat.ValueDouble == null)
                {
                    lat.ValueDouble = 0;
                }

                if (longi.ValueDouble == null)
                {
                    longi.ValueDouble = 0;
                }

                context.Settings.Update(longi);
                context.Settings.Update(lat);
            }

            var apiKey = context.Settings.SingleOrDefault(a => a.ValueKey == "apiKey");

            if (apiKey == null)
            {
                context.Settings.Add(new Setting
                {
                    ValueKey  = "apiKey",
                    Type      = (long)PropertyTemplateType.Text,
                    Value     = "",
                    Group     = "SERVER.SETTINGS",
                    IsVisible = true,
                    Order     = 0
                });
            }

            var autoUpdate      = context.Settings.SingleOrDefault(a => a.ValueKey == "autoUpdate");
            var autoUpdateTime  = context.Settings.SingleOrDefault(a => a.ValueKey == "autoUpdateTime");
            var reportCrashLogs = context.Settings.SingleOrDefault(a => a.ValueKey == "reportCrashLogs");

            if (autoUpdate == null)
            {
                context.Settings.Add(new Setting
                {
                    ValueKey  = "autoUpdate",
                    Type      = (long)PropertyTemplateType.Bool,
                    Value     = false,
                    Group     = "SERVER.SETTINGS",
                    IsVisible = true,
                    Order     = 20
                });
            }
            else
            {
                autoUpdate.Order = 20;
                context.Update(autoUpdate);
            }

            if (autoUpdateTime == null)
            {
                context.Settings.Add(new Setting
                {
                    ValueKey  = "autoUpdateTime",
                    Type      = (long)PropertyTemplateType.Time,
                    Value     = new DateTime(2000, 12, 31, 2, 0, 0),
                    Group     = "SERVER.SETTINGS",
                    IsVisible = true,
                    Order     = 21
                });
            }
            else
            {
                autoUpdateTime.Order = 21;
                autoUpdateTime.Type  = (long)PropertyTemplateType.Time;
                context.Update(autoUpdateTime);
            }

            if (reportCrashLogs == null)
            {
                context.Settings.Add(new Setting
                {
                    ValueKey  = "reportCrashLogs",
                    Type      = (long)PropertyTemplateType.Bool,
                    Value     = false,
                    Group     = "SERVER.SETTINGS",
                    IsVisible = true,
                    Order     = 22
                });
            }
            else
            {
                reportCrashLogs.Order = 22;
                context.Update(reportCrashLogs);
            }

            var cloudUrl = context.Settings.SingleOrDefault(a => a.ValueKey == "cloudUrl");

            if (cloudUrl == null)
            {
                context.Settings.Add(new Setting
                {
                    ValueKey  = "cloudUrl",
                    Type      = (long)PropertyTemplateType.Text,
                    Value     = "https://automatica-core-cloud.azurewebsites.net",
                    Group     = "SERVER.SETTINGS",
                    IsVisible = true,
                    Order     = 1
                });
            }

            var cloudEnvironment = context.Settings.SingleOrDefault(a => a.ValueKey == "cloudEnvironment");

            if (cloudEnvironment == null)
            {
                var cloudEnvironmentType = "master";
#if DEBUG
                cloudEnvironmentType = "develop";
#else
                cloudEnvironmentType = "master";
#endif

                context.Settings.Add(new Setting
                {
                    ValueKey  = "cloudEnvironment",
                    Type      = (long)PropertyTemplateType.Text,
                    Value     = cloudEnvironmentType,
                    Group     = "SERVER.SETTINGS",
                    IsVisible = true,
                    Order     = 2
                });
            }

            var projectName = context.Settings.SingleOrDefault(a => a.ValueKey == "projectName");

            if (projectName == null)
            {
                context.Settings.Add(new Setting
                {
                    ValueKey  = "projectName",
                    Type      = (long)PropertyTemplateType.Text,
                    Value     = "Automatica.Core",
                    Group     = "SERVER.SETTINGS",
                    IsVisible = true,
                    Order     = 3
                });
            }

            var propertyTypes = Enum.GetValues(typeof(PropertyTemplateType));

            foreach (var propertyType in propertyTypes)
            {
                var propertyTypeDb = context.PropertyTypes.SingleOrDefault(a => a.Type == Convert.ToInt64(propertyType));
                var isNewObject    = false;
                if (propertyTypeDb == null)
                {
                    propertyTypeDb = new PropertyType {
                        Type = (int)propertyType
                    };
                    isNewObject = true;
                }

                var type       = propertyType.GetType();
                var memInfo    = type.GetMember(propertyType.ToString());
                var attributes = memInfo[0].GetCustomAttributes(typeof(PropertyTemplateTypeAttribute), false);

                if (attributes.Length > 0 && attributes[0] is PropertyTemplateTypeAttribute attribute)
                {
                    propertyTypeDb.Name        = attribute.Name;
                    propertyTypeDb.Description = attribute.Description;
                    propertyTypeDb.Meta        = attribute.Meta;
                }
                else
                {
                    propertyTypeDb.Name        = propertyType.GetType().Name;
                    propertyTypeDb.Description = propertyType.GetType().Name;
                    propertyTypeDb.Meta        = null;
                }

                if (isNewObject)
                {
                    context.PropertyTypes.Add(propertyTypeDb);
                }
                else
                {
                    context.PropertyTypes.Update(propertyTypeDb);
                }
            }

            var nodeDataTypes = Enum.GetValues(typeof(NodeDataType));

            foreach (var nodeDataType in nodeDataTypes)
            {
                var nodeDataTypeDb = context.NodeDataTypes.SingleOrDefault(a => a.Type == Convert.ToInt64(nodeDataType));
                var isNewObject    = false;
                if (nodeDataTypeDb == null)
                {
                    nodeDataTypeDb      = new EF.Models.NodeDataType();
                    nodeDataTypeDb.Type = (int)nodeDataType;
                    isNewObject         = true;
                }

                var type       = nodeDataType.GetType();
                var memInfo    = type.GetMember(nodeDataType.ToString());
                var attributes = memInfo[0].GetCustomAttributes(typeof(NodeDataTypeEnumAttribute), false);

                if (attributes.Length > 0 && attributes[0] is NodeDataTypeEnumAttribute attribute)
                {
                    nodeDataTypeDb.Name        = attribute.Name;
                    nodeDataTypeDb.Description = attribute.Description;
                }
                else
                {
                    nodeDataTypeDb.Name        = nodeDataType.GetType().Name;
                    nodeDataTypeDb.Description = nodeDataType.GetType().Name;
                }

                if (isNewObject)
                {
                    context.NodeDataTypes.Add(nodeDataTypeDb);
                }
                else
                {
                    context.NodeDataTypes.Update(nodeDataTypeDb);
                }
            }


            context.SaveChanges();
            visuInitFactory.Initialize(context, config);
            context.SaveChanges();

            CreateInterfaceTypes(context);
            context.SaveChanges();

            AddSystemTemplates(context);

            IDatabaseBoardType boardType = null;

            if (BoardTypes.Docker.Docker.InDocker)
            {
                boardType = new BoardTypes.Docker.Docker();
            }
            else
            {
                boardType = new RaspberryPi();
            }

            ServerInfo.BoardType = boardType;

            AddBoard(context, boardType);

            AddAreaData(context);
            CategoryGroup.GenerateDefault(context);
            context.SaveChanges();
            CategoryInstance.GenerateDefault(context);

            context.SaveChanges();


            if (!context.AreaInstances.Any())
            {
                var projectInstance = new AreaInstance
                {
                    ObjId             = Guid.NewGuid(),
                    Name              = "Project",
                    Description       = "",
                    Icon              = "home",
                    This2AreaTemplate = AreaTemplateAttribute.GetFromEnum(AreaTemplates.Project),
                    This2Parent       = null
                };
                context.AreaInstances.Add(projectInstance);
            }

            if (dbCreated)
            {
                var rootNodeTemplate = context.NodeTemplates.SingleOrDefault(a => a.ObjId == GuidTemplateTypeAttribute.GetFromEnum(boardType.BoardType));
                var rootNode         = NodeInstanceFactory.CreateNodeInstanceFromTemplate(rootNodeTemplate);

                rootNode.Name        = boardType.Name;
                rootNode.Description = "";

                context.NodeInstances.Add(rootNode);

                var childs = context.NodeTemplates.Where(a => a.NeedsInterface2InterfacesType == rootNodeTemplate.ObjId);

                foreach (var child in childs)
                {
                    if (child.NeedsInterface2InterfacesType == child.ProvidesInterface2InterfaceType)
                    {
                        continue;
                    }
                    var node = NodeInstanceFactory.CreateNodeInstanceFromTemplate(child);
                    node.This2ParentNodeInstance = rootNode.ObjId;
                    context.NodeInstances.Add(node);
                }


                var rulePage = new RulePage
                {
                    ObjId             = Guid.NewGuid(),
                    Name              = "Page1",
                    Description       = "",
                    This2RulePageType = 1
                };

                context.RulePages.Add(rulePage);


                var visuPage = new VisuPage
                {
                    ObjId             = Guid.NewGuid(),
                    Name              = "Page1",
                    Description       = "",
                    This2VisuPageType = 2,
                    DefaultPage       = true
                };

                context.VisuPages.Add(visuPage);

                AddInitUserManagementData(context);

                context.SaveChanges();
            }


            context.SaveChanges();
        }
Exemplo n.º 2
0
        public static void EnusreDatabaseCreated(IServiceProvider services)
        {
            var context         = services.GetRequiredService <AutomaticaContext>();
            var visuInitFactory = services.GetRequiredService <IVisualisationFactory>();
            var config          = services.GetRequiredService <IConfiguration>();

            context.Database.Migrate();

            bool dbCreated = !context.BoardTypes.Any();



            if (dbCreated)
            {
                context.Database.ExecuteSqlCommand($@"	       

		            INSERT INTO RuleInterfaceDirections (ObjId, Name, Description, Key) VALUES 
		                (1, 'Input', 'Input', 'I'),
		                (2, 'Output', 'Output', 'O'),
		                (3, 'Parameter', 'Parameter', 'P');

	                INSERT INTO Settings (ObjId, ValueKey, ValueInt, Type) VALUES (1, 'ConfigVersion', 0, 0);

		        
		        INSERT INTO RulePageTypes (ObjId, Name, Description, Key) VALUES 
		        (1, 'Rules', 'Rules', 'rules');


		        INSERT INTO VisuPageTypes (ObjId, Name, Description, Key) VALUES 
		        (1, 'PC', 'PC', 'pc');


		        INSERT INTO VisuPageTypes (ObjId, Name, Description, Key) VALUES 
		        (2, 'Mobile', 'Mobile', 'mobile')"        );
                context.SaveChanges();
            }

            var lat = context.Settings.SingleOrDefault(a => a.ValueKey == "Latitude");

            if (lat == null)
            {
                context.Settings.Add(new Setting
                {
                    ValueKey  = "Latitude",
                    Type      = (long)PropertyTemplateType.Numeric,
                    Value     = 0,
                    Group     = "SERVER.SETTINGS",
                    IsVisible = true,
                    Order     = 10
                });

                context.Settings.Add(new Setting
                {
                    ValueKey  = "Longitude",
                    Type      = (long)PropertyTemplateType.Numeric,
                    Value     = 0,
                    Group     = "SERVER.SETTINGS",
                    IsVisible = true,
                    Order     = 11
                });
            }
            else
            {
                var longi = context.Settings.SingleOrDefault(a => a.ValueKey == "Longitude");

                if (lat.ValueDouble == null)
                {
                    lat.ValueDouble = 0;
                }

                if (longi.ValueDouble == null)
                {
                    longi.ValueDouble = 0;
                }

                context.Settings.Update(longi);
                context.Settings.Update(lat);
            }

            var apiKey = context.Settings.SingleOrDefault(a => a.ValueKey == "apiKey");

            if (apiKey == null)
            {
                context.Settings.Add(new Setting
                {
                    ValueKey  = "apiKey",
                    Type      = (long)PropertyTemplateType.Text,
                    Value     = "",
                    Group     = "SERVER.SETTINGS",
                    IsVisible = true,
                    Order     = 0
                });
            }

            var autoUpdate      = context.Settings.SingleOrDefault(a => a.ValueKey == "autoUpdate");
            var autoUpdateTime  = context.Settings.SingleOrDefault(a => a.ValueKey == "autoUpdateTime");
            var reportCrashLogs = context.Settings.SingleOrDefault(a => a.ValueKey == "reportCrashLogs");

            if (autoUpdate == null)
            {
                context.Settings.Add(new Setting
                {
                    ValueKey  = "autoUpdate",
                    Type      = (long)PropertyTemplateType.Bool,
                    Value     = false,
                    Group     = "SERVER.SETTINGS",
                    IsVisible = true,
                    Order     = 20
                });
            }
            else
            {
                autoUpdate.Order = 20;
                context.Update(autoUpdate);
            }

            if (autoUpdateTime == null)
            {
                context.Settings.Add(new Setting
                {
                    ValueKey  = "autoUpdateTime",
                    Type      = (long)PropertyTemplateType.Time,
                    Value     = new DateTime(2000, 12, 31, 2, 0, 0),
                    Group     = "SERVER.SETTINGS",
                    IsVisible = true,
                    Order     = 21
                });
            }
            else
            {
                autoUpdateTime.Order = 21;
                autoUpdateTime.Type  = (long)PropertyTemplateType.Time;
                context.Update(autoUpdateTime);
            }

            if (reportCrashLogs == null)
            {
                context.Settings.Add(new Setting
                {
                    ValueKey  = "reportCrashLogs",
                    Type      = (long)PropertyTemplateType.Bool,
                    Value     = false,
                    Group     = "SERVER.SETTINGS",
                    IsVisible = true,
                    Order     = 22
                });
            }
            else
            {
                reportCrashLogs.Order = 22;
                context.Update(reportCrashLogs);
            }

            var cloudUrl = context.Settings.SingleOrDefault(a => a.ValueKey == "cloudUrl");

            if (cloudUrl == null)
            {
                context.Settings.Add(new Setting
                {
                    ValueKey  = "cloudUrl",
                    Type      = (long)PropertyTemplateType.Text,
                    Value     = "https://automatica-core-cloud.azurewebsites.net",
                    Group     = "SERVER.SETTINGS",
                    IsVisible = true,
                    Order     = 1
                });
            }

            var propertyTypes = Enum.GetValues(typeof(PropertyTemplateType));

            foreach (var propertyType in propertyTypes)
            {
                var propertyTypeDb = context.PropertyTypes.SingleOrDefault(a => a.Type == (int)propertyType);
                var isNewObject    = false;
                if (propertyTypeDb == null)
                {
                    propertyTypeDb = new PropertyType {
                        Type = (int)propertyType
                    };
                    isNewObject = true;
                }

                var type       = propertyType.GetType();
                var memInfo    = type.GetMember(propertyType.ToString());
                var attributes = memInfo[0].GetCustomAttributes(typeof(PropertyTemplateTypeAttribute), false);

                if (attributes.Length > 0 && attributes[0] is PropertyTemplateTypeAttribute attribute)
                {
                    propertyTypeDb.Name        = attribute.Name;
                    propertyTypeDb.Description = attribute.Description;
                    propertyTypeDb.Meta        = attribute.Meta;
                }
                else
                {
                    propertyTypeDb.Name        = propertyType.GetType().Name;
                    propertyTypeDb.Description = propertyType.GetType().Name;
                    propertyTypeDb.Meta        = null;
                }

                if (isNewObject)
                {
                    context.PropertyTypes.Add(propertyTypeDb);
                }
                else
                {
                    context.PropertyTypes.Update(propertyTypeDb);
                }
            }

            var nodeDataTypes = Enum.GetValues(typeof(NodeDataType));

            foreach (var nodeDataType in nodeDataTypes)
            {
                var nodeDataTypeDb = context.NodeDataTypes.SingleOrDefault(a => a.Type == (int)nodeDataType);
                var isNewObject    = false;
                if (nodeDataTypeDb == null)
                {
                    nodeDataTypeDb      = new EF.Models.NodeDataType();
                    nodeDataTypeDb.Type = (int)nodeDataType;
                    isNewObject         = true;
                }

                var type       = nodeDataType.GetType();
                var memInfo    = type.GetMember(nodeDataType.ToString());
                var attributes = memInfo[0].GetCustomAttributes(typeof(NodeDataTypeEnumAttribute), false);

                if (attributes.Length > 0 && attributes[0] is NodeDataTypeEnumAttribute attribute)
                {
                    nodeDataTypeDb.Name        = attribute.Name;
                    nodeDataTypeDb.Description = attribute.Description;
                }
                else
                {
                    nodeDataTypeDb.Name        = nodeDataType.GetType().Name;
                    nodeDataTypeDb.Description = nodeDataType.GetType().Name;
                }

                if (isNewObject)
                {
                    context.NodeDataTypes.Add(nodeDataTypeDb);
                }
                else
                {
                    context.NodeDataTypes.Update(nodeDataTypeDb);
                }
            }


            context.SaveChanges();
            visuInitFactory.Initialize(context, config);
            context.SaveChanges();

            CreateInterfaceTypes(context);
            AddSystemTemplates(context);
            AddRaspberryPi3Board(context);
            AddAreaData(context);
            CategoryGroup.GenerateDefault(context);
            context.SaveChanges();
            CategoryInstance.GenerateDefault(context);

            context.SaveChanges();


            if (!context.AreaInstances.Any())
            {
                var projectInstance = new AreaInstance
                {
                    ObjId             = Guid.NewGuid(),
                    Name              = "Project",
                    Description       = "",
                    Icon              = "home",
                    This2AreaTemplate = AreaTemplateAttribute.GetFromEnum(AreaTemplates.Project),
                    This2Parent       = null
                };
                context.AreaInstances.Add(projectInstance);
            }

            if (dbCreated)
            {
                var rootNodeTemplate = context.NodeTemplates.SingleOrDefault(a => a.ObjId == GuidTemplateTypeAttribute.GetFromEnum(BoardTypeEnum.RaspberryPi3));
                var rootNode         = NodeInstanceFactory.CreateNodeInstanceFromTemplate(rootNodeTemplate);

                rootNode.Name        = "Raspberry PI 3";
                rootNode.Description = "";

                context.NodeInstances.Add(rootNode);

                var childs = context.NodeTemplates.Where(a => a.NeedsInterface2InterfacesType == rootNodeTemplate.ObjId);

                foreach (var child in childs)
                {
                    if (child.NeedsInterface2InterfacesType == child.ProvidesInterface2InterfaceType)
                    {
                        continue;
                    }
                    var node = NodeInstanceFactory.CreateNodeInstanceFromTemplate(child);
                    node.This2ParentNodeInstance = rootNode.ObjId;
                    context.NodeInstances.Add(node);
                }


                var rulePage = new RulePage
                {
                    ObjId             = Guid.NewGuid(),
                    Name              = "Page1",
                    Description       = "",
                    This2RulePageType = 1
                };

                context.RulePages.Add(rulePage);


                var visuPage = new VisuPage
                {
                    ObjId             = Guid.NewGuid(),
                    Name              = "Page1",
                    Description       = "",
                    This2VisuPageType = 2,
                    DefaultPage       = true
                };

                context.VisuPages.Add(visuPage);

                AddInitUserManagementData(context);

                context.SaveChanges();
            }


            context.SaveChanges();
        }