From 73e4c06d4db640e04b265f8090ddb654915b6676 2023-02-28 22:49:29 From: Renato Huallpa Date: 2023-02-28 22:49:29 Subject: [PATCH] Se recuperó el avance de implementación mqtt para abs --- diff --git a/.env b/.env index 9747704..92bedb2 100644 --- a/.env +++ b/.env @@ -18,7 +18,7 @@ TZ=America/Lima DOCKER_DATA=/data/dockers/radarsys/ LOCAL_IP=192.168.1.128 -MQTT_SERVER=10.10.10.102 +MQTT_SERVER=10.10.10.200 #MQTT_SERVER = 0.0.0.0 MQTT_PORT = 1883 MQTT_KEEPALIVE = 3660 @@ -30,3 +30,4 @@ MQTT_PASSWORD = abs MQTT_CLIENT_ID= abs_id TOPIC_ABS=abs/beams TOPIC_ABS_ACK=abs/beams_ack +TOPIC_ABS_CHANGE=abs/change_beam diff --git a/apps/atrad/mqtt.py b/apps/atrad/mqtt.py index 89dcbe0..e2d5bf1 100644 --- a/apps/atrad/mqtt.py +++ b/apps/atrad/mqtt.py @@ -33,7 +33,7 @@ client.on_connect = on_connect client.on_message = on_message client.username_pw_set(os.environ.get('MQTT_USER_ATRAD', 'atrad'), os.environ.get('MQTT_PASSWORD_ATRAD', 'atrad')) client.connect( - host=os.environ.get('MQTT_SERVER', '0.0.0.0'), + host=os.environ.get('MQTT_SERVER', '10.10.10.200'), port=int(settings.os.environ.get('MQTT_PORT', 1883)), keepalive=int(os.environ.get('MQTT_KEEPALIVE', 36000)) ) \ No newline at end of file diff --git a/apps/atrad/templates/atrad_conf.html b/apps/atrad/templates/atrad_conf.html index 0a87d6e..0cb7d14 100644 --- a/apps/atrad/templates/atrad_conf.html +++ b/apps/atrad/templates/atrad_conf.html @@ -272,7 +272,7 @@ function ligthStatus(div1,div2,status){ if(status==='0000'){ document.getElementById(div1).style.backgroundColor = "green"; - document.getElementById(div2).innerHTML = "Desabilitado"; + document.getElementById(div2).innerHTML = "Deshabilitado"; } else if(status==='1111'){ document.getElementById(div1).style.backgroundColor = "green"; diff --git a/apps/atrad/views.py b/apps/atrad/views.py index bf2ecf8..80c6355 100644 --- a/apps/atrad/views.py +++ b/apps/atrad/views.py @@ -88,15 +88,15 @@ def prueba(request): return render(request, 'prueba.html', kwargs) @sio.on('connection-bind') -def connection_bind(sid, data): +def atrad_connection_bind(sid, data): print("sid:",sid,"data",data) @sio.on('disconnect') -def test_disconnect(sid): +def atrad_disconnect(sid): print("Disconnected") @sio.event -def control_event(sid,message): +def atrad_control_event(sid,message): mqtt_client.publish('test/data2',message['data']) def hello(data): diff --git a/apps/main/__init__.py b/apps/main/__init__.py index e69de29..81831c6 100644 --- a/apps/main/__init__.py +++ b/apps/main/__init__.py @@ -0,0 +1,2 @@ +from . import mqtt +mqtt.client.loop_start() \ No newline at end of file diff --git a/apps/main/models.py b/apps/main/models.py index 95c3b03..2ed9bbe 100644 --- a/apps/main/models.py +++ b/apps/main/models.py @@ -805,6 +805,9 @@ class Configuration(PolymorphicModel): def get_absolute_url_write(self): return reverse('url_write_dev_conf', args=[str(self.id)]) + + def get_absolute_url_write_mqtt(self): + return reverse('url_write_mqtt_dev_conf', args=[str(self.id)]) def get_absolute_url_read(self): return reverse('url_read_dev_conf', args=[str(self.id)]) diff --git a/apps/main/mqtt.py b/apps/main/mqtt.py new file mode 100644 index 0000000..6b4dc87 --- /dev/null +++ b/apps/main/mqtt.py @@ -0,0 +1,44 @@ +import os +import paho.mqtt.client as mqtt +from radarsys import settings +from radarsys.socketconfig import sio as sio + + +lista_ack=[ + chr( 33 ), chr( 34 ), chr( 35 ), chr( 36 ), chr( 37 ), chr( 38 ), chr( 39 ), chr( 40 ), chr( 41 ), chr( 42 ), chr( 43 ), chr( 44 ), chr( 45 ), chr( 46 ), chr( 47 ), chr( 48 ), chr( 49 ), chr( 50 ), chr( 51 ), chr( 52 ), chr( 53 ), chr( 54 ), chr( 55 ), chr( 56 ), chr( 57 ), chr( 58 ), chr( 59 ), chr( 60 ), chr( 61 ), chr( 62 ), chr( 63 ), chr( 64 ), chr( 65 ), chr( 66 ), chr( 67 ), chr( 68 ), chr( 69 ), chr( 70 ), chr( 71 ), chr( 72 ), chr( 73 ), chr( 74 ), chr( 75 ), chr( 76 ), chr( 77 ), chr( 78 ), chr( 79 ), chr( 80 ), chr( 81 ), chr( 82 ), chr( 83 ), chr( 84 ), chr( 85 ), chr( 86 ), chr( 87 ), chr( 88 ), chr( 89 ), chr( 90 ), chr( 91 ), chr( 92 ), chr( 93 ), chr( 94 ), chr( 95 ), chr( 96 ) + ] + +lista_ack_dismatching=[ + chr( 97 ), chr( 98 ), chr( 99 ), chr( 100 ), chr( 101 ), chr( 102 ), chr( 103 ), chr( 104 ), chr( 105 ), chr( 106 ), chr( 107 ), chr( 108 ), chr( 109 ), chr( 110 ), chr( 111 ), chr( 112 ), chr( 113 ), chr( 114 ), chr( 115 ), chr( 116 ), chr( 117 ), chr( 118 ), chr( 119 ), chr( 120 ), chr( 121 ), chr( 122 ), chr( 123 ), chr( 124 ), chr( 125 ), chr( 126 ), chr( 127 ), chr( 128 ), chr( 129 ), chr( 130 ), chr( 131 ), chr( 132 ), chr( 133 ), chr( 134 ), chr( 135 ), chr( 136 ), chr( 137 ), chr( 138 ), chr( 139 ), chr( 140 ), chr( 141 ), chr( 142 ), chr( 143 ), chr( 144 ), chr( 145 ), chr( 146 ), chr( 147 ), chr( 148 ), chr( 149 ), chr( 150 ), chr( 151 ), chr( 152 ), chr( 153 ), chr( 154 ), chr( 155 ), chr( 156 ), chr( 157 ), chr( 158 ), chr( 159 ), chr(160) +] + + + +def on_connect(mqtt_client, userdata, flags, rc): + if rc == 0: + print('Connected successfully') + mqtt_client.subscribe(os.environ.get('TOPIC_ABS_ACK','abs/beams_ack')) + else: + print('Bad connection. Code:', rc) + +def on_message(mqtt_client, userdata, msg): + # print(f'Received message on topic: {msg.topic} with payload: {msg.payload}', flush=True) + # message= str(msg.payload) + # sio.emit('abs_ws',data={'msg':message}) + print("HOLA",flush=True) + # message=msg.payload[1] + # print("HOLAAA ",message,flush=True) + # #lista_ack.remove(msg.payload) + # print("LISTA ",lista_ack) + + + +client = mqtt.Client() +client.on_connect = on_connect +client.on_message = on_message +client.username_pw_set(os.environ.get('MQTT_USER', 'abs'), os.environ.get('MQTT_PASSWORD', 'abs')) +client.connect( + host=os.environ.get('MQTT_SERVER', '10.10.10.200'), + port=int(settings.os.environ.get('MQTT_PORT', 1883)), + keepalive=int(os.environ.get('MQTT_KEEPALIVE', 60000)) +) \ No newline at end of file diff --git a/apps/main/templates/dev_conf.html b/apps/main/templates/dev_conf.html index fb7678d..5560613 100644 --- a/apps/main/templates/dev_conf.html +++ b/apps/main/templates/dev_conf.html @@ -26,6 +26,9 @@
  • Stop
  • {% endif %}
  • Write
  • + {% if dev_conf.device.device_type.name == 'abs' %} +
  • Write MQTT
  • + {% endif %} {% if dev_conf.device.device_type.name != 'abs' %}
  • Read
  • {% endif %} diff --git a/apps/main/templates/header_igp.html b/apps/main/templates/header_igp.html index aedb714..d3c95ef 100644 --- a/apps/main/templates/header_igp.html +++ b/apps/main/templates/header_igp.html @@ -1,140 +1,4 @@ - - - - - - - - {% load static %} {% load django_bootstrap5 %} +{% load static %} {% load django_bootstrap5 %}