Skip to content

Commit 43364a4

Browse files
authored
Merge branch 'master' into arp
2 parents 67119c7 + 664ddd7 commit 43364a4

11 files changed

Lines changed: 128 additions & 5 deletions

File tree

.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ pip-log.txt
2828
.tox
2929
nosetests.xml
3030

31-
# Translations
32-
*.mo
33-
3431
# Mr Developer
3532
.mr.developer.cfg
3633
.project

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,26 @@ urlpatterns = [
3131
```
3232

3333
Profit!
34+
35+
### Settings
36+
37+
The optional MQTT integration has the following settings:
38+
39+
```
40+
LABADMIN_MQTT_CONFIG = {
41+
'HOSTNAME': 'localhost',
42+
'PORT': 1883,
43+
'AUTH': None,
44+
'TLS': None,
45+
'PROTOCOL': MQTTv311,
46+
'TRANSPORT': 'tcp',
47+
}
48+
49+
# Should we publish on MQTT each entrance
50+
LABADMIN_NOTIFY_MQTT_ENTRANCE = False
51+
52+
# The MQTT topic where to publish
53+
LABADMIN_MQTT_ENTRANCE_TOPIC = 'labadmin/entrance'
54+
```
55+
56+
See [Paho MQTT documentation](https://github.com/eclipse/paho.mqtt.python#single) for `LABADMIN_MQTT_CONFIG` values.
357 Bytes
Binary file not shown.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# This file is distributed under the same license as the LabAdmin package.
2+
#
3+
msgid ""
4+
msgstr ""
5+
"Project-Id-Version: LabAdmin\n"
6+
"Report-Msgid-Bugs-To: \n"
7+
"POT-Creation-Date: 2017-01-31 15:06+0100\n"
8+
"PO-Revision-Date: 2017-01-31 14:56+0100\n"
9+
"Last-Translator: LabAdmin contributors\n"
10+
"Language-Team: English \n"
11+
"Language: en\n"
12+
"MIME-Version: 1.0\n"
13+
"Content-Type: text/plain; charset=UTF-8\n"
14+
"Content-Transfer-Encoding: 8bit\n"
15+
16+
#: views.py:84
17+
msgid "entrance"
18+
msgstr ""
475 Bytes
Binary file not shown.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# This file is distributed under the same license as the LabAdmin package.
2+
#
3+
# Translators:
4+
# Riccardo Magliocchetti <riccardo.magliocchetti@gmail.com>, 2017.
5+
#
6+
msgid ""
7+
msgstr ""
8+
"Project-Id-Version: LabAdmin\n"
9+
"Report-Msgid-Bugs-To: \n"
10+
"POT-Creation-Date: 2017-01-31 15:06+0100\n"
11+
"PO-Revision-Date: 2017-01-31 14:57+0100\n"
12+
"Last-Translator: Riccardo Magliocchetti <riccardo.magliocchetti@gmail.com\n"
13+
"Language-Team: Italian\n"
14+
"Language: it\n"
15+
"MIME-Version: 1.0\n"
16+
"Content-Type: text/plain; charset=UTF-8\n"
17+
"Content-Transfer-Encoding: 8bit\n"
18+
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
19+
20+
#: views.py:84
21+
msgid "entrance"
22+
msgstr "entrata"

labAdmin/notifications.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from .settings import MQTT_CONFIG
2+
from paho.mqtt import publish
3+
4+
5+
def mqtt_publish(topic, payload):
6+
config = {k.lower(): v for k, v in MQTT_CONFIG.items()}
7+
publish.single(topic, payload, **config)

labAdmin/settings.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from django.conf import settings
2+
3+
from paho.mqtt.client import MQTTv311
4+
5+
6+
BASE_MQTT_CONFIG = {
7+
'HOSTNAME': 'localhost',
8+
'PORT': 1883,
9+
'AUTH': None,
10+
'TLS': None,
11+
'PROTOCOL': MQTTv311,
12+
'TRANSPORT': 'tcp',
13+
}
14+
MQTT_CONFIG = {}
15+
16+
17+
USER_MQTT_CONFIG = getattr(settings, 'LABADMIN_MQTT_CONFIG', {})
18+
MQTT_ENTRANCE_NOTIFICATION = getattr(settings, 'LABADMIN_NOTIFY_MQTT_ENTRANCE', False)
19+
MQTT_ENTRANCE_TOPIC = getattr(settings, 'LABADMIN_MQTT_ENTRANCE_TOPIC', 'labadmin/entrance')
20+
21+
MQTT_CONFIG = BASE_MQTT_CONFIG.copy()
22+
MQTT_CONFIG.update(USER_MQTT_CONFIG)

labAdmin/tests.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
from unittest.mock import mock_open, patch
66

77
from django.contrib.auth.models import User
8-
from django.test import TestCase, Client
8+
from django.test import TestCase, Client, override_settings
99
from django.urls import reverse
1010
from django.utils import timezone, dateparse
1111

1212
from .models import (
1313
Card, Group, LogAccess, Role, TimeSlot, UserProfile,
14-
LogCredits, Category, Device, LogDevice
14+
LogCredits, Category, Device, LogDevice, LogError
1515
)
1616
from .arp import get_neighbours
1717

@@ -135,6 +135,21 @@ def test_open_door_by_nfc(self):
135135
response = client.get(url, HTTP_AUTHORIZATION=auth)
136136
self.assertEqual(response.status_code, 405)
137137

138+
@override_settings(LABADMIN_NOTIFY_MQTT_ENTRANCE=True)
139+
def test_open_door_by_nfc_mqtt_error_log(self):
140+
client = Client()
141+
auth = 'Token {}'.format(self.device.token)
142+
url = reverse('open-door-nfc')
143+
data = {
144+
'nfc_id': self.card.nfc_id
145+
}
146+
147+
mqtt_errors = LogError.objects.filter(description__endswith='failed to publish to mqtt')
148+
self.assertFalse(mqtt_errors.exists())
149+
response = client.post(url, data, format='json', HTTP_AUTHORIZATION=auth)
150+
mqtt_errors = LogError.objects.filter(description__endswith='failed to publish to mqtt')
151+
self.assertEqual(mqtt_errors.count(), 1)
152+
138153
def test_get_card_credits(self):
139154

140155
client = Client()

labAdmin/views.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import json
2+
13
from django.http import HttpResponse, Http404
24
from django.shortcuts import get_object_or_404
5+
from django.utils.translation import ugettext as _
36
from django.db import transaction, IntegrityError
47

58
from labAdmin.serializers import *
@@ -11,9 +14,14 @@
1114
from oauth2_provider.models import AccessToken
1215

1316
from . import functions
17+
from .notifications import mqtt_publish
1418
from .permissions import (
1519
get_token_from_request, DeviceTokenPermission
1620
)
21+
from .settings import (
22+
MQTT_ENTRANCE_NOTIFICATION,
23+
MQTT_ENTRANCE_TOPIC
24+
)
1725

1826

1927
class LoginByNFC(APIView):
@@ -73,6 +81,16 @@ def post(self, request, format=None):
7381
else:
7482
utype = "other"
7583

84+
if MQTT_ENTRANCE_NOTIFICATION and log_access.opened:
85+
payload = {
86+
'user': user.name,
87+
'state': _('entrance')
88+
}
89+
try:
90+
mqtt_publish(MQTT_ENTRANCE_TOPIC, json.dumps(payload))
91+
except Exception as e:
92+
LogError(description="Api: Open Door By NFC - failed to publish to mqtt", code=e).save()
93+
7694
data = {
7795
"users": UserProfileSerializer(users, many=True).data,
7896
"type": utype,

0 commit comments

Comments
 (0)