Ejemplo n.º 1
0
        /// <summary>
        /// Chage output relay state
        /// </summary>
        /// <param name="PortNumber">Relay port number, int [1..9]</param>
        /// <param name="bPortValue">Port value flase = 0, true = 1</param>
        /// <returns>Returns true in case of success</returns>
        public bool setOutputStatus(string PortName, bool bPortValue)
        {
            tl.LogMessage("setOutputStatus", "Enter (" + PortName + "," + bPortValue + ")");

            //convert port value to int
            int intPortValue = (bPortValue ? 1 : 0);


            //return data
            bool ret = false;

            if (string.IsNullOrEmpty(ip_addr))
            {
                tl.LogMessage("setOutputStatus", "ERROR (ip_addr wasn't set)!");
                // report a problem with the port name
                ASCOM_ERROR_MESSAGE = "setOutputStatus(): no IP address was specified";
                throw new ASCOM.ValueNotSetException(ASCOM_ERROR_MESSAGE);
                //return ret;
            }

            string paramString = "channel=" + PortName + "&state=" + intPortValue;
            string siteipURL;

            if (debugFlag)
            {
                //FOR DEBUGGING
                siteipURL = "http://localhost/power/set.php";
            }
            else
            {
                // Vedrus style
                // http://192.168.2.199/relay.php
                // {"boris_pc":1,"boris_scope":0,"roman_pc":0,"roman_scope":0}
                siteipURL = "http://" + ip_addr + ":" + ip_port + "/relay.php";
            }
            tl.LogMessage("setOutputStatus", "Download url:" + siteipURL);
            tl.LogMessage("setOutputStatus", "Param String:" + paramString);


            // Send http query
            tlsem.LogMessage("setOutputStatus", "WaitOne");
            VedrusSemaphore.WaitOne(); // lock working with IP9212
            tlsem.LogMessage("setOutputStatus", "WaitOne passed");
            string      s      = "";
            MyWebClient client = new MyWebClient();

            try
            {
                s = client.UploadPOST(siteipURL, paramString);

                VedrusSemaphore.Release();//unlock ip9212 device for others
                tlsem.LogMessage("setOutputStatus", "Release");

                tl.LogMessage("setOutputStatus", "Download str:" + s);

                ret = true;
            }
            catch (Exception e)
            {
                VedrusSemaphore.Release();//unlock ip9212 device for others
                tlsem.LogMessage("setOutputStatus", "Release on WebException");

                ret = false;

                tl.LogMessage("setOutputStatus", "Error:" + e.Message);
                ASCOM_ERROR_MESSAGE = "setOutputStatus(" + PortName + "," + intPortValue + "): Couldn't reach network server";
                //throw new ASCOM.NotConnectedException(ASCOM_ERROR_MESSAGE);
                tl.LogMessage("setOutputStatus", "Exit by web error");
                return(ret);
            }

            // Reset cached read values
            lastOutputReadCheck = EXPIRED_CACHE;

            return(ret);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Get output relay status
        /// </summary>
        /// <returns>Returns int array [0..8] with status flags of each realya status. arr[0] is for read status (-1 for error, 1 for good read, 0 for smth else)</returns>
        public bool getOutputStatus()
        {
            tl.LogMessage("getOutputStatus", "Enter");

            // get the ip9212 settings from the profile
            //readSettings();

            if (string.IsNullOrEmpty(ip_addr))
            {
                tl.LogMessage("getOutputStatus", "ERROR (ip_addr wasn't set)!");
                // report a problem with the port name
                ASCOM_ERROR_MESSAGE = "getOutputStatus(): no IP address was specified";
                SWITCH_DATA_LIST.Clear();
                throw new ASCOM.ValueNotSetException(ASCOM_ERROR_MESSAGE);
                //return input_state_arr;
            }


            string siteipURL;

            if (debugFlag)
            {
                //FOR DEBUGGING
                siteipURL = "http://localhost/power/get.php";
            }
            else
            {
                // Vedrus style
                // http://192.168.2.199/power/get/
                // {"boris_pc":1,"boris_scope":0,"roman_pc":0,"roman_scope":0}
                siteipURL = "http://" + ip_addr + ":" + ip_port + "/power/get/";
            }
            tl.LogMessage("getOutputStatus", "Download url:" + siteipURL);


            // Send http query
            tlsem.LogMessage("getOutputStatus", "WaitOne");
            VedrusSemaphore.WaitOne(); // lock working with IP9212

            string      s      = "";
            MyWebClient client = new MyWebClient();

            try
            {
                Stream       data   = client.OpenRead(siteipURL);
                StreamReader reader = new StreamReader(data);
                s = reader.ReadToEnd();
                data.Close();
                reader.Close();

                VedrusSemaphore.Release();//unlock ip9212 device for others
                tlsem.LogMessage("getOutputStatus", "Release");

                //Bonus: checkconnection
                hardware_connected_flag = true;
                lastConnectedCheck      = DateTime.Now;

                tl.LogMessage("getOutputStatus", "Download str:" + s);
            }
            catch (Exception e)
            {
                VedrusSemaphore.Release();//unlock ip9212 device for others
                tlsem.LogMessage("getOutputStatus", "Release on WebException");

                //Bonus: checkconnection
                hardware_connected_flag = false;
                SWITCH_DATA_LIST.Clear();

                tl.LogMessage("getOutputStatus", "Error:" + e.Message);
                ASCOM_ERROR_MESSAGE = "getInputStatus(): Couldn't reach network server";
                //throw new ASCOM.NotConnectedException(ASCOM_ERROR_MESSAGE);
                Trace("> IP9212_harware.getOutputStatus(): exit by web error");
                tl.LogMessage("getOutputStatus", "Exit by web error");

                return(false); //error
            }

            // Parse data
            //{ "boris_pc":1,"boris_scope":0,"roman_pc":0,"roman_scope":0}
            try
            {
                //Deserialize into dictionary
                Dictionary <string, Int16> relaysRead = new Dictionary <string, Int16>();
                var jsSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                relaysRead = jsSerializer.Deserialize <Dictionary <string, Int16> >(s);

                //Read into LIST with SWITCH values
                List <switchDataRawClass> tempSwitchData = new List <switchDataRawClass>();

                SWITCH_DATA_LIST.Clear();
                foreach (KeyValuePair <string, Int16> rel in relaysRead)
                {
                    SWITCH_DATA_LIST.Add(new switchDataRawClass()
                    {
                        Name = rel.Key, Val = rel.Value
                    });
                }

                lastOutputReadCheck = DateTime.Now; //mark cache was renewed
                tl.LogMessage("getOutputStatus", "Data was read");
            }
            catch (Exception ex)
            {
                SWITCH_DATA_LIST.Clear();
                tl.LogMessage("getOutputStatus", "ERROR parsing data (Exception: " + ex.Message + ")!");
                tl.LogMessage("getOutputStatus", "exit by parse error");
                return(false); //error
            }
            return(true);
        }