-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmartEntrance.kt
164 lines (121 loc) · 4.59 KB
/
SmartEntrance.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package com.example.iot_assignment
import android.content.Context
import android.graphics.Color
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.os.Bundle
import android.service.autofill.Sanitizer
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.google.firebase.database.*
class SmartEntrance : AppCompatActivity(), SensorEventListener {
private var sensorManager: SensorManager? = null
private var sensor : Sensor? = null
private var resume = false
lateinit var open: Button
lateinit var close: Button
lateinit var text: TextView
lateinit var seatText: TextView
var database = FirebaseDatabase.getInstance("https://bait2123-202101-12-default-rtdb.firebaseio.com/")
var data = database.getReference("PI_12_CONTROL")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_smart_entrance)
resetFBData()
sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager?
sensor = sensorManager!!.getDefaultSensor(Sensor.TYPE_PROXIMITY)
open = findViewById(R.id.open)
close = findViewById(R.id.close)
text = findViewById(R.id.text)
val toolbar: TextView = findViewById(R.id.toolbar_title)
toolbar.setText("Smart Entrance")
val back: ImageView = findViewById(R.id.arrow_back_icon)
back.setOnClickListener {
onBackPressed()
}
open.setOnClickListener {
on()
}
close.setOnClickListener {
off()
}
}
override fun onSensorChanged(event: SensorEvent?) {
text = findViewById(R.id.text)
if (event?.sensor?.type == Sensor.TYPE_PROXIMITY) {
val light1 = event.values[0]
val sensordetect = sensor?.maximumRange
if (light1 < sensordetect!!){
on()
}
else
off()
}
}
private fun on(){
resetFBData()
data.child("relay1").setValue("1")
data.child("relay2").setValue("0")
data.child("lcdbkB").setValue("0")
data.child("lcdbkG").setValue("50")
data.child("lcdbkR").setValue("50")
data.child("oledsc").setValue("1")
data.child("lcdscr").setValue("1")
data.child("buzzer").setValue("1")
data.child("lcdtxt").setValue("Door is open....")
data.child("camera").setValue("1")
text.setText("Door is opening !!!")
window.decorView.setBackgroundColor(Color.CYAN)
}
private fun off(){
resetFBData()
data.child("relay1").setValue("0")
data.child("relay2").setValue("1")
data.child("lcdbkB").setValue("0")
data.child("lcdbkG").setValue("10")
data.child("lcdbkR").setValue("50")
data.child("lcdtxt").setValue("Door is close...")
data.child("oledsc").setValue("0")
data.child("lcdscr").setValue("1")
data.child("camera").setValue("1")
text.setText("Door is closing !!!")
window.decorView.setBackgroundColor(Color.GRAY)
}
override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
}
//When the sensor is resumed, call back the sensor manager
override fun onResume() {
super.onResume()
sensorManager!!.registerListener(this, sensorManager!!.getDefaultSensor(Sensor.TYPE_PROXIMITY), 2 * 1000 * 1000)
}
//When the sensor is paused, stop reading value
override fun onPause() {
super.onPause()
sensorManager!!.unregisterListener(this)
}
fun resumeReading(view: View) {
this.resume = true
}
fun pauseReading(view: View) {
this.resume = false
}
fun resetFBData(){
data.child("relay1").setValue("0")
data.child("relay2").setValue("0")
data.child("lcdbkB").setValue("0")
data.child("lcdbkG").setValue("0")
data.child("lcdbkR").setValue("0")
data.child("lcdtxt").setValue(" ")
data.child("buzzer").setValue("0")
data.child("lcdscr").setValue("0")
data.child("ledlgt").setValue("0")
data.child("sound").setValue("0")
data.child("camera").setValue("0")
}
}