Back to project page twawm2.
The source code is released under:
Copyright (c) 2014, afnf All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistr...
If you think the Android project twawm2 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.appspot.afnf4199ga.twawm.router; /*from www. ja va 2s .c o m*/ import java.net.InetAddress; import java.net.SocketException; import java.net.SocketTimeoutException; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.regex.Matcher; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.NameValuePair; import org.apache.http.NoHttpResponseException; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import android.annotation.SuppressLint; import com.appspot.afnf4199ga.twawm.Const; import com.appspot.afnf4199ga.utils.Logger; import com.appspot.afnf4199ga.utils.MyStringUtlis; import com.appspot.afnf4199ga.wmgraph.app.MainActivity; import com.appspot.afnf4199ga.wmgraph.app.MyPreferenceActivity; public class RouterControlByHttp { public static final int CTRL_OK = 0; public static final int CTRL_STANBY_FAILED = 33; public static final int CTRL_PASS_NOT_INITIALIZED = 23; protected static String routerName = null; protected static long lastUpdate; protected static HashMap<String, String> hiddenMap = new HashMap<String, String>(); enum CTRL { GET_INFO, STANDBY } public static int exec(CTRL ctrl, RouterInfo routerInfo) { MyHttpClient httpClient = null; try { MainActivity service = MainActivity.getInstance(); if (service == null) { Logger.e("service is null on RouterControlByHttp.exec"); return 10; } // ????IP????????? String routerIpAddr = getRouterIpAddr(new InetLookupWrappter(), Const.getPrefApIpAddr(service)); if (routerIpAddr == null) { return 11; } // httpClient??? httpClient = MyHttpClient.createClient(routerIpAddr); // ??????????????????????????????????????ID?????? boolean communicated = false; long now = System.currentTimeMillis(); if (ctrl == CTRL.GET_INFO || now - lastUpdate >= Const.ROUTER_SESSION_TIMEOUT) { //Logger.i("RouterControlByHttp GET_INFO"); try { // ?????????? HttpGet method = new HttpGet("http://" + routerIpAddr + Const.ROUTER_URL_INFO); //method.setHeader("Connection", "close"); HttpResponse response = httpClient.execute(method); int statusCode = response.getStatusLine().getStatusCode(); HttpEntity entity = response.getEntity(); // ???? if (statusCode == HttpStatus.SC_OK && entity != null) { String content = EntityUtils.toString(entity, Const.ROUTER_PAGE_CHARSET); entity.consumeContent(); if (routerInfo != null) { updateRouterInfo(content, routerInfo); } lastUpdate = now; communicated = true; // ????????????????? if (routerInfo.notInitialized) { return CTRL_PASS_NOT_INITIALIZED; // 20 } } else { Logger.w("RouterControlByHttp GET_INFO error, statusCode=" + statusCode); return 21; } } catch (Throwable e) { Logger.w("RouterControlByHttp GET_INFO error, e=" + e.toString()); return 22; } } // ???????? if (ctrl == CTRL.STANDBY) { // ??????????????????????? if (communicated) { Logger.i("RouterControlByHttp STANDBY start (check skipped)"); } // ???????????????????????? else { long start = System.currentTimeMillis(); try { // ????????? httpClient.disableCredentialsProvider(); // 401:UNAUTHORIZED????????????????????????????? HttpGet method = new HttpGet("http://" + routerIpAddr + "/"); HttpResponse response = httpClient.execute(method); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_UNAUTHORIZED) { Logger.e("RouterControlByHttp STANDBY check failed, communication failed, statusCode=" + statusCode); return 31; } // ???????????????? httpClient.enableCredentialsProvider(); } catch (Throwable e) { Logger.e("RouterControlByHttp STANDBY check failed, router unreachable, e=" + e.toString()); return 32; } Logger.i("RouterControlByHttp STANDBY start (check " + (System.currentTimeMillis() - start) + "ms)"); } // ???????? { // TODO 3800????ROUTER_URL_BTSTANDBY????? HttpPost method = new HttpPost("http://" + routerIpAddr + Const.ROUTER_URL_STANDBY); try { // ?????ID???? List<NameValuePair> params = new ArrayList<NameValuePair>(); Iterator<String> iterator = hiddenMap.keySet().iterator(); while (iterator.hasNext()) { String key = iterator.next(); params.add(new BasicNameValuePair(key, hiddenMap.get(key))); } method.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); // ???????? HttpResponse response = httpClient.execute(method); // ???????????????????????????????????????????? int statusCode = response.getStatusLine().getStatusCode(); Logger.e("RouterControlByHttp STANDBY error, statusCode=" + statusCode); return CTRL_STANBY_FAILED; // 33 } catch (NoHttpResponseException e) { // success (WM3800R Android4.2) } catch (SocketTimeoutException e) { // success (WM3600R Android2.2.2) } catch (SocketException e) { // success (WM3600R Android4.0.4) } catch (Throwable e) { Logger.e("RouterControlByHttp STANDBY error", e); return 34; } } } return CTRL_OK; } catch (Throwable e) { Logger.w("RouterControlByHttp error", e); return 101; } finally { MyHttpClient.close(httpClient); } } protected static String getRouterIpAddr(InetLookupWrappter wrapper, String confRouterIpAddr) throws Throwable { String routerIpAddr = null; // ????IP?????????????????????????? if (MyStringUtlis.isEmpty(confRouterIpAddr) == false) { Matcher m = MyPreferenceActivity.IP_ADDR_PATTERN.matcher(confRouterIpAddr); if (m.matches()) { routerIpAddr = confRouterIpAddr; // ??????????????????????????????????????? } else { Logger.w("RouterControlByHttp confRouterIpAddr format is invalid"); // ????if????????? } } // ?????????? if (MyStringUtlis.isEmpty(routerIpAddr)) { // ?????????????? InetAddress address = null; try { address = wrapper.getByName(Const.ROUTER_HOSTNAME); } catch (Throwable e) { // do nothing } // null??????????NG if (address == null) { Logger.w("RouterControlByHttp resolved IP Addr is null"); return null; } // ???????????????????????NG????????????????? else if (address.isSiteLocalAddress() == false) { Logger.w("RouterControlByHttp " + Const.ROUTER_HOSTNAME + " is not private address"); return null; } // OK????routerIpAddr??? else { routerIpAddr = address.getHostAddress(); } } return routerIpAddr; } /** * info_remote_main???html?????????routerInfo???hiddenMap??????? * * @param content */ @SuppressLint("DefaultLocale") protected static void updateRouterInfo(String content, RouterInfo routerInfo) { // ????????? hiddenMap.clear(); if (content == null) { Logger.w("RouterControlByHttp failed, content is empty"); return; } else if (content.indexOf("??????????????????") != -1) { Logger.w("RouterControlByHttp failed, not initialized"); routerInfo.notInitialized = true; return; } else { try { Document doc = Jsoup.parse(content); // ?????? { Elements trs = doc.select(".table_common .small_item_info_tr"); if (trs != null) { Iterator<Element> iterator = trs.iterator(); while (iterator.hasNext()) { Element tr = (Element) iterator.next(); if (tr != null) { Elements tds = tr.getElementsByTag("td"); if (tds != null && tds.size() == 2) { Element td0 = tds.get(0); Element td1 = tds.get(1); if (td0 != null && td1 != null) { String td0txt = MyStringUtlis.normalize(td0.text()).toLowerCase(); String td1txt = MyStringUtlis.normalize(td1.text()); if (td0txt.indexOf("????") != -1) { routerInfo.antennaLevelText = MyStringUtlis.normalize(td1txt.replace("????", "")); } else if (td0txt.indexOf("rssi") != -1) { routerInfo.rssiText = MyStringUtlis.normalize(MyStringUtlis.subStringBefore(td1txt, "(")); } else if (td0txt.indexOf("cinr") != -1) { routerInfo.cinrText = MyStringUtlis.normalize(MyStringUtlis.subStringBefore(td1txt, "(")); } else if (td0txt.indexOf("mac????(bluetooth)") != -1) { routerInfo.bluetoothAddress = td1txt; } else if (td0txt.indexOf("?????") != -1) { if (td1txt.indexOf("???") != -1) { routerInfo.batteryText = "" + (MyStringUtlis.count(td1txt, '?') * 10) + "+"; } else { String tmp = td1txt; int index = -1; index = td1txt.indexOf("?"); if (index != -1) { tmp = tmp.substring(index + 1); index = tmp.indexOf("?"); if (index != -1) { tmp = tmp.substring(0, index); routerInfo.batteryText = MyStringUtlis.normalize(tmp) + "%"; } } } } } } } } } } // // hidden? // { // Elements hiddens = doc.select("input[type=hidden]"); // if (hiddens != null) { // Iterator<Element> iterator = hiddens.iterator(); // while (iterator.hasNext()) { // Element e = (Element) iterator.next(); // String name = MyStringUtlis.normalize(e.attr("name")); // if (MyStringUtlis.isEmpty(name) == false) { // String value = MyStringUtlis.normalize(e.attr("value")); // hiddenMap.put(name, value); // } // } // } // } // // // ??????? // if (routerName == null) { // Elements e = doc.select(".product span"); // if (e != null) { // String newRouterName = MyStringUtlis.normalize(e.text()); // if (MyStringUtlis.isEmpty(newRouterName) == false) { // routerInfo.routerName = newRouterName; // if (MyStringUtlis.eqauls(newRouterName, routerName) == false) { // Logger.i("routerName=" + newRouterName); // routerName = newRouterName; // } // } // } // } // else { // routerInfo.routerName = routerName; // } } catch (Throwable e) { Logger.w("RouterControlByHttp parsing failed", e); } } } public static void resetPrevious() { hiddenMap.clear(); lastUpdate = 0; } }