Add MQTT subscribe functionality in main dashboard #299
This commit is contained in:
parent
bbaaf250c6
commit
ece4e6dd4d
2 changed files with 38 additions and 7 deletions
|
|
@ -10,13 +10,10 @@
|
||||||
<!-- END: Subheader -->
|
<!-- END: Subheader -->
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block scripts %}
|
|
||||||
{{ include('map/' ~ map_js_file) }}
|
|
||||||
<script>
|
|
||||||
|
|
||||||
{% if 'OpenStreet' in map_js_file %}
|
{% block scripts %}
|
||||||
initMap();
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.min.js" type="text/javascript"></script>
|
||||||
{% endif %}
|
<script>
|
||||||
|
|
||||||
function initMap() {
|
function initMap() {
|
||||||
var default_lat = {% trans %}default_lat{% endtrans %};
|
var default_lat = {% trans %}default_lat{% endtrans %};
|
||||||
|
|
@ -25,6 +22,38 @@ function initMap() {
|
||||||
var map = mapCreate('dashboard_map', default_lat, default_lng, 'road', 13);
|
var map = mapCreate('dashboard_map', default_lat, default_lng, 'road', 13);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{{ include('map/' ~ map_js_file) }}
|
||||||
|
|
||||||
|
// TODO: put this in .env
|
||||||
|
var mqtt;
|
||||||
|
var timeout = 2000;
|
||||||
|
var host = '192.168.56.4';
|
||||||
|
var port = 8083;
|
||||||
|
|
||||||
|
function onConnect() {
|
||||||
|
console.log('connected!');
|
||||||
|
|
||||||
|
mqtt.subscribe('test.channel');
|
||||||
|
}
|
||||||
|
|
||||||
|
function onMessage(msg) {
|
||||||
|
console.log(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
function mqttConnect() {
|
||||||
|
mqtt = new Paho.MQTT.Client(host, port, "clientjs");
|
||||||
|
var options = {
|
||||||
|
timeout: 3,
|
||||||
|
onSuccess: onConnect,
|
||||||
|
};
|
||||||
|
|
||||||
|
mqtt.onMessageArrived = onMessage;
|
||||||
|
|
||||||
|
console.log('connecting to mqtt server...');
|
||||||
|
mqtt.connect(options);
|
||||||
|
}
|
||||||
|
|
||||||
|
mqttConnect();
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -120,4 +120,6 @@ function mapCreate(div_id, center_lat, center_lng, map_type, zoom) {
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
initMap();
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue