Skip to content

Latest commit

 

History

History
123 lines (96 loc) · 2.72 KB

implementation.md

File metadata and controls

123 lines (96 loc) · 2.72 KB

Implementation

App Configuration

Bind-Mount Volume for configuration file

volumes:
      - './publish/:/publish/'
      - './cfg-data/:/cfg-data/'

Default app configuration

const defaultConfiguration = {
    "API_SECURITY": {
        "USERNAME": "admin",
        "PASSWORD": "changeMe1!"
    },
    "MQTT": {
        "SERVER_IP": "ie_databus",
        "DEFAULT_TOPIC_NAME": "ie/d/j/simatic/v1/s7c1/dp/",
        "DATA_SOURCE_NAME": "tank",
        "VAR_ID_START": "GDB.appSignals.APP_Start",
        "VAR_ID_STOP": "GDB.appSignals.APP_Stop",
        "VAR_ID_RESET": "GDB.appSignals.APP_Reset",
        "USER": "edge",
        "PASSWORD": "edge"
    },
    "INFLUXDB": {
        "INFLUXDB_IP": "influxdb",
        "INFLUXDB_DATABASE": "databus_values"
    }
}

Read configuration file

const fileContent = JSON.parse(fs.readFileSync('/cfg-data/config_mqtt-service.json', 'utf8') )

Connect to IE Databus

MQTT-Client options

const options = {
    'clientId': 'mqttjs_' + Math.random().toString(16).substr(2, 8),
    'protocolId': 'MQTT',
    'username': MQTT.USER,
    'password': MQTT.PASSWORD
}

Connect MQTT-Client to IE Databus

const mqttClient = mqtt.connect('mqtt://' + MQTT.SERVER_IP, options);

Subscribe to Topics on IE Databus

mqttClient.on('connect', () => {
    mqttClient.subscribe(mqttSubTopic, (err) => {
        if (!err) {
            console.log('Subscribed to ' + mqttSubTopic)
        }
    })
});

Publish to Topic on IE Databus

mqttClient.publish(mqttPubTopic, msg);

On Message

mqttClient.on('message', (topic, message) => {
    //do something
}

API with NodeJs and Express

Http Get-Request Endpoint

app.get('/start', checkAuth, (req, res) => {
    //do something
})

Start Webserver

app.listen(port, () => {
    console.log(`App listening at http://localhost:${port}`)
})