-
Notifications
You must be signed in to change notification settings - Fork 4
Added webserver #30
base: master
Are you sure you want to change the base?
Added webserver #30
Conversation
There was a problem hiding this 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) |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why those definitions?
There was a problem hiding this comment.
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'])) |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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!") |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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') |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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')] |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
SQL und datafiles databases. Django sql is pretty straight forward!
Sending control to pump per pulling is not there yet.