Package com.grt192.core

Source Code of com.grt192.core.RobotModel

package com.grt192.core;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import com.grt192.networking.GRTClientSocket;
import com.grt192.networking.SocketEvent;
import com.grt192.networking.SocketListener;

import java.util.Vector;

public class RobotModel implements SocketListener {
  public static final String HOST = "10.1.92.2";
  public static final int PORT = 1234;

  private GRTClientSocket socket;
  protected double x;
  protected double y;
  protected double heading;
  protected boolean armExtended;
  protected boolean firing;
  protected boolean readyToFire;
  protected Vector<RobotModelListener> listeners;

  public RobotModel() {
    this.x = 0;
    this.y = 0;
    this.heading = 0;
    this.armExtended = false;
    this.firing = false;
    this.readyToFire = false;
    this.socket = new GRTClientSocket(HOST, PORT);
    socket.start();
    listeners = new Vector<RobotModelListener>();
  }

  public void addRobotModelListener(RobotModelListener rml) {
    listeners.add(rml);
  }

  public void moveTo(double r, double theta, double s) {
    JSONObject json = new JSONObject();
    try {
      json.put("command", "moveto");
      json.put("r", "" + r);
      json.put("theta", "" + theta);
      json.put("s", "" + s);
      socket.sendData(json.toString());
    } catch (JSONException e) {
      e.printStackTrace();
    }

  }

  public void stop() {
    JSONObject json = new JSONObject();
    try {
      json.put("command", "stop");
      socket.sendData(json.toString());
    } catch (JSONException e) {
      e.printStackTrace();
    }
  }

  public void fire() {
    JSONObject json = new JSONObject();
    try {
      json.put("command", "fire");
      socket.sendData(json.toString());
    } catch (JSONException e) {
      e.printStackTrace();
    }

  }

  public void toggleArm() {
    JSONObject json = new JSONObject();
    try {
      json.put("command", "arm");
      socket.sendData(json.toString());
    } catch (JSONException e) {
      e.printStackTrace();
    }

  }

  public void getStatus() {
    JSONObject json = new JSONObject();
    try {
      json.put("command", "getstatus");
      socket.sendData(json.toString());
    } catch (JSONException e) {
      e.printStackTrace();
    }
  }

  @Override
  public void dataRecieved(SocketEvent e) {
    try {
      JSONObject json = new JSONObject(new JSONTokener(e.getData()));
      x = Double.parseDouble(json.getString("x"));
      y = Double.parseDouble(json.getString("y"));
      heading = Double.parseDouble(json.getString("heading"));
      armExtended = json.getBoolean("arm");
      firing = json.getBoolean("firing");
      readyToFire = json.getBoolean("ready");
      notifyListeners();
    } catch (JSONException e1) {
      e1.printStackTrace();
    }
  }

  @Override
  public void onConnect(SocketEvent e) {
    // TODO Auto-generated method stub

  }

  @Override
  public void onDisconnect(SocketEvent e) {
    // TODO Auto-generated method stub

  }

  public double getX() {
    return x;
  }

  public double getY() {
    return y;
  }

  public double getHeading() {
    return heading;
  }

  public boolean isArmExtended() {
    return armExtended;
  }

  public boolean isFiring() {
    return firing;
  }

  public boolean isReadyToFire() {
    return readyToFire;
  }

  protected void notifyListeners() {
    for (RobotModelListener rml : listeners) {
      rml.stateChanged();
    }
  }

  public void removeRobotModelListener(RobotModelListener l){
    listeners.remove(l);
  }
 
}
TOP

Related Classes of com.grt192.core.RobotModel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.