aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Utilities/Tango.AlarmParametersGenerator/Program.cs
blob: ae60e04b4c011731b8915ceddc63be0791d47945 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Core.ExtensionMethods;
using Tango.Core.Helpers;
using Tango.Documents;
using Tango.PMR.Debugging;
using Tango.PMR.Diagnostics;
using Tango.PMR.EmbeddedParameters;

namespace Tango.AlarmParametersGenerator
{
    class Program
    {
        private class ExcelEventType
        {
            public String Code { get; set; }
            public String Name { get; set; }
            public String Title { get; set; }
            public String Description { get; set; }
            public String TechnicalDescription { get; set; }
            public String Index { get; set; }
            public String Category { get; set; }
            public String Group { get; set; }
            public String UserInterventionRequired { get; set; }
            public String ActionsUI { get; set; }
            public String ActionsEmbedded { get; set; }
            public String NotificationTime { get; set; }
            public String Guidance { get; set; }

            //Embedded
            public String Persistent { get; set; }
            public String SWIndex { get; set; }
            public String Frequency { get; set; }
            public String Source { get; set; }
            public String DeviceId { get; set; }
            public String ApplicationDeviceID { get; set; }
            public String Criteria { get; set; }
            public String Direction { get; set; }
            public String Severity { get; set; }
            public String Predeccesor { get; set; }
            public String DebounceValue { get; set; }

            public override string ToString()
            {
                return this.ToJsonString();
            }
        }

        static void Main(string[] args)
        {
            var color = Console.ForegroundColor;

            try
            {

                var options = new Options();

                if (!CommandLine.Parser.Default.ParseArguments(args, options))
                {
                    ExitError();
                    return;
                }

                if (!File.Exists(options.ExcelFile))
                {
                    throw new FileNotFoundException($"Could not locate excel file '{Path.GetFullPath(options.ExcelFile)}'.");
                }

                Console.WriteLine();

                Console.WriteLine("Generating alarm parameters file...");

                Console.WriteLine();

                Console.WriteLine($"Source: {Path.GetFullPath(options.ExcelFile)}");
                Console.WriteLine($"Target: {Path.GetFullPath(options.OutputFile)}");

                Console.WriteLine();

                ExcelReader reader = new ExcelReader(options.ExcelFile);
                var results = reader.GetDataByIndex<ExcelEventType>("ALARM EVENTS HANDLING", 2);

                AlarmParameters parameters = new AlarmParameters();

                foreach (var item in results)
                {
                    if (!String.IsNullOrWhiteSpace(item.Source))
                    {
                        Console.WriteLine($"Parsing event {item.Code}, {item.Name},{item.Source}...");

                        AlarmHandlingItem alarm = new AlarmHandlingItem();
                        alarm.AlarmSource = item.Source.ToEnum<AlarmSourceType>();
                        //alarm.Frequency = item.Frequency.ToUint();
                        alarm.DeviceId = item.DeviceId.ToUint();
                        alarm.ModuleDeviceId = item.ApplicationDeviceID.ToUint();
                        alarm.AlarmValue = item.Criteria.ToUint();
                        alarm.AlarmDirection = item.Direction.ToBoolean();
                        alarm.Severity = item.Severity.ToEnum<DebugLogCategory>();
                        //alarm.Predecessor = item.Predeccesor.ToUint();
                        alarm.DebounceValue = item.DebounceValue.ToUint();
                        alarm.EventType = (EventType)item.Code.ToUint();
                        alarm.IsPersistent = item.Persistent.ToBooleanYesNo();

                        parameters.AlarmItem.Add(alarm);
                    }
                }

                Console.WriteLine("Writing file...");
                File.WriteAllBytes(options.OutputFile, parameters.ToBytes());

                Console.WriteLine("Validating file...");
                AlarmParameters checkParameters = AlarmParameters.Parser.ParseFrom(File.ReadAllBytes(options.OutputFile));

                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("File generated successfully.");
                Console.ForegroundColor = color;
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex.Message);
                Console.ForegroundColor = color;
            }
        }

        private static void ExitError()
        {
            Environment.Exit(-1);
        }
    }

    public static class Extensions
    {
        public static UInt32 ToUint(this String str)
        {
            return String.IsNullOrWhiteSpace(str) ? 0 : UInt32.Parse(str);
        }

        public static T ToEnum<T>(this String str) where T : struct
        {
            return String.IsNullOrWhiteSpace(str) ? default(T) : (T)Enum.Parse(typeof(T), str);
        }

        public static bool ToBoolean(this String str)
        {
            return String.IsNullOrWhiteSpace(str) ? false : (str.Contains("0") ? false : true);
        }

        public static bool ToBooleanYesNo(this String str)
        {
            return String.IsNullOrWhiteSpace(str) ? false : (str.Contains("Yes") ? true : false);
        }
    }
}