Skip to content

Commit 47f7eac

Browse files
committed
agriculture: add support for weather station in agriculture demo
- Added some more fixes Signed-off-by: Victor Coman <victor.coman@digi.com>
1 parent 5e998f0 commit 47f7eac

File tree

8 files changed

+299
-180
lines changed

8 files changed

+299
-180
lines changed

agriculture/agriculturecommon/templates/base.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
<script defer src="https://cdn.sobekrepository.org/includes/gmaps-markerwithlabel/1.9.1/gmaps-markerwithlabel-1.9.1.js"></script>
2121
<script src="https://www.gstatic.com/charts/loader.js"></script>
2222
<script>
23-
google.charts.load("current", {"packages":["line"]});
23+
<!--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.-->
24+
google.charts.load("42", {"packages":["line", "corechart"]});
2425
</script>
2526
</head>
2627
<body>

agriculture/agriculturecore/drm_requests.py

Lines changed: 21 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,15 @@
5353
DO_CMD_XBEE_DISCOVER = "<discover option='current' />"
5454
DO_CMD_XBEE_SETTING = "<radio_command addr='{}' id='{}' format='{}' timeout='1000' />"
5555

56-
ID_WIND = "wind"
56+
ID_WIND = "wind_speed"
57+
ID_WIND_DIR = "wind_direction"
5758
ID_RADIATION = "radiation"
5859
ID_RAIN = "rain"
5960
ID_LEVEL = "level"
6061
ID_VALVE = "valve"
6162
ID_TEMPERATURE = "temperature"
63+
ID_PRESSURE = "pressure"
64+
ID_LUMINOSITY = "luminosity"
6265
ID_BATTERY = "battery"
6366
ID_MOISTURE = "moisture"
6467

@@ -67,8 +70,6 @@
6770
ID_WEATHER = "weather"
6871
ID_TANK = "tank"
6972

70-
ID_ERROR = "error"
71-
7273
REGEX_DEV_REQUEST_RESPONSE = ".*<device_request .*>(.*)<\\/device_request>.*"
7374
REGEX_DO_CMD_RESPONSE = ".*<do_command target=[^>]*>(.*)<\\/do_command>.*"
7475

@@ -146,41 +147,6 @@ def get_device_cloud_session(session):
146147
base_url=user_serialized.server)
147148

148149

149-
def check_ajax_request(request):
150-
"""
151-
Checks whether the given AJAX request is valid and the user is
152-
authenticated.
153-
154-
Args:
155-
request (:class:`.WSGIRequest`): The HTTP request.
156-
157-
Returns:
158-
`None` if the request is valid, or a `JsonResponse` with the error
159-
if it is not.
160-
"""
161-
if is_authenticated(request):
162-
if not request.is_ajax or request.method != "POST":
163-
return JsonResponse({ID_ERROR: "AJAX request must be sent using POST"}, status=400)
164-
return None
165-
else:
166-
return JsonResponse({ID_ERROR: "Not authenticated"}, status=401)
167-
168-
169-
def get_exception_response(e):
170-
"""
171-
Returns the JSON response with the error contained in the given exception.
172-
173-
Args:
174-
e (:class:`.Exception`): The exception.
175-
176-
Returns:
177-
A JSON response with the details of the exception.
178-
"""
179-
return JsonResponse({ID_ERROR: ("Error in the DRM request: {}.".format(e.response.text)
180-
if isinstance(e, DeviceCloudHttpException) else str(e))},
181-
status=400)
182-
183-
184150
def send_device_request(request, target):
185151
"""
186152
Sends a Device Request to DRM to the device with the given ID.
@@ -192,12 +158,13 @@ def send_device_request(request, target):
192158
Returns:
193159
A JSON with the response or the error.
194160
"""
195-
# Check if the AJAX request is valid.
196-
error = check_ajax_request(request)
197-
if error is not None:
198-
return error
161+
if not request.is_ajax or request.method != "POST":
162+
return JsonResponse({"error": "AJAX request must be sent using POST"},
163+
status=400)
199164

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

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

213182

214183
def send_request(dc, device_id, target, data=None):
@@ -564,12 +533,13 @@ def get_data_points(request, stream_name):
564533
Returns:
565534
A JSON with the data points or the error.
566535
"""
567-
# Check if the AJAX request is valid.
568-
error = check_ajax_request(request)
569-
if error is not None:
570-
return error
536+
if not request.is_ajax or request.method != "POST":
537+
return JsonResponse({"error": "AJAX request must be sent using POST"},
538+
status=400)
571539

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

574544
device_id = request.POST[views.PARAM_CONTROLLER_ID]
575545
interval = int(
@@ -629,6 +599,10 @@ def get_general_farm_status(request, device_id, stations):
629599
# Weather station.
630600
if stream_id == STREAM_FORMAT_CONTROLLER.format(device_id, ID_WIND):
631601
status[ID_WEATHER][ID_WIND] = data
602+
elif stream_id == STREAM_FORMAT_CONTROLLER.format(device_id, ID_WIND_DIR):
603+
status[ID_WEATHER][ID_WIND_DIR] = data
604+
elif stream_id == STREAM_FORMAT_CONTROLLER.format(device_id, ID_LUMINOSITY):
605+
status[ID_WEATHER][ID_LUMINOSITY] = data
632606
elif stream_id == STREAM_FORMAT_CONTROLLER.format(device_id, ID_RAIN):
633607
status[ID_WEATHER][ID_RAIN] = data
634608
elif stream_id == STREAM_FORMAT_CONTROLLER.format(device_id, ID_RADIATION):

agriculture/agriculturecore/templates/dashboard.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ <h6 class="card-title">Weather station</h6>
8888
<td><span class="digi-icon-color fas fa-sun fa-2x"></span></td>
8989
</tr>
9090
<tr>
91-
<td><span id="wind"><i class="fas fa-circle-notch fa-spin"></i></span> km/h</td>
91+
<td><span id="wind_speed"><i class="fas fa-circle-notch fa-spin"></i></span> km/h</td>
9292
<td><span id="rain"><i class="fas fa-circle-notch fa-spin"></i></span> L/m²</td>
93-
<td><span id="radiation"><i class="fas fa-circle-notch fa-spin"></i></span> W/m²</td>
93+
<td><span id="luminosity"><i class="fas fa-circle-notch fa-spin"></i></span> lux</td>
9494
</tr>
9595
</tbody>
9696
</table>

agriculture/agriculturecore/templates/history.html

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
<div class="card-body">
1111
<h5 class="card-title">Historic data: Irrigation controller</h5>
1212
<div class="row">
13-
<div class="col-xl-4 chart-container">
13+
<div class="col-xl-6 chart-container">
1414
<div class="d-flex justify-content-center align-items-center">
1515
<span class="fas fa-wind fa-2x mr-3"></span>
16-
<span>Wind speed</span>
16+
<span>Wind</span>
1717
</div>
1818
<div class="chart-wrapper">
1919
<div id="wind-chart" class="big-chart"></div>
@@ -36,7 +36,7 @@ <h5 class="card-title">Historic data: Irrigation controller</h5>
3636
</label>
3737
</div>
3838
</div>
39-
<div class="col-xl-4 chart-container">
39+
<div class="col-xl-6 chart-container">
4040
<div class="d-flex justify-content-center align-items-center">
4141
<span class="fas fa-cloud-rain fa-2x mr-3"></span>
4242
<span>Rain</span>
@@ -62,32 +62,58 @@ <h5 class="card-title">Historic data: Irrigation controller</h5>
6262
</label>
6363
</div>
6464
</div>
65-
<div class="col-xl-4 chart-container">
65+
<div class="col-xl-6 chart-container">
6666
<div class="d-flex justify-content-center align-items-center">
6767
<span class="fas fa-sun fa-2x mr-3"></span>
68-
<span>Solar radiation</span>
68+
<span>Luminosity</span>
6969
</div>
7070
<div class="chart-wrapper">
71-
<div id="radiation-chart" class="big-chart"></div>
72-
<div id="radiation-chart-loading" class="chart-loading">
71+
<div id="luminosity-chart" class="big-chart"></div>
72+
<div id="luminosity-chart-loading" class="chart-loading">
7373
<img class="loading-chart-image" src="{% static 'images/loading.gif' %}" alt="Loading..." />
7474
</div>
7575
</div>
7676
<div class="btn-group btn-group-toggle d-flex justify-content-center" data-toggle="buttons">
7777
<label class="btn btn-secondary btn-sm active">
78-
<input type="radio" name="radiation-interval" value="1" checked> Hour
78+
<input type="radio" name="luminosity-interval" value="1" checked> Hour
7979
</label>
8080
<label class="btn btn-secondary btn-sm">
81-
<input type="radio" name="radiation-interval" value="24"> Day
81+
<input type="radio" name="luminosity-interval" value="24"> Day
8282
</label>
8383
<label class="btn btn-secondary btn-sm">
84-
<input type="radio" name="radiation-interval" value="168"> Week
84+
<input type="radio" name="luminosity-interval" value="168"> Week
8585
</label>
8686
<label class="btn btn-secondary btn-sm">
87-
<input type="radio" name="radiation-interval" value="720"> Month
87+
<input type="radio" name="luminosity-interval" value="720"> Month
8888
</label>
8989
</div>
9090
</div>
91+
<div class='col-xl-6 chart-container'>
92+
<div class='d-flex justify-content-center align-items-center'>
93+
<span class='fas fa-thermometer-half fa-2x mr-3'></span>
94+
<span>Temperature</span>
95+
</div>
96+
<div class='chart-wrapper'>
97+
<div id='temperature-chart' class='big-chart'></div>
98+
<div id='temperature-chart-loading' class='chart-loading'>
99+
<img class='loading-chart-image' src='../static/images/loading.gif' alt='Loading...' />
100+
</div>
101+
</div>
102+
<div class='btn-group btn-group-toggle d-flex justify-content-center' data-toggle='buttons'>
103+
<label class='btn btn-secondary btn-sm active'>
104+
<input type='radio' name='temperature-interval' value='1' checked> Hour
105+
</label>
106+
<label class='btn btn-secondary btn-sm'>
107+
<input type='radio' name='temperature-interval' value='24'> Day
108+
</label>
109+
<label class='btn btn-secondary btn-sm'>
110+
<input type='radio' name='temperature-interval' value='168'> Week
111+
</label>
112+
<label class='btn btn-secondary btn-sm'>
113+
<input type='radio' name='temperature-interval' value='720'> Month
114+
</label>
115+
</div>
116+
</div>
91117
</div>
92118
</div>
93119
</div>
@@ -113,9 +139,14 @@ <h5 class="card-title">Historic data: Irrigation controller</h5>
113139
drawRainChart(true, true);
114140
});
115141

116-
$("input[type=radio][name=radiation-interval]").change(function() {
117-
radiationInterval = this.value;
118-
drawRadiationChart(true, true);
142+
$("input[type=radio][name=luminosity-interval]").change(function() {
143+
luminosityInterval = this.value;
144+
drawLuminosityChart(true, true);
145+
});
146+
147+
$("input[type=radio][name=temperature-interval]").change(function() {
148+
temperatureInterval = this.value;
149+
drawTemperatureChart(true, true);
119150
});
120151
});
121152

agriculture/agriculturecore/urls.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
path('ajax/get_stations', views.get_irrigation_stations, name='get_irrigation_stations'),
3636
path('ajax/set_valve', views.set_valve, name='set_valve'),
3737
path('ajax/get_wind', views.get_wind, name="get_wind"),
38+
path('ajax/get_wind_dir', views.get_wind_dir, name="get_wind_dir"),
39+
path('ajax/get_luminosity', views.get_luminosity, name="get_luminosity"),
3840
path('ajax/get_rain', views.get_rain, name="get_rain"),
3941
path('ajax/get_radiation', views.get_radiation, name="get_radiation"),
4042
path('ajax/get_temperature', views.get_temperature, name="get_temperature"),

0 commit comments

Comments
 (0)