Search in sources :

Example 1 with PropertyHandler

use of org.openmuc.framework.lib.osgi.config.PropertyHandler in project OpenMUC by isc-konstanz.

the class DbConnector method setSqlProperties.

/**
 * returns a properties object with the attributes the datasource needs
 *
 * @return a properties object with the attributes the datasource needs
 */
private Properties setSqlProperties() {
    PropertyHandler propertyHandler = PropertyHandlerProvider.getInstance().getPropertyHandler();
    Properties properties = new Properties();
    properties.setProperty("url", url);
    properties.setProperty("password", propertyHandler.getString(Settings.PASSWORD));
    properties.setProperty("user", propertyHandler.getString(Settings.USER));
    if (!url.contains("h2")) {
        if (url.contains(POSTGRESQL)) {
            properties.setProperty("ssl", propertyHandler.getString(Settings.SSL));
        }
        properties.setProperty("tcpKeepAlive", propertyHandler.getString(Settings.TCP_KEEP_ALIVE));
        properties.setProperty("socketTimeout", propertyHandler.getString(Settings.SOCKET_TIMEOUT));
    }
    return properties;
}
Also used : PropertyHandler(org.openmuc.framework.lib.osgi.config.PropertyHandler) Properties(java.util.Properties)

Example 2 with PropertyHandler

use of org.openmuc.framework.lib.osgi.config.PropertyHandler in project OpenMUC by isc-konstanz.

the class DbConnector method updateTimescale.

/**
 * Updates the PostgreSQL timescale extension by executing a SQL query as a console command
 */
private void updateTimescale() {
    try {
        String line;
        String[] cmd = new String[3];
        int startPoint = url.lastIndexOf('/');
        String dbName = url.substring(startPoint + 1);
        if (System.getProperty("os.name").toLowerCase().startsWith("windows")) {
            cmd[0] = "cmd.exe";
        } else {
            cmd[0] = "sh";
        }
        PropertyHandler propertyHandler = PropertyHandlerProvider.getInstance().getPropertyHandler();
        cmd[1] = "-c";
        cmd[2] = "PGPASSWORD=" + propertyHandler.getString(Settings.PSQL_PASS) + " psql -c 'ALTER EXTENSION timescaledb UPDATE;'  -U postgres -h localhost -d " + dbName;
        Process process = Runtime.getRuntime().exec(cmd);
        BufferedReader stdOutReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        while ((line = stdOutReader.readLine()) != null) {
            logger.info(line);
        }
        BufferedReader stdErrReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        while ((line = stdErrReader.readLine()) != null) {
            logger.info(line);
        }
    } catch (Exception e) {
        logger.error(MessageFormat.format("Unable to execute shell command: {0}", e.getMessage()));
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) PropertyHandler(org.openmuc.framework.lib.osgi.config.PropertyHandler) BufferedReader(java.io.BufferedReader) SQLException(java.sql.SQLException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 3 with PropertyHandler

use of org.openmuc.framework.lib.osgi.config.PropertyHandler in project OpenMUC by isc-konstanz.

the class TableSetupTest method setupInitializer.

@BeforeEach
void setupInitializer() throws SQLException {
    accessMock = mock(DbAccess.class);
    channelList = new ArrayList<>();
    channelList.add(getMockedChannel("gridPower"));
    channelList.add(getMockedChannel("pvPower"));
    ResultSet resultMocked = mock(ResultSet.class);
    ResultSetMetaData resultMetaMock = mock(ResultSetMetaData.class);
    when(resultMetaMock.getColumnCount()).thenReturn(0);
    when(resultMocked.getMetaData()).thenReturn(resultMetaMock);
    when(accessMock.executeQuery(any())).thenReturn(resultMocked);
    when(accessMock.getColumnLength(anyList(), anyString())).thenReturn(Collections.nCopies(20, 20));
    PropertyHandler propHandlerMock = mock(PropertyHandler.class);
    when(propHandlerMock.getString(Settings.URL)).thenReturn("jdbc:h2");
    PropertyHandlerProvider.getInstance().setPropertyHandler(propHandlerMock);
    tableSetup = new TableSetup(channelList, accessMock);
    metaBuilder = new MetaBuilder(channelList, accessMock);
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) MetaBuilder(org.openmuc.framework.datalogger.sql.MetaBuilder) PropertyHandler(org.openmuc.framework.lib.osgi.config.PropertyHandler) ResultSet(java.sql.ResultSet) TableSetup(org.openmuc.framework.datalogger.sql.TableSetup) DbAccess(org.openmuc.framework.datalogger.sql.DbAccess) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with PropertyHandler

use of org.openmuc.framework.lib.osgi.config.PropertyHandler in project OpenMUC by isc-konstanz.

the class MqttLoggerTest method connect.

// @BeforeAll
static void connect() {
    String packageName = MqttLogger.class.getPackage().getName().toLowerCase();
    System.setProperty(packageName + ".host", "localhost");
    System.setProperty(packageName + ".port", "1883");
    System.setProperty(packageName + ".username", "guest");
    System.setProperty(packageName + ".password", "guest");
    System.setProperty(packageName + ".topic", "device/data");
    System.setProperty(packageName + ".maxFileCount", "2");
    System.setProperty(packageName + ".maxFileSize", "1");
    System.setProperty(packageName + ".maxBufferSize", "1");
    String pid = MqttLogger.class.getName();
    MqttLoggerSettings settings = new MqttLoggerSettings();
    PropertyHandler propertyHandler = new PropertyHandler(settings, pid);
    MqttSettings Mqttsettings = new MqttSettings(propertyHandler.getString(MqttLoggerSettings.HOST), propertyHandler.getInt(MqttLoggerSettings.PORT), propertyHandler.getString(MqttLoggerSettings.USERNAME), propertyHandler.getString(MqttLoggerSettings.PASSWORD), propertyHandler.getBoolean(MqttLoggerSettings.SSL), propertyHandler.getInt(MqttLoggerSettings.MAX_BUFFER_SIZE), propertyHandler.getInt(MqttLoggerSettings.MAX_FILE_SIZE), propertyHandler.getInt(MqttLoggerSettings.MAX_FILE_COUNT), propertyHandler.getInt(MqttLoggerSettings.CONNECTION_RETRY_INTERVAL), propertyHandler.getInt(MqttLoggerSettings.CONNECTION_ALIVE_INTERVAL), propertyHandler.getString(MqttLoggerSettings.PERSISTENCE_DIRECTORY));
    MqttConnection connection = new MqttConnection(Mqttsettings);
    MqttWriter mqttWriter = new MqttWriter(connection, "mqttlogger");
    mqttWriter.getConnection().connect();
}
Also used : MqttConnection(org.openmuc.framework.lib.mqtt.MqttConnection) MqttWriter(org.openmuc.framework.lib.mqtt.MqttWriter) PropertyHandler(org.openmuc.framework.lib.osgi.config.PropertyHandler) MqttSettings(org.openmuc.framework.lib.mqtt.MqttSettings)

Aggregations

PropertyHandler (org.openmuc.framework.lib.osgi.config.PropertyHandler)4 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ResultSet (java.sql.ResultSet)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 SQLException (java.sql.SQLException)1 Properties (java.util.Properties)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 DbAccess (org.openmuc.framework.datalogger.sql.DbAccess)1 MetaBuilder (org.openmuc.framework.datalogger.sql.MetaBuilder)1 TableSetup (org.openmuc.framework.datalogger.sql.TableSetup)1 MqttConnection (org.openmuc.framework.lib.mqtt.MqttConnection)1 MqttSettings (org.openmuc.framework.lib.mqtt.MqttSettings)1 MqttWriter (org.openmuc.framework.lib.mqtt.MqttWriter)1