Exemplo n.º 1
0
        private static async Task <T> ParseFile <T>(
            string path,
            IDictionary <string, AsyncLazy <KongPluginSchema> > availablePlugins,
            FileErrorMessages fileErrorMessages) where T : class, IKongvergeConfigObject
        {
            Log.Verbose($"Reading {path}");
            string text;

            using (var reader = File.OpenText(path))
            {
                text = await reader.ReadToEndAsync();
            }

            var errorMessages = new List <string>();

            try
            {
                var data = JsonConvert.DeserializeObject <T>(text);
                await data.Validate(availablePlugins, errorMessages);

                if (errorMessages.Any())
                {
                    fileErrorMessages.AddErrors(path, errorMessages.ToArray());
                }
                return(data);
            }
            catch (Exception e)
            {
                fileErrorMessages.AddErrors(path, e.Message);
            }

            return(null);
        }
Exemplo n.º 2
0
        private async Task <T> ParseFile <T>(
            string path,
            IDictionary <string, AsyncLazy <KongSchema> > schemas,
            FileErrorMessages fileErrorMessages) where T : class, IKongvergeConfigObject
        {
            Log.Verbose($"Reading {path}");
            var text = await _fileProvider.LoadTextContent(path);

            var errorMessages = new List <string>();

            try
            {
                var data = JsonConvert.DeserializeObject <T>(text);
                await data.Validate(schemas, errorMessages);

                if (errorMessages.Any())
                {
                    fileErrorMessages.AddErrors(path, errorMessages.ToArray());
                }
                return(data);
            }
            catch (Exception e)
            {
                fileErrorMessages.AddErrors(path, e.Message);
            }

            return(null);
        }
Exemplo n.º 3
0
        public async Task <KongvergeConfiguration> ReadConfiguration(string folderPath, IDictionary <string, AsyncLazy <KongSchema> > schemas)
        {
            Log.Information($"Reading files from {folderPath}");

            var filePaths = _fileProvider.EnumerateFiles(folderPath).ToArray();

            var          fileErrorMessages     = new FileErrorMessages();
            var          services              = new Dictionary <string, KongService>();
            GlobalConfig globalConfig          = null;
            var          globalConfigFilePaths = filePaths.Where(x => x.EndsWith(Constants.GlobalConfigFileName)).ToArray();

            if (globalConfigFilePaths.Length > 1)
            {
                foreach (var globalConfigFilePath in globalConfigFilePaths)
                {
                    fileErrorMessages.AddErrors(globalConfigFilePath, $"Cannot have more than one {Constants.GlobalConfigFileName} file.");
                    await ParseFile <GlobalConfig>(globalConfigFilePath, schemas, fileErrorMessages);
                }
            }
            else if (globalConfigFilePaths.Any())
            {
                globalConfig = await ParseFile <GlobalConfig>(globalConfigFilePaths.Single(), schemas, fileErrorMessages);
            }
            foreach (var serviceConfigFilePath in filePaths.Except(globalConfigFilePaths))
            {
                services.Add(serviceConfigFilePath, await ParseFile <KongService>(serviceConfigFilePath, schemas, fileErrorMessages));
            }
            foreach (var serviceConfigFilePath in services.Keys)
            {
                if (services.Keys.Except(new [] { serviceConfigFilePath }).Any(x => services[x]?.Name == services[serviceConfigFilePath]?.Name))
                {
                    fileErrorMessages.AddErrors(serviceConfigFilePath, $"{KongSchema.Violation<KongService>()} (field 'name' must be unique).");
                }
            }

            if (fileErrorMessages.Any())
            {
                throw new InvalidConfigurationFilesException(fileErrorMessages.CreateMessage());
            }

            var configuration = new KongvergeConfiguration
            {
                Services     = services.Values.ToArray(),
                GlobalConfig = globalConfig ?? new GlobalConfig()
            };

            Log.Information($"Configuration from files: {configuration}");
            return(configuration);
        }
Exemplo n.º 4
0
        public virtual async Task <KongvergeConfiguration> ReadConfiguration(string folderPath, IReadOnlyCollection <string> availablePlugins)
        {
            Log.Information($"Reading files from {folderPath}");

            var filePaths = Directory.EnumerateFiles(folderPath, $"*{Settings.FileExtension}", SearchOption.AllDirectories).ToArray();

            var          fileErrorMessages     = new FileErrorMessages();
            var          services              = new List <KongService>();
            GlobalConfig globalConfig          = null;
            var          globalConfigFilePaths = filePaths.Where(x => x.EndsWith(Settings.GlobalConfigFileName)).ToArray();

            if (globalConfigFilePaths.Length > 1)
            {
                foreach (var globalConfigFilePath in globalConfigFilePaths)
                {
                    fileErrorMessages.AddErrors(globalConfigFilePath, $"Cannot have more than one {Settings.GlobalConfigFileName} file.");
                    await ParseFile <GlobalConfig>(globalConfigFilePath, availablePlugins, fileErrorMessages);
                }
            }
            else if (globalConfigFilePaths.Any())
            {
                globalConfig = await ParseFile <GlobalConfig>(globalConfigFilePaths.Single(), availablePlugins, fileErrorMessages);
            }
            foreach (var serviceConfigFilePath in filePaths.Except(globalConfigFilePaths))
            {
                services.Add(await ParseFile <KongService>(serviceConfigFilePath, availablePlugins, fileErrorMessages));
            }

            if (fileErrorMessages.Any())
            {
                throw new InvalidConfigurationFilesException(fileErrorMessages.CreateMessage());
            }

            var configuration = new KongvergeConfiguration
            {
                Services     = services.AsReadOnly(),
                GlobalConfig = globalConfig ?? new GlobalConfig()
            };

            LogObjectCounts(configuration);
            return(configuration);
        }
Exemplo n.º 5
0
    public MotorolaSRECfile_long(string filename)
    {
        if (filename.ToLower().EndsWith(".s19") | filename.ToLower().EndsWith(".srec"))
        {
        }
        else
        {
            FileErrorMessages.Add(" Файл имеет расширение отличное от .s19(.srec)");
        }
        try
        {
            StreamReader sr         = new StreamReader(filename);
            bool         eof        = false;
            int          lineNumber = 0;
            while (!eof)
            {
                lineNumber++;
                SRECline line = new SRECline(sr.ReadLine(), lineNumber);
                //if (ErrorMessage == "") ErrorMessage = line.ErrorMessages;
                FileErrorMessages.AddRange(line.LineErrorMessages);
                if (line.CriticalErrors == true)
                {
                    CriticalError = true;
                }

                switch (line.recordtype)
                {
                case SRECline.RecordType.DataRecordS19:
                case SRECline.RecordType.DataRecordSREC:
                    AddressLineSorted.Add((long)(line.address), line.data);
                    AddressLine.Add((long)(line.address), line.data);
                    int ij = 0;
                    foreach (byte bt in line.data)
                    {
                        AddressByteSorted.Add((long)(line.address + ij), line.data[ij]);
                        ij++;
                    }

                    break;

                case SRECline.RecordType.EndOfFileS19:
                case SRECline.RecordType.EndOfFileSREC:
                    eof = true;
                    break;
                }
                if (sr.EndOfStream)
                {
                    eof = true;
                }
            }
            if (AddressByteSorted.Count != 0)
            {
                foreach (KeyValuePair <long, byte> dabs in AddressByteSorted)
                {
                    Addresses.Add(dabs.Key);
                    Bytes.Add(dabs.Value);
                }

                for (int i = 0; i < AddressByteSorted.Count; i++)
                {
                    if (Addresses[i] < minAddress)
                    {
                        minAddress = Addresses[i];
                    }
                }

                for (int i = 0; i < AddressByteSorted.Count; i++)
                {
                    if (Addresses[i] > maxAddress)
                    {
                        maxAddress = Addresses[i];
                    }
                }
            }
            else
            {
                FileErrorMessages.Add(" Файл не имеет адресов.");
                minAddress = maxAddress = long.MinValue;
            }

            sr.Close();
            sr.Dispose();
        }
        catch (Exception ex)
        {
            ErrorMessage  = ex.Message;
            CriticalError = true;
        }
    }
Exemplo n.º 6
0
    public IntelHEXfile(string filename)
    {
        if (filename.EndsWith(".hex") != true)
        {
            FileErrorMessages.Add("Файл имеет расширение отличное от hex");
        }

        try
        {
            StreamReader sr         = new StreamReader(filename);
            bool         eof        = false;
            int          lineNumber = 0;
            while (!eof)
            {
                lineNumber++;
                HEXline line = new HEXline(sr.ReadLine(), lineNumber);
                //if (ErrorMessage == "") ErrorMessage = line.ErrorMessages;
                FileErrorMessages.AddRange(line.LineErrorMessages);
                if (line.CriticalErrors == true)
                {
                    CriticalError = true;
                }

                switch (line.recordtype)
                {
                case HEXline.RecordType.DataRecord:
                    //data.AddRange(line.data);
                    int ij = 0;
                    foreach (byte bt in line.data)
                    {
                        AddressByteSorted.Add((long)(line.address + ij), line.data[ij]);
                        ij++;
                    }

                    break;

                case HEXline.RecordType.EndOfFile:
                    eof = true;
                    break;
                }
                if (sr.EndOfStream)
                {
                    eof = true;
                }
            }

            foreach (KeyValuePair <long, byte> dabs in AddressByteSorted)
            {
                Addresses.Add(dabs.Key);
                Bytes.Add(dabs.Value);
            }

            for (int i = 0; i < AddressByteSorted.Count; i++)
            {
                if (Addresses[i] < minAddress)
                {
                    minAddress = Addresses[i];
                }
            }

            for (int i = 0; i < AddressByteSorted.Count; i++)
            {
                if (Addresses[i] > maxAddress)
                {
                    maxAddress = Addresses[i];
                }
            }



            sr.Close();
            sr.Dispose();
        }
        catch (Exception ex)
        {
            ErrorMessage  = ex.Message;
            CriticalError = true;
        }
    }
Exemplo n.º 7
0
 public string[] GetErrorMessages()
 {
     return(FileErrorMessages.ToArray());
 }
Exemplo n.º 8
0
    public MotorolaSRECfile(string filename)
    {
        // if (filename.ToLower().EndsWith(".s19") != true) FileErrorMessages.Add("Файл имеет расширение отличное от s19");
        if (filename.ToLower().EndsWith(".s19") | filename.ToLower().EndsWith(".s28") | filename.ToLower().EndsWith(".s37") |
            filename.ToLower().EndsWith(".srec") | filename.ToLower().EndsWith(".sx"))
        {
        }
        else
        {
            FileErrorMessages.Add(" Файл имеет расширение отличное от srec (s19, s28, s37, sx)");
        }
        try
        {
            StreamReader sr         = new StreamReader(filename);
            bool         eof        = false;
            int          lineNumber = 0;
            while (!eof)
            {
                lineNumber++;
                SRECline line = new SRECline(sr.ReadLine(), lineNumber);
                //if (ErrorMessage == "") ErrorMessage = line.ErrorMessages;
                FileErrorMessages.AddRange(line.LineErrorMessages);
                if (line.CriticalErrors == true)
                {
                    CriticalError = true;
                }
                LinesOfFile.Add(line.crntLine);

                switch (line.recordtype)
                {
                case SRECline.RecordType.DataRecord16:
                case SRECline.RecordType.DataRecord24:
                case SRECline.RecordType.DataRecord32:
                    //data.AddRange(line.data);
                    AddressLineSorted.Add(line.address, line.data);
                    int ij = 0;
                    foreach (byte bt in line.data)
                    {
                        AddressByteSorted.Add((long)(line.address + ij), line.data[ij]);
                        Addresses.Add((long)(line.address + ij));
                        Bytes.Add(line.data[ij]);
                        if (minAddress > (long)(line.address + ij))
                        {
                            minAddress = (long)(line.address + ij);
                        }
                        if (maxAddress < (long)(line.address + ij))
                        {
                            maxAddress = (long)(line.address + ij);
                        }
                        ij++;
                    }

                    break;


                case SRECline.RecordType.CountRecord16:
                case SRECline.RecordType.CountRecord24:
                    //recordCount = line.address;

                    break;


                case SRECline.RecordType.StartAddressRecord32:
                case SRECline.RecordType.StartAddressRecord24:
                case SRECline.RecordType.StartAddressRecord16:
                    eof = true;
                    break;
                }
                if (sr.EndOfStream)
                {
                    eof = true;
                }
            }

            sr.Close();
            sr.Dispose();
        }
        catch (Exception ex)
        {
            ErrorMessage  = ex.Message;
            CriticalError = true;
        }
    }