|
| 1 | +package com.rugged.application.hestia; |
| 2 | + |
| 3 | +import android.os.AsyncTask; |
| 4 | +import android.os.Bundle; |
| 5 | +import android.support.v4.app.Fragment; |
| 6 | +import android.text.Editable; |
| 7 | +import android.text.TextWatcher; |
| 8 | +import android.view.LayoutInflater; |
| 9 | +import android.view.View; |
| 10 | +import android.view.ViewGroup; |
| 11 | +import android.widget.Button; |
| 12 | +import android.widget.EditText; |
| 13 | + |
| 14 | +import java.util.UUID; |
| 15 | + |
| 16 | +//Fragment class taking care of a specific Peripheral |
| 17 | +public class PeripheralFragment extends Fragment { |
| 18 | + private Peripheral mPeripheral; |
| 19 | + //lock and unlock button |
| 20 | + private Button onButton; |
| 21 | + private Button offButton; |
| 22 | + @Override |
| 23 | + public void onCreate(Bundle savedInstanceState) { |
| 24 | + super.onCreate(savedInstanceState); |
| 25 | + //receive the id from the PeripheralActivity |
| 26 | + int peripheralId = (int) getActivity().getIntent() |
| 27 | + .getSerializableExtra(PeripheralActivity.EXTRA_PERIPHERAl_ID); |
| 28 | + mPeripheral = PeripheralLab.get(getActivity()).getPeripheral(peripheralId); |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 33 | + Bundle savedInstanceState) { |
| 34 | + View v = inflater.inflate(R.layout.fragment_peripheral, container, false); |
| 35 | + onButton = (Button)v.findViewById(R.id.on_button); |
| 36 | + offButton = (Button)v.findViewById(R.id.off_button); |
| 37 | + |
| 38 | + switch (mPeripheral.getType()) { |
| 39 | + case "Lock" : |
| 40 | + onButton.setText("Lock"); |
| 41 | + offButton.setText("Unlock"); |
| 42 | + break; |
| 43 | + case "Light" : |
| 44 | + onButton.setText("Light on"); |
| 45 | + offButton.setText("Light off"); |
| 46 | + break; |
| 47 | + default: |
| 48 | + onButton.setText("On"); |
| 49 | + offButton.setText("Off"); |
| 50 | + break; |
| 51 | + } |
| 52 | + |
| 53 | + onButton.setOnClickListener(new View.OnClickListener() { |
| 54 | + @Override |
| 55 | + public void onClick(View view) { |
| 56 | + new SendJSONFile().execute("172.20.10.2", "openLock"); |
| 57 | + } |
| 58 | + }); |
| 59 | + |
| 60 | + offButton.setOnClickListener(new View.OnClickListener() { |
| 61 | + @Override |
| 62 | + public void onClick(View view) { |
| 63 | + new SendJSONFile().execute("172.20.10.2", "closeLock"); |
| 64 | + |
| 65 | + } |
| 66 | + }); |
| 67 | + return v; |
| 68 | + } |
| 69 | + |
| 70 | + //Thread classes taking care of sending commands |
| 71 | + private class SendJSONFile extends AsyncTask<String,Void,Void> { |
| 72 | + @Override |
| 73 | + protected Void doInBackground(String... strings) { |
| 74 | + Client client = new Client("172.20.10.2", 8850); |
| 75 | + client.sendActionRequest(mPeripheral.getId(), "openLock"); |
| 76 | + client.closeClientSocket(); |
| 77 | + return null; |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments