/// <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 int[] getOutputStatus() { tl.LogMessage("Switch_getOutputStatus", "Enter"); // get the ip9212 settings from the profile //readSettings(); //return data int[] ipdata = new int[1] { 0 }; if (string.IsNullOrEmpty(ip_addr)) { ipdata[0] = -1; tl.LogMessage("Switch_getOutputStatus", "ERROR (ip_addr wasn't set)!"); // report a problem with the port name ASCOM_ERROR_MESSAGE = "getOutputStatus(): no IP address was specified"; throw new ASCOM.ValueNotSetException(ASCOM_ERROR_MESSAGE); //return input_state_arr; } string siteipURL; siteipURL = "http://" + ip_login + ":" + ip_pass + "@" + ip_addr + ":" + ip_port + "/set.cmd?cmd=getpower"; // new style siteipURL = "http://" + ip_addr + ":" + ip_port + "/Set.cmd?user="******"+pass="******"CMD=getpower"; //FOR DEBUGGING if (debugFlag) { siteipURL = "http://localhost/ip9212/getpower.php"; } tl.LogMessage("Switch_getOutputStatus", "Download url:" + siteipURL); // Send http query tl.LogMessage(">Semaphore", "waitone"); DeviceSemaphore.WaitOne(Semaphore_timeout); // 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(); tl.LogMessage("Switch_getOutputStatus", "Download str:" + s); tl.LogMessage("<Semaphore", "Release"); DeviceSemaphore.Release();//unlock ip9212 device for others //wait //Thread.Sleep(1000); } catch (WebException e) { tl.LogMessage("<Semaphore", "Release"); DeviceSemaphore.Release();//unlock ip9212 device for others ipdata[0] = -1; tl.LogMessage("Switch_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("Switch_getOutputStatus", "Exit by web error"); return(ipdata); } // Parse data try { string[] stringSeparators = new string[] { "P6" }; string[] iprawdata_arr = s.Split(stringSeparators, StringSplitOptions.None); Array.Resize(ref ipdata, iprawdata_arr.Length); //Parse an array for (var i = 1; i < iprawdata_arr.Length; i++) { //Убираем запятую if (iprawdata_arr[i].Length > 3) { iprawdata_arr[i] = iprawdata_arr[i].Substring(0, 3); } //Console.WriteLine(iprawdata_arr[i]); //Разбиваем на пары "номер порта"="значение" char[] delimiterChars = { '=' }; string[] data_arr = iprawdata_arr[i].Split(delimiterChars); //st = st + " |" + i + ' ' + data_arr[1]; if (data_arr.Length > 1) { ipdata[i] = Convert.ToInt16(data_arr[1]); //Trace(ipdata[i]); } else { ipdata[i] = -1; } } ipdata[0] = 1; tl.LogMessage("Switch_getOutputStatus", "Data was read"); } catch { ipdata[0] = -1; tl.LogMessage("Switch_getOutputStatus", "ERROR (Exception)!"); tl.LogMessage("Switch_getOutputStatus", "exit by parse error"); return(ipdata); } return(ipdata); }
/// <summary> /// Chage output relay state /// </summary> /// <param name="PortNumber">Relay port number, int [1..9]</param> /// <param name="PortValue">Port value [0,1]</param> /// <returns>Returns true in case of success</returns> public bool setOutputStatus(int PortNumber, int PortValue) { tl.LogMessage("Switch_setOutputStatus", "Enter (" + PortNumber + "," + PortValue + ")"); // get the ip9212 settings from the profile //readSettings(); //return data bool ret = false; if (string.IsNullOrEmpty(ip_addr)) { tl.LogMessage("Switch_setOutputStatus", "ERROR (ip_addr wasn't set)!"); // report a problem with the port name ASCOM_ERROR_MESSAGE = "Switch_setOutputStatus(): no IP address was specified"; throw new ASCOM.ValueNotSetException(ASCOM_ERROR_MESSAGE); //return ret; } string siteipURL = "http://" + ip_login + ":" + ip_pass + "@" + ip_addr + ":" + ip_port + "/set.cmd?cmd=setpower+P6" + PortNumber + "=" + PortValue; // new style siteipURL = "http://" + ip_addr + ":" + ip_port + "/Set.cmd?user="******"+pass="******"CMD=setpower+P6" + PortNumber + "=" + PortValue; //FOR DEBUGGING if (debugFlag) { siteipURL = "http://localhost/ip9212/set.php?cmd=setpower+P6" + PortNumber + "=" + PortValue; } tl.LogMessage("Switch_setOutputStatus", "Download url:" + siteipURL); // Send http query tl.LogMessage(">Semaphore", "waitone"); DeviceSemaphore.WaitOne(Semaphore_timeout); // 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(); tl.LogMessage("Switch_setOutputStatus", "Download str:" + s); //wait //Thread.Sleep(1000); tl.LogMessage("<Semaphore", "Release"); DeviceSemaphore.Release();//unlock ip9212 device for others ret = true; } catch (WebException e) { tl.LogMessage("<Semaphore", "Release"); DeviceSemaphore.Release();//unlock ip9212 device for others ret = false; tl.LogMessage("Switch_setOutputStatus", "Error:" + e.Message); ASCOM_ERROR_MESSAGE = "setOutputStatus(" + PortNumber + "," + PortValue + "): Couldn't reach network server"; //throw new ASCOM.NotConnectedException(ASCOM_ERROR_MESSAGE); tl.LogMessage("Switch_setOutputStatus", "Exit by web error"); // report a problem with the port name (never get there) } // Parse data // not implemented yet //Clear input cache tl.LogMessage("Switch_setOutputStatus", "Clear InputStatus cache"); clearCache(); return(ret); }
/// <summary> /// Get input sensor status /// Notes: /// 1. It is also base procedure to check if device is connected, so sync checklink call it (simply is a wrapper). But for async check it calls its own check /// 2. Because this procedure returns also data for shutter status check, it caches it's result, so OpenedState() and ClosedState() would reacquire this data /// </summary> /// <returns>Returns int array [0..8] with status flags of each input sensor. arr[0] is for read status (-1 for error, 1 for good read, 0 for smth else)</returns> // http://192.168.2.199/roof/status/ //ret: 0 closed // 1 opened // emulation: // http://localhost/vedrus/roof_status.php public int getInputStatus() { tl.LogMessage("Switch_getInputStatus", "Enter"); input_dome_state = -1; if (string.IsNullOrEmpty(ip_addr)) { input_dome_state = -1; hardware_connected_flag = false; tl.LogMessage("Switch_getInputStatus", "ERROR (ip_addr wasn't set)!"); // report a problem with the port name return(input_dome_state); } string siteipURL; siteipURL = "http://" + ip_addr + ":" + ip_port + "/roof/status/"; //FOR DEBUGGING if (debugFlag) { siteipURL = "http://localhost/vedrus/roof_status.php"; } tl.LogMessage("Switch_getInputStatus", "Download url:" + siteipURL); // Send http query tl.LogMessage(">Semaphore", "waitone"); DeviceSemaphore.WaitOne(Semaphore_timeout); // 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(); tl.LogMessage("Switch_getInputStatus", "Download str:" + s); tl.LogMessage("<Semaphore", "Release"); DeviceSemaphore.Release();//unlock ip9212 device for others //wait //Thread.Sleep(1000); if (s.Length > 0 && s.Length <= 3) //3 for \r symbol and etc { hardware_connected_flag = true; tl.LogMessage("Switch_getInputStatus", "Downloaded data is ok"); lastConnectedCheck = DateTime.Now; lastShutterStatusCheck = DateTime.Now; //Parse input data just in case it will be usefull parseInputData(s); } else { hardware_connected_flag = false; tl.LogMessage("Switch_getInputStatus", "Downloaded data error - string not found"); } } catch (WebException e) { tl.LogMessage("<Semaphore", "Release"); DeviceSemaphore.Release();//unlock ip9212 device for others input_dome_state = -1; hardware_connected_flag = false; tl.LogMessage("Switch_getInputStatus", "Error:" + e.Message); tl.LogMessage("Switch_getInputStatus", "Exit by web error"); } tl.LogMessage("Switch_getInputStatus", "Exit"); return(input_dome_state); }