Skip to content

Instantly share code, notes, and snippets.

@Dr-Emann
Created February 27, 2012 18:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dr-Emann/1926098 to your computer and use it in GitHub Desktop.
Save Dr-Emann/1926098 to your computer and use it in GitHub Desktop.
Getting ESuds info from Java
package;
public class Machine {
public Machine()
{
this.status = MachineStatus.UNKNOWN;
}
public Machine(final int id, final int num, MachineStatus status)
{
this.id = id;
this.num = num;
this.status = status;
}
public int id;
public int num;
public MachineStatus status;
public MachineType type;
public int timeRemaining;
@Override
public String toString()
{
return this.type + ": " +this.num + " "+this.status.toString()+ " " + timeRemaining;
}
}
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Vector;
import org.xml.sax.SAXException;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.util.Xml;
public class MachineService
{
private static final String BASE_URL = "http://esuds.net/RoomStatus/machineStatus.i?bottomLocationId=";
private final Context context;
private final Vector<Machine> _machineList;
public MachineService(Context context)
{
_machineList = new Vector<Machine>(10,5);
this.context = context;
}
@Override
public void load(final int room) throws IOException
{
ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
Boolean connectedNetwork = false;
for(NetworkInfo info : cm.getAllNetworkInfo())
{
if(info.getState()==NetworkInfo.State.CONNECTED || info.getState()==NetworkInfo.State.CONNECTING)
{
connectedNetwork = true;
break;
}
}
if(!connectedNetwork)
{
_machineList.clear();
IOException e = new IOException("Not Connected to Internet");
throw(e);
}
final String urlString = BASE_URL + room;
final URL url;
InputStream is = null;
final DataInputStream dis;
String xml = "";
try
{
url = new URL(urlString);
is = url.openStream();
dis = new DataInputStream(new BufferedInputStream(is));
String nextLine = "";
do
{
xml += nextLine;
nextLine = dis.readLine();
}
while(nextLine != null);
}
catch(MalformedURLException mue)
{
mue.printStackTrace();
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
catch(Exception e)
{
Log.e("Error", e.getMessage());
xml = "<bob></bob>";
}
finally
{
try
{
is.close();
}
catch(Exception e)
{
}
}
// After xml is downloaded, parse
xml = xml.replace("xmlns=\"http://www.w3.org/1999/xhtml\"", "");
xml = xml.replace("&nbsp;","BLECK"); // XML Parser doesn't like &nbsp;
xml = xml.replace('#', 'f'); // XML Parser doesn't seem to like # either
xml = xml.replaceAll("<script.*/script>", ""); // Need to remove the script block outside of the main data block, to make the html valid.
MyMachineContentHandler handler = new MyMachineContentHandler();
try
{
Xml.parse(xml, handler);
_machineList.clear();
_machineList.addAll(handler.getMachines());
handler = null;
}
catch(SAXException sae)
{
_machineList.clear();
}
}
public Vector<Machine> getList()
{
return _machineList;
}
public Machine getMachine(final int machineNum)
{
for(Machine cur : _machineList)
{
if(cur.num == machineNum)
return cur;
}
// If no machines match:
return null;
}
public Vector<Machine> getListOfType(MachineType type) {
Vector<Machine> returnList = new Vector<Machine>();
for(Machine machine : _machineList)
{
if(machine.type.equals(type))
{
returnList.add(machine);
}
}
return returnList;
}
}
public enum MachineStatus
{
AVAILABLE,
CYCLE_COMPLETE,
IN_USE,
UNAVAILABLE,
UNKNOWN
}
package;
public enum MachineType
{
WASHER,
DRYER,
UNKNOWN
}
import java.util.Vector;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class MachineXMLHandler extends DefaultHandler {
private Boolean _lastClassName;
private boolean _inRow;
private int _col;
private String lastContent;
private Vector<Machine> machines;
/**
* Calling when we're within an element. Here we're checking to see if there
* is any content in the tags that we're interested in and populating it in
* the Config object.
*
* @param ch
* @param start
* @param length
*/
@Override
public void characters(char ch[], int start, int length)
throws SAXException {
super.characters(ch, start, length);
String chars = new String(ch, start, length);
chars = chars.trim();
lastContent = chars;
}
/**
* Called at the end of the element. Setting the booleans to false, so we
* know that we've just left that tag.
*
* @param namespaceURI
* @param localName
* @param qName
* @throws SAXException
*/
@Override
public void endElement(String namespaceURI, String localName, String qName)
throws SAXException {
super.endElement(namespaceURI, localName, qName);
if (_inRow) {
if (localName.equals("tr")) {
_inRow = false;
lastContent = "";
return;
}
if(_lastClassName)
{
final Machine machine = machines.lastElement();
// Log.i("Col: "+_col+" , "+((_lastClassName)?"true":"false"),
// lastContent);
if (localName.equals("td")) {
switch (_col) {
case 0:
if (!lastContent.equals("BLECK"))
{
try
{
machine.id = Integer.parseInt(lastContent);
}
catch(NumberFormatException e)
{
machine.id = 0;
}
}
break;
case 1:
try{
machine.num = Integer.parseInt(lastContent);
}
catch(NumberFormatException e)
{
machine.num = 0;
}
break;
case 2:
if(lastContent.toLowerCase().contains("washer"))
machine.type = MachineType.WASHER;
else if(lastContent.toLowerCase().contains("dryer"))
machine.type = MachineType.DRYER;
else
machine.type = MachineType.UNKNOWN;
case 4:
if (!lastContent.equals("BLECK"))
{
try{
machine.timeRemaining = Integer.parseInt(lastContent);
}
catch (NumberFormatException e)
{
machine.timeRemaining = 0;
}
}
else
machine.timeRemaining = 0;
break;
default:
lastContent = "";
break;
}
_col++;
} else if (localName.equals("font")) {
if(lastContent.equalsIgnoreCase("available"))
machine.status = MachineStatus.AVAILABLE;
else if (lastContent.equalsIgnoreCase("cycle complete"))
machine.status = MachineStatus.CYCLE_COMPLETE;
else if (lastContent.equalsIgnoreCase("in use"))
machine.status = MachineStatus.IN_USE;
else if (lastContent.equalsIgnoreCase("unavailable"))
machine.status = MachineStatus.UNAVAILABLE;
else
machine.status = MachineStatus.UNKNOWN;
}
}
}
}
public Machine getMachineAt(int machineNum) {
for (final Machine curr : machines) {
if (curr.num == machineNum)
return curr;
}
// If no machines have the chosen number
return null;
}
public Vector<Machine> getMachines()
{
return machines;
}
@Override
public void startDocument() throws SAXException {
super.startDocument();
machines = new Vector<Machine>(10, 5);
lastContent = "";
_col = 0;
}
@Override
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts) throws SAXException {
super.startElement(namespaceURI, localName, qName, atts);
if (localName.equals("tr")) {
final String name = atts.getValue("class");
if (name != null)
_lastClassName = (name.contains("even") || name.contains("odd"));
else
_lastClassName = false;
_inRow = true;
_col = 0;
if(_lastClassName)
machines.add(new Machine());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment