Skip to content

Commit

Permalink
Merge pull request #46 from GabrielSirtoriCorrea/setandoDevicesStatusApp
Browse files Browse the repository at this point in the history
setandoDevicesStatusApp
  • Loading branch information
GabrielSirtoriCorrea authored Jul 21, 2020
2 parents 74c7388 + a2fc019 commit 487405f
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
import com.gazeboindustries.sextafeiramobile.R;
import com.gazeboindustries.sextafeiramobile.ServerConnection;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class ViewDevicesFragment extends Fragment {
private Intent intent;
private EditText txtDevice;
Expand All @@ -28,20 +34,26 @@ public class ViewDevicesFragment extends Fragment {

private Button btnEditSend;
private Button btnDeleteCancel;
private Button btnAction;
private Button btnOnOff;

private Drawable cancelIcon;
private Drawable saveIcon;

private Drawable editIcon;
private Drawable removeIcon;

private int deviceAction;
private int ID;

private ServerConnection connection;

private AlertDialog.Builder removeAlert;
private AlertDialog removeDialog;

private JSONObject status;
private JSONObject deviceStatus;


@SuppressLint("SetTextI18n")
@Nullable
Expand All @@ -54,6 +66,10 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
txtDevice = view.findViewById(R.id.txtViewDevice);
txtDescription = view.findViewById(R.id.txtViewDeviceDescription);
txtActions = view.findViewById(R.id.txtViewDeviceActions);
btnEditSend = view.findViewById(R.id.btnEditDevice);
btnDeleteCancel = view.findViewById(R.id.btnRemoveDevice);
btnAction = view.findViewById(R.id.btnActionDevice);
btnOnOff = view.findViewById(R.id.btnOnOffDevice);

intent = getActivity().getIntent();

Expand All @@ -62,8 +78,18 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
txtDescription.setText(intent.getStringExtra("Description"));
txtActions.setText(Integer.toString(intent.getIntExtra("Actions", 0)));

btnEditSend = view.findViewById(R.id.btnEditDevice);
btnDeleteCancel = view.findViewById(R.id.btnRemoveDevice);
try {
status = (JSONObject) connection.sendJSONRequest(connection.prepareRequest("getDevicesStatus"));
deviceStatus = (JSONObject) status.get(txtDevice.getText().toString());
System.out.println(deviceStatus.toString());
deviceAction = deviceStatus.getInt("action");
if(deviceAction != 0){
btnOnOff.setText("OFF");
}

} catch (JSONException e) {
e.printStackTrace();
}

removeAlert = new AlertDialog.Builder(view.getContext());
removeAlert.setMessage("Deseja remover o device?");
Expand All @@ -79,6 +105,8 @@ public void onClick(DialogInterface dialogInterface, int i) {
removeAlert.setPositiveButton("Excluir", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
connection = null;
connection = new ServerConnection();
connection.sendRequest(connection.prepareDelete("deleteDevice", ID, txtDevice.getText().toString()));

if(connection.getMsgStatus()) {
Expand Down Expand Up @@ -108,6 +136,8 @@ public void onClick(View view) {
btnDeleteCancel.setCompoundDrawablesWithIntrinsicBounds(cancelIcon, null, null, null);

}else{
connection = null;
connection = new ServerConnection();
connection.sendRequest(connection.prepareUpdateDevice("updateDevice",intent.getStringExtra("Device"), ID, txtDevice.getText().toString(), txtDescription.getText().toString(),
Integer.parseInt(txtActions.getText().toString())));

Expand Down Expand Up @@ -165,6 +195,37 @@ public void onClick(View view) {
}
});

btnOnOff.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
connection = null;
connection = new ServerConnection();

if(btnOnOff.getText().equals("ON")){
connection.sendRequest(connection.prepareSetDevice(txtDevice.getText().toString(), 1, ".com"));
deviceAction = 1;
btnOnOff.setText("OFF");
}else{
connection.sendRequest(connection.prepareSetDevice(txtDevice.getText().toString(), 0, ".com"));
deviceAction = 0;
btnOnOff.setText("ON");

}
}
});

btnAction.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
connection = null;
connection = new ServerConnection();
if(deviceAction != 0){
deviceAction++;
connection.sendRequest(connection.prepareSetDevice(txtDevice.getText().toString(), deviceAction, ".com"));
}
}
});

return view;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,40 +34,71 @@ public class ServerConnection extends AsyncTask<JSONObject, Integer, ArrayList<J
@Override
protected ArrayList<JSONArray> doInBackground(JSONObject... params) {
try {
this.socket = new Socket(IP, port);
this.out = new PrintWriter(this.socket.getOutputStream(), true);
this.in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
if(params[0].get("request").equals("getDevicesStatus")) {
this.socket = new Socket(IP, port);
this.out = new PrintWriter(this.socket.getOutputStream(), true);
this.in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));

this.out.println(params[0]);
this.out.println(params[0]);

this.msgStatus = true;
this.msgStatus = true;

try {
sleep(125);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
sleep(125);
} catch (InterruptedException e) {
e.printStackTrace();
}

this.buffer = new char[this.socket.getReceiveBufferSize()];
this.buffer = new char[this.socket.getReceiveBufferSize()];

this.in.read(this.buffer);
this.in.read(this.buffer);

this.data = new String(this.buffer);
this.data = new String(this.buffer);

this.jsonResponse = new JSONObject(this.data);
this.jsonResponse = new JSONObject(this.data);

System.out.println(this.jsonResponse);
System.out.println(this.jsonResponse);

this.list = new ArrayList<>();
this.socket.close();
this.out.close();
this.in.close();

for (int c = 0; c < jsonResponse.length(); c++){
this.arrayResponse = (JSONArray) this.jsonResponse.get(Integer.toString(c));
this.list.add(arrayResponse);
}
}else{
this.socket = new Socket(IP, port);
this.out = new PrintWriter(this.socket.getOutputStream(), true);
this.in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));

this.out.println(params[0]);

this.msgStatus = true;

try {
sleep(125);
} catch (InterruptedException e) {
e.printStackTrace();
}

this.buffer = new char[this.socket.getReceiveBufferSize()];

this.in.read(this.buffer);

this.socket.close();
this.out.close();
this.in.close();
this.data = new String(this.buffer);

this.jsonResponse = new JSONObject(this.data);

System.out.println(this.jsonResponse);

this.list = new ArrayList<>();

for (int c = 0; c < jsonResponse.length(); c++) {
this.arrayResponse = (JSONArray) this.jsonResponse.get(Integer.toString(c));
this.list.add(arrayResponse);
}

this.socket.close();
this.out.close();
this.in.close();
}

} catch (IOException | JSONException e) {
System.out.println("DEU ERRO");
Expand All @@ -80,6 +111,20 @@ public boolean getMsgStatus(){
return this.msgStatus;
}

public JSONObject sendJSONRequest(JSONObject request){
try {
execute(request);
while(this.jsonResponse == null){
sleep(100);
}
} catch (InterruptedException e) {
e.printStackTrace();
}

return this.jsonResponse;

}

public ArrayList<JSONArray> sendRequest(JSONObject request){
try {
execute(request);
Expand Down Expand Up @@ -267,19 +312,21 @@ public JSONObject prepareDelete(String request, int deleteId, String name){
return jsonRequest;
}

public JSONObject prepareSetDevice(String request, String device, int action){
public JSONObject prepareSetDevice(String device, int action, String url){
try {
this.jsonRequest = new JSONObject();
this.jsonRequest.put("header", "gazeboindustries09082004");
this.jsonRequest.put("request", request);
this.jsonRequest.put("request", "setDevicesStatus");
this.jsonRequest.put("receiverID", device);
this.jsonRequest.put("action", action);
this.jsonRequest.put("url", "url");
this.jsonRequest.put("url", url);

} catch (JSONException e) {
e.printStackTrace();
}
return jsonRequest;

}


}
4 changes: 2 additions & 2 deletions Server/Server.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def convertHeader(header):
def getDevicesStatus():
file = open('E:/Sexta-Feira-Mark_6/Server/DevicesStatus.json', 'r')
return json.load(file)

def setDevicesStatus(receiverID, action, url):
readFile = open('E:/Sexta-Feira-Mark_6/Server/DevicesStatus.json', 'r')

Expand Down Expand Up @@ -239,4 +239,4 @@ def handle(self):
print('error')

server = socketserver.ThreadingTCPServer((host, port), ClientManage)
server.serve_forever()
server.serve_forever()

0 comments on commit 487405f

Please sign in to comment.