Esempio n. 1
0
        /// <summary>
        /// Establish connection to server
        /// </summary>
        /// <returns>true if successful</returns>
        public bool EstablishConnection()
        {
            bool res = false;

            if (ProgramSocket == null)
            {
                //Connect
                string output = SocketServerClass.ConnectToServer(IPAddress.Parse("127.0.0.1"), ServerPort, out ProgramSocket, out Error);
                if (Error >= 0)
                {
                    Thread.Sleep(1000);

                    //Read response
                    string output2 = SocketServerClass.ReceiveFromServer(ProgramSocket, out Error);

                    //Parse response
                    HandleServerResponse(output2);

                    res = true;
                }
            }
            else
            {
                Logging.AddLog(LogPrefix + " already connected", LogLevel.Activity);
                res = true;
            }
            return(res);
        }
Esempio n. 2
0
        public string ConnectEquipment_old()
        {
            string message = @"{""method"": ""set_connected"", ""params"": [true], ""id"": 1}";

            message = message + "\r\n";
            string output = SocketServerClass.ConnectToServerAndSendMessage(IPAddress.Parse("127.0.0.1"), ServerPort, message, out Error);
            string attribs = "", id = "";

            Parse_PHD_ResponseString(output, out attribs, out id);
            if (!output.Contains("{\"jsonrpc\":\"2.0\",\"result\":0,\"id\":1}\r\n"))
            {
                Error = -2;
            }
            //good response: {"jsonrpc":"2.0","result":0,"id":2}
            //bad response:  {"jsonrpc":"2.0","error":{"code":1,"message":"cannot connect equipment when Connect Equipment dialog is open"},"id":1}
            ErrorSt = output;
            //Error = 0;
            if (Error < 0)
            {
                Logging.AddLog("PHD2 equipment connection error", LogLevel.Important, Highlight.Error);
                Logging.AddLog("Error: " + ErrorSt, LogLevel.Chat, Highlight.Error);
            }
            else
            {
                Logging.AddLog("PHD2 equipment connected", LogLevel.Activity);
            }
            return(output);
        }
Esempio n. 3
0
        /// <summary>
        /// Send commnad - just send, no read
        /// </summary>
        /// <returns></returns>
        public bool SendCommand2(string message, out string str_result)
        {
            bool   res        = false;
            string outMessage = "";

            //if wasn't connected earlier, connect again
            if (ProgramSocket == null)
            {
                EstablishConnection();
            }


            //Send command
            Logging.AddLog("PHD2 sending comand: " + message, LogLevel.Debug);
            int resSrv = SocketServerClass.SendToServer2(ProgramSocket, message, out outMessage);

            if (resSrv >= 0)
            {
                res = true;
            }
            else
            {
                res = false;
            }

            str_result = outMessage;
            return(res);
        }
Esempio n. 4
0
        public string GET_TelescopePos()
        {
            string output = SocketServerClass.ConnectToServerAndSendMessage(IPAddress.Parse("127.0.0.1"), ServerPort, "GETSCOPERADEC\r\n", out Error);

            ErrorSt = output;
            Logging.AddLog("GET_TelescopePos: " + output, LogLevel.Debug, Highlight.Error);

            return(output);
        }
Esempio n. 5
0
        /// <summary>
        /// Send commnad
        /// </summary>
        /// <returns></returns>
        public bool SendCommand(string message, out string result)
        {
            bool res = false;

            //if wasn't connected earlier, connect again
            if (ProgramSocket == null)
            {
                EstablishConnection();
            }

            Logging.AddLog("PHD2 sending comand: " + message, LogLevel.Debug);

            //Send command
            string output = SocketServerClass.SendToServer(ProgramSocket, message, out Error);

            if (Error >= 0)
            {
                Thread.Sleep(200);

                //Read response
                string output2 = SocketServerClass.ReceiveFromServer(ProgramSocket, out Error);
                Logging.AddLog("PHD2_SendCommand: server response = " + output2, LogLevel.Debug, Highlight.Error);

                //Parse response
                Handle_PHD_Response(output2, out result);

                //Check
                if (!LastCommand_Result)
                {
                    Error   = -1;
                    ErrorSt = LastCommand_Message;
                    Logging.AddLog("PHD2 command failed: " + LastCommand_Message + "]", LogLevel.Debug, Highlight.Error);
                }
                else
                {
                    Error   = 0;
                    ErrorSt = "";
                    Logging.AddLog("PHD2 command succesfull", LogLevel.Debug);
                    res = true;
                }
            }
            else
            {
                result = "";
                Logging.AddLog("PHD2 send command error: " + ErrorSt, LogLevel.Debug, Highlight.Error);
            }

            return(res);
        }
Esempio n. 6
0
        /// <summary>
        /// Run this method to check if there are incoming PHD messages and parse them
        /// </summary>
        /// <returns>true if new events</returns>
        public bool CheckProgramEvents()
        {
            bool res = false;
            //Read response
            string output = SocketServerClass.ReceiveFromServer(ProgramSocket, out Error);

            //Parse response
            if (output != null)
            {
                Handle_PHD_Response(output);
                res = true;
            }

            return(res);
        }
Esempio n. 7
0
        public string PerformCommand(string CommandName)
        {
            string output = SocketServerClass.ConnectToServerAndSendMessage(IPAddress.Parse("127.0.0.1"), ServerPort, CommandName + "\r\n", out Error);

            ErrorSt = output;
            //Error = 0;
            if (Error < 0)
            {
                Logging.AddLog("Error for CdC command [" + CommandName + "]", LogLevel.Important, Highlight.Error);
            }
            else
            {
                Logging.AddLog("CdC command [" + CommandName + "] ok", LogLevel.Activity);
            }
            return(output);
        }
Esempio n. 8
0
        public string ConnectTelescope_old(string ChartName = "")
        {
            //Select CHART or use default (i thing it will always be Chart_1

            string output = SocketServerClass.ConnectToServerAndSendMessage(IPAddress.Parse("127.0.0.1"), ServerPort, "CONNECTTELESCOPE\r\n", out Error);

            ErrorSt = output;
            //Error = 0;
            if (Error < 0)
            {
                Logging.AddLog("Telescope connection error in CdC", LogLevel.Important, Highlight.Error);
            }
            else
            {
                Logging.AddLog("Telescope connected in CdC", LogLevel.Activity);
            }
            return(output);
        }
Esempio n. 9
0
        /// <summary>
        /// Send commnad through Socket Interface
        /// </summary>
        /// <returns></returns>
        public bool SendCommand(string CommandString, out string result)
        {
            bool res = false;

            //if wasn't connected earlier, connect again
            if (ProgramSocket == null)
            {
                if (!EstablishConnection())
                {
                    Logging.AddLog("Failed to connect to " + LogPrefix, LogLevel.Activity, Highlight.Error);
                    result = "";
                    return(false);
                }
            }

            Logging.AddLog(LogPrefix + " sending comand: " + CommandString, LogLevel.Debug);

            //////////////////
            //Send command
            //////////////////
            string output = SocketServerClass.SendToServer(ProgramSocket, CommandString, out Error);

            //Release socket
            if (ProgramSocket != null && Error != 0)
            {
                ProgramSocket.Shutdown(SocketShutdown.Both);
                ProgramSocket.Close();
                ProgramSocket = null;
            }

            if (Error >= 0)
            {
                //Wait a bit
                Thread.Sleep(300);

                //////////////////
                //Read response
                //////////////////
                string output2 = SocketServerClass.ReceiveFromServer(ProgramSocket, out Error);

                //Check
                if (output2 == null || output2 == String.Empty)
                {
                    Error   = -1;
                    ErrorSt = LastCommand_Message;
                    result  = "";
                    Logging.AddLog(LogPrefix + " command failed: " + LastCommand_Message + "]", LogLevel.Debug, Highlight.Error);
                }
                else
                {
                    Error   = 0;
                    ErrorSt = "";
                    result  = output2;
                    Logging.AddLog(LogPrefix + " command succesfull", LogLevel.Debug);
                    res = true;
                }
            }
            else
            {
                result = "";
                Logging.AddLog(LogPrefix + " send command error: " + ErrorSt, LogLevel.Debug, Highlight.Error);
            }

            return(res);
        }
Esempio n. 10
0
        /// <summary>
        /// get connect equipment status
        /// </summary>
        /// <returns></returns>
        public void CMD_GetConnectEquipmentStatus()
        {
            if (!this.IsRunning())
            {
                return;
            }

            //calc id
            int    CMD_ID = 80;
            Random rand   = new Random();
            int    temp   = rand.Next(999);
            int    id     = CMD_ID * 1000 + temp;

            //make message
            string message = @"{""method"": ""get_connected"", ""id"": " + id + "}" + "\r\n";

            //send message to PHD2
            string st_result = "";
            bool   resSend   = SendCommand2(message, out st_result);

            if (!resSend)
            {
                Logging.AddLog("PHD2 CMD_GetConnectEquipmentStatus send message error: [" + resSend + "]" + st_result, LogLevel.Debug, Highlight.Error);
                EquipmentConnected = false;
                return;
            }

            //wait
            Thread.Sleep(200);

            //Read response
            int    errorCode          = -999;
            string output_from_server = SocketServerClass.ReceiveFromServer(ProgramSocket, out errorCode);

            if (errorCode < 0)
            {
                Logging.AddLog("PHD2 CMD_GetConnectEquipmentStatus receive message error: [" + resSend + "]" + st_result, LogLevel.Debug, Highlight.Error);
                EquipmentConnected = false;
                return;
            }
            Logging.AddLog("PHD2_SendCommand: server response = " + output_from_server, LogLevel.Debug, Highlight.Error);

            //Parse response
            Handle_PHD_Response(output_from_server, out st_result, id);

            //Check
            if (st_result == "true")
            {
                Error   = 0;
                ErrorSt = "";
                Logging.AddLog("PHD2 equipment connected", LogLevel.Debug);
            }
            else
            {
                Error   = -1;
                ErrorSt = LastCommand_Message;
                Logging.AddLog("PHD2 equipment not connected or error: " + LastCommand_Message + "]", LogLevel.Debug, Highlight.Error);
            }

            this.EquipmentConnected = (Error == 0);

            return;
        }
Esempio n. 11
0
        /// <summary>
        /// Main form load event - startup actions take place here
        /// </summary>
        private void Form1_Load(object sender, EventArgs e)
        {
            //DefBackColor
            DefBackColor          = chkMaxim.BackColor;
            DefBackColorTextBoxes = txtShortAlt.BackColor;

            //Load config
            ConfigManagement.Load();

            //Load parameters
            LoadParams();
            //Update interface for current Settings values
            //IQP_UpdateSettingsDialogFields();


            //Init programs objects using loaded settings
            ObsControl.InitProgramsObj();

            //Load parameters after all objects was initialized
            LoadOtherParamsFromXML();


            //Dump log, because interface may hang wating for connection
            Logging.DumpToFile();


            //Connect Devices, which are general adapters (no need to power or control something)
            try
            {
                ObsControl.ASCOMSwitch.Connect = true;
                ObsControl.ASCOMSwitch.CheckPowerDeviceStatus_async();
            }
            catch (Exception ex)
            {
                Logging.AddLog("Error connecting Switch on startup [" + ex.Message + "]", LogLevel.Important, Highlight.Error);
                Logging.AddLog("Exception details: " + ex.ToString(), LogLevel.Debug, Highlight.Error);
            }

            try
            {
                ObsControl.ASCOMDome.Connect = true;
            }
            catch (Exception ex)
            {
                Logging.AddLog("Error connecting Dome on startup [" + ex.Message + "]", LogLevel.Important, Highlight.Error);
                Logging.AddLog("Exception details: " + ex.ToString(), LogLevel.Debug, Highlight.Error);
            }

            //Update visual interface statuses
            UpdateStatusbarASCOMStatus();
            UpdatePowerButtonsStatus();

            //init graphic elements
            ROOF_startPos = rectRoof.Location;
            //Update visual Roof Status
            UpdateRoofStatus();

            //Start tcp server
            SocketServer = new SocketServerClass(this);
            toolStripStatus_Connection.Text = "CONNECTIONS: 0";
            if (true)
            {
                backgroundWorker_SocketServer.RunWorkerAsync();
                toolStripStatus_Connection.ForeColor = Color.Black;
            }
            else
            {
                toolStripStatus_Connection.ForeColor = Color.Gray;
            }

            //init vars
            //DrawTelescopeV(panelTelescope);

            //Init versiondata static class
            //Display about information
            VersionData.initVersionData();
            LoadAboutData();

            //Init Log DropDown box
            foreach (LogLevel C in Enum.GetValues(typeof(LogLevel)))
            {
                toolStripDropDownLogLevel.DropDownItems.Add(Enum.GetName(typeof(LogLevel), C));
            }
            toolStripDropDownLogLevel.Text = Enum.GetName(typeof(LogLevel), LogLevel.Activity);

            //Fill in Combobox Boltwood
            comboBoxRainFlag.DataSource = Enum.GetNames(typeof(Enum_RainFlag));
            comboBoxWetFlag.DataSource  = Enum.GetNames(typeof(Enum_WetFlag));

            comboBoxCloudCond.DataSource    = Enum.GetNames(typeof(Enum_CloudCond));
            comboBoxWindCond.DataSource     = Enum.GetNames(typeof(Enum_WindCond));
            comboBoxRainCond.DataSource     = Enum.GetNames(typeof(Enum_RainCond));
            comboBoxDaylightCond.DataSource = Enum.GetNames(typeof(Enum_DayCond));

            comboBoxRoofCloseFlag.DataSource = Enum.GetNames(typeof(Enum_RoofFlag));
            comboBoxAlertFlag.DataSource     = Enum.GetNames(typeof(Enum_AlertFlag));

            //Run all timers at the end
            mainTimer_Short.Enabled    = true;
            mainTimer_Long.Enabled     = true;
            mainTimer_VeryLong.Enabled = true;
            logRefreshTimer.Enabled    = true;


            weatherSmallChart.Series[0].XValueType = ChartValueType.DateTime;
            weatherSmallChart.ChartAreas["ChartArea1"].AxisX.LabelStyle.Format = "HH:mm";

            foreach (Series Sr in chartWT.Series)
            {
                Sr.XValueType = ChartValueType.DateTime;
            }
            foreach (ChartArea CA in chartWT.ChartAreas)
            {
                CA.AxisX.LabelStyle.Format = "HH:mm";
            }


            //FORM APPEARENCE
            Form_Normal_Width = this.Width;
            borderWidth       = (this.Width - this.ClientSize.Width) / 2;
            titleBarHeight    = this.Height - this.ClientSize.Height - 2 * borderWidth;
            statusBarHeight   = statusBar.Height;
            prevX             = this.Location.X;
            prevY             = this.Location.Y;
            Form_SwitchTo_Maximum_Mode();


            // TEST COMMAND LINE PARAMETERS
            bool   autostart    = false;
            string comport_over = string.Empty;

            AuxilaryProc.CheckStartParams(out autostart, out comport_over);


            if (autostart)
            {
                this.btnStartAll.PerformClick();
            }
        }