add random value for testing the graph if isTesting is set to true before compilation
This commit is contained in:
parent
a3c02e28cb
commit
26055a8e38
@ -26,6 +26,9 @@ import java.util.*
|
|||||||
import android.graphics.Color
|
import android.graphics.Color
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
import android.widget.TextView
|
import android.widget.TextView
|
||||||
|
import java.io.InputStream
|
||||||
|
import java.io.PipedInputStream
|
||||||
|
import java.io.PipedOutputStream
|
||||||
|
|
||||||
class MainActivity : AppCompatActivity() {
|
class MainActivity : AppCompatActivity() {
|
||||||
|
|
||||||
@ -48,6 +51,8 @@ class MainActivity : AppCompatActivity() {
|
|||||||
private lateinit var tempCircle: TextView
|
private lateinit var tempCircle: TextView
|
||||||
private lateinit var humCircle: TextView
|
private lateinit var humCircle: TextView
|
||||||
|
|
||||||
|
private val isTesting = true
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
enableEdgeToEdge()
|
enableEdgeToEdge()
|
||||||
@ -78,6 +83,15 @@ class MainActivity : AppCompatActivity() {
|
|||||||
|
|
||||||
connectToHC05()
|
connectToHC05()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isTesting) {
|
||||||
|
Toast.makeText(this, "Running in testing mode", Toast.LENGTH_SHORT).show()
|
||||||
|
Log.d(tag, "Starting fake data stream")
|
||||||
|
|
||||||
|
Thread {
|
||||||
|
listenForData()
|
||||||
|
}.start()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun connectToHC05() {
|
private fun connectToHC05() {
|
||||||
@ -119,7 +133,11 @@ class MainActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun listenForData() {
|
private fun listenForData() {
|
||||||
val inputStream = bluetoothSocket?.inputStream ?: return
|
val inputStream = if (isTesting) {
|
||||||
|
fakeTestingData()
|
||||||
|
} else {
|
||||||
|
bluetoothSocket?.inputStream ?: return
|
||||||
|
}
|
||||||
val reader = BufferedReader(InputStreamReader(inputStream))
|
val reader = BufferedReader(InputStreamReader(inputStream))
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
@ -215,4 +233,31 @@ class MainActivity : AppCompatActivity() {
|
|||||||
humChart.invalidate()
|
humChart.invalidate()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun fakeTestingData(): InputStream {
|
||||||
|
val pipedInput = PipedInputStream()
|
||||||
|
val pipedOutput = PipedOutputStream(pipedInput)
|
||||||
|
|
||||||
|
Thread {
|
||||||
|
val random = Random()
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
val temp = 0.0 + random.nextFloat() * 50.0
|
||||||
|
val hum = 10.0 + random.nextFloat() * 80.0
|
||||||
|
|
||||||
|
val line = String.format(Locale.US, "%.1f;%.1f", temp, hum)
|
||||||
|
|
||||||
|
try {
|
||||||
|
pipedOutput.write("$line\n".toByteArray())
|
||||||
|
pipedOutput.flush()
|
||||||
|
Log.d(tag, "Fake sent: $line")
|
||||||
|
Thread.sleep(1000)
|
||||||
|
} catch (e: IOException) {
|
||||||
|
Log.e(tag, "Fake stream write failed", e)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.start()
|
||||||
|
|
||||||
|
return pipedInput
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user