Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Django 2 #1

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions INSTALL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Installation guide
This guide is written to be used with a raspbian.

## Setup network
Set a static IP adress in '/etc/dhcpcd.conf'

```
sudo -i
echo "interface eth0" >> /etc/dhcpcd.conf
echo "static ip_address=10.2.3.4/24" >> /etc/dhcpcd.conf
echo "static routers=10.2.3.1" >> /etc/dhcpcd.conf
echo "static domain_name_server=10.2.3.2 10.2.3.3" >> /etc/dhcpcd.conf
reboot
```

## Setup SSH(optional, but handy)
Edit /etc/ssh/sshd_config to fit your needs
```
sudo vim /etc/ssh/sshd_config
```
Start and enable ssh
```
sudo systemctl enable --now ssh
```
## Install oversight
Install poetry
```
sudo pip3 install poetry
```
Create a user
```
sudo adduser --system oversight
```
Change to the overside user
```
sudo su - oversight -s /bin/bash
```
Go to the home directory
```
cd /home/oversight
```
Clone the oversight git repo
```
git clone https://github.com/feanor12/oversight.git
```
Change working directory to the cloned repository
```
cd oversight
```
Checkout the django2 branch
```
git checkout django2
```
Install dependencies
```
poetry install
```
Prepare config file
```
cp -r example/ config
vim config/settings.py
```
Generate Database, Superuser and static files
```
poetry shell
env PYTHONPATH="./config" ./manage.py migrate –settings="settings"
env PYTHONPATH="./config" ./manage.py collectstatic –settings="settings"
env PYTHONPATH="./config" ./manage.py createsuperuser –settings="settings"
```
Test server
```
env PYTHONPATH=“./config” ./manage.py runserver –settings="settings"
firefox http://127.0.0.1:8000
```

## Install Proxy
Install nginx
```
sudo apt install nginx
```
Copy template from oversight
```
sudo cp /home/oversight/oversight/ansible/roles/nginx/templates/nginx.conf /etc/nginx/nginx.conf
```
Replace the placeholders in the template and remove template if-lines
```
sudo sed -i -e "s/inventory_hostname/my.domain.com/g" /etc/nginx/nginx.conf
sudo sed -i -e "s/{{ 443 if nginx_no_ssl is undefined else 80 }}/443/g" /etc/nginx/nginx.conf
sudo sed -i -e "/{%.*%}/d" /etc/nginx/nginx.conf
```
Copy ssl certificates
```
sudo mkdir /etc/nginx/ssl
sudo cp server.crt server.key dhparams.pem /etc/nginx/etc
```

TODO
- fix server.py
- start services
- copy systemd unit files
- start services(systemctl)
17 changes: 17 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[[source]]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ich verwende inzwischen poetry; bei pipenv wird für mich zu wenig wert auf backward-compat und so gelegt.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dann leg ich noch ein poetry file an.

url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
pyserial = "*"
minimalmodbus = "*"
django = "*"
cherrypy = "*"
requests = "*"
cheroot = "*"

[dev-packages]

[requires]
python_version = "3.7"
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# oversight

Django based web application for data logging.

# Features
* Overview of the latest sensor values
* Detailed view with the latest values and a diagram showing the history
* Compare multiple sensors using a diagram
* Add sensor with the Django admin web interface
* API to set and get sensor data
* Logging daemon and web application are run in parallel
* Ansible scripts for easier setup on Raspberry Pi
* Easy to include new sensors

20 changes: 15 additions & 5 deletions example/settings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
Expand Down Expand Up @@ -31,7 +29,7 @@
'oversight.apps.OversightConfig',
)

MIDDLEWARE_CLASSES = (
MIDDLEWARE = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
Expand Down Expand Up @@ -73,6 +71,7 @@
# https://docs.djangoproject.com/en/dev/howto/static-files/

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
OVERSIGHT_KEY = 'hallo'

LOGGING = {
Expand All @@ -81,7 +80,7 @@
'handlers': {
'console': {
'class': 'logging.FileHandler',
'filename': '/dev/stderr',
'filename': '/tmp/oversight.log',
}
},
'loggers': {
Expand All @@ -92,4 +91,15 @@
}
}

TEMPLATE_CONTEXT_PROCESSORS = TEMPLATE_CONTEXT_PROCESSORS + ('django.core.context_processors.request',)
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages"
]
}
},
]
12 changes: 6 additions & 6 deletions oversight/management/commands/sensordaemon.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import logging
import threading
import Queue
from SimpleXMLRPCServer import SimpleXMLRPCServer
from multiprocessing import Queue
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

das direkte äquivalent zu Queue.Queue in python2 wäre queue.Queue: https://docs.python.org/3.8/library/queue.html

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wird in dem Fall queue.task_done() überhaupt benötigt? Ich sehe kein queue.join, oder ähnliches.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stimmt wahrscheinlich; ich würde es allerdings lassen fallst den code mal umbaust (sauberer ist es auf jeden fall einen task zu quittieren)

from xmlrpc.server import SimpleXMLRPCServer

from django.conf import settings
from django.core.management.base import NoArgsCommand
from django.core.management.base import BaseCommand

import requests

Expand Down Expand Up @@ -87,12 +87,12 @@ def worker(queue, tasks):
tasks[item[0]](*item[1:])
except Exception as e:
logger.error("Task failed: ", exc_info=e)
queue.task_done()
#queue.task_done()


class Command(NoArgsCommand):
class Command(BaseCommand):
def handle(self, **options):
queue = Queue.Queue()
queue = Queue()
server = SimpleXMLRPCServer(("localhost", 12345))
sensor_manager = SensorManager()
server.register_instance(sensor_manager)
Expand Down
36 changes: 24 additions & 12 deletions oversight/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,50 @@
# encoding: utf8
from django.db import models, migrations
# Generated by Django 2.1.1 on 2018-09-12 10:53

from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='LogEntry',
fields=[
(u'id', models.AutoField(verbose_name=u'ID', serialize=False, auto_created=True, primary_key=True)),
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('datetime', models.DateTimeField(default=django.utils.timezone.now)),
('value', models.CharField(max_length=255)),
],
options={
},
bases=(models.Model,),
),
migrations.CreateModel(
name='Sensor',
fields=[
(u'id', models.AutoField(verbose_name=u'ID', serialize=False, auto_created=True, primary_key=True)),
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=255)),
('api_endpoint', models.SlugField()),
('api_endpoint', models.SlugField(unique=True)),
('unit', models.CharField(max_length=255)),
('sensor_class', models.CharField(max_length=255)),
('params', models.TextField()),
('current_log', models.ForeignKey(to='oversight.LogEntry', to_field=u'id', null=True)),
('log_plot', models.BooleanField(default=False)),
('logging_enabled', models.BooleanField(default=True)),
('alarm_below', models.CharField(blank=True, max_length=255)),
('alarm_above', models.CharField(blank=True, max_length=255)),
('alarm_acked', models.BooleanField(default=True)),
('current_log', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='+', to='oversight.LogEntry')),
],
options={
},
bases=(models.Model,),
),
migrations.AddField(
model_name='logentry',
name='sensor',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='oversight.Sensor'),
),
migrations.AlterIndexTogether(
name='logentry',
index_together={('sensor', 'datetime')},
),
]
69 changes: 0 additions & 69 deletions oversight/migrations/0001_initial_squashed.py

This file was deleted.

18 changes: 0 additions & 18 deletions oversight/migrations/0001_logentry_sensor.py

This file was deleted.

22 changes: 0 additions & 22 deletions oversight/migrations/0002_auto_20140131_1403.py

This file was deleted.

Loading