Advertisement
Guest User

Bilai.cs

a guest
Jan 8th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.35 KB | None | 0 0
  1. /**
  2.     Bilai-PnP-Gui, Desktop stat viewer for Banglalion devices
  3.     Copyright (C) 2016 Md. Minhazul Haque
  4.  
  5.     This program is free software; you can redistribute it and/or
  6.     modify it under the terms of the GNU General Public License
  7.     as published by the Free Software Foundation; either version 3
  8.     of the License, or (at your option) any later version.
  9. */
  10.  
  11. using System.Net;
  12. using System.Text;
  13. using System.IO;
  14. using System;
  15.  
  16. namespace Bilai_PnP_Gui
  17. {
  18.     public class Bilai
  19.     {
  20.         const string BilaiURLLogin = "http://192.168.2.1/apply.cgi";
  21.         const string BilaiURLUpdate = "http://192.168.2.1/apply.cgi";
  22.  
  23.         const string PostDataLogin = "submit_button=login&submit_type=do_login&change_action=gozila_cgi&username=admin&passwd=admin";
  24.         const string PostDataStatus = "submit_button=wimaxinterfaceInfo&submit_type=ref&change_action=gozila_cgi";
  25.  
  26.         private string Cookie;
  27.         public string BSID { get; private set; }
  28.         public string Freq { get; private set; }
  29.         public string CINR { get; private set; }
  30.         public string RSSI { get; private set; }
  31.         public string ULRate { get; private set; }
  32.         public string DLRate { get; private set; }
  33.         public string Uptime { get; private set; }
  34.         public int Signal { get; private set; }
  35.  
  36.         public string Login()
  37.         {
  38.             try
  39.             {
  40.                 WebClient client = new WebClient();
  41.                 String username = "admin";
  42.                 String password = "admin";
  43.                 string url = BilaiURLLogin + username;
  44.  
  45.                 client.Credentials = new System.Net.NetworkCredential(username, password);
  46.  
  47.                 string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(username + ":" + password));
  48.  
  49.                 client.Headers[HttpRequestHeader.Authorization] = "Basic " + credentials;
  50.  
  51.                 var result = client.DownloadString(url);
  52.                
  53.                 return result;
  54.                 //textBox1.Text = result;
  55.  
  56.                 /*var request = (HttpWebRequest)WebRequest.Create(BilaiURL);
  57.                 var data = Encoding.ASCII.GetBytes(PostDataLogin);
  58.  
  59.                 request.Method = "POST";
  60.                 request.ContentType = "application/x-www-form-urlencoded";
  61.                 request.ContentLength = data.Length;
  62.  
  63.                 var stream = request.GetRequestStream();
  64.                 stream.Write(data, 0, data.Length);
  65.                 stream.Close();
  66.  
  67.                 var response = (HttpWebResponse)request.GetResponse();
  68.  
  69.                 Cookie = response.GetResponseHeader("Set-Cookie").Replace("; Path=/", "");
  70.  
  71.                 stream = response.GetResponseStream();
  72.                 var reader = new StreamReader(stream);
  73.                 string responseFromServer = reader.ReadToEnd();
  74.  
  75.                 reader.Close();
  76.                 stream.Close();
  77.                 response.Close(); */
  78.  
  79.                 /*if (result =="Connected")
  80.                     return "";
  81.                 else
  82.                     return "Login Failed";*/
  83.             }
  84.             catch (Exception exc)
  85.             {
  86.                 return exc.Message;
  87.             }
  88.         }
  89.  
  90.         public string Update()
  91.         {
  92.             try
  93.             {
  94.                 var request = (HttpWebRequest)WebRequest.Create(BilaiURLUpdate);
  95.                 var data = Encoding.ASCII.GetBytes(PostDataStatus);
  96.  
  97.                 request.Method = "GET";
  98.                 request.ContentType = "application/x-www-form-urlencoded";
  99.                 request.ContentLength = data.Length;
  100.  
  101.                 var stream = request.GetRequestStream();
  102.                 stream.Write(data, 0, data.Length);
  103.                 stream.Close();
  104.  
  105.                 var response = (HttpWebResponse)request.GetResponse();
  106.  
  107.                 Cookie = response.GetResponseHeader("Cookie").Replace("; Path=/", "");
  108.  
  109.                 stream = response.GetResponseStream();
  110.                 var reader = new StreamReader(stream);
  111.                 string[] StatusData = reader.ReadToEnd().Split(';');
  112.  
  113.                 BSID = StatusData[1];
  114.                 Freq = StatusData[2];
  115.                 CINR = StatusData[4];
  116.                 RSSI = StatusData[3];
  117.                 ULRate = StatusData[20];
  118.                 DLRate = StatusData[21];
  119.                 Uptime = StatusData[19];
  120.  
  121.                 double nRSSI = Convert.ToSingle(RSSI.Replace(" dBm", ""));
  122.                
  123.                 // Calculate signal quality from RSSI
  124.                 nRSSI = (nRSSI + 100F) * 1.33;
  125.  
  126.                 if (nRSSI >= 80)
  127.                     Signal = 5;
  128.                 else if (nRSSI >= 60)
  129.                     Signal = 4;
  130.                 else if (nRSSI >= 40)
  131.                     Signal = 3;
  132.                 else if (nRSSI >= 20)
  133.                     Signal = 2;
  134.                 else if (nRSSI >= 10)
  135.                     Signal = 1;
  136.                 else
  137.                     Signal = 0;
  138.  
  139.                 // Convert 12:34 to 12h 34m format
  140.                 Uptime = Uptime.Split(':')[0] + "h " + Uptime.Replace(" ", "").Split(':')[1] + "m";
  141.  
  142.                 reader.Close();
  143.                 stream.Close();
  144.                 response.Close();
  145.             }
  146.             catch (Exception exc)
  147.             {
  148.                 return exc.Message;
  149.             }
  150.             return "";
  151.         }
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement