Beispiel #1
0
        private int setDIO(int no)
        {
            if (no > 2 || no < 0)
            {
                return(-1);
            }

            string setdio = "{\"jsonrpc\":\"2.0\", \"id\":1, \"method\":\"call\"," +
                            "\"params\":[\"" + Token + "\", \"ioman.gpio.dio" + no + "\", \"update\",{" +
                            "\"save_conf\":" + TRB2DIOState.I.DIOSaveConfig[no].ToString() + "," +
                            "\"direction\":\"" + (TRB2DIOState.I.DIODirection[no] ? "in" : "out\", " +
                                                  "\"value\":\"" + (TRB2DIOState.I.DIOState[no]? "1" : "0")) + "\" } ] }";

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL + "/ubus");

            request.Method        = "POST";
            request.ContentType   = "application/json";
            request.ContentLength = setdio.Length;
            using (Stream webStream = request.GetRequestStream())
                using (StreamWriter requestWriter = new StreamWriter(webStream, System.Text.Encoding.ASCII))
                {
                    requestWriter.Write(setdio);
                }

            try
            {
                WebResponse webResponse = request.GetResponse();
                using (Stream webStream = webResponse.GetResponseStream() ?? Stream.Null)
                    using (StreamReader responseReader = new StreamReader(webStream))
                    {
                        string  response = responseReader.ReadToEnd();
                        JsonRPC rpc      = JsonConvert.DeserializeObject <JsonRPC>(response);

                        if (rpc.error != null)
                        {
                            return(1);
                        }

                        if (int.Parse(rpc.result[0].ToString()) == 0)
                        {
                            return(0);
                        }
                        else
                        {
                            return(int.Parse(rpc.result[0].ToString()));
                        }
                    }
            }
            catch (Exception e)
            {
                Console.Out.WriteLine(e.Message);
            }
            return(-1);
        }
Beispiel #2
0
        private int login()
        {
            string RequestData = "{\"jsonrpc\":\"2.0\", \"id\":1, \"method\":\"call\"," +
                                 "\"params\":[\"00000000000000000000000000000000\", \"session\", \"login\",{" +
                                 "\"username\":\"" + Username + "\", \"password\":\"" + Password + "\" } ] }";


            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL + "/ubus");

            request.Method        = "POST";
            request.ContentType   = "application/json";
            request.ContentLength = RequestData.Length;
            using (Stream webStream = request.GetRequestStream())
                using (StreamWriter requestWriter = new StreamWriter(webStream, System.Text.Encoding.ASCII))
                {
                    requestWriter.Write(RequestData);
                }

            try
            {
                WebResponse webResponse = request.GetResponse();
                using (Stream webStream = webResponse.GetResponseStream() ?? Stream.Null)
                    using (StreamReader responseReader = new StreamReader(webStream))
                    {
                        string  response = responseReader.ReadToEnd();
                        JsonRPC rpc      = JsonConvert.DeserializeObject <JsonRPC>(response);

                        if (rpc.error != null)
                        {
                            return(1);
                        }

                        if (int.Parse(rpc.result[0].ToString()) == 0)
                        {
                            Token = rpc.result[1]["ubus_rpc_session"].ToString();
                            return(0);
                        }
                        else
                        {
                            return(int.Parse(rpc.result[0].ToString()));
                        }
                    }
            }
            catch (Exception e)
            {
                Console.Out.WriteLine(e.Message);
            }
            return(-1);
        }
Beispiel #3
0
        private int adc()
        {
            string getADC = "{\"jsonrpc\":\"2.0\", \"id\":1, \"method\":\"call\"," +
                            "\"params\":[\"" + Token + "\", \"ioman.adc.adc0\", \"status\",{ } ] }";

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL + "/ubus");

            request.Method        = "POST";
            request.ContentType   = "application/json";
            request.ContentLength = getADC.Length;
            using (Stream webStream = request.GetRequestStream())
                using (StreamWriter requestWriter = new StreamWriter(webStream, System.Text.Encoding.ASCII))
                {
                    requestWriter.Write(getADC);
                }

            try
            {
                WebResponse webResponse = request.GetResponse();
                using (Stream webStream = webResponse.GetResponseStream() ?? Stream.Null)
                    using (StreamReader responseReader = new StreamReader(webStream))
                    {
                        string  response = responseReader.ReadToEnd();
                        JsonRPC rpc      = JsonConvert.DeserializeObject <JsonRPC>(response);

                        if (rpc.error != null)
                        {
                            return(1);
                        }

                        if (int.Parse(rpc.result[0].ToString()) == 0)
                        {
                            TRB2DIOState.I.ADCValue = double.Parse(rpc.result[1]["value"].ToString());
                            return(0);
                        }
                        else
                        {
                            return(int.Parse(rpc.result[0].ToString()));
                        }
                    }
            }
            catch (Exception e)
            {
                Console.Out.WriteLine(e.Message);
            }
            return(-1);
        }