45 lines
1.1 KiB
Kotlin
45 lines
1.1 KiB
Kotlin
package drills.drill06.exercise4.sensor
|
|
|
|
import drills.drill06.exercise4.SensorNet
|
|
import org.apache.activemq.ActiveMQConnectionFactory
|
|
import javax.jms.Session
|
|
import javax.jms.TopicConnectionFactory
|
|
|
|
class TemperatureSensorNode(val id: Int, val uri: String, val topicName: String) {
|
|
private val sampler: Sampler<Double> =
|
|
SampleGenerator(id)
|
|
|
|
fun start() {
|
|
val factory: TopicConnectionFactory = ActiveMQConnectionFactory(uri)
|
|
val connection = factory.createTopicConnection()
|
|
|
|
val session = connection.createTopicSession(
|
|
false,
|
|
Session.AUTO_ACKNOWLEDGE
|
|
)
|
|
|
|
val topic = session.createTopic(topicName)
|
|
val publisher = session.createPublisher(topic)
|
|
|
|
val msg = session.createMessage()
|
|
|
|
while (true) {
|
|
val sample = sampler.getSample()
|
|
msg.setDoubleProperty(SensorNet.PROPERTY, sample)
|
|
publisher.publish(msg)
|
|
|
|
println("Sensor-$id: $sample")
|
|
|
|
Thread.sleep(2000)
|
|
}
|
|
}
|
|
}
|
|
|
|
fun main() {
|
|
TemperatureSensorNode(
|
|
0,
|
|
SensorNet.URI,
|
|
SensorNet.TOPIC
|
|
).start()
|
|
}
|