Skip to content

Commit

Permalink
Auto-update of the sensors geolocation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aquaj committed Sep 12, 2016
1 parent 7ebdaed commit 97ab347
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 4 deletions.
39 changes: 39 additions & 0 deletions app/assets/javascripts/visualization.js.coffee
Expand Up @@ -170,6 +170,7 @@
$.extend(true, @options, @element.data("visualization"))
@mapElement = $("<div>", class: "map").insertAfter(@element)
@map = L.map(@mapElement[0], @options.map)
@layers = []

if @options.map.setDefaultBackground
opts = {}
Expand Down Expand Up @@ -208,6 +209,9 @@
this._destroy()
this._create()

layrs: ->
return @layers

mappo: ->
return @map

Expand Down Expand Up @@ -320,6 +324,7 @@
# Add layer overlay
overlayLayer = L.layerGroup(layerGroup)
overlayLayer.name = layer.name
@layers.push layer
layer.overlay = overlays[layer.label] = overlayLayer
@map.addLayer(overlayLayer)
@layersScheduler.insert overlayLayer._leaflet_id
Expand Down Expand Up @@ -406,8 +411,42 @@
$.loadVisualizations = ->
$("*[data-visualization]").each ->
$(this).visualization()
$(".refresh-locations[data-visualization]").each ->
refreshSensors($(this))
return

# Needed to easily write setTimeout in CoffeeScript
delay = (time, method) -> setTimeout method, time

refreshSensors = (mapElement) ->
console.log "Test"
unless mapElement.data("refreshTimeout")?
console.log "Setting timeout"
timeoutId = delay 10000, -> updateSensorLocations(mapElement)
mapElement.data("refreshTimeout", timeoutId)

updateSensorLocations = (mapElement) ->
sensorLayer = $(mapElement.visualization("layrs")).filter(-> this.name == "sensors")[0]
layers = sensorLayer.overlay._layers
layer_keys = Object.keys(sensorLayer.overlay._layers)
marker_keys = $(layer_keys).filter (index) -> layers[layer_keys[index]].sensorId
shadow_keys = $(layer_keys).filter (index) -> layers[layer_keys[index]].markerSensorId
markers = $.map marker_keys, (element, index) -> layers[element]
shadows = $.map shadow_keys, (element, index) -> layers[element]
$.get "/backend/sensors/last_locations", (data) ->
$(Object.keys(data)).each (index, sensorId) ->
marker = (marker for marker in markers when marker.sensorId == parseInt(sensorId))[0]
shadow = (shadow for shadow in shadows when shadow.markerSensorId == parseInt(sensorId))[0]
newPos = new L.LatLng(data[sensorId].coordinates[1], data[sensorId].coordinates[0])
if marker? && shadow?
unless marker.getLatLng().equals(newPos)
marker.setLatLng(newPos)
shadow.setLatLng(newPos)
console.log "Updated markers positions"
timeoutId = delay 10000, -> updateSensorLocations(mapElement)
clearTimeout mapElement.data("refreshTimeout")
mapElement.data("refreshTimeout", timeoutId)

$(document).ready $.loadVisualizations
$(document).on "page:load cocoon:after-insert cell:load", $.loadVisualizations

Expand Down
6 changes: 5 additions & 1 deletion app/assets/javascripts/visualization/point_group.js.coffee
Expand Up @@ -12,6 +12,7 @@ class visualization.PointGroup
group: zone.group
fillColor: zone.shapeColor
popup: zone.popup
sensorId: zone.sensorId
if this.valid()
@items = @items.sort (a, b) ->
a.name > b.name
Expand All @@ -34,7 +35,9 @@ class visualization.PointGroup
stroke: false
fillOpacity: 0.8
console.log zone.point
group.push new L.circleMarker(zone.point, zoneStyle)
shadow = new L.circleMarker(zone.point, zoneStyle)
shadow.markerSensorId = zone.sensorId
group.push shadow
# Core
for zone in @items
console.log zone
Expand All @@ -45,6 +48,7 @@ class visualization.PointGroup
fillOpacity: 1
console.log zoneStyle
zoneLayer = new L.circleMarker(zone.point, zoneStyle)
zoneLayer.sensorId = zone.sensorId
widget._bindPopup(zoneLayer, zone)
group.push(zoneLayer)
group
Expand Down
14 changes: 14 additions & 0 deletions app/controllers/backend/sensors_controller.rb
Expand Up @@ -81,5 +81,19 @@ def retrieve
@sensor.retrieve(started_at: Time.zone.now - 1.hour, stopped_at: Time.zone.now)
redirect_to params[:redirect] || { action: :show, id: params[:id] }
end

def last_locations
@geolocations = Sensor
.includes(:analyses)
.find_each
.map do |sensor|
if (geoloc = sensor.analyses.last.geolocation)
[sensor.id, geoloc && JSON(geoloc.to_json)]
end
end
.compact
.to_h
render json: @geolocations.to_json
end
end
end
8 changes: 5 additions & 3 deletions app/views/backend/cells/map_cells/show.html.haml
Expand Up @@ -18,6 +18,7 @@

popup_lines << link_to(:see_more_details.tl, sensor.partner_url) if sensor.partner_url.present?
{
sensor_id: sensor.id,
name: sensor.name,
shape: analysis.geolocation,
shape_color: '#'+Digest::MD5.hexdigest(model)[0, 6].upcase,
Expand Down Expand Up @@ -151,7 +152,7 @@
- if data.empty?
.placeholding-message= :no_production_defined_for_current_campaign.tn
- else
= visualization do |v|
= visualization({}, { class: "refresh-locations" }) do |v|
- v.serie :main, data
- if params[:visualization] == "nitrogen_footprint"
- v.choropleth :interventions_count, :main
Expand All @@ -170,8 +171,9 @@
- v.choropleth :time_cost, :main, stop_color: "#E77000"
- v.categories :activity, :main

- v.serie :sensor_data, sensor_data
- v.point_group :sensors, :sensor_data
- unless sensor_data.blank?
- v.serie :sensor_data, sensor_data
- v.point_group :sensors, :sensor_data

- v.control :zoom
- v.control :scale
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Expand Up @@ -773,6 +773,7 @@
collection do
get :models
get :detail
get :last_locations
end
member do
get :list_analyses
Expand Down

0 comments on commit 97ab347

Please sign in to comment.