Add devices type functionality

This commit is contained in:
Mateusz Wankowski 2023-01-10 14:32:18 +01:00
parent b1065bfeea
commit ae3ecfa943
12 changed files with 304 additions and 83 deletions

View File

@ -2,13 +2,21 @@ package com.example.wrsd;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import java.util.List;
public class AddDeviceActivity extends AppCompatActivity {
static String[] deviceTypes = new String[]{"Blinds", "Bulb", "Heater"};
Spinner deviceTypeDropdown;
EditText editTextTextName;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -16,6 +24,12 @@ public class AddDeviceActivity extends AppCompatActivity {
setContentView(R.layout.activity_add_device);
editTextTextName = (EditText) findViewById(R.id.editTextTextName);
deviceTypeDropdown = (Spinner) findViewById(R.id.deviceTypeDropdown);
ArrayAdapter<String> adapter = new ArrayAdapter<>(AddDeviceActivity.this, android.R.layout.simple_spinner_item, deviceTypes);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
deviceTypeDropdown.setAdapter(adapter);
}
@ -30,8 +44,10 @@ public class AddDeviceActivity extends AppCompatActivity {
public void DeviceName(View view){
String editTextName = editTextTextName.getText().toString().trim();
String deviceType = deviceTypeDropdown.getSelectedItem().toString();
Intent intent = new Intent();
intent.putExtra("name", editTextName);
intent.putExtra("type", deviceType);
setResult(RESULT_OK, intent);
finish();
}

View File

@ -1,2 +1,25 @@
package com.example.wrsd;public class BlindsDevice {
package com.example.wrsd;
public class BlindsDevice extends Device{
public boolean state;
public BlindsDevice(String id, String name) {
super(id, name, DeviceType.BLINDS);
state = false;
}
public boolean isState() {
return state;
}
public void setState(boolean state) {
this.state = state;
}
@Override
public String toString() {
String stateString = state? "1" : "0";
return deviceType.toString() + name + "?" + stateString;
}
}

View File

@ -1,2 +1,37 @@
package com.example.wrsd;public class BulbDevice {
package com.example.wrsd;
public class BulbDevice extends Device{
public boolean state;
public int brightness;
public BulbDevice(String id, String name) {
super(id, name, DeviceType.BULB);
this.state = false;
this.brightness = 0;
}
public boolean isState() {
return state;
}
public void setState(boolean state) {
this.state = state;
}
public int getBrightness() {
return brightness;
}
public void setBrightness(int brightness) {
this.brightness = brightness;
}
@Override
public String toString() {
String stateString = state? "1" : "0";
String brightnessString = Integer.toString(brightness);
return deviceType.toString() + name + "?" + stateString + brightnessString;
}
}

View File

@ -4,10 +4,12 @@ public class Device {
String id;
String name;
DeviceType deviceType;
public Device(String id, String name) {
public Device(String id, String name, DeviceType deviceType) {
this.id = id;
this.name = name;
this.deviceType = deviceType;
}
public String getId() {
@ -25,4 +27,9 @@ public class Device {
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return deviceType.toString() + id;
}
}

View File

@ -12,6 +12,8 @@ import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
@ -24,7 +26,10 @@ import com.google.firebase.firestore.FirebaseFirestore;
public class DeviceSettings extends AppCompatActivity {
private Button deleteButton;
private String device, userEmail, room;
private TextView progressLabel;
private Switch stateSwitch;
private SeekBar progressBar;
private String device, userEmail, type, room, code;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -39,13 +44,40 @@ public class DeviceSettings extends AppCompatActivity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_device_settings);
progressLabel = (TextView) findViewById(R.id.progressLabel);
stateSwitch = (Switch) findViewById(R.id.state);
progressBar = (SeekBar) findViewById(R.id.progress);
progressLabel.setVisibility(View.INVISIBLE);
stateSwitch.setVisibility(View.INVISIBLE);
progressBar.setVisibility(View.INVISIBLE);
Bundle extras = getIntent().getExtras();
if (extras != null) {
device = extras.getString("device");
userEmail = extras.getString("email");
room = extras.getString("room");
type = extras.getString("type");
code = extras.getString("code");
//The key argument here must match that used in the other activity
if (type.equals("Blinds")) {
stateSwitch.setText("Up/Down");
stateSwitch.setVisibility(View.VISIBLE);
} else if (type.equals("Bulb")) {
stateSwitch.setText("On/Off");
progressLabel.setText("Brightness");
stateSwitch.setVisibility(View.VISIBLE);
progressLabel.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.VISIBLE);
} else if (type.equals("Heater")) {
stateSwitch.setText("On/Off");
progressLabel.setText("Temperature");
stateSwitch.setVisibility(View.VISIBLE);
progressLabel.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.VISIBLE);
}
}
deleteButton = (Button) findViewById(R.id.deleteButton);
@ -57,7 +89,7 @@ public class DeviceSettings extends AppCompatActivity {
public void onClick(View v){
DocumentReference docRef = rootRef.collection("users").document(userEmail).collection("rooms").document(room);
docRef.update("Urzadzenia", FieldValue.arrayRemove(device));
docRef.update("Urzadzenia", FieldValue.arrayRemove(code));
finish();

View File

@ -1,2 +1,18 @@
package com.example.wrsd;public enum DeviceType {
package com.example.wrsd;
public enum DeviceType {
BLINDS("b"),
BULB("l"),
HEATER("h");
private String shortCut;
DeviceType(String shortcut) {
this.shortCut = shortcut;
}
@Override
public String toString() {
return this.shortCut;
}
}

View File

@ -1,2 +1,37 @@
package com.example.wrsd;public class HeaterDevice {
package com.example.wrsd;
public class HeaterDevice extends Device {
public boolean state;
public int temperature;
public HeaterDevice(String id, String name) {
super(id, name, DeviceType.HEATER);
this.state = false;
this.temperature = 20;
}
public boolean isState() {
return state;
}
public void setState(boolean state) {
this.state = state;
}
public int getTemperature() {
return temperature;
}
public void setTemperature(int temperature) {
this.temperature = temperature;
}
@Override
public String toString() {
String stateString = state? "1" : "0";
String temperatureString = Integer.toString(temperature);
return deviceType.toString() + name + "?" + stateString + temperatureString;
}
}

View File

@ -28,8 +28,14 @@ import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.firestore.FirebaseFirestore;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import kotlin.text.Typography;
@ -116,6 +122,7 @@ public class RegisterUser extends AppCompatActivity{
mAuth.createUserWithEmailAndPassword(email, password).addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
Rooms();
Toast.makeText(getApplicationContext(), "Użytkownik zarejestrowany", Toast.LENGTH_LONG).show();
Login();
@ -128,7 +135,25 @@ public class RegisterUser extends AppCompatActivity{
}
});
}
public void Rooms() {
String email = editTextEmail.getText().toString().trim();
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
Map<String, Object> room = new HashMap<>();
List<String> rooms = new ArrayList<>();
rooms.add("Salon");
rooms.add("Sypialnia");
rooms.add("Kuchnia");
rooms.add("Lazienka");
rooms.add("Toaleta");
Map<String, Object> docData = new HashMap<>();
docData.put("Urzadzenia", Arrays.asList());
for (String r1 : rooms) {
rootRef.collection("users").document(email).collection("rooms").document(r1).set(docData);
}
}
public void Login() {
Intent intent = new Intent(this, LoginActivity.class);

View File

@ -11,34 +11,20 @@ import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FieldValue;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
@ -51,11 +37,9 @@ public class RoomActivity_1 extends AppCompatActivity {
private RecyclerView recyclerView;
private SingleRoomAdapter mAdapter;
private String userEmail;
Integer roomIndex;
public String roomvalue;
private TextView roomName1;
TextView myTextView;
private TextView deviceName;
public static void setWindowFlag(Activity activity, final int bits, boolean on) {
@ -121,28 +105,19 @@ public class RoomActivity_1 extends AppCompatActivity {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
if (documentSnapshot!=null) {
temp.add(documentSnapshot.getData().toString());
if(!temp.get(0).equals("{Urzadzenia=[]}")) {
Map<String, Object> data = documentSnapshot.getData();
if (data != null) {
temp.add(data.toString());
if (!temp.get(0).equals("{Urzadzenia=[]}")) {
String[] parts = temp.get(0).split(Pattern.quote("["));
String[] p2 = parts[1].split(Pattern.quote("]"));
String[] devices = p2[0].split("(,)|(\\s+)");
for (Integer i = 0; i < devices.length; i = i + 2) {
deviceFromBase.add(devices[i]);
}
Device d1;
Integer j = 0;
for (String d : deviceFromBase) {
String jStr = Integer.toString(j);
d1 = new Device(jStr, d);
deviceList.add(d1);
}
decodeDevices(devices);
mAdapter.notifyDataSetChanged();
}
else{
} else {
Toast.makeText(getApplicationContext(), "Brak urządzeń w pokoju", Toast.LENGTH_LONG).show();
}
}
} else {
Toast.makeText(getApplicationContext(), "Brak urządzeń w pokoju", Toast.LENGTH_LONG).show();
}
@ -157,9 +132,35 @@ public class RoomActivity_1 extends AppCompatActivity {
});
}
private void decodeDevices(String[] devicesNames) {
for (String deviceName : devicesNames) {
decodeDevice(deviceName);
}
}
private void decodeDevice(String encodedDevice) {
if (encodedDevice.equals("")) {
return;
}
String[] parts = encodedDevice.split("\\?");
String deviceType = parts[0].substring(0,1);
String deviceName = parts[0].substring(1);
String deviceId = getLastUsedId();
if (deviceType.equals(DeviceType.BLINDS.toString())) {
deviceList.add(new BlindsDevice(deviceId, deviceName));
} else if (deviceType.equals(DeviceType.BULB.toString())) {
deviceList.add(new BulbDevice(deviceId, deviceName));
} else if (deviceType.equals(DeviceType.HEATER.toString())) {
deviceList.add(new HeaterDevice(deviceId, deviceName));
}
}
private String getLastUsedId() {
String lastIdString = deviceList.isEmpty()? "0" : deviceList.get(deviceList.size() - 1).id;
int incrementedId = Integer.parseInt(lastIdString) + 1;
return Integer.toString(incrementedId);
}
// CollectionReference usersCol = rootRef.collection("users");
// Query query = usersCol.whereEqualTo("email", userEmail).whereEqualTo("rooms", roomvalue);
@ -183,35 +184,6 @@ public class RoomActivity_1 extends AppCompatActivity {
// }
// private void prepareRoomData() {
// getData();
//
// Device d1;
// Integer j = 0;
//
// for(String d: deviceFromBase){
// String jStr = Integer.toString(j);
// d1 = new Device(jStr, d);
// deviceList.add(d1);
// }
// d1 = new Device("1", "Oświetlenie");
// deviceList.add(d1);
// d1 = new Device("2", "Głośniki");
// deviceList.add(d1);
// d1 = new Device("3", "Rolety");
// deviceList.add(d1);
// d1 = new Device("4", "Ogrzewanie");
// deviceList.add(d1);
// d1 = new Device("5", "Odkurzacz");
// deviceList.add(d1);
// mAdapter.notifyDataSetChanged();
// }
public void onBackClicked(View view) {
startActivity(new Intent(getApplicationContext(), MainActivity.class));
finish();
@ -229,13 +201,25 @@ public class RoomActivity_1 extends AppCompatActivity {
if(resultCode == RESULT_OK) {
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
String strEditText = data.getStringExtra("name");
Device d1 = new Device("6", strEditText);
deviceList.add(d1);
String deviceType = data.getStringExtra("type");
Device newDevice;
String deviceId = getLastUsedId();
if (deviceType.equals("Blinds")) {
newDevice = new BlindsDevice(deviceId, strEditText);
} else if (deviceType.equals("Bulb")) {
newDevice = new BulbDevice(deviceId, strEditText);
} else if (deviceType.equals("Heater")) {
newDevice = new HeaterDevice(deviceId, strEditText);
} else {
newDevice = new BlindsDevice(deviceId, strEditText);
}
deviceList.add(newDevice);
mAdapter.notifyDataSetChanged();
DocumentReference docRef = rootRef.collection("users").document(userEmail).collection("rooms").document(roomvalue);
docRef.update("Urzadzenia", FieldValue.arrayUnion(strEditText));
docRef.update("Urzadzenia", FieldValue.arrayUnion(newDevice.toString()));
}
}
@ -245,10 +229,26 @@ public class RoomActivity_1 extends AppCompatActivity {
TextView b = (TextView) view;
String dn = b.getText().toString();
String type = "";
String code = "";
for (Device device : deviceList) {
if (device.name.equals(dn)) {
code = device.toString();
if (device.deviceType == DeviceType.BLINDS) {
type = "Blinds";
} else if (device.deviceType == DeviceType.BULB) {
type = "Bulb";
} else if (device.deviceType == DeviceType.HEATER) {
type = "Heater";
}
}
}
Intent intent = new Intent(this, DeviceSettings.class);
intent.putExtra("device", dn);
intent.putExtra("email", userEmail);
intent.putExtra("room", roomvalue);
intent.putExtra("type", type);
intent.putExtra("code", code);
startActivity(intent);
finish();

View File

@ -40,13 +40,25 @@
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.775" />
<Spinner
android:id="@+id/deviceTypeDropdown"
android:layout_width="216dp"
android:layout_height="49dp"
android:layout_marginStart="24dp"
android:layout_marginBottom="20dp"
app:layout_constraintBottom_toTopOf="@+id/editTextTextName"
app:layout_constraintEnd_toEndOf="@+id/editTextTextName"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
tools:ignore="SpeakableTextPresentCheck" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dodaj"
android:clickable="true"
android:onClick="DeviceName"
android:text="Dodaj"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.863"

View File

@ -16,6 +16,36 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintTop_toBottomOf="@+id/progress"
app:layout_constraintVertical_bias="0.136" />
<SeekBar
android:id="@+id/progress"
android:layout_width="142dp"
android:layout_height="23dp"
android:layout_marginTop="28dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/progressLabel" />
<TextView
android:id="@+id/progressLabel"
android:layout_width="93dp"
android:layout_height="50dp"
android:layout_marginTop="28dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/state" />
<Switch
android:id="@+id/state"
android:layout_width="117dp"
android:layout_height="59dp"
android:layout_marginTop="48dp"
android:text="Switch"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -1,10 +0,0 @@
## This file is automatically generated by Android Studio.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file should *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
sdk.dir=C\:\\Users\\Acer\\AppData\\Local\\Android\\Sdk