PageRenderTime 48ms CodeModel.GetById 34ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/v3.2/nimbits-sdk/src/com/nimbits/MainClass.java

http://nimbits-server.googlecode.com/
Java | 187 lines | 148 code | 33 blank | 6 comment | 35 complexity | 4159de9c2d0b3308ac86f73b142d2e70 MD5 | raw file
  1package com.nimbits;
  2
  3import com.nimbits.client.NimbitsClient;
  4import com.nimbits.client.NimbitsClientFactory;
  5import com.nimbits.client.model.Action;
  6import com.nimbits.client.model.Const;
  7import com.nimbits.client.model.common.CommonFactoryLocator;
  8import com.nimbits.client.model.email.EmailAddress;
  9import com.nimbits.client.model.point.PointName;
 10import com.nimbits.client.model.value.Value;
 11import com.nimbits.client.model.value.ValueModelFactory;
 12import com.nimbits.console.KeyFile;
 13import com.nimbits.gson.GsonFactory;
 14import com.nimbits.user.GoogleUser;
 15import com.nimbits.user.NimbitsUser;
 16
 17import java.io.IOException;
 18import java.util.Date;
 19import java.util.HashMap;
 20import java.util.Map;
 21
 22/**
 23 * Created by bsautner
 24 * User: benjamin
 25 * Date: 4/8/11
 26 * Time: 2:42 PM
 27 */
 28public class MainClass {
 29
 30    public static void main(final String[] args) throws IOException {
 31        final HashMap<String, String> argsMap = new HashMap<String, String>();
 32
 33        if (args == null || args.length == 0) {
 34            printUsage();
 35            return;
 36        } else {
 37            processArgs(args, argsMap);
 38        }
 39
 40        if (argsMap.containsKey(Const.PARAM_I)) {
 41            String[] fileArgs = KeyFile.processKeyFile(argsMap);
 42            processArgs(fileArgs, argsMap);
 43        }
 44
 45
 46        final boolean verbose = argsMap.containsKey(Const.PARAM_VERBOSE);
 47        final String host = argsMap.containsKey(Const.PARAM_HOST) ? argsMap.get(Const.PARAM_HOST) : Const.PATH_NIMBITS_PUBLIC_SERVER;
 48        final String emailParam = argsMap.containsKey(Const.PARAM_EMAIL) ? argsMap.get(Const.PARAM_EMAIL) : null;
 49        final String key = argsMap.containsKey(Const.PARAM_KEY) ? argsMap.get(Const.PARAM_KEY) : null;
 50        final String password = argsMap.containsKey(Const.PARAM_PASSWORD) ? argsMap.get(Const.PARAM_PASSWORD) : null;
 51        final EmailAddress email = CommonFactoryLocator.getInstance().createEmailAddress(emailParam);
 52        final NimbitsClient client = createClient(host, email, key, password);
 53
 54        final boolean loggedIn = checkLoggedIn(client, verbose);
 55
 56        if (!loggedIn) {
 57            out(true, "Access Denied.");
 58            return;
 59        }
 60
 61        if (argsMap.containsKey(Const.PARAM_ACTION) && loggedIn) {
 62            Action action = Action.valueOf(argsMap.get(Const.PARAM_ACTION));
 63
 64            switch (action) {
 65                case read:
 66                case readvalue:
 67                case readgps:
 68                case readjson:
 69                case readnote:
 70                    readValue(client, argsMap, action);
 71                    break;
 72                case record:
 73                case recordvalue:
 74                    recordValue(client, argsMap, verbose);
 75                    break;
 76                default:
 77                    printUsage();
 78            }
 79        } else if (argsMap.containsKey(Const.PARAM_GENKEY) && argsMap.containsKey(Const.PARAM_OUT)) {
 80
 81            out(true, KeyFile.genKey(argsMap));
 82
 83        }
 84
 85
 86    }
 87
 88    private static void readValue(final NimbitsClient client, final Map<String, String> argsMap, Action action) {
 89        final PointName pointName = CommonFactoryLocator.getInstance().createPointName(argsMap.get(Const.PARAM_POINT));
 90        final Value v = client.getCurrentRecordedValue(pointName);
 91
 92        switch (action) {
 93            case read:
 94            case readvalue:
 95                out(true, String.valueOf(v.getValue()));
 96                break;
 97            case readgps:
 98                out(true, v.getLatitude() + "," + v.getLongitude());
 99                break;
100            case readnote:
101                out(true, v.getNote());
102                break;
103            case readjson:
104                out(true, GsonFactory.getInstance().toJson(v));
105                break;
106        }
107    }
108
109    private static Value buildValue(final Map<String, String> argsMap) {
110        final double d = argsMap.containsKey(Const.PARAM_VALUE) ? Double.valueOf(argsMap.get(Const.PARAM_VALUE)) : 0.0;
111        final String note = argsMap.containsKey(Const.PARAM_NOTE) ? argsMap.get(Const.PARAM_NOTE) : null;
112        final double lat = argsMap.containsKey(Const.PARAM_LAT) ? Double.valueOf(argsMap.get(Const.PARAM_LAT)) : 0.0;
113        final double lng = argsMap.containsKey(Const.PARAM_LNG) ? Double.valueOf(argsMap.get(Const.PARAM_LNG)) : 0.0;
114        return ValueModelFactory.createValueModel(lat, lng, d, new Date(), 0, note, "");
115
116    }
117
118    private static void recordValue(final NimbitsClient client, final Map<String, String> argsMap, final boolean verbose) throws IOException {
119        out(verbose, "Recording values");
120
121        final Value v = buildValue(argsMap);
122        final PointName pointName = CommonFactoryLocator.getInstance().createPointName(argsMap.get(Const.PARAM_POINT));
123        final Value result = client.recordValue(pointName, v);
124        if (result == null) {
125            out(verbose, "An error occurred recording your data");
126        } else {
127            out(verbose, result.getValue() + " recorded to " + pointName);
128        }
129    }
130
131    private static boolean checkLoggedIn(final NimbitsClient client, final boolean verbose) throws IOException {
132        boolean loggedIn = false;
133        if (client != null) {
134            out(verbose, "Authenticating...");
135            loggedIn = client.isLoggedIn();
136            if (loggedIn) {
137                out(verbose, "Success");
138            } else {
139                out(verbose, "Authentication Failure");
140            }
141        }
142        return loggedIn;
143    }
144
145    private static NimbitsClient createClient(final String host, final EmailAddress email, final String key, final String password) {
146        NimbitsClient client = null;
147
148        if (key != null && email != null) {
149            NimbitsUser n = new NimbitsUser(email, key);
150            client = NimbitsClientFactory.getInstance(n, host);
151        } else if (password != null && email != null) {
152            GoogleUser g = new GoogleUser(email, password);
153            try {
154                client = NimbitsClientFactory.getInstance(g, host);
155            } catch (Exception e) {
156
157                out(true, e.getMessage());
158            }
159        }
160        return client;
161    }
162
163    private static void processArgs(final String[] args, final Map<String, String> argsMap) {
164        for (String s : args) {
165
166            String[] a = s.split("=");
167            if (a.length == 2) {
168                argsMap.put(a[0].replaceFirst("-", "").toLowerCase(), a[1].trim());
169            } else {
170                argsMap.put(s.replaceFirst("-", "").toLowerCase(), "");
171            }
172
173
174        }
175    }
176
177    private static void out(boolean verbose, final String message) {
178        if (verbose) {
179            System.out.println(message);
180        }
181    }
182
183    private static void printUsage() {
184        out(true, "Unexpected parameters. Please refer to the SDK Manual on www.nimbits.com.");
185    }
186
187}