From fe4f05bd544255b4874ac8e60d2095a7a0b69dd4 Mon Sep 17 00:00:00 2001 From: Samo Penic <samo.penic@gmail.com> Date: Thu, 11 Apr 2019 19:41:14 +0000 Subject: [PATCH] Spremembe narejene na zadnji delavnici. --- djsenzor/calculator/tests.py | 3 djsenzor/calculator/admin.py | 3 djsenzor/calculator/__init__.py | 0 djsenzor/calculator/models.py | 3 Pipfile | 2 djsenzor/djsenzor/urls.py | 3 ARDUINO/DigitalReadSerial/DigitalReadSerial.ino | 14 ++ djsenzor/calculator/views.py | 24 ++++ djsenzor/calculator/apps.py | 5 + djsenzor/calculator/urls.py | 5 + djsenzor/db.sqlite3 | 0 ARDUINO/serial_read.py | 9 + djsenzor/sensorui/views.py | 41 ++++--- djsenzor/sensorui/urls.py | 4 djsenzor/djsenzor/settings.py | 17 ++ djsenzor/sensorui/migrations/0001_initial.py | 48 +++++++++ djsenzor/calculator/forms.py | 5 + djsenzor/calculator/templates/calculator/sestej.html | 18 +++ djsenzor/sensorui/admin.py | 8 + djsenzor/sensorui/static/sensorui/muca.jpg | 0 djsenzor/calculator/migrations/__init__.py | 0 djsenzor/sensorui/templates/sensorui/index.html | 19 +++ djsenzor/sensorui/models.py | 26 +++++ 23 files changed, 230 insertions(+), 27 deletions(-) diff --git a/ARDUINO/DigitalReadSerial/DigitalReadSerial.ino b/ARDUINO/DigitalReadSerial/DigitalReadSerial.ino new file mode 100644 index 0000000..9dc171f --- /dev/null +++ b/ARDUINO/DigitalReadSerial/DigitalReadSerial.ino @@ -0,0 +1,14 @@ + +void setup() { + Serial.begin(9600); +} +long randNo; +// the loop routine runs over and over again forever: +void loop() { + randNo=random(10,40); + Serial.println(randNo); + delay(1000); // delay in between reads for stability +} + + + diff --git a/ARDUINO/serial_read.py b/ARDUINO/serial_read.py new file mode 100644 index 0000000..0198d0c --- /dev/null +++ b/ARDUINO/serial_read.py @@ -0,0 +1,9 @@ +import serial +import io + +ser=serial.Serial('/dev/ttyUSB0',9600,timeout=10) +while True: + ret=ser.readline() + print(ret.decode('ascii').strip()) + +ser.close() diff --git a/Pipfile b/Pipfile index 4cc9973..4d24082 100644 --- a/Pipfile +++ b/Pipfile @@ -5,6 +5,8 @@ [packages] django = "*" +djangorestframework = "*" +mysqlclient = "*" [dev-packages] diff --git a/djsenzor/calculator/__init__.py b/djsenzor/calculator/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/djsenzor/calculator/__init__.py diff --git a/djsenzor/calculator/admin.py b/djsenzor/calculator/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/djsenzor/calculator/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/djsenzor/calculator/apps.py b/djsenzor/calculator/apps.py new file mode 100644 index 0000000..e13641c --- /dev/null +++ b/djsenzor/calculator/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class CalculatorConfig(AppConfig): + name = 'calculator' diff --git a/djsenzor/calculator/forms.py b/djsenzor/calculator/forms.py new file mode 100644 index 0000000..866caf3 --- /dev/null +++ b/djsenzor/calculator/forms.py @@ -0,0 +1,5 @@ +from django import forms + +class Stevila(forms.Form): + a=forms.FloatField() + b=forms.FloatField() diff --git a/djsenzor/calculator/migrations/__init__.py b/djsenzor/calculator/migrations/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/djsenzor/calculator/migrations/__init__.py diff --git a/djsenzor/calculator/models.py b/djsenzor/calculator/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/djsenzor/calculator/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/djsenzor/calculator/templates/calculator/sestej.html b/djsenzor/calculator/templates/calculator/sestej.html new file mode 100644 index 0000000..4bc4780 --- /dev/null +++ b/djsenzor/calculator/templates/calculator/sestej.html @@ -0,0 +1,18 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <title>Sestej stevili</title> +</head> +<body> +<p>Sestej stevili</p> + +<form method="POST"> + {{ formular.as_p }} +{% csrf_token %} + <button type="submit">Poslji</button> +</form> +<hr /> +<p>Vsota je: {{ vsota }}</p> +</body> +</html> \ No newline at end of file diff --git a/djsenzor/calculator/tests.py b/djsenzor/calculator/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/djsenzor/calculator/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/djsenzor/calculator/urls.py b/djsenzor/calculator/urls.py new file mode 100644 index 0000000..20a8c25 --- /dev/null +++ b/djsenzor/calculator/urls.py @@ -0,0 +1,5 @@ +from django.urls import path +from . import views +urlpatterns = [ + path('sestej', views.sestej, name='sestej'), +] diff --git a/djsenzor/calculator/views.py b/djsenzor/calculator/views.py new file mode 100644 index 0000000..b644a66 --- /dev/null +++ b/djsenzor/calculator/views.py @@ -0,0 +1,24 @@ +from django.shortcuts import render +from django.http import HttpResponse + +from .forms import Stevila + +# Create your views here. + + +def sestej(request): + + form=Stevila() + vsota="NaN" + + if request.method == "POST": + print(request.POST['a']) + form=Stevila(request.POST) + + if form.is_valid(): + vsota=float(form.data['a'])+float(form.data['b']) + + print(form.data) + #form=Stevila() + return render(request, 'calculator/sestej.html', context={'formular':form, 'vsota':vsota}) + diff --git a/djsenzor/db.sqlite3 b/djsenzor/db.sqlite3 index bd9e1b7..0790c37 100644 --- a/djsenzor/db.sqlite3 +++ b/djsenzor/db.sqlite3 Binary files differ diff --git a/djsenzor/djsenzor/settings.py b/djsenzor/djsenzor/settings.py index cf8f085..f6136ef 100644 --- a/djsenzor/djsenzor/settings.py +++ b/djsenzor/djsenzor/settings.py @@ -25,7 +25,7 @@ # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True -ALLOWED_HOSTS = [] +ALLOWED_HOSTS = ['localhost', 'moja.domena.si'] # Application definition @@ -37,7 +37,9 @@ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', - 'sensorui.apps' + 'sensorui', + 'calculator', + 'rest_framework' ] MIDDLEWARE = [ @@ -76,8 +78,14 @@ DATABASES = { 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), +# 'ENGINE': 'django.db.backends.sqlite3', +# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), + 'ENGINE': 'django.db.backends.mysql', + 'NAME': 'samo', + 'USER': 'samo', + 'PASSWORD': 'samo', + 'HOST': 'localhost', # Or an IP Address that your DB is hosted on + 'PORT': '3306', } } @@ -119,3 +127,4 @@ # https://docs.djangoproject.com/en/2.1/howto/static-files/ STATIC_URL = '/static/' +STATIC_ROOT = os.path.join(BASE_DIR, 'static/') diff --git a/djsenzor/djsenzor/urls.py b/djsenzor/djsenzor/urls.py index 16c5ddd..606f475 100644 --- a/djsenzor/djsenzor/urls.py +++ b/djsenzor/djsenzor/urls.py @@ -18,5 +18,6 @@ urlpatterns = [ path('admin/', admin.site.urls), - path('sensorui/', include('sensorui.urls')) + path('sensorui/', include('sensorui.urls')), + path('calculator/', include('calculator.urls')) ] diff --git a/djsenzor/sensorui/admin.py b/djsenzor/sensorui/admin.py index 8c38f3f..70d9878 100644 --- a/djsenzor/sensorui/admin.py +++ b/djsenzor/sensorui/admin.py @@ -1,3 +1,11 @@ from django.contrib import admin # Register your models here. +from .models import * + +admin.site.register(Measurement) +admin.site.register(Sensor) +admin.site.register(SensorType) + + + diff --git a/djsenzor/sensorui/migrations/0001_initial.py b/djsenzor/sensorui/migrations/0001_initial.py new file mode 100644 index 0000000..dbd7b1b --- /dev/null +++ b/djsenzor/sensorui/migrations/0001_initial.py @@ -0,0 +1,48 @@ +# Generated by Django 2.1.7 on 2019-03-25 17:17 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Measurement', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('value', models.FloatField()), + ('timestamp', models.DateTimeField(auto_now_add=True)), + ], + ), + migrations.CreateModel( + name='Sensor', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=30)), + ], + ), + migrations.CreateModel( + name='SensorType', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=30)), + ('unit', models.CharField(max_length=10)), + ], + ), + migrations.AddField( + model_name='sensor', + name='sensor_type', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='sensorui.SensorType'), + ), + migrations.AddField( + model_name='measurement', + name='sensor_id', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='sensorui.Sensor'), + ), + ] diff --git a/djsenzor/sensorui/models.py b/djsenzor/sensorui/models.py index 71a8362..afbc44a 100644 --- a/djsenzor/sensorui/models.py +++ b/djsenzor/sensorui/models.py @@ -1,3 +1,29 @@ from django.db import models + # Create your models here. + + +class SensorType(models.Model): + name = models.CharField(max_length=30) + unit = models.CharField(max_length=10) + + def __str__(self): + return self.name + + +class Sensor(models.Model): + sensor_type = models.ForeignKey(SensorType, on_delete=models.CASCADE) + name = models.CharField(max_length=30) + + def __str__(self): + return self.name + + +class Measurement(models.Model): + sensor_id = models.ForeignKey(Sensor, on_delete=models.CASCADE) + value = models.FloatField() + timestamp = models.DateTimeField(auto_now_add=True) + + def __str__(self): + return "{} {} {} {}".format(self.sensor_id, self.value, self.sensor_id.sensor_type.unit, self.timestamp) diff --git a/djsenzor/sensorui/static/sensorui/muca.jpg b/djsenzor/sensorui/static/sensorui/muca.jpg new file mode 100644 index 0000000..48bc080 --- /dev/null +++ b/djsenzor/sensorui/static/sensorui/muca.jpg Binary files differ diff --git a/djsenzor/sensorui/templates/sensorui/index.html b/djsenzor/sensorui/templates/sensorui/index.html index 5a64b92..d575c37 100644 --- a/djsenzor/sensorui/templates/sensorui/index.html +++ b/djsenzor/sensorui/templates/sensorui/index.html @@ -1,11 +1,24 @@ +{% load static %} + <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Hello world stran</title> +<script> + function myFunction(){ + var xhttp = new XMLHttpRequest(); + xhttp.open("GET","my-ajax-test/", true); + xhttp.send(); + } + +</script> + </head> <body> <h1>hello world</h1> +<img src='{% static "sensorui/muca.jpg" %}'> +<button onclick="myFunction()">Vzgi lucko</button> sin(pi/2)={{ sinpi2 }} <hr /> Zahtevek podal: {{ moje_ime }} @@ -19,9 +32,9 @@ </tr> {% for row in data_table %} <tr> - <td>{{ row.sensor_id }}</td> - <td>{{ row.data }}</td> - <td>{{ row.date }}</td> + <td>{{ row.sensor_id.pk }}</td> + <td>{{ row.value }}{{ row.sensor_id.sensor_type.unit }}</td> + <td>{{ row.timestamp }}</td> </tr> {% endfor %} </table> diff --git a/djsenzor/sensorui/urls.py b/djsenzor/sensorui/urls.py index 46d396c..8805e44 100644 --- a/djsenzor/sensorui/urls.py +++ b/djsenzor/sensorui/urls.py @@ -17,5 +17,7 @@ from django.urls import path from . import views urlpatterns = [ - path('', views.index, name='index'), + path('<int:pk>', views.index, name='index'), + path('api/measurement', views.SensorData.as_view(), name='measurement'), + path('my-ajax-test/', views.myajaxtestview, name='ajax-test-view'), ] diff --git a/djsenzor/sensorui/views.py b/djsenzor/sensorui/views.py index 786e1bb..25533a0 100644 --- a/djsenzor/sensorui/views.py +++ b/djsenzor/sensorui/views.py @@ -1,37 +1,42 @@ from django.shortcuts import render - +from .models import * from django.http.response import HttpResponse # Create your views here. from math import pi, sin +from rest_framework.views import APIView +from rest_framework.response import Response -def index(request): + +class SensorData(APIView): + def post(self, request): + data=request.data + m=Measurement(sensor_id=Sensor.objects.get(pk=data['sensor_id']), value=data['value']) + m.save() + return Response(None) + + + + +def index(request, pk): sinpi2=sin(pi/2) intlist=list(range(1,11)) moj_template='sensorui/index.html' - data_table=[] + measurements=Measurement.objects.filter(sensor_id=pk).all() - data_table.append({'sensor_id':1, - 'data': 10.0, - 'date': '10.10.1010' - }) - - data_table.append({'sensor_id':1, - 'data': 8.0, - 'date': '11.10.1010' - }) - - data_table.append({'sensor_id':1, - 'data': 12.0, - 'date': '11.11.1010' - }) context={ 'sinpi2':sinpi2, 'moje_ime': "Samo Penic", 'intlist': intlist, - 'data_table': data_table, + 'data_table': measurements, } return render(request, moj_template, context) + + +def myajaxtestview(request): + print("AJAX REQUEST") + rezultat=(int(request.GET['a'])+int(request.GET['b'])) + return HttpResponse(rezultat) -- Gitblit v1.9.3