001package var.mom.mqtt.smarthome;
002
003import org.eclipse.paho.client.mqttv3.MqttClient;
004import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
005import org.eclipse.paho.client.mqttv3.MqttException;
006import org.eclipse.paho.client.mqttv3.MqttMessage;
007
008/**
009 * client for the Smart Home application: sensor simulator
010 *
011 * @author Sandro Leuchter
012 *
013 */
014class SensorSelfDescribung {
015        /**
016         * topic to which this sensor publishes its observations
017         */
018        static String topic;
019        /**
020         * floor where sensor is located
021         */
022        static String floor;
023        /**
024         * room where sensor is located
025         */
026        static String room;
027        /**
028         * type of sensor
029         */
030        static String sensorType;
031        /**
032         * unique name of client
033         */
034        static String clientId = MqttClient.generateClientId();
035
036        /**
037         * get the description of sensor (class)
038         *
039         * @return description of sensor
040         */
041        public static String getDescription() {
042                return clientId + "\n  Topic: " + topic + "\n  Type: " + sensorType + "\n  Location: " + floor + "." + room;
043        }
044
045        /**
046         * main routine and starting point of program
047         *
048         * @param args[0] floor where sensor is located
049         * @param args[1] room where sensor is located
050         * @param args[2] type of sensor
051         * @param args[3] initial observation of sensor
052         * @throws MqttException Paho library exceptions that have something to do with
053         *                       MQTT
054         * 
055         */
056        public static void main(String[] args) throws MqttException {
057                floor = args[0];
058                room = args[1];
059                sensorType = args[2];
060                topic = Conf.TOPICSTART + "/" + floor + "/" + room + "/" + sensorType;
061                double lastObservation = Double.valueOf(args[3]);
062                MqttClient client;
063                client = new MqttClient(Conf.BROKER, clientId);
064                MqttConnectOptions options = new MqttConnectOptions();
065                options.setWill(topic, ("deregistering sensor: " + getDescription()).getBytes(), Conf.QOS, true);
066                client.connect(options);
067                MqttMessage selfIntroduction = new MqttMessage();
068                selfIntroduction.setRetained(true);
069                selfIntroduction.setPayload(("registering sensor: " + getDescription()).getBytes());
070                client.publish(topic, selfIntroduction);
071                MqttMessage message = new MqttMessage();
072                try {
073                        while (true) {
074                                if (Math.random() > 0.5) { // randomly +/- 0.1
075                                        lastObservation += 0.1;
076                                } else {
077                                        lastObservation -= 0.1;
078                                }
079                                message.setPayload(String.valueOf(lastObservation).getBytes());
080                                client.publish(topic, message);
081                                Thread.sleep((int) (Math.random() * 1000)); // < 1 s waiting
082                        }
083                } catch (InterruptedException e) {
084                        System.err.println(e.getMessage());
085                } finally {
086                        try {
087                                client.disconnect();
088                        } catch (MqttException e) {
089                                // unrecoverable
090                        }
091                }
092        }
093}