forked from atendrasingh90/MqttApp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
“Atendra”
committed
May 3, 2024
1 parent
3611d8e
commit b2611d3
Showing
27 changed files
with
1,485 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.