Ejemplo 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);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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;
        }