import java.lang.reflect.*;
import java.net.URL;
import gw.*;

public class Get
{
    public static double TIMEOUT = 2.0;
    private GWPort proxy;

    Get(String gateway)
    {
        try {
            URL portURL = new URL("http://restsoap:AIIT@" + gateway + "/gw/soap");
            GWServiceLocator locator = new GWServiceLocator();
            proxy = locator.getGWPort(portURL);
        }
        catch (Exception e) {
            e.printStackTrace();
            System.exit(2);
        }
    }

    public void get(String name, String addr)
    {
        GW__Attribute_Result[] results;
        GW__Attribute value;

        try {
            results = proxy.attributesGet(name, addr, TIMEOUT, "*");
            for (int i = 0; i < results.length; i++) {
                value = results[i].getValue();
                Method m = value.getClass().getMethod("get" + name, 
                                                      (Class[])null);
                Object o = m.invoke(value, (Object[])null);
                System.out.println("Mote " + results[i].getAddr() + ": " + 
                                   name + " value is " + o);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            System.exit(2);
        }
    }

    public static void main(String [] args)
    {
        if (args.length != 2 && args.length != 3) {
            System.out.println("Usage: java Get gateway attrName [mote]");
            System.exit(3);
        }
        String gateway = args[0];
        String attrName = args[1];
        String mote;
        if (args.length == 2)
            mote = "ffffffffffffffff";
        else
            mote = args[2];
        Get get = new Get(gateway);
        get.get(attrName, mote);
    }
}