diff --git a/nabrfid2server/migrations/0001_initial.py b/nabrfid2server/migrations/0001_initial.py index 2e223f64..4d33734d 100644 --- a/nabrfid2server/migrations/0001_initial.py +++ b/nabrfid2server/migrations/0001_initial.py @@ -7,17 +7,29 @@ class Migration(migrations.Migration): initial = True - dependencies = [ - ] + dependencies = [] operations = [ migrations.CreateModel( - name='Config', + name="Config", fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('rfid_2_server_test', models.BooleanField(default=False)), - ('rfid_2_server_mode', models.IntegerField(default=2)), - ('rfid_2_server_url', models.TextField(default='Ex: https://MY_SERVER/core/api/jeeApi.php?apikey=MY_API_KEY&type=scenario&id=MY_SCENARIO_ID&action=start&tags=rfid="#RFID_TAG#"%20etat="#RFID_STATE#"%20flags=#34;#RFID_FLAGS##34;%20app=#34;#RFID_APP##34;')), + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("rfid_2_server_test", models.BooleanField(default=False)), + ("rfid_2_server_mode", models.IntegerField(default=2)), + ( + "rfid_2_server_url", + models.TextField( + default="Ex: https://MY_SERVER/core/api/jeeApi.php?apikey=MY_API_KEY&type=scenario&id=MY_SCENARIO_ID&action=start&tags=rfid="#RFID_TAG#"%20etat="#RFID_STATE#"%20flags=#34;#RFID_FLAGS##34;%20app=#34;#RFID_APP##34;" + ), + ), ], ), ] diff --git a/nabrfid2server/models.py b/nabrfid2server/models.py index 7a7a3307..0ad73e67 100644 --- a/nabrfid2server/models.py +++ b/nabrfid2server/models.py @@ -5,7 +5,13 @@ class Config(singleton_model.SingletonModel): rfid_2_server_test = models.BooleanField(default=False) rfid_2_server_mode = models.IntegerField(default=2) - rfid_2_server_url = models.TextField(default="Ex: https://MY_SERVER/core/api/jeeApi.php?apikey=MY_API_KEY&type=scenario&id=MY_SCENARIO_ID&action=start&tags=rfid="#RFID_TAG#"%20etat="#RFID_STATE#"%20flags=#34;#RFID_FLAGS##34;%20app=#34;#RFID_APP##34;") + rfid_2_server_url = models.TextField( + default="Ex: https://MY_SERVER/core/api/jeeApi.php?apikey=MY_API_KEY&" + "type=scenario&id=MY_SCENARIO_ID&action=start&" + "tags=rfid="#RFID_TAG#"%20etat="" + "#RFID_STATE#"%20flags=#34;#RFID_FLAGS##34;" + "%20app=#34;#RFID_APP##34; " + ) class Meta: app_label = "nabrfid2server" diff --git a/nabrfid2server/nabrfid2server.py b/nabrfid2server/nabrfid2server.py index 28c05d02..a42b4108 100644 --- a/nabrfid2server/nabrfid2server.py +++ b/nabrfid2server/nabrfid2server.py @@ -16,40 +16,70 @@ def __init__(self): async def reload_config(self): from . import models + self.config = await models.Config.load_async() - #logging.info("reload config: mode=" + str(self.config.rfid_2_server_mode) + " test="+str(self.config.rfid_2_server_test) + " url="+self.config.rfid_2_server_url) - if self.config.rfid_2_server_test : self.send_rfid_2_url("rfid_uid_test","event_test","app_test","support_test","packet_test") + # logging.info("reload config: mode=" + str( + # self.config.rfid_2_server_mode) + " test=" + str( + # self.config.rfid_2_server_test) + " url=" + \ + # self.config.rfid_2_server_url) + if self.config.rfid_2_server_test: + self.send_rfid_2_url( + "rfid_uid_test", + "event_test", + "app_test", + "support_test", + "packet_test", + ) async def process_nabd_packet(self, packet): - if ( self.config.rfid_2_server_mode==0 or (packet["type"] != "rfid_event") ): return # Never send url + if self.config.rfid_2_server_mode == 0 or ( + packet["type"] != "rfid_event" + ): + return # Never send url - if "app" not in packet: app = "none" - else: app = packet["app"] - if "support" not in packet: supp = "support unknown" - else: supp = packet["support"] - if "event" not in packet: _event = "no event" - else: _event = packet["event"] + if "app" not in packet: + app = "none" + else: + app = packet["app"] + if "support" not in packet: + supp = "support unknown" + else: + supp = packet["support"] + if "event" not in packet: + _event = "no event" + else: + _event = packet["event"] - if (self.config.rfid_2_server_mode==1) and (supp=="formatted") and (app=="none") : return # Send only unknown tags - self.send_rfid_2_url(packet["uid"],_event,app,supp,packet) + if ( + (self.config.rfid_2_server_mode == 1) + and (supp == "formatted") + and (app == "none") + ): + return # Send only unknown tags + self.send_rfid_2_url(packet["uid"], _event, app, supp, packet) - def send_rfid_2_url(self, uid,_event,app,supp,packet): - #logging.info("send rfid 2 url: mode=" + str(self.config.rfid_2_server_mode) + " test="+str(self.config.rfid_2_server_test) + " url="+self.config.rfid_2_server_url) - url_message = self.config.rfid_2_server_url.replace("#RFID_TAG#",uid) - url_message = url_message.replace("#RFID_APP#",app) - url_message = url_message.replace("#RFID_FLAGS#",supp) - url_message = url_message.replace("#RFID_STATE#",_event) - str_pack = json.dumps(packet); - url_message = url_message.replace("#RFID_PACK#",str_pack) + def send_rfid_2_url(self, uid, _event, app, supp, packet): + # logging.info("send rfid 2 url: mode=" + str( + # self.config.rfid_2_server_mode) + " test=" + str( + # self.config.rfid_2_server_test) + " url=" + \ + # self.config.rfid_2_server_url) + url_message = self.config.rfid_2_server_url.replace("#RFID_TAG#", uid) + url_message = url_message.replace("#RFID_APP#", app) + url_message = url_message.replace("#RFID_FLAGS#", supp) + url_message = url_message.replace("#RFID_STATE#", _event) + str_pack = json.dumps(packet) + url_message = url_message.replace("#RFID_PACK#", str_pack) str_pack = str_pack.lower() - str_pack = str_pack.replace('\"','') - url_message = url_message.replace("#RFID_JEEPACK#",str_pack) + str_pack = str_pack.replace('"', "") + url_message = url_message.replace("#RFID_JEEPACK#", str_pack) f = requests.get(url_message) async def client_loop(self): try: - idle_packet = '{"type":"mode","mode":"idle","events":["rfid/*"]}\r\n' + idle_packet = ( + '{"type":"mode","mode":"idle","events":["rfid/*"]}\r\n' + ) self.writer.write(idle_packet.encode()) while self.running and not self.reader.at_eof(): line = await self.reader.readline() @@ -70,5 +100,6 @@ async def client_loop(self): if self.running: self.loop.stop() + if __name__ == "__main__": NabRfid2server.main(sys.argv[1:]) diff --git a/nabrfid2server/urls.py b/nabrfid2server/urls.py index a089a7fb..df88dadf 100644 --- a/nabrfid2server/urls.py +++ b/nabrfid2server/urls.py @@ -2,5 +2,5 @@ from .views import SettingsView urlpatterns = [ - path("settings", SettingsView.as_view()), + path("settings", SettingsView.as_view()), ] diff --git a/nabrfid2server/views.py b/nabrfid2server/views.py index 18bb3585..8d64e431 100644 --- a/nabrfid2server/views.py +++ b/nabrfid2server/views.py @@ -4,6 +4,7 @@ from .models import Config from .nabrfid2server import NabRfid2server + class SettingsView(TemplateView): template_name = "nabrfid2server/settings.html" @@ -14,9 +15,11 @@ def get_context_data(self, **kwargs): def post(self, request, *args, **kwargs): config = Config.load() - config.rfid_2_server_test = request.POST["rfid_2_server_test"] == "true" + config.rfid_2_server_test = ( + request.POST["rfid_2_server_test"] == "true" + ) config.rfid_2_server_mode = int(request.POST["rfid_2_server_mode"]) - config.rfid_2_server_url = request.POST["rfid_2_server_url"] + config.rfid_2_server_url = request.POST["rfid_2_server_url"] config.save() NabRfid2server.signal_daemon() context = super().get_context_data(**kwargs) diff --git a/nabweb/settings.py b/nabweb/settings.py index 7f79912e..aa168c79 100644 --- a/nabweb/settings.py +++ b/nabweb/settings.py @@ -161,4 +161,4 @@ # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/2.1/howto/static-files/ -STATIC_URL = "/static/" \ No newline at end of file +STATIC_URL = "/static/"