Skip to content

Commit 3a2ccf3

Browse files
authored
Added a password authentication for the PIs & added the delete function
1 parent 2c9c25e commit 3a2ccf3

File tree

7 files changed

+138
-55
lines changed

7 files changed

+138
-55
lines changed

390application/app/src/main/java/com/example/a390application/DatabaseHelper.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ public interface DataStatus{
2929
}
3030

3131
void readPlants(final DataStatus dataStatus){
32-
3332
reference.addValueEventListener(new ValueEventListener() {
3433
@Override
3534
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

390application/app/src/main/java/com/example/a390application/InsertPlant/InsertPlant.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,20 @@ public void modifyPlant(Plant newPlantData) {
147147
}
148148
}
149149

150+
public void removePlant(Plant newPlantData) {
151+
SQLiteDatabase db = this.getWritableDatabase();
152+
153+
try {
154+
db.delete(Config.PLANT_TABLE_NAME, Config.COLUMN_PLANT_NAME + "=?", new String[]{newPlantData.getName()});
155+
}
156+
catch (SQLiteException e) {
157+
Toast.makeText(context, "Operation Failed!: " + e, Toast.LENGTH_LONG).show();
158+
}
159+
finally {
160+
db.close();
161+
}
162+
}
163+
150164
public String checkUID(){
151165
SQLiteDatabase db = this.getWritableDatabase();
152166

390application/app/src/main/java/com/example/a390application/InsertPlant/PI.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@ public class PI {
44

55
private String PlantName;
66
private String OwnerID;
7+
private String Password;
78

8-
public PI(String PlantName, String id) {
9+
10+
11+
public PI(String PlantName, String id, String pw) {
912
this.PlantName = PlantName;
1013
OwnerID = id;
14+
Password = pw;
1115
}
1216

1317
public String getName() {
@@ -25,4 +29,12 @@ public String getOwnerID() {
2529
public void setOwnerID(String ownerID) {
2630
OwnerID = ownerID;
2731
}
32+
33+
public String getPassword() {
34+
return Password;
35+
}
36+
37+
public void setPassword(String password) {
38+
Password = password;
39+
}
2840
}

390application/app/src/main/java/com/example/a390application/LinkPiDialogFragment.java

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
import com.example.a390application.InsertPlant.InsertPlant;
1818
import com.example.a390application.InsertPlant.PI;
1919
import com.example.a390application.InsertPlant.Plant;
20+
import com.google.firebase.database.DataSnapshot;
21+
import com.google.firebase.database.DatabaseError;
2022
import com.google.firebase.database.DatabaseReference;
2123
import com.google.firebase.database.FirebaseDatabase;
24+
import com.google.firebase.database.ValueEventListener;
2225

2326
import java.util.ArrayList;
2427
import java.util.List;
@@ -29,6 +32,7 @@ public class LinkPiDialogFragment extends DialogFragment implements AdapterView.
2932

3033
private List<Plant> plants;
3134
private EditText PiIdEditText;
35+
private EditText PiPwEditText;
3236
private String plantPicked;
3337
protected String ownerID;
3438
protected ArrayList<String> currentPlants = new ArrayList<>();
@@ -42,6 +46,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
4246
View view = inflater.inflate(R.layout.fragment_link_pi, container, false);
4347

4448
PiIdEditText = view.findViewById(R.id.PiIdEditText);
49+
PiPwEditText = view.findViewById(R.id.PwEditText);
4550
Spinner plantOptionsSpinner = view.findViewById(R.id.plantOptions);
4651

4752

@@ -53,7 +58,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
5358
plantOptionsSpinner.setOnItemSelectedListener(this);
5459

5560
note = view.findViewById(R.id.note);
56-
note.setText("Please Note: \nIf the all sensors of the Raspberry PI you are linking are already in use, the oldest link will be removed and will be replaced by this new link.");
61+
note.setText("Please Note: \nIf all the sensors of the Raspberry PI you are linking are already in use, the oldest link will be removed and will be replaced by this new link.");
5762

5863
Button linkButton = view.findViewById(R.id.linkButton);
5964
Button cancelPlantButton = view.findViewById(R.id.cancelButton);
@@ -62,20 +67,37 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
6267
@Override
6368
public void onClick(View v) {
6469

65-
String PiId = PiIdEditText.getText().toString();
6670

67-
InsertPlant dbInsertPlant = new InsertPlant(getActivity());
68-
plants = dbInsertPlant.getAllPlants();
6971

70-
for(int i = 0; i < plants.size(); i++) {
71-
if(plants.get(i).getName().equals(plantPicked)){
72-
storePlantInDatabase(plants.get(i), PiId);
73-
break;
74-
}
72+
final String PiId = PiIdEditText.getText().toString();
73+
final String PiPassword = PiPwEditText.getText().toString();
74+
75+
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
76+
77+
if(!PiPassword.equals("") && !PiPassword.contains(".") && !PiPassword.contains("#") && !PiPassword.contains("$") && !PiPassword.contains("[") && !PiPassword.contains("]")) {
78+
reference.addListenerForSingleValueEvent(new ValueEventListener() {
79+
@Override
80+
public void onDataChange(DataSnapshot dataSnapshot) {
81+
try {
82+
checkPiPassword(PiId, dataSnapshot.child("PIs").child(PiId).child("password").getValue().toString(), PiPassword);
83+
Toast.makeText(getContext(), dataSnapshot.child("PIs").child(PiId).child("password").getValue().toString(), Toast.LENGTH_SHORT).show();
84+
} catch (Exception e) {
85+
Toast.makeText(getContext(), e.getMessage().toString(), Toast.LENGTH_LONG).show();
86+
Toast.makeText(getContext(), "There is no PI with that Id!", Toast.LENGTH_SHORT).show();
87+
}
88+
}
89+
90+
@Override
91+
public void onCancelled(DatabaseError databaseError) {
92+
93+
}
94+
});
95+
}
96+
else{
97+
Toast.makeText(getContext(), "Plant name must not be empty nor contain any of '.#$[]'", Toast.LENGTH_SHORT).show();
7598
}
7699

77100
getDialog().dismiss();
78-
79101
}
80102
});
81103

@@ -101,10 +123,25 @@ public void onNothingSelected(AdapterView<?> parent) {
101123

102124
}
103125

104-
private void storePlantInDatabase(Plant givenPlant, String PiId) {
105-
DatabaseReference userReference = FirebaseDatabase.getInstance().getReference().child("Users").child(ownerID).child(givenPlant.getName());
106-
userReference.setValue(givenPlant);
126+
private void checkPiPassword(String PiId, String PiPassword, String givenPassword) {
127+
if (PiPassword.equals(givenPassword)) {
128+
InsertPlant dbInsertPlant = new InsertPlant(getActivity());
129+
plants = dbInsertPlant.getAllPlants();
130+
131+
for (int i = 0; i < plants.size(); i++) {
132+
if (plants.get(i).getName().equals(plantPicked)) {
133+
linkPlant(plants.get(i), PiId,PiPassword);
134+
break;
135+
}
136+
}
137+
} else {
138+
Toast.makeText(getContext(), "Password is incorrect!", Toast.LENGTH_SHORT).show();
139+
}
140+
}
141+
142+
143+
private void linkPlant(Plant givenPlant, String PiId, String password) {
107144
DatabaseReference PiReference = FirebaseDatabase.getInstance().getReference().child("PIs").child(PiId);
108-
PiReference.setValue(new PI(givenPlant.getName(),givenPlant.getOwnerID()));
145+
PiReference.setValue(new PI(givenPlant.getName(),givenPlant.getOwnerID(),password));
109146
}
110147
}

390application/app/src/main/java/com/example/a390application/MainActivity.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@
2020
import androidx.core.app.NotificationCompat.InboxStyle;
2121
import androidx.core.app.NotificationManagerCompat;
2222
import com.example.a390application.InsertPlant.InsertPlant;
23+
import com.example.a390application.InsertPlant.PI;
2324
import com.example.a390application.InsertPlant.Plant;
2425
import com.google.android.material.floatingactionbutton.FloatingActionButton;
26+
import com.google.firebase.database.DatabaseReference;
27+
import com.google.firebase.database.FirebaseDatabase;
28+
2529
import java.util.ArrayList;
2630
import java.util.List;
2731

@@ -43,7 +47,12 @@ public class MainActivity extends AppCompatActivity {
4347
protected void onCreate(Bundle savedInstanceState) {
4448
super.onCreate(savedInstanceState);
4549
setContentView(R.layout.activity_main);
46-
50+
51+
/*USED TO CREATE A NEW PI ID*/
52+
//Plant plant = new Plant("DEFAULT","Spider","DEFAULT");
53+
//DatabaseReference PiReference = FirebaseDatabase.getInstance().getReference().child("PIs").child("123");
54+
//PiReference.setValue(new PI(plant.getName(),plant.getOwnerID(),"password"));
55+
4756
notificationManager = NotificationManagerCompat.from(this);
4857

4958
uniqueID = dbInsertPlant.checkUID();
@@ -224,7 +233,7 @@ else if((moistureSmileyEnglishIvy(plants.get(i))<75) && (moistureSmileyEnglishIv
224233
tempData+= " " + emojiSad + "\n";
225234
}
226235
}
227-
//********************************************// //*************************************//
236+
228237
else if(plants.get(i).getType().equals("Spider")){
229238
if(moistureSmileySpider(plants.get(i))>=75){
230239
tempData+= " " + emojiHappy + "\n";
@@ -288,7 +297,7 @@ else if((tempSmileyEnglishIvy(plants.get(i))<75) && (tempSmileySansevieria(plant
288297
tempData+= " " + emojiSad + "\n";
289298
}
290299
}
291-
//********************************************// //*************************************//
300+
292301
else if(plants.get(i).getType().equals("Spider")){
293302
if(tempSmileySpider(plants.get(i))>=75){
294303
tempData+= " " + emojiHappy + "\n";
@@ -701,22 +710,20 @@ protected double healthBarAlgoSpider(Plant givenPlant){ //specifically for
701710
//double FinalPercentage = 0.33*FinalMoisturePercentage + 0.33*FinalLightIntensityPercentage + 0.33*FinalTemperaturePercentage; // weighting , all equal 1/3
702711
return 0.5*FinalMoisturePercentage + 0.5*FinalTemperaturePercentage;
703712
}
704-
//********************************************// //*************************************//
705-
protected double moistureSmileySpider(Plant givenPlant){ //specifically for Sansevieria
706713

714+
protected double moistureSmileySpider(Plant givenPlant){ //specifically for Sansevieria
707715
double FinalMoisturePercentage;
708716

709-
if (givenPlant.getMoisture() >= 767){
710-
FinalMoisturePercentage = 100-((givenPlant.getMoisture()-512)*0.1953); // 1023 pts (Dry) = 0%
711-
} // 512 pts = 100%
717+
if (givenPlant.getMoisture() >= 256){
718+
FinalMoisturePercentage = Math.abs(100-((givenPlant.getMoisture()-256)*0.1304)); // 1023 pts (Dry) = 0%
719+
} // 256 pts = 100%
712720
else{
713-
FinalMoisturePercentage = givenPlant.getMoisture() * 0.1953; // 0 pts = 0 %
721+
FinalMoisturePercentage = 50 + (givenPlant.getMoisture() * 0.1953); // 0 pts (wet) = 50%
714722
}
715723

716724
return FinalMoisturePercentage;
717725
}
718726

719-
//********************************************// //*************************************//
720727

721728
protected double tempSmileySpider(Plant givenPlant){ //specifically for Sansevieria
722729

390application/app/src/main/java/com/example/a390application/inspectPlantActivity.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.app.Notification;
44
import android.content.Intent;
5+
import android.database.sqlite.SQLiteDatabase;
56
import android.graphics.Bitmap;
67
import android.graphics.BitmapFactory;
78
import android.os.Bundle;
@@ -57,7 +58,7 @@ protected void onCreate(Bundle savedInstanceState) {
5758

5859
givenPlant = null;
5960

60-
InsertPlant dbInsertPlant = new InsertPlant(this);
61+
final InsertPlant dbInsertPlant = new InsertPlant(this);
6162

6263
List<Plant> plants = dbInsertPlant.getAllPlants();
6364
for (int i = 0; i < plants.size(); i++) {
@@ -93,8 +94,6 @@ protected void onCreate(Bundle savedInstanceState) {
9394
else
9495
plantImage.setImageBitmap(bitmap);
9596
break;
96-
97-
9897
}
9998
case "Sansevieria": {
10099
Bitmap bitmap = new ImageSaver(this).
@@ -188,32 +187,28 @@ public void onClick(View v) {
188187
Toast.makeText(getApplicationContext(), "I want image", Toast.LENGTH_SHORT).show();
189188
dispatchTakePictureIntent();
190189

191-
192-
193190
}
194191
});
195192

196193

197194

198195

199-
//'Delete Plant' button not yet implemented
200-
/*deletePlantButton.setOnClickListener(new View.OnClickListener() {
196+
deletePlantButton.setOnClickListener(new View.OnClickListener() {
201197
@Override
202198
public void onClick(View v) {
203199

204-
//https://developer.android.com/training/data-storage/sqlite check here
200+
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Users").child(givenPlant.getOwnerID()).child(givenPlant.getName());
201+
reference.removeValue();
205202

206-
}
207-
});*/
203+
dbInsertPlant.removePlant(givenPlant);
208204

205+
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
206+
startActivity(intent);
207+
}
208+
});
209209
}
210210

211211

212-
//'Delete Plant' button not yet implemented.
213-
/*public void removePlantByID(long id){
214-
}*/
215-
216-
217212
@Override
218213
protected void onStart() {
219214
super.onStart();

390application/app/src/main/res/layout/fragment_link_pi.xml

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,31 @@
2727
android:layout_alignParentRight="true"
2828
android:layout_marginStart="30dp"
2929
android:layout_marginLeft="30dp"
30-
android:layout_marginTop="41dp"
30+
android:layout_marginTop="35dp"
3131
android:layout_marginEnd="26dp"
3232
android:layout_marginRight="26dp"
3333
android:ems="10"
3434
android:hint="ID of the Raspberry Pi Kit"
3535
android:inputType="textPersonName" />
3636

37+
<EditText
38+
android:id="@+id/PwEditText"
39+
android:layout_width="wrap_content"
40+
android:layout_height="103dp"
41+
android:layout_below="@+id/PiIdEditText"
42+
android:layout_alignParentStart="true"
43+
android:layout_alignParentLeft="true"
44+
android:layout_alignParentEnd="true"
45+
android:layout_alignParentRight="true"
46+
android:layout_marginStart="30dp"
47+
android:layout_marginLeft="30dp"
48+
android:layout_marginTop="35dp"
49+
android:layout_marginEnd="26dp"
50+
android:layout_marginRight="26dp"
51+
android:ems="10"
52+
android:hint="Password"
53+
android:inputType="textPersonName" />
54+
3755
<Button
3856
android:id="@+id/cancelButton"
3957
android:layout_width="wrap_content"
@@ -53,28 +71,29 @@
5371
android:layout_height="95dp"
5472
android:layout_alignParentStart="true"
5573
android:layout_alignParentLeft="true"
74+
android:layout_alignParentTop="true"
5675
android:layout_alignParentEnd="true"
5776
android:layout_alignParentRight="true"
58-
android:layout_marginStart="30dp"
59-
android:layout_marginLeft="30dp"
60-
android:layout_marginTop="100dp"
61-
android:layout_marginEnd="23dp"
62-
android:layout_marginRight="23dp" />
77+
android:layout_marginStart="28dp"
78+
android:layout_marginLeft="28dp"
79+
android:layout_marginTop="65dp"
80+
android:layout_marginEnd="25dp"
81+
android:layout_marginRight="25dp" />
6382

6483
<TextView
6584
android:id="@+id/note"
66-
android:layout_width="349dp"
67-
android:layout_height="173dp"
68-
android:layout_below="@+id/PiIdEditText"
85+
android:layout_width="wrap_content"
86+
android:layout_height="98dp"
87+
android:layout_below="@+id/PwEditText"
6988
android:layout_alignParentStart="true"
7089
android:layout_alignParentLeft="true"
7190
android:layout_alignParentEnd="true"
7291
android:layout_alignParentRight="true"
73-
android:layout_marginStart="32dp"
74-
android:layout_marginLeft="32dp"
75-
android:layout_marginTop="49dp"
76-
android:layout_marginEnd="30dp"
77-
android:layout_marginRight="30dp"
92+
android:layout_marginStart="29dp"
93+
android:layout_marginLeft="29dp"
94+
android:layout_marginTop="35dp"
95+
android:layout_marginEnd="32dp"
96+
android:layout_marginRight="32dp"
7897
android:text="TextView" />
7998

8099
</RelativeLayout>

0 commit comments

Comments
 (0)