summaryrefslogtreecommitdiff
path: root/writer/Program.cs
blob: 2d4128f9a562c825c05a7e600225dbb91afef9a0 (plain) (blame)
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
using Microsoft.CSharp.RuntimeBinder;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

using System;
using System.IO;

namespace writer
{

    class Program
    {
        static void Send(JToken settingsToken)
        {
            var settings = settingsToken.ToObject<DriverSettings>();

            var errors = DriverInterop.GetSettingsErrors(settings);
            if (errors.Empty())
            {
                DriverInterop.Write(settings);
                return;
            }

            foreach (var err in errors.x)
            {
                Console.WriteLine(err + (settings.combineMagnitudes ? "" : " (x)"));
            }
            foreach (var err in errors.y)
            {
                Console.WriteLine(err + " (y)");
            }
        }

        static void Main(string[] args)
        {
            if (args.Length != 1 || args[0].Equals("help"))
            {
                Console.WriteLine("USAGE: {0} <file>", System.AppDomain.CurrentDomain.FriendlyName);
                return;
            }

            if (!File.Exists(args[0]))
            {
                Console.WriteLine("Settings file not found at {0}", args[0]);
                return;
            }

            try
            {
                var JO = JObject.Parse(File.ReadAllText(args[0]));

                if (JO.ContainsKey(DriverSettings.Key))
                {
                    Send(JO[DriverSettings.Key]);
                    return;
                }

                Send(JO);
            }
            catch (JsonException e)
            {
                Console.WriteLine("Settings invalid:\n{0}", e.Message.ToString());
            }
            catch (DriverNotInstalledException)
            {
                Console.WriteLine("Driver is not installed");
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: {0}", e.Message.ToString());
            }
        }
    }
}