Skip to content
This repository has been archived by the owner on Aug 30, 2022. It is now read-only.

Added webserver #30

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Conversation

OverDriveGain
Copy link
Contributor

SQL und datafiles databases. Django sql is pretty straight forward!

Sending control to pump per pulling is not there yet.

Copy link
Collaborator

@dingens dingens left a comment

Choose a reason for hiding this comment

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

Please add __pycache__ and the database to .gitignore. Also please check git status before committing :)

And please remove all those files, of course. Is there a way to do this without doing git commit --amend and force-push (which would probably make us loose my comments below)?

Apart from that, I'm unsure about some design decisions, but we should probably discuss this in person. (Just noting so I don't forget: what is water? why two tables? possible to just store temperature or humidity, not both?)

You might want to use this for vim:

set directory=~/.cache/vimswp   " Don't clutter working dirs with swap files
if !isdirectory($HOME . '/.cache/vimswp')
    call mkdir($HOME . '/.cache/vimswp')
endif

sensor_id = models.IntegerField(default = 0)
water = models.IntegerField(default=0)
temperature = models.IntegerField(default=0)
humidity = models.IntegerField(default=0)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are you sure default=0 is sensible here? I think it's better to allow NULL values instead, to be able to differentiate between unset values and the value of 0. (Applies to other places as well)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed to default = null

sensor_id = 0
humidity = 0
water = 0
temperature = 0
Copy link
Collaborator

Choose a reason for hiding this comment

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

why those definitions?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Object makes it easier to build an html template in django

lines = f.read().splitlines()
last_line = lines[-1]
sensor_data = json.loads(last_line)
sensors_obj_array.append(Sensor(sensor_data['node_id'],sensor_data['humidity'],sensor_data['water'],sensor_data['temperature']))
Copy link
Collaborator

Choose a reason for hiding this comment

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

You can just do Sensor(**sensor_data) :) (in case you've written the json yourself, never do this on untrusted input of course)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nice

write_sensor_data_db(json_data)
except KeyError:
print("INVALID REQUEST")
HttpResponseServerError("malformed data!")
Copy link
Collaborator

Choose a reason for hiding this comment

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

you probably need a raise or return here, don't you? (btw, there's also one that returns error code 400 instead of 500, that'd fit better here)

Copy link
Contributor Author

@OverDriveGain OverDriveGain Jul 1, 2018

Choose a reason for hiding this comment

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

500 is more suitable because the error is caused by a bad request, it wasn't caused by an internal error

return os.path.join(here,database_dir)

def get_sensor_file(note_id):
return os.path.join(get_db_dir(), 'sensor_'+node_id+'.data')
Copy link
Collaborator

Choose a reason for hiding this comment

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

as a safety measure, I'd use 'sensor_%d.data' % node_id to make sure node_id is an integer, to be sure this can never be used to open arbitrary files

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OKK
This is not gonna be used in sqlite anyways

humidity = json_data['humidity']
temperature = json_data['temperature']
water = json_data['water']
write_sensor_data_db(json_data)
Copy link
Collaborator

Choose a reason for hiding this comment

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

you never use all those variables

return HttpResponse("Hello, world. You're at the pflanzen1 index.")

def get_sensors_data(request):
db_files = [f for f in os.listdir(get_db_dir()) if f.endswith('.data')]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hm why did you implement both sql and json storage?

It seems that now get_data uses json and set_data stores in the database, so data added using the test script doesn't appear in the get data page.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants