raspisensormodule.NodeSensorHandler.java Source code

Java tutorial

Introduction

Here is the source code for raspisensormodule.NodeSensorHandler.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package raspisensormodule;

import SensorsPack.LightSensor;
import SensorsPack.PressureSensor;
import SensorsPack.Temp_Hum_sensor;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author thor
 */
public class NodeSensorHandler extends BusConnecter {

    ArrayList<LightSensor> lightSensors = new ArrayList<>();
    ArrayList<PressureSensor> pressureSensors = new ArrayList<>();
    ArrayList<Temp_Hum_sensor> tempHumSensors = new ArrayList<>();

    public NodeSensorHandler(Connection busConnection, String exchange, String baseRoutingKey) throws Exception {
        super(busConnection, exchange, baseRoutingKey);

        initSensors();

        Channel channelTemp = null;
        Channel channelPress = null;
        Channel channelLight = null;

        try {
            channelTemp = busConnection.createChannel();
            channelTemp.exchangeDeclare(exchange, "topic");
            channelPress = busConnection.createChannel();
            channelPress.exchangeDeclare(exchange, "topic");
            channelLight = busConnection.createChannel();
            channelLight.exchangeDeclare(exchange, "topic");
        } catch (IOException ex) {
            Logger.getLogger(NodeSensorHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        NodeSensor tempUpdater = new NodeSensor(6000, tempHumSensors, channelTemp, exchange,
                baseRoutingKey + ".temp", this);
        Thread t = new Thread(tempUpdater);
        addThread(t);

        NodeSensor pressUpdater = new NodeSensor(3000, pressureSensors, channelPress, exchange,
                baseRoutingKey + ".pressure", this);
        t = new Thread(pressUpdater);
        addThread(t);

        NodeSensor lightUpdater = new NodeSensor(12000, lightSensors, channelLight, exchange,
                baseRoutingKey + ".light", this);
        t = new Thread(lightUpdater);
        addThread(t);

    }

    public void initSensors() {

        final int tempSensorCount = 3;
        final int lightSensorCount = 5;
        final int pressureSensorCount = 7;

        for (int i = 0; i < tempSensorCount; i++) {
            Temp_Hum_sensor foo = new Temp_Hum_sensor();
            tempHumSensors.add(foo);
            sensorCount++;
        }

        for (int i = 0; i < lightSensorCount; i++) {
            lightSensors.add(new LightSensor());
            sensorCount++;

        }
        for (int i = 0; i < pressureSensorCount; i++) {
            pressureSensors.add(new PressureSensor());
            sensorCount++;

        }

    }

}