aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/web/client/model/StateReader.java
blob: 10ed9809255c7baabc195263e23bf58ccf31c166 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package org.traccar.web.client.model;

import java.util.LinkedList;
import java.util.List;

import org.traccar.web.client.ApplicationContext;
import org.traccar.web.shared.model.Position;

import com.google.gwt.xml.client.Node;
import com.google.gwt.xml.client.NodeList;
import com.google.gwt.xml.client.XMLParser;

public class StateReader {

    private static String toString(Object object) {
        if (object != null) {
            return object.toString();
        }
        return null;
    }

    public static List<StateItem> getState(Position position) {
        List<StateItem> state = new LinkedList<StateItem>();

        state.add(new StateItem("valid", toString(position.getValid())));
        state.add(new StateItem("time", ApplicationContext.getInstance().getFormatterUtil().getTimeFormat().format(position.getTime())));
        state.add(new StateItem("latitude", toString(position.getLatitude())));
        state.add(new StateItem("longitude", toString(position.getLongitude())));
        state.add(new StateItem("altitude", toString(position.getAltitude())));
        state.add(new StateItem("speed", ApplicationContext.getInstance().getFormatterUtil().getSpeedFormat().format(position.getSpeed())));
        state.add(new StateItem("course", toString(position.getCourse())));
        state.add(new StateItem("power", toString(position.getPower())));
        state.add(new StateItem("address", position.getAddress()));

        String other = position.getOther();
        if (other != null) {
            try {
                NodeList nodes = XMLParser.parse(other).getFirstChild().getChildNodes();
                for (int i = 0; i < nodes.getLength(); i++) {
                    Node node = nodes.item(i);
                    state.add(new StateItem(node.getNodeName(), node.getFirstChild().getNodeValue()));
                }
            } catch (Exception error) {
            }
        }

        return state;
    }

}