Example #1
0
        static public void Save(ClassSavedSettings theData)
        {
            using (StreamWriter file = File.CreateText(theFilename))
            {
                ClassSavedSettings c = (ClassSavedSettings)theData.MemberwiseClone();

                JsonSerializer serializer = new JsonSerializer();
                serializer.Formatting = Formatting.Indented;

                c.Password = Encrypt.EncryptString(theData.Password, key);
                serializer.Serialize(file, c);
            }
        }
Example #2
0
        static public ClassSavedSettings Load(bool doNotLoad = false)
        {
            ClassSavedSettings theData;

            // Load the Profiles for diagnostics
            if (File.Exists(theFilename) &&
                doNotLoad == false)
            {
                try
                {
                    using (StreamReader file = File.OpenText(theFilename))
                    {
                        JsonSerializer serializer = new JsonSerializer();
                        theData = (ClassSavedSettings)serializer.Deserialize(file, typeof(ClassSavedSettings));

                        if (theData != null)
                        {
                            theData.Password = Encrypt.DecryptString(theData.Password, key);
                        }
                        else
                        {
                            theData = new ClassSavedSettings();
                        }
                    }
                }
                catch
                {
                    theData = new ClassSavedSettings();
                }
            }
            else
            {
                theData = new ClassSavedSettings();
            }

            if (theData == null)
            {
                theData = new ClassSavedSettings();
            }

            return(theData);
        }
Example #3
0
        private static int Main(string[] args)
        {
            HttpClientHandler clientHandler = new HttpClientHandler();

            clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return(true); };
            int                brightnessValueForTestPattern = 10;
            ReturnValue        returnValue = new ReturnValue();
            ClassSavedSettings theData     = ClassSavedSettings.Load();

            // Pass the handler to httpclient(from you are calling api)
            HttpClient    client = new HttpClient(clientHandler);
            SM_Monitoring theMonitoringData;


            Parser.Default.ParseArguments <Options>(args)
            .WithParsed <Options>(o =>
            {
                RepeatedOptionErrorForvever:

                if (o.version)
                {
                    Console.WriteLine("Version: " + System.Reflection.Assembly.GetExecutingAssembly());
                }

                if (o.address != null)
                {
                    theData.Address = o.address;
                }

                if (o.port != null)
                {
                    theData.Port = o.port;
                }

                if (o.username != null)
                {
                    theData.Username = o.username;
                }

                if (o.password != null)
                {
                    theData.Password = o.password;
                }

                if (o.brightnessValue != null)
                {
                    brightnessValueForTestPattern = (int)o.brightnessValue;
                }

                if (brightnessValueForTestPattern == -1)
                {
                    brightnessValueForTestPattern = 10;
                }

                if (o.saveCredentials)
                {
                    Console.WriteLine("Saving settings");
                    ClassSavedSettings.Save(theData);
                }

                theMonitoringData = new SM_Monitoring(o.address);

                // I some commands require the monitoring data override it.
                if (o.listDisplayIds == true || o.saveJson == true || o.DisplayList != null)
                {
                    o.SuppressMonitoringDataFetch = false;
                }

                var jsonRawDataAsString = "";
                var byteData            = Encoding.UTF8.GetBytes($"{theData.Username}:{theData.Password}");
                string _authorization   = Convert.ToBase64String(byteData);

                client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", _authorization);
                client.Timeout = TimeSpan.FromSeconds(15);

                //Console.WriteLine($"{theData.Username}:{theData.Password}");

                bool connectFailed = false;

                if (o.SuppressMonitoringDataFetch == false)
                {
                    string uRL_Monitoring = "https://" + theData.Address + ":" + theData.Port + "/api/monitoring";

                    if (!o.quiteMode)
                    {
                        Console.WriteLine("Retrieving Monitoring information...");
                        Console.WriteLine("URL:'" + uRL_Monitoring + "'");
                    }

                    try
                    {
                        HttpResponseMessage response = client.GetAsync(uRL_Monitoring).Result;

                        var myjsonRawDataAsString = response.Content.ReadAsStringAsync().Result;
                        jsonRawDataAsString       = (string)myjsonRawDataAsString.Clone();

                        PV_NuGet_SystemMatrixAPIAccessCore.SM_API.Root theJsonConvertedToObject = JsonConvert.DeserializeObject <PV_NuGet_SystemMatrixAPIAccessCore.SM_API.Root>(myjsonRawDataAsString);
                        string jsonFormatted = JsonConvert.SerializeObject(theJsonConvertedToObject, Newtonsoft.Json.Formatting.Indented);

                        if (o.saveJson)
                        {
                            if (!o.quiteMode)
                            {
                                Console.WriteLine("Saving Monitoring.json");
                            }

                            using (System.IO.StreamWriter file = new StreamWriter(@"Monitoring.json"))
                            {
                                file.Write(jsonFormatted);
                            }
                        }


                        SM_API_Call theSM_API = new SM_API_Call();
                        theMonitoringData.JsonPayload.httpCallResultDetail        = "OK";
                        theMonitoringData.JsonPayload.httpCallJsonRaw             = myjsonRawDataAsString;
                        theMonitoringData.JsonPayload.httpCallJsonResultFormatted = jsonFormatted;
                        theMonitoringData.JsonPayload.httpCallCommunicationError  = false;
                        theMonitoringData.JsonPayload.SetData(theJsonConvertedToObject);
                        theMonitoringData.Ingest_SystemMatrix_JSON_Information(theMonitoringData.JsonPayload, "NA", false);

                        if (o.listDisplayIds)
                        {
                            Console.WriteLine("Displays");
                            foreach (var i in theMonitoringData.theData.Displays)
                            {
                                Console.WriteLine("Display: '" + i.Name + "' GUID: " + i.Id);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        connectFailed = true;

                        returnValue.theValue = -2;
                        Console.WriteLine("Connection Failed!");
                        Console.WriteLine("Exception:");
                        Console.WriteLine(e.Message);
                        Console.WriteLine("Note: --help for how to use this app");
                    }
                }


                List <displayToSendTo> theDisplayToSendTo = new List <displayToSendTo>();

                foreach (string d in o.DisplayList)
                {
                    bool isGuid       = false;
                    Guid passedInGuid = Guid.Empty;

                    if (Guid.TryParse(d, out passedInGuid))
                    {
                        isGuid = true;
                    }

                    bool foundIt = false;
                    foreach (var i in theMonitoringData.theData.Displays)
                    {
                        if (isGuid)
                        {
                            if (i.Id == passedInGuid)
                            {
                                theDisplayToSendTo.Add(new displayToSendTo(i.Id, i.Name));
                                foundIt = true;
                            }
                        }
                        else
                        {
                            if (d == i.Name)
                            {
                                theDisplayToSendTo.Add(new displayToSendTo(i.Id, i.Name));
                                foundIt = true;
                            }
                        }
                    }

                    if (foundIt == false)
                    {
                        returnValue.theValue = -4;
                        Console.WriteLine("Error: The selected display [" + d + "] ID is not found.");
                        return;
                    }
                }


                // If a monitoring get faile don't bother to do anything else.
                if (connectFailed == false)
                {
                    if (o.brightnessValue != null)
                    {
                        string uRL_SetBrightness = null;
                        bool validBrightness     = false;

                        if (o.brightnessValue >= 0 && o.brightnessValue <= 100)
                        {
                            validBrightness = true;
                        }
                        else
                        {
                            returnValue.theValue = -5;

                            Console.WriteLine("Error: Test Pattern brightness value not valid: " + o.brightnessValue.ToString());
                        }

                        if (validBrightness == true)
                        {
                            ClassSystemMatrixPayloadOutputLevels payload = new ClassSystemMatrixPayloadOutputLevels();
                            if (theDisplayToSendTo.Count == 0)      // Global
                            {
                                if (o.brightnessValue == -1)        // Set to default
                                {
                                    returnValue.theValue = PostIt("[ALL]", client, "https://" + theData.Address + ":" + theData.Port + "/api/global/commands/defaultbrightness", o.quiteMode);
                                }
                                else
                                {
                                    returnValue.theValue = PostIt("[ALL]", client, "https://" + theData.Address + ":" + theData.Port + "/api/global/commands/brightness/" + o.brightnessValue.ToString(), o.quiteMode);
                                }
                            }
                            else        // Display ID selected
                            {
                                foreach (displayToSendTo g in theDisplayToSendTo)
                                {
                                    if (o.brightnessValue == -1)        // Set to default
                                    {
                                        returnValue.theValue = PostIt(g.displayName, client, uRL_SetBrightness = "https://" + theData.Address + ":" + theData.Port + "/api/displays/" + g.displayGuid + "/commands/defaultbrightness", o.quiteMode);
                                    }
                                    else
                                    {
                                        returnValue.theValue = PostIt(g.displayName, client, uRL_SetBrightness = "https://" + theData.Address + ":" + theData.Port + "/api/displays/" + g.displayGuid + "/commands/brightness/" + o.brightnessValue.ToString(), o.quiteMode);
                                    }
                                }
                            }
                        }
                    }


                    if (o.powerOff == true)
                    {
                        if (theDisplayToSendTo.Count == 0)        // Global
                        {
                            returnValue.theValue = PostIt("[ALL]", client, "https://" + theData.Address + ":" + theData.Port + "/api/global/commands/powerOff", o.quiteMode);
                        }
                        else
                        {
                            foreach (displayToSendTo g in theDisplayToSendTo)
                            {
                                returnValue.theValue = PostIt(g.displayName, client, "https://" + theData.Address + ":" + theData.Port + "/api/displays/" + g.displayGuid + "/commands/powerOff", o.quiteMode);
                            }
                        }
                    }

                    if (o.powerOn == true)
                    {
                        if (theDisplayToSendTo.Count == 0)        // Global
                        {
                            returnValue.theValue = PostIt("[ALL]", client, "https://" + theData.Address + ":" + theData.Port + "/api/global/commands/powerOn", o.quiteMode);
                        }
                        else
                        {
                            foreach (displayToSendTo g in theDisplayToSendTo)
                            {
                                returnValue.theValue = PostIt(g.displayName, client, "https://" + theData.Address + ":" + theData.Port + "/api/displays/" + g.displayGuid + "/commands/powerOn", o.quiteMode);
                            }
                        }
                    }

                    if (o.powerCycle == true)
                    {
                        if (theDisplayToSendTo.Count == 0)        // Global
                        {
                            returnValue.theValue = PostIt("[ALL]", client, "https://" + theData.Address + ":" + theData.Port + "/api/global/commands/powerCycle", o.quiteMode);
                        }
                        else
                        {
                            foreach (displayToSendTo g in theDisplayToSendTo)
                            {
                                returnValue.theValue = PostIt(g.displayName, client, "https://" + theData.Address + ":" + theData.Port + "/api/displays/" + g.displayGuid + "/commands/powerCycle", o.quiteMode);
                            }
                        }
                    }

                    if (o.enableOutput == true)
                    {
                        if (theDisplayToSendTo.Count == 0)        // Global
                        {
                            returnValue.theValue = PostIt("[ALL]", client, "https://" + theData.Address + ":" + theData.Port + "/api/global/commands/enableOutput", o.quiteMode);
                        }
                        else
                        {
                            foreach (displayToSendTo g in theDisplayToSendTo)
                            {
                                returnValue.theValue = PostIt(g.displayName, client, "https://" + theData.Address + ":" + theData.Port + "/api/displays/" + g.displayGuid + "/commands/enableOutput", o.quiteMode);
                            }
                        }
                    }

                    if (o.disableOutput == true)
                    {
                        if (theDisplayToSendTo.Count == 0)        // Global
                        {
                            returnValue.theValue = PostIt("[ALL]", client, "https://" + theData.Address + ":" + theData.Port + "/api/global/commands/disableOutput", o.quiteMode);
                        }
                        else
                        {
                            foreach (displayToSendTo g in theDisplayToSendTo)
                            {
                                returnValue.theValue = PostIt(g.displayName, client, "https://" + theData.Address + ":" + theData.Port + "/api/displays/" + g.displayGuid + "/commands/disableOutput", o.quiteMode);
                            }
                        }
                    }

                    if (o.refreshHeader == true)
                    {
                        if (theDisplayToSendTo.Count == 0)        // Global
                        {
                            returnValue.theValue = PostIt("[ALL]", client, "https://" + theData.Address + ":" + theData.Port + "/api/global/commands/refreshHeader", o.quiteMode);
                        }
                        else
                        {
                            foreach (displayToSendTo g in theDisplayToSendTo)
                            {
                                returnValue.theValue = PostIt(g.displayName, client, "https://" + theData.Address + ":" + theData.Port + "/api/displays/" + g.displayGuid + "/commands/sendHeader", o.quiteMode);
                            }
                        }
                    }

                    List <string> TestpatternOptions = new List <string>();
                    if (o.testPattern != null)
                    {
                        foreach (object s in o.testPattern)
                        {
                            TestpatternOptions.Add((string)s);
                            Console.WriteLine("Parameter:" + (string)s);
                        }
                    }


                    if (o.testPatternStop)
                    {
                        ClassSystemMatrixPayloadTestPattern thePatternPayload = new ClassSystemMatrixPayloadTestPattern();
                        thePatternPayload.Action     = SystemMatrixCommandPayload_Action.Stop;
                        thePatternPayload.Brightness = brightnessValueForTestPattern;

                        var postPayload = new StringContent(thePatternPayload.Json(), Encoding.UTF8, "application/json");

                        if (theDisplayToSendTo.Count == 0)        // Global
                        {
                            foreach (var i in theMonitoringData.theData.Displays)
                            {
                                returnValue.theValue = PostIt(i.Name, client, "https://" + theData.Address + ":" + theData.Port + "/api/displays/" + i.Id + "/commands/testpatterns", o.quiteMode, postPayload);
                            }
                        }
                        else
                        {
                            foreach (displayToSendTo g in theDisplayToSendTo)
                            {
                                returnValue.theValue = PostIt(g.displayName, client, "https://" + theData.Address + ":" + theData.Port + "/api/displays/" + g.displayGuid + "/commands/testpatterns", o.quiteMode, postPayload);
                            }
                        }
                    }


                    if (TestpatternOptions.Count > 0)
                    {
                        ClassSystemMatrixPayloadTestPattern thePatternPayload = new ClassSystemMatrixPayloadTestPattern();
                        thePatternPayload.Action     = SystemMatrixCommandPayload_Action.Start;
                        thePatternPayload.Brightness = brightnessValueForTestPattern;
                        bool validTestPattern        = true;
                        bool validBrightness         = false;


                        if (TestpatternOptions.Count == 2)
                        {
                            switch (TestpatternOptions[0].ToLower())
                            {
                            case "solidred": thePatternPayload.TestPattern = SystemMatrixCommandPayload_TestPattern.SolidRed; break;

                            case "solidgreen": thePatternPayload.TestPattern = SystemMatrixCommandPayload_TestPattern.SolidGreen; break;

                            case "solidblue": thePatternPayload.TestPattern = SystemMatrixCommandPayload_TestPattern.SolidBlue; break;

                            case "solidwhite": thePatternPayload.TestPattern = SystemMatrixCommandPayload_TestPattern.SolidWhite; break;

                            case "cyclecolors": thePatternPayload.TestPattern = SystemMatrixCommandPayload_TestPattern.CycleColors; break;

                            case "linesvertical": thePatternPayload.TestPattern = SystemMatrixCommandPayload_TestPattern.LinesVertical; break;

                            case "linesdiagonal": thePatternPayload.TestPattern = SystemMatrixCommandPayload_TestPattern.LinesDiagonal; break;

                            case "gridcolors": thePatternPayload.TestPattern = SystemMatrixCommandPayload_TestPattern.GridColors; break;

                            case "gridnumbered": thePatternPayload.TestPattern = SystemMatrixCommandPayload_TestPattern.GridNumbered; break;

                            default:
                                {
                                    validTestPattern     = false;
                                    returnValue.theValue = -5;
                                    Console.WriteLine("Error: Test pattern - not valid pattern: '" + o.testPattern + "'");
                                }; break;
                            }

                            try
                            {
                                thePatternPayload.Brightness = Convert.ToDouble(TestpatternOptions[1]);

                                if (thePatternPayload.Brightness >= 0 && thePatternPayload.Brightness <= 100)
                                {
                                    Console.WriteLine("Test pattern brightness: " + thePatternPayload.Brightness);
                                    validBrightness = true;
                                }
                                else
                                {
                                    returnValue.theValue = -5;
                                    Console.WriteLine("Error: Test Pattern brightness value not valid: " + thePatternPayload.Brightness.ToString());
                                }
                            }
                            catch
                            {
                                returnValue.theValue = -5;
                                Console.WriteLine("Error: Test pattern Brightness not correct.");
                            }


                            if (validTestPattern == true && validBrightness == true)
                            {
                                var postPayload = new StringContent(thePatternPayload.Json(), Encoding.UTF8, "application/json");

                                if (theDisplayToSendTo.Count == 0)        // Global
                                {
                                    foreach (var i in theMonitoringData.theData.Displays)
                                    {
                                        returnValue.theValue = PostIt(i.Name, client, "https://" + theData.Address + ":" + theData.Port + "/api/displays/" + i.Id + "/commands/testpatterns", o.quiteMode, postPayload);
                                    }
                                }
                                else
                                {
                                    foreach (displayToSendTo g in theDisplayToSendTo)
                                    {
                                        returnValue.theValue = PostIt(g.displayName, client, "https://" + theData.Address + ":" + theData.Port + "/api/displays/" + g.displayGuid + "/commands/testpatterns", o.quiteMode, postPayload);
                                    }
                                }
                            }
                        }
                        else
                        {
                            returnValue.theValue = -1;
                            Console.WriteLine("Test Pattern missing required parameters. Required [pattern] [brightness] # Parameters provided:" + TestpatternOptions.Count);
                        }
                    }
                }
            });

            return(returnValue.theValue);
        }