aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Utilities/Tango.AlarmParametersGenerator/Program.cs
blob: 2972b48929a59cfc1e5ba250b5894ad72a1f6639 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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}...");

                        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);
        }
    }
}