Exemplo n.º 1
0
        static void Main(string[] args)
        {
            debugFlag = true;
            Console.WriteLine("Litetouch 5000LC CCU Time Update Utility\n");
            if (args.Length == 0)
            {
                Console.WriteLine("This utility updates the time on a Litetouch 5000LC CCU.\n");

                Console.WriteLine("XXX.XXX.XXX.XXX = First argument is the Target CCU Host Address (IPv4 Address, FQDN, etc)");
                Console.WriteLine("-c = Use Computer Clock");
                Console.WriteLine("-i = Use Internet Time Service (time.nist.gov) (Default)");
                Console.WriteLine("-z = Check CCU Clock only. (No Update)");

                Console.WriteLine("Example Usage: \n\tLitouch5000LCTimeUpdate 192.168.1.10 -i\n\tUpdates the CCU at 192.168.1.10 with time.nist.gov");
            }
            else
            {
                //First argument should be a network or web address
                //Let's attempt to gather that
                string targetCCUaddress = getIPAddress(args[0]);
                if (debugFlag)
                {
                    Console.WriteLine("Target CCU:" + targetCCUaddress);
                }
                //If there is a second argument, it should specify the update type
                bool noUpdate       = false;
                bool internetUpdate = true;
                if (args.Length >= 2)
                {
                    switch (args[1])
                    {
                    case "-c":
                    {
                        internetUpdate = false;
                        break;
                    }

                    case "-i":
                    {
                        internetUpdate = true;
                        break;
                    }

                    case "-z":
                    {
                        noUpdate = true;
                        break;
                    }
                    }
                }
                //We ignore further arguments


                //Now we grab the CCU's reported time
                AsynchronousClient targetCCU = new AsynchronousClient();
                targetCCU.StartClient(targetCCUaddress, 10001);     //Default Port for Litetouch Control = 10001

                //Send "GET CLOCK" Command to CCU
                //Format = R,DGCLK
                string commandString  = "R,DGCLK\r";
                string responseString = "";
                targetCCU.SendCommand(commandString);
                responseString = targetCCU.ReceiveResponse();
                if (debugFlag)
                {
                    Console.WriteLine("Command Sent: " + commandString);
                    Console.WriteLine("Response Received: " + responseString);
                }


                //DateTime oldTime = getCCUTime(targetCCUaddress);

                DateTime oldTime = convertCCUTimeResponse(responseString);
                Console.WriteLine("Old CCU Time:" + oldTime.ToString("yyyy-MM-dd HH:mm:ss"));


                //If we update, we get time from our sources, and then push to CCU
                DateTime newTime = DateTime.Parse("1900-01-01");  //Pre-assigned clearly wrong date, in case anything goes wrong
                if (!noUpdate)
                {
                    if (internetUpdate)
                    {
                        newTime = getInternetTime();
                        if (debugFlag)
                        {
                            Console.WriteLine("NTP Server Time:" + newTime.ToString("yyyy-MM-dd HH:mm:ss"));
                        }
                    }
                    else
                    {
                        newTime = getComputerTime();
                        if (debugFlag)
                        {
                            Console.WriteLine("Computer Clock Time:" + newTime.ToString("yyyy-MM-dd HH:mm:ss"));
                        }
                    }
                    commandString = convertNewCCUTime(newTime);

                    //Send "SET CLOCK" Command to CCU
                    //Format = R,DSCLK,yyyymmddhhmmss
                    targetCCU.SendCommand(commandString);
                    if (debugFlag)
                    {
                        Console.WriteLine("Time Update Command Sent:" + commandString);
                    }

                    //Send "GET CLOCK" Command to CCU
                    //Format = R,DGCLK
                    commandString  = "R,DGCLK\r";
                    responseString = "";
                    targetCCU.SendCommand(commandString);
                    responseString = targetCCU.ReceiveResponse();
                    if (debugFlag)
                    {
                        Console.WriteLine("Command Sent: " + commandString);
                        Console.WriteLine("Response Received: " + responseString);
                    }

                    DateTime updatedTime = convertCCUTimeResponse(responseString);
                    Console.WriteLine("Old CCU Time:" + oldTime.ToString("yyyy-MM-dd HH:mm:ss"));

                    //Calculate the difference between the times. If they are less than a 10s apart, we will accept it
                    TimeSpan duration = updatedTime - newTime;
                    Console.WriteLine("Old CCU Time: " + oldTime);
                    Console.WriteLine("Updated Time: " + newTime);
                    Console.WriteLine("CCU Time: " + updatedTime);
                    Console.WriteLine("Difference:" + duration.TotalSeconds + " seconds");
                    if (duration.TotalSeconds < 10)
                    {
                        Console.WriteLine("Success: " + oldTime);
                    }
                    else
                    {
                        Console.WriteLine("Something went wrong.");
                    }
                }
                targetCCU.StopClient();
                Console.WriteLine("Finished");
            }

            if (debugFlag)
            {
                Console.WriteLine("End of Program");
            }
        }