Skip to content

Commit

Permalink
PYLINT+FLAKE8+BLACK+ISORT: code reformatted to comply with expected c…
Browse files Browse the repository at this point in the history
…oding style
  • Loading branch information
Commit-La-Grenouille committed Apr 6, 2022
1 parent 4327bb9 commit 1f4d457
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 40 deletions.
26 changes: 19 additions & 7 deletions nabrfid2server/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -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;"
),
),
],
),
]
9 changes: 8 additions & 1 deletion nabrfid2server/models.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
from django.db import models

from nabcommon import singleton_model


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"
87 changes: 60 additions & 27 deletions nabrfid2server/nabrfid2server.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import sys
import json
import logging
import sys

import requests

from nabcommon.nabservice import NabService


Expand All @@ -16,40 +18,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 "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)

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)
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 (
(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)
str_pack = str_pack.lower()
str_pack = str_pack.replace('\"','')
url_message = url_message.replace("#RFID_JEEPACK#",str_pack)
f = requests.get(url_message)
str_pack = str_pack.replace('"', "")
url_message = url_message.replace("#RFID_JEEPACK#", str_pack)
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()
Expand All @@ -70,5 +102,6 @@ async def client_loop(self):
if self.running:
self.loop.stop()


if __name__ == "__main__":
NabRfid2server.main(sys.argv[1:])
3 changes: 2 additions & 1 deletion nabrfid2server/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.urls import path

from .views import SettingsView

urlpatterns = [
path("settings", SettingsView.as_view()),
path("settings", SettingsView.as_view()),
]
10 changes: 7 additions & 3 deletions nabrfid2server/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from django.http import JsonResponse
from django.shortcuts import render
from django.views.generic import TemplateView
from django.http import JsonResponse

from .models import Config
from .nabrfid2server import NabRfid2server


class SettingsView(TemplateView):
template_name = "nabrfid2server/settings.html"

Expand All @@ -14,9 +16,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)
Expand Down
2 changes: 1 addition & 1 deletion nabweb/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,4 @@
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/

STATIC_URL = "/static/"
STATIC_URL = "/static/"

0 comments on commit 1f4d457

Please sign in to comment.