mirror of
https://github.com/daniviga/django-ram.git
synced 2025-08-03 20:57:50 +02:00
Improvements and driver stub for serial comms
This commit is contained in:
@@ -38,6 +38,7 @@ INSTALLED_APPS = [
|
||||
'django.contrib.sessions',
|
||||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
'django_countries',
|
||||
'dcc',
|
||||
'roster',
|
||||
]
|
||||
|
@@ -1,11 +1,11 @@
|
||||
from django.contrib import admin
|
||||
from roster.models import Cab, Decoder, Manufacturer
|
||||
from roster.models import Cab, Decoder, Manufacturer, Company
|
||||
|
||||
|
||||
@admin.register(Cab)
|
||||
class CabAdmin(admin.ModelAdmin):
|
||||
readonly_fields = ('image_thumbnail', 'creation_time', 'updated_time',)
|
||||
list_display = ('identifier', 'manufacturer', 'address')
|
||||
list_display = ('identifier', 'address', 'manufacturer', 'company')
|
||||
list_filter = list_display
|
||||
search_fields = list_display
|
||||
|
||||
@@ -15,6 +15,7 @@ class CabAdmin(admin.ModelAdmin):
|
||||
'address',
|
||||
'manufacturer',
|
||||
'decoder',
|
||||
'company',
|
||||
'epoch',
|
||||
'production_year',
|
||||
'purchase_date',
|
||||
@@ -30,4 +31,5 @@ class CabAdmin(admin.ModelAdmin):
|
||||
|
||||
|
||||
admin.site.register(Decoder)
|
||||
admin.site.register(Company)
|
||||
admin.site.register(Manufacturer)
|
||||
|
22
dcc/roster/migrations/0012_company.py
Normal file
22
dcc/roster/migrations/0012_company.py
Normal file
@@ -0,0 +1,22 @@
|
||||
# Generated by Django 4.0 on 2021-12-16 21:26
|
||||
|
||||
from django.db import migrations, models
|
||||
import django_countries.fields
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('roster', '0011_remove_cab_decoder_model_cab_decoder'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Company',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('name', models.CharField(max_length=128, unique=True)),
|
||||
('country', django_countries.fields.CountryField(max_length=2)),
|
||||
],
|
||||
),
|
||||
]
|
19
dcc/roster/migrations/0013_cab_company.py
Normal file
19
dcc/roster/migrations/0013_cab_company.py
Normal file
@@ -0,0 +1,19 @@
|
||||
# Generated by Django 4.0 on 2021-12-16 21:27
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('roster', '0012_company'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='cab',
|
||||
name='company',
|
||||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='roster.company'),
|
||||
),
|
||||
]
|
@@ -1,5 +1,6 @@
|
||||
from uuid import uuid4
|
||||
from django.db import models
|
||||
from django_countries.fields import CountryField
|
||||
# from django.core.files.storage import FileSystemStorage
|
||||
# from django.dispatch import receiver
|
||||
|
||||
@@ -21,13 +22,15 @@ class Manufacturer(models.Model):
|
||||
|
||||
class Company(models.Model):
|
||||
name = models.CharField(max_length=128, unique=True)
|
||||
country = models.CharField(max_length=128, unique=True)
|
||||
country = CountryField()
|
||||
|
||||
class Meta:
|
||||
verbose_name_plural = "Companies"
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
||||
|
||||
class Decoder(models.Model):
|
||||
name = models.CharField(max_length=128, unique=True)
|
||||
manufacturer = models.ForeignKey(
|
||||
@@ -50,6 +53,9 @@ class Cab(models.Model):
|
||||
decoder = models.ForeignKey(
|
||||
Decoder, on_delete=models.CASCADE,
|
||||
null=True, blank=True)
|
||||
company = models.ForeignKey(
|
||||
Company, on_delete=models.CASCADE,
|
||||
null=True, blank=True)
|
||||
epoch = models.CharField(max_length=32, blank=True)
|
||||
production_year = models.SmallIntegerField(null=True, blank=True)
|
||||
purchase_date = models.DateField(null=True, blank=True)
|
||||
|
41
deamons/serial.py
Normal file
41
deamons/serial.py
Normal file
@@ -0,0 +1,41 @@
|
||||
import serial
|
||||
import logging
|
||||
import socket
|
||||
import asyncio
|
||||
import time
|
||||
import paho.mqtt.client as mqtt
|
||||
from asyncio_mqtt import Client
|
||||
|
||||
MQTT_HOST = "127.0.0.1"
|
||||
MQTT_PORT = 1883
|
||||
|
||||
|
||||
async def mqtt_broker(ser):
|
||||
async with Client(MQTT_HOST, port=MQTT_PORT) as client:
|
||||
await client.subscribe("dcc/commands")
|
||||
async with client.unfiltered_messages() as messages:
|
||||
async for message in messages:
|
||||
print(message.payload.decode())
|
||||
ser.write(message.payload)
|
||||
|
||||
|
||||
def main():
|
||||
client = mqtt.Client()
|
||||
ser = serial.Serial('/dev/pts/7')
|
||||
ser.baudrate = 9600
|
||||
while True:
|
||||
try:
|
||||
client.connect(MQTT_HOST, MQTT_PORT)
|
||||
break
|
||||
except (socket.gaierror, ConnectionRefusedError):
|
||||
logging.warning('Broker not available')
|
||||
time.sleep(5)
|
||||
|
||||
logging.info('Broker subscribed')
|
||||
client.disconnect()
|
||||
asyncio.run(mqtt_broker(ser))
|
||||
ser.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
@@ -4,5 +4,5 @@ djangorestframework
|
||||
django-health-check
|
||||
# psycopg2-binary
|
||||
paho-mqtt
|
||||
# asyncio-mqtt
|
||||
# PyYAML
|
||||
asyncio-mqtt
|
||||
pySerial
|
||||
|
Reference in New Issue
Block a user