-
Notifications
You must be signed in to change notification settings - Fork 31
<6.2> Flyver Server
#Flyver Server
Flyver server is a module, that allows connectivity between the SmartDrone and some different application. It is implemented as a constantly running background service and is not intended for any different use, hence it should be instantiated only once, via intent.
It provides JSON based communication which allows to control the drone and the attached devices. Every JSON must have a field named "key" with the respective value for the command associated with it.
So how can you use it in your application.
final Intent intent = new Intent(getApplicationContext(), Server.class);
ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Server.LocalBinder localBinder = (Server.LocalBinder) service;
startService(intent);
}
@Override
public void onServiceDisconnected(ComponentName name) {
//Custom code here
}
};
bindService(intent, connection, BIND_AUTO_CREATE);
The LocalBinder interface may be used to request a server instance, so that you can modify it on demand via dependency injection.
Socket connection = new Socket(MainControl.getServerIP(), 51342);
and then open the input/output streams
BufferedReader input = new BufferedReader(new InputStreamReader(connection.getInputStream()));
PrintWriter output = new PrintWriter(connection.getOutputStream(), true);
Now you can send a command to the server, and respectively the drone
String json = "{\"action\":\"increase\",\"key\":\"throttle\",\"value\":1.0}\n";
output.println(json);
output.flush();
This sends a message to the drone, that it's throttle should be increased by 1, which in this context means 1%. Google GSON library should be used for easier interaction with JSON messages.
Also on the server side, a custom callback associated with the value paired with "key" may be defined, so it would be executed when such JSON is received. The callback must be an object implementing the ServerCallback interface from the Server class and must be registered via the static method Server.RegisterCallback(String key, ServerCallback callback). The callback receives the JSON associated with it as a parameter.
ServerCallback onThrottleReceived = new ServerCallback() {
@Override
public void run(String json) {
Log.e(TAG, "Throttle command received " + json);
}
};
Server.registerCallback("throttle", onThrottleReceived);
The Server.sendMsgToClient(String msg) method could be used in the callbacks to send custom messages to the client application.
ServerCallback onThrottleReceived = new ServerCallback() {
@Override
public void run(String json) {
Log.e(TAG, "Throttle command received " + json);
Server.sendMsgToClient("JSON received");
}
};
Server.registerCallback("throttle", onThrottleReceived);