diff --git a/App/app/src/main/java/com/gazeboindustries/sextafeiramobile/Fragments/DevicesFragments/ViewDevicesFragment.java b/App/app/src/main/java/com/gazeboindustries/sextafeiramobile/Fragments/DevicesFragments/ViewDevicesFragment.java index 0cf12b7..d6ee60e 100644 --- a/App/app/src/main/java/com/gazeboindustries/sextafeiramobile/Fragments/DevicesFragments/ViewDevicesFragment.java +++ b/App/app/src/main/java/com/gazeboindustries/sextafeiramobile/Fragments/DevicesFragments/ViewDevicesFragment.java @@ -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; @@ -28,6 +34,8 @@ public class ViewDevicesFragment extends Fragment { private Button btnEditSend; private Button btnDeleteCancel; + private Button btnAction; + private Button btnOnOff; private Drawable cancelIcon; private Drawable saveIcon; @@ -35,6 +43,7 @@ public class ViewDevicesFragment extends Fragment { private Drawable editIcon; private Drawable removeIcon; + private int deviceAction; private int ID; private ServerConnection connection; @@ -42,6 +51,9 @@ public class ViewDevicesFragment extends Fragment { private AlertDialog.Builder removeAlert; private AlertDialog removeDialog; + private JSONObject status; + private JSONObject deviceStatus; + @SuppressLint("SetTextI18n") @Nullable @@ -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(); @@ -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?"); @@ -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()) { @@ -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()))); @@ -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; } } diff --git a/App/app/src/main/java/com/gazeboindustries/sextafeiramobile/ServerConnection.java b/App/app/src/main/java/com/gazeboindustries/sextafeiramobile/ServerConnection.java index f51559f..77c02f8 100644 --- a/App/app/src/main/java/com/gazeboindustries/sextafeiramobile/ServerConnection.java +++ b/App/app/src/main/java/com/gazeboindustries/sextafeiramobile/ServerConnection.java @@ -34,40 +34,71 @@ public class ServerConnection extends AsyncTask 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"); @@ -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 sendRequest(JSONObject request){ try { execute(request); @@ -267,14 +312,14 @@ 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(); @@ -282,4 +327,6 @@ public JSONObject prepareSetDevice(String request, String device, int action){ return jsonRequest; } + + } diff --git a/Server/Server.py b/Server/Server.py index 19ea1fc..62865bc 100644 --- a/Server/Server.py +++ b/Server/Server.py @@ -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') @@ -239,4 +239,4 @@ def handle(self): print('error') server = socketserver.ThreadingTCPServer((host, port), ClientManage) -server.serve_forever() +server.serve_forever() \ No newline at end of file