working queue, and get user tasks

This commit is contained in:
aj 2020-01-31 23:58:30 +00:00
parent f597cf6456
commit d3bff0333c
7 changed files with 187 additions and 9 deletions

View File

@ -0,0 +1,33 @@
package com.example.applogin.model;
public class Session {
private String location_id;
private String charger_id;
private int id;
private User user;
public User getUser() {
return user;
}
public int getId() {
return id;
}
public String getCharger_id() {
return charger_id;
}
public String getLocation_id() {
return location_id;
}
public Session(String location_id, String charger_id, int id, User user){
this.location_id = location_id;
this.charger_id = charger_id;
this.id = id;
this.user = user;
}
}

View File

@ -0,0 +1,40 @@
package com.example.applogin.model;
public class User {
public String getUsername() {
return username;
}
public Type getType() {
return type;
}
public State getState() {
return state;
}
public enum Type{
user, admin, service
}
public enum State{
inactive, in_queue, assigned, connected_charging, connected_full
}
private String username;
private Type type;
private State state;
public User(String username, Type type, State state){
this.username = username;
this.type = type;
this.state = state;
}
@Override
public String toString() {
return username + " " + type + " " + state;
}
}

View File

@ -2,6 +2,9 @@ package com.example.applogin.network;
import android.os.AsyncTask;
import com.example.applogin.model.Session;
import com.example.applogin.model.User;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
@ -14,7 +17,7 @@ import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
public abstract class AbstractNetworkTask<X, Y, Z> extends AsyncTask<X, Y, Z> {
public abstract class AbstractNetworkTask<X, Y> extends AsyncTask<X, Void, Y> {
private static final int READ_TIMEOUT = 15000;
private static final int CONNECTION_TIMEOUT = 15000;
@ -58,7 +61,7 @@ public abstract class AbstractNetworkTask<X, Y, Z> extends AsyncTask<X, Y, Z> {
if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
connection.setRequestProperty(entry.getKey(), entry.getKey());
connection.setRequestProperty(entry.getKey(), entry.getValue());
}
}
@ -70,12 +73,14 @@ public abstract class AbstractNetworkTask<X, Y, Z> extends AsyncTask<X, Y, Z> {
connection.connect();
if(method == HTTPMethod.POST || method == HTTPMethod.PUT){
JSONObject body = new JSONObject(params);
OutputStream os = connection.getOutputStream();
byte[] input = body.toString().getBytes("utf-8");
os.write(input, 0, input.length);
os.flush();
os.close();
if (params != null) {
JSONObject body = new JSONObject(params);
OutputStream os = connection.getOutputStream();
byte[] input = body.toString().getBytes("utf-8");
os.write(input, 0, input.length);
os.flush();
os.close();
}
}
InputStreamReader streamReader = new InputStreamReader(connection.getInputStream());
@ -106,4 +111,26 @@ public abstract class AbstractNetworkTask<X, Y, Z> extends AsyncTask<X, Y, Z> {
return result;
}
protected User parseUser(JSONObject obj){
try {
return new User(obj.getString("username"),
User.Type.valueOf(obj.getString("type")),
User.State.valueOf(obj.getString("state")));
} catch (JSONException e) {
return null;
}
}
protected Session parseSession(JSONObject obj){
try {
User user = parseUser(obj.getJSONObject("user"));
return new Session(obj.getString("location_id"),
obj.getString("charger_id"),
obj.getInt("session_id"),
user);
} catch (JSONException e) {
return null;
}
}
}

View File

@ -0,0 +1,21 @@
package com.example.applogin.network;
public class DeQueueTask extends AbstractNetworkTask<Void, Boolean> {
private String api_key;
private String location_id;
public DeQueueTask(String location_id, String api_key){
super();
this.api_key = api_key;
this.location_id = location_id;
}
@Override
protected Boolean doInBackground(Void... params) {
Result result = makeAuthenticatedRequest(getAPIUrl(String.format("api/location/%s/queue", location_id)), HTTPMethod.DELETE, null, null, api_key);
return 200 <= result.statusCode && result.statusCode < 300;
}
}

View File

@ -0,0 +1,36 @@
package com.example.applogin.network;
import com.example.applogin.model.User;
import org.json.JSONException;
import org.json.JSONObject;
public class GetUserTask extends AbstractNetworkTask<Void, User> {
private String api_key;
public GetUserTask(String api_key){
super();
this.api_key = api_key;
}
@Override
protected User doInBackground(Void... params) {
Result result = makeAuthenticatedRequest(getAPIUrl("api/user"), HTTPMethod.GET, null, null, api_key);
try {
if (200 <= result.statusCode && result.statusCode < 300) {
JSONObject user = result.response.getJSONObject("user");
return parseUser(user);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}

View File

@ -4,7 +4,7 @@ import org.json.JSONException;
import java.util.HashMap;
public class LoginTask extends AbstractNetworkTask<Void, Void, String> {
public class LoginTask extends AbstractNetworkTask<Void, String> {
private String username;
private String password;

View File

@ -0,0 +1,21 @@
package com.example.applogin.network;
public class QueueTask extends AbstractNetworkTask<Void, Boolean> {
private String api_key;
private String location_id;
public QueueTask(String location_id, String api_key){
super();
this.api_key = api_key;
this.location_id = location_id;
}
@Override
protected Boolean doInBackground(Void... params) {
Result result = makeAuthenticatedRequest(getAPIUrl(String.format("api/location/%s/queue", location_id)), HTTPMethod.POST, null, null, api_key);
return 200 <= result.statusCode && result.statusCode < 300;
}
}