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

Vcoman/ces demo #6

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ media
# Backup files #
*.bak

# If you are using PyCharm #
# If you are using PyCharm #
.idea/*
*/.idea/*
*.iws /out/

Expand Down Expand Up @@ -103,3 +104,4 @@ GitHub.sublime-settings
!.vscode/launch.json
!.vscode/extensions.json
.history

3 changes: 2 additions & 1 deletion agriculture/agriculturecommon/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
<script defer src="https://cdn.sobekrepository.org/includes/gmaps-markerwithlabel/1.9.1/gmaps-markerwithlabel-1.9.1.js"></script>
<script src="https://www.gstatic.com/charts/loader.js"></script>
<script>
google.charts.load("current", {"packages":["line"]});
<!--42 is for version 42 cause there is a bug that causes the charts popups to flicker and version 42 did not have this issue.-->
google.charts.load("42", {"packages":["line", "corechart"]});
</script>
</head>
<body>
Expand Down
68 changes: 21 additions & 47 deletions agriculture/agriculturecore/drm_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,15 @@
DO_CMD_XBEE_DISCOVER = "<discover option='current' />"
DO_CMD_XBEE_SETTING = "<radio_command addr='{}' id='{}' format='{}' timeout='1000' />"

ID_WIND = "wind"
ID_WIND = "wind_speed"
ID_WIND_DIR = "wind_direction"
ID_RADIATION = "radiation"
ID_RAIN = "rain"
ID_LEVEL = "level"
ID_VALVE = "valve"
ID_TEMPERATURE = "temperature"
ID_PRESSURE = "pressure"
ID_LUMINOSITY = "luminosity"
ID_BATTERY = "battery"
ID_MOISTURE = "moisture"

Expand All @@ -67,8 +70,6 @@
ID_WEATHER = "weather"
ID_TANK = "tank"

ID_ERROR = "error"

REGEX_DEV_REQUEST_RESPONSE = ".*<device_request .*>(.*)<\\/device_request>.*"
REGEX_DO_CMD_RESPONSE = ".*<do_command target=[^>]*>(.*)<\\/do_command>.*"

Expand Down Expand Up @@ -146,41 +147,6 @@ def get_device_cloud_session(session):
base_url=user_serialized.server)


def check_ajax_request(request):
Copy link
Member

Choose a reason for hiding this comment

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

Why all these changes (until line 543)?

"""
Checks whether the given AJAX request is valid and the user is
authenticated.

Args:
request (:class:`.WSGIRequest`): The HTTP request.

Returns:
`None` if the request is valid, or a `JsonResponse` with the error
if it is not.
"""
if is_authenticated(request):
if not request.is_ajax or request.method != "POST":
return JsonResponse({ID_ERROR: "AJAX request must be sent using POST"}, status=400)
return None
else:
return JsonResponse({ID_ERROR: "Not authenticated"}, status=401)


def get_exception_response(e):
"""
Returns the JSON response with the error contained in the given exception.

Args:
e (:class:`.Exception`): The exception.

Returns:
A JSON response with the details of the exception.
"""
return JsonResponse({ID_ERROR: ("Error in the DRM request: {}.".format(e.response.text)
if isinstance(e, DeviceCloudHttpException) else str(e))},
status=400)


def send_device_request(request, target):
"""
Sends a Device Request to DRM to the device with the given ID.
Expand All @@ -192,12 +158,13 @@ def send_device_request(request, target):
Returns:
A JSON with the response or the error.
"""
# Check if the AJAX request is valid.
error = check_ajax_request(request)
if error is not None:
return error
if not request.is_ajax or request.method != "POST":
return JsonResponse({"error": "AJAX request must be sent using POST"},
status=400)

dc = get_device_cloud(request)
if dc is None:
return JsonResponse({"error": "Invalid credentials."}, status=400)

device_id = request.POST[views.PARAM_CONTROLLER_ID]
data = request.POST[PARAM_DATA] if PARAM_DATA in request.POST else None
Expand All @@ -208,7 +175,9 @@ def send_device_request(request, target):
return JsonResponse({"data": resp}, status=200)
return JsonResponse({"valid": True}, status=200)
except DeviceCloudHttpException as e:
return get_exception_response(e)
return JsonResponse(
{"error": "Error in the DRM request: {}.".format(e.response.text)},
status=e.response.status_code)


def send_request(dc, device_id, target, data=None):
Expand Down Expand Up @@ -564,12 +533,13 @@ def get_data_points(request, stream_name):
Returns:
A JSON with the data points or the error.
"""
# Check if the AJAX request is valid.
error = check_ajax_request(request)
if error is not None:
return error
if not request.is_ajax or request.method != "POST":
return JsonResponse({"error": "AJAX request must be sent using POST"},
status=400)

dc = get_device_cloud(request)
if dc is None:
return JsonResponse({"error": "Invalid credentials."}, status=400)

device_id = request.POST[views.PARAM_CONTROLLER_ID]
interval = int(
Expand Down Expand Up @@ -629,6 +599,10 @@ def get_general_farm_status(request, device_id, stations):
# Weather station.
if stream_id == STREAM_FORMAT_CONTROLLER.format(device_id, ID_WIND):
status[ID_WEATHER][ID_WIND] = data
elif stream_id == STREAM_FORMAT_CONTROLLER.format(device_id, ID_WIND_DIR):
status[ID_WEATHER][ID_WIND_DIR] = data
elif stream_id == STREAM_FORMAT_CONTROLLER.format(device_id, ID_LUMINOSITY):
status[ID_WEATHER][ID_LUMINOSITY] = data
elif stream_id == STREAM_FORMAT_CONTROLLER.format(device_id, ID_RAIN):
status[ID_WEATHER][ID_RAIN] = data
elif stream_id == STREAM_FORMAT_CONTROLLER.format(device_id, ID_RADIATION):
Expand Down
4 changes: 2 additions & 2 deletions agriculture/agriculturecore/templates/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ <h6 class="card-title">Weather station</h6>
<td><span class="digi-icon-color fas fa-sun fa-2x"></span></td>
</tr>
<tr>
<td><span id="wind"><i class="fas fa-circle-notch fa-spin"></i></span> km/h</td>
<td><span id="wind_speed"><i class="fas fa-circle-notch fa-spin"></i></span> km/h</td>
<td><span id="rain"><i class="fas fa-circle-notch fa-spin"></i></span> L/m²</td>
<td><span id="radiation"><i class="fas fa-circle-notch fa-spin"></i></span> W/m²</td>
<td><span id="luminosity"><i class="fas fa-circle-notch fa-spin"></i></span> lux</td>
</tr>
</tbody>
</table>
Expand Down
59 changes: 45 additions & 14 deletions agriculture/agriculturecore/templates/history.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
<div class="card-body">
<h5 class="card-title">Historic data: Irrigation controller</h5>
<div class="row">
<div class="col-xl-4 chart-container">
<div class="col-xl-6 chart-container">
<div class="d-flex justify-content-center align-items-center">
<span class="fas fa-wind fa-2x mr-3"></span>
<span>Wind speed</span>
<span>Wind</span>
</div>
<div class="chart-wrapper">
<div id="wind-chart" class="big-chart"></div>
Expand All @@ -36,7 +36,7 @@ <h5 class="card-title">Historic data: Irrigation controller</h5>
</label>
</div>
</div>
<div class="col-xl-4 chart-container">
<div class="col-xl-6 chart-container">
<div class="d-flex justify-content-center align-items-center">
<span class="fas fa-cloud-rain fa-2x mr-3"></span>
<span>Rain</span>
Expand All @@ -62,32 +62,58 @@ <h5 class="card-title">Historic data: Irrigation controller</h5>
</label>
</div>
</div>
<div class="col-xl-4 chart-container">
<div class="col-xl-6 chart-container">
<div class="d-flex justify-content-center align-items-center">
<span class="fas fa-sun fa-2x mr-3"></span>
<span>Solar radiation</span>
<span>Luminosity</span>
</div>
<div class="chart-wrapper">
<div id="radiation-chart" class="big-chart"></div>
<div id="radiation-chart-loading" class="chart-loading">
<div id="luminosity-chart" class="big-chart"></div>
<div id="luminosity-chart-loading" class="chart-loading">
<img class="loading-chart-image" src="{% static 'images/loading.gif' %}" alt="Loading..." />
</div>
</div>
<div class="btn-group btn-group-toggle d-flex justify-content-center" data-toggle="buttons">
<label class="btn btn-secondary btn-sm active">
<input type="radio" name="radiation-interval" value="1" checked> Hour
<input type="radio" name="luminosity-interval" value="1" checked> Hour
</label>
<label class="btn btn-secondary btn-sm">
<input type="radio" name="radiation-interval" value="24"> Day
<input type="radio" name="luminosity-interval" value="24"> Day
</label>
<label class="btn btn-secondary btn-sm">
<input type="radio" name="radiation-interval" value="168"> Week
<input type="radio" name="luminosity-interval" value="168"> Week
</label>
<label class="btn btn-secondary btn-sm">
<input type="radio" name="radiation-interval" value="720"> Month
<input type="radio" name="luminosity-interval" value="720"> Month
</label>
</div>
</div>
<div class='col-xl-6 chart-container'>
<div class='d-flex justify-content-center align-items-center'>
<span class='fas fa-thermometer-half fa-2x mr-3'></span>
<span>Temperature</span>
</div>
<div class='chart-wrapper'>
<div id='temperature-chart' class='big-chart'></div>
<div id='temperature-chart-loading' class='chart-loading'>
<img class='loading-chart-image' src='../static/images/loading.gif' alt='Loading...' />
</div>
</div>
<div class='btn-group btn-group-toggle d-flex justify-content-center' data-toggle='buttons'>
<label class='btn btn-secondary btn-sm active'>
<input type='radio' name='temperature-interval' value='1' checked> Hour
</label>
<label class='btn btn-secondary btn-sm'>
<input type='radio' name='temperature-interval' value='24'> Day
</label>
<label class='btn btn-secondary btn-sm'>
<input type='radio' name='temperature-interval' value='168'> Week
</label>
<label class='btn btn-secondary btn-sm'>
<input type='radio' name='temperature-interval' value='720'> Month
</label>
</div>
</div>
</div>
</div>
</div>
Expand All @@ -113,9 +139,14 @@ <h5 class="card-title">Historic data: Irrigation controller</h5>
drawRainChart(true, true);
});

$("input[type=radio][name=radiation-interval]").change(function() {
radiationInterval = this.value;
drawRadiationChart(true, true);
$("input[type=radio][name=luminosity-interval]").change(function() {
luminosityInterval = this.value;
drawLuminosityChart(true, true);
});

$("input[type=radio][name=temperature-interval]").change(function() {
temperatureInterval = this.value;
drawTemperatureChart(true, true);
});
});

Expand Down
2 changes: 2 additions & 0 deletions agriculture/agriculturecore/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
path('ajax/get_stations', views.get_irrigation_stations, name='get_irrigation_stations'),
path('ajax/set_valve', views.set_valve, name='set_valve'),
path('ajax/get_wind', views.get_wind, name="get_wind"),
path('ajax/get_wind_dir', views.get_wind_dir, name="get_wind_dir"),
path('ajax/get_luminosity', views.get_luminosity, name="get_luminosity"),
path('ajax/get_rain', views.get_rain, name="get_rain"),
path('ajax/get_radiation', views.get_radiation, name="get_radiation"),
path('ajax/get_temperature', views.get_temperature, name="get_temperature"),
Expand Down
Loading