Skip to content

Commit

Permalink
working on data set type
Browse files Browse the repository at this point in the history
  • Loading branch information
“Atendra” committed May 3, 2024
1 parent 3611d8e commit b2611d3
Show file tree
Hide file tree
Showing 27 changed files with 1,485 additions and 16 deletions.
15 changes: 15 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ android {
kotlinOptions {
jvmTarget = "1.8"
}
viewBinding {
enable = true
}
}

dependencies {
Expand All @@ -44,4 +47,16 @@ dependencies {
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
implementation("org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5")
implementation("org.eclipse.paho:org.eclipse.paho.android.service:1.1.1")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.2.2")
implementation("androidx.localbroadcastmanager:localbroadcastmanager:1.1.0")
implementation("androidx.legacy:legacy-support-v4:1.0.0")
implementation("com.github.hannesa2:paho.mqtt.android:4.2.4")
implementation("androidx.room:room-runtime:2.3.0")
implementation("com.jakewharton.timber:timber:5.0.1")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2")
implementation("de.hdodenhof:circleimageview:3.1.0")


}
9 changes: 8 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,16 @@
android:supportsRtl="true"
android:theme="@style/Theme.MqttApp"
tools:targetApi="31">
<activity
android:name=".SecondActivity"
android:exported="false"
android:theme="@style/AppTheme"
/>
<activity
android:name=".MainActivity"
android:exported="true">
android:exported="true"
android:theme="@style/AppTheme">
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/com/owen/mqttapp/ButtonClickInterface.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.owen.mqttapp

interface ButtonClickInterface {
fun onButtonClick(position:Int,message:String)
}
16 changes: 16 additions & 0 deletions app/src/main/java/com/owen/mqttapp/ButtonDataClass.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.owen.mqttapp

data class Button(
val number: Int,
val type: String,
val state: Boolean,
val visible: Boolean,
val textColorOn: String,
val colorOn: Int,
val textOn: String,
val emitOn: String,
val textColorOff: String,
val colorOff: Int,
val textOff: String,
val emitOff: String
)
9 changes: 9 additions & 0 deletions app/src/main/java/com/owen/mqttapp/Led.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.owen.mqttapp

data class Led(
val number: Int,
val state: Boolean,
val visible: Boolean,
val colorOn: String,
val colorOff: String
)
63 changes: 63 additions & 0 deletions app/src/main/java/com/owen/mqttapp/LedAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.owen.mqttapp

import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import de.hdodenhof.circleimageview.CircleImageView

class LedAdapter(private val ledList: MutableList<Led>) :
RecyclerView.Adapter<LedAdapter.LedViewHolder>() {

inner class LedViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val ivLedColor: CircleImageView = itemView.findViewById(R.id.ivLedColor)
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LedViewHolder {
val itemView = LayoutInflater.from(parent.context)
.inflate(R.layout.led, parent, false)
return LedViewHolder(itemView)
}

override fun onBindViewHolder(holder: LedViewHolder, position: Int) {
val led = ledList[position]

// Preprocess color strings to ensure they're in the correct format
val colorOn = preprocessColor(led.colorOn)
val colorOff = preprocessColor(led.colorOff)

// Set LED color based on its state

val color = if (led.state) colorOn else colorOff
// val parsedColor = Color.parseColor(preprocessColor(color))
// holder.ivLedColor.setColorFilter(parsedColor)
val backgroundDrawable = GradientDrawable().apply {
shape = GradientDrawable.OVAL
setColor(Color.parseColor(color))
}

// Set the circular background drawable as the background of the CircleImageView
holder.ivLedColor.background = backgroundDrawable
holder.ivLedColor.visibility = if (led.visible) View.VISIBLE else View.INVISIBLE
}

override fun getItemCount(): Int {
return ledList.size
}

fun addLed(led: Led) {
ledList.add(led)
notifyDataSetChanged()
}

/* private fun preprocessColor(color: String): String {
return if (color.length == 6) "#$color" else "#$color"
}*/
private fun preprocessColor(color: String): String {
return if (color.length == 6 && !color.startsWith("#")) "#$color" else color
}

}
151 changes: 149 additions & 2 deletions app/src/main/java/com/owen/mqttapp/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,158 @@
package com.owen.mqttapp

import android.app.ProgressDialog
import android.content.Intent
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.ProgressBar
import android.widget.Toast
import com.owen.mqttapp.databinding.ActivityMainBinding
import com.owen.mqttapp.preferences.Preference
import com.owen.mqttapp.utils.MQTTClient
import com.owen.mqttapp.utils.MessageCallback
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private lateinit var preferences: Preference
private val mqttClient by lazy {
MQTTClient(this, preferences)
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
preferences = Preference(this)
if (preferences.isDetailsSaved() == true) {
binding.etServer.setText(preferences.getMqttIp().toString())
binding.etPort.setText(preferences.getMqttPort().toString())
binding.etUsername.setText(preferences.getMqttName().toString())
binding.etPassword.setText(preferences.getPassword().toString())
binding.etTopic.setText(preferences.getMqtTopic().toString())
}
binding.btnSubscribe.setOnClickListener {
val server = binding.etServer.text.toString()
val port = binding.etPort.text.toString()
val username = binding.etUsername.text.toString()
val password = binding.etPassword.text.toString()

val topic = binding.etTopic.text.toString()
if (server.isEmpty() || port.isEmpty() || username.isEmpty() || password.isEmpty() || topic.isEmpty()) {
// Show alert that the fields are empty
Toast.makeText(this@MainActivity, "Please fill in all fields", Toast.LENGTH_SHORT)
.show()
return@setOnClickListener // Exit the click listener
}
preferences.setMqttTopic(topic)

val brokerUrl = "tcp://$server:$port"


preferences.setMqttUsername(username)
preferences.setMqttPassword(password)
preferences.setMqttBrokerUrl(brokerUrl)
val progressDialog = ProgressDialog(this)
progressDialog.setMessage("Connecting to MQTT...")
progressDialog.setCancelable(false)
progressDialog.show()
GlobalScope.launch {
mqttClient.setupMqttClient()
delay(5000)
progressDialog.dismiss()
runOnUiThread {
if (mqttClient.isConnected()) {
changeView()
mqttClient.subscribe(topic) { success ->
if (success) {

val intent = Intent(this@MainActivity, SecondActivity::class.java)
startActivity(intent)
} else {

}
}
} else {
Toast.makeText(this@MainActivity, "Failed to Connect", Toast.LENGTH_SHORT)
.show()
}
}
}
}
binding.btnSave.setOnClickListener {
if (binding.etServer.text.toString()
.isEmpty() || binding.etPort.text.toString()
.isEmpty() || binding.etUsername.text.toString()
.isEmpty() || binding.etPassword.text.toString()
.isEmpty() || binding.etTopic.text.toString().isEmpty()
) {
Toast.makeText(this@MainActivity, "Please fill in all fields", Toast.LENGTH_SHORT)
.show()
return@setOnClickListener // Exit the click listener
}
preferences.setMqttDetails(
binding.etServer.text.toString(),
binding.etPort.text.toString(),
binding.etUsername.text.toString(),
binding.etPassword.text.toString(),
binding.etTopic.text.toString()
)
Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show()
preferences.setDetailsSaved(true)

}


binding.btnDisconnect.setOnClickListener {
mqttClient.disconnectClient() { disconnected ->
if (disconnected) {
preferences.setMqttConnected(false)
binding.btnDisconnect.visibility = View.GONE
binding.btnSubscribe.visibility = View.VISIBLE
}
}

}

/*binding.btnSubscribe.setOnClickListener {
val intent = Intent(this, SecondActivity::class.java)
startActivity(intent)
}*/
}
override fun onDestroy() {
super.onDestroy()
preferences.setMqttConnected(false)
mqttClient.disconnectClient {}
}

override fun onResume() {
super.onResume()
// if (preferences.isDetailsSaved() == false) {
// binding.etServer.text.clear()
// binding.etPort.text.clear()
// binding.etUsername.text.clear()
// binding.etPassword.text.clear()
// binding.etTopic.text.clear()
// }
//
if (preferences.isMqttConnected()) {
binding.btnSubscribe.visibility = View.GONE
binding.btnDisconnect.visibility = View.VISIBLE
} else {
binding.btnSubscribe.visibility = View.VISIBLE
binding.btnDisconnect.visibility = View.GONE
}
}
}


private fun changeView() {
binding.btnSubscribe.visibility = View.GONE
binding.btnDisconnect.visibility = View.VISIBLE
}
}

//0a80df440665e9e1
Loading

0 comments on commit b2611d3

Please sign in to comment.