aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Visuals/Components/PieAxisTicks.xaml
diff options
context:
space:
mode:
Diffstat (limited to 'Software/Visual_Studio/Tango.Visuals/Components/PieAxisTicks.xaml')
0 files changed, 0 insertions, 0 deletions
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL;
using Tango.BL.Enumerations;
using Tango.Core.Helpers;
using Tango.Documents;

namespace Tango.PortsListGenerator
{
    class Program
    {
        private class PortItem
        {
            public String Designator { get; set; }
            public String Id { get; set; }
            public String Asm { get; set; }
            public String InterfaceName { get; set; }
            public String Type { get; set; }
            public String Sensor { get; set; }
            public String SampleRate { get; set; }
            public String Averaging { get; set; }
            public String InitValue { get; set; }

            public override string ToString()
            {
                return String.Format("{0}, {1}, {2}, {3}, {4}, {5}, {6}", Designator, Asm, InterfaceName, Type, Sensor, Averaging, InitValue);
            }
        }

        static void Main(string[] args)
        {
            Console.WriteLine("Initializing Ports List...");

            ExcelReader reader = new ExcelReader(PathHelper.GetStartupPath() + "\\Tech IOs v5.xlsx");
            var results = reader.GetDataByIndex<PortItem>("Ports List", 2);

            results.RemoveAll(x => String.IsNullOrWhiteSpace(x.ToString().Replace(",", "")));

            foreach (var item in results)
            {
                item.InterfaceName = item.InterfaceName.Replace("-", "_");
            }

            var duplicates = results.GroupBy(x => x.InterfaceName).ToList().Where(y => y.Count() > 1).ToList();

            if (duplicates.Count > 0)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Found duplicate interface names! Please check your document.");

                foreach (var group in duplicates)
                {
                    foreach (var item in group)
                    {
                        Console.WriteLine(item.ToString());
                    }

                    Console.WriteLine();
                }

                Console.ReadLine();
                return;
            }

            using (ObservablesContext db = ObservablesContext.CreateDefault())
            {
                var toUpdateAdd = results.Where(x => !String.IsNullOrWhiteSpace(x.InterfaceName)).ToList();

                List<PortItem> toUpdate = new List<PortItem>();
                List<PortItem> toUpdateDb = new List<PortItem>();
                List<PortItem> toAdd = new List<PortItem>();
                List<PortItem> identical = new List<PortItem>();

                foreach (var item in toUpdateAdd)
                {
                    var existingIO = db.TechIos.SingleOrDefault(x => x.InterfaceName == item.InterfaceName);

                    if (existingIO != null)
                    {
                        bool shouldUpdate =
                            existingIO.Name != item.InterfaceName
                            ||
                            existingIO.Designator != item.Designator
                            ||
                            existingIO.Asm != item.Asm
                            ||
                            existingIO.InterfaceName != item.InterfaceName
                            ||
                            existingIO.Sensor != item.Sensor
                            ||
                            existingIO.Type != (int)TypeNameToIOType(item.Type)
                            ||
                            existingIO.Averaging != (int)StringToNumber(item.Averaging)
                            ||
                            existingIO.InitValue != StringToNumber(item.InitValue);

                        if (shouldUpdate)
                        {
                            toUpdate.Add(item);

                            toUpdateDb.Add(new PortItem()
                            {
                                Id = existingIO.ID.ToString(),
                                Asm = existingIO.Asm,
                                Designator = existingIO.Designator,
                                Averaging = existingIO.Averaging.ToString(),
                                InitValue = existingIO.InitValue.ToString(),
                                InterfaceName = existingIO.InterfaceName,
                                Sensor = existingIO.Sensor,
                                Type = ((IOType)existingIO.Type).ToString()
                            });
                        }
                        else
                        {
                            identical.Add(item);
                        }
                    }
                    else
                    {
                        toAdd.Add(item);
                    }
                }

                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine($"IDENTICAL ({identical.Count}) ---------------------------------------------------------");
                Console.WriteLine();

                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine($"ITEMS TO ADD ({toAdd.Count}) ---------------------------------------------------------");
                Console.WriteLine();

                foreach (var item in toAdd)
                {
                    Console.WriteLine(item.ToString());
                }

                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine($"ITEMS TO UPDATE ({toUpdate.Count}) ---------------------------------------------------------");
                Console.WriteLine();

                for (int i = 0; i < toUpdate.Count; i++)
                {
                    Console.WriteLine($"ID: {toUpdateDb[i].Id}: {toUpdateDb[i].ToString()}  =>  {toUpdate[i].ToString()}");
                }

                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine("Press 'Y' to perform the operation...");

                var key = Console.ReadKey();

                if (key.Key != ConsoleKey.Y)
                {
                    Environment.Exit(0);
                    return;
                }

                Console.WriteLine();
                Console.WriteLine("Starting procedure...");

                int code = 0;

                if (db.TechIos.Count() > 0)
                {
                    code = db.TechIos.Max(x => x.Code) + 1;
                }

                foreach (var item in toUpdate)
                {
                    var existingIO = db.TechIos.SingleOrDefault(x => x.InterfaceName == item.InterfaceName);

                    if (existingIO != null)
                    {
                        Console.WriteLine("Updating " + item.ToString());
                        existingIO.Name = item.InterfaceName;
                        existingIO.Designator = item.Designator;
                        existingIO.Asm = item.Asm;
                        existingIO.InterfaceName = item.InterfaceName;
                        existingIO.Sensor = item.Sensor;
                        existingIO.Type = (int)TypeNameToIOType(item.Type);
                        existingIO.Averaging = (int)StringToNumber(item.Averaging);
                        existingIO.InitValue = StringToNumber(item.InitValue);
                    }
                }

                foreach (var item in toAdd)
                {
                    Console.WriteLine("Adding " + item.ToString());
                    db.TechIos.Add(new BL.Entities.TechIo()
                    {
                        Code = code++,
                        Name = item.InterfaceName,
                        Designator = item.Designator,
                        Asm = item.Asm,
                        InterfaceName = item.InterfaceName,
                        Sensor = item.Sensor,
                        Type = (int)TypeNameToIOType(item.Type),
                        Averaging = (int)StringToNumber(item.Averaging),
                        InitValue = StringToNumber(item.InitValue),
                    });
                }

                Console.WriteLine("Saving changes to db...");
                db.SaveChanges();

                var no_name_items = results.Where(x => String.IsNullOrWhiteSpace(x.InterfaceName) && !String.IsNullOrWhiteSpace(x.ToString().Replace(",", ""))).ToList();

                if (no_name_items.Count > 0)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Found invalid rows with null name!");

                    for (int i = 0; i < no_name_items.Count; i++)
                    {
                        Console.WriteLine((i + 1) + ". " + no_name_items[i].ToString());
                    }
                }
            }

            reader.Dispose();

            Console.ForegroundColor = ConsoleColor.White;

            Console.WriteLine("Done!");
            Console.ReadLine();
        }

        private static double StringToNumber(String str)
        {
            double result;

            if (double.TryParse(str, out result))
            {
                return result;
            }
            else
            {
                return 0;
            }
        }

        private static IOType TypeNameToIOType(String name)
        {
            switch (name)
            {
                case "3Y Valve":
                    return IOType.Valve3Way;
                case "AI":
                    return IOType.AnalogInput;
                case "AO":
                    return IOType.AnalogOutput;
                case "COM":
                    return IOType.COM;
                case "GPI":
                    return IOType.DigitalInput;
                case "GPO":
                    return IOType.DigitalOutput;
                case "I2C":
                    return IOType.I2C;
                case "Motor":
                    return IOType.Motor;
                case "Motor - High Current":
                    return IOType.MotorHighCurrent;
                case "ROT-ENC-ABZ":
                    return IOType.ROT_ENC_ABZ;
                case "ROT-ENC-SSI":
                    return IOType.ROT_ENC_SSI;
                case "TACHO":
                    return IOType.TACHO;
                default:
                    return IOType.AnalogOutput;
            }
        }
    }
}