Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions app/controllers/ui/devices_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ def device_params
:notify_stopped_publishing,
:hardware_version_override,
:mac_address,
:forwarding_destination_id,
{ :tag_ids => [] },
{ :postprocessing_attributes => :hardware_url },
)
Expand Down
13 changes: 12 additions & 1 deletion app/controllers/v0/devices_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,19 @@ def device_params

# Researchers + Admins can update is_test and enable_forwarding
if current_user.is_admin_or_researcher?
params_to_permit.push({postprocessing_attributes: [:blueprint_url, :hardware_url, :latest_postprocessing, :meta, :forwarding_params]})
params_to_permit.push(
{
postprocessing_attributes: [
:blueprint_url,
:hardware_url,
:latest_postprocessing,
:meta,
:forwarding_params
]
}
)
params_to_permit.push(:enable_forwarding)
params_to_permit.push(:forwarding_destination_id)
end

if current_user.is_admin?
Expand Down
5 changes: 3 additions & 2 deletions app/lib/presenters/device_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ def data_policy
{
is_private: device.is_private,
enable_forwarding: device.enable_forwarding,
precise_location: device.precise_location
}
precise_location: device.precise_location,
forwarding_destination: device.forwarding_destination&.name
}.compact
end
end

Expand Down
6 changes: 4 additions & 2 deletions app/models/device.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Device < ActiveRecord::Base
multisearchable :against => [:name, :description, :city, :country_name], if: :active?

belongs_to :owner, class_name: 'User'
belongs_to :forwarding_destination, optional: true

has_many :devices_tags, dependent: :destroy
has_many :tags, through: :devices_tags
Expand Down Expand Up @@ -117,7 +118,7 @@ def self.ransackable_associations(auth_object = nil)
[
"components", "devices_tags", "owner",
"pg_search_document" , "postprocessing", "sensors",
"tags"
"tags", "forwarding_destination"
]
end

Expand Down Expand Up @@ -296,7 +297,8 @@ def data_policy(authorized=false)
{
is_private: is_private,
enable_forwarding: authorized ? enable_forwarding : "[FILTERED]",
precise_location: authorized ? precise_location : "[FILTERED]"
precise_location: authorized ? precise_location : "[FILTERED]",
forwarding_destination: authorized ? forwarding_destination&.name : "[FILTERED]"
}
end

Expand Down
8 changes: 8 additions & 0 deletions app/models/forwarding_destination.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class ForwardingDestination < ActiveRecord::Base
has_many :devices
validates_uniqueness_of :name

def self.ransackable_attributes(_auth_object = nil)
["name"]
end
end
4 changes: 4 additions & 0 deletions app/views/ui/devices/_fields.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
<h2 class="mb-4"><%= t(:device_form_researcher_options_subhead) %></h2>
<% if device.owner.forward_device_readings? %>
<%= form.check_box :enable_forwarding, label: t(:device_form_enable_forwarding_label) %>
<%= form.select :forwarding_destination_id, options_for_select(
ForwardingDestination.all.map { |fd| [fd.name, fd.id] },
device.forwarding_destination_id
), label: t(:device_form_forwarding_destination_label), include_blank: "Default" %>
<% end %>
<%= form.fields_for :postprocessing, device.postprocessing || Postprocessing.new do |fp| %>
<%= fp.text_field :hardware_url, label: t(:device_form_hardware_url_label), help: t(:device_form_postprocessing_blurb_html) %>
Expand Down
1 change: 1 addition & 0 deletions config/locales/views/devices/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ en:
device_form_is_private_label: "Make this kit private"
device_form_precise_location_label: "Enable precise location"
device_form_enable_forwarding_label: "Enable MQTT forwarding"
device_form_forwarding_destination_label: "Destination for MQTT forwarding"
device_form_notifications_subhead: "Notifications"
device_form_notifications_blurb: "Get emails when the following events occur:"
device_form_notify_low_battery_label: "Battery level drops below 15%"
Expand Down
14 changes: 14 additions & 0 deletions db/migrate/20250715180503_add_forwarding_destinations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class AddForwardingDestinations < ActiveRecord::Migration[6.1]
def change
create_table :forwarding_destinations do |t|
t.string :name, null: false
t.timestamps
end

add_index :forwarding_destinations, :name, unique: true

change_table :devices do |t|
t.belongs_to :forwarding_destination, index: true, null: true, foreign_key: true
end
end
end
12 changes: 11 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2025_05_05_081245) do
ActiveRecord::Schema.define(version: 2025_07_15_180503) do

# These are extensions that must be enabled in order to support this database
enable_extension "adminpack"
Expand Down Expand Up @@ -109,7 +109,9 @@
t.string "hardware_slug_override"
t.boolean "precise_location", default: true, null: false
t.boolean "enable_forwarding", default: false, null: false
t.bigint "forwarding_destination_id"
t.index ["device_token"], name: "index_devices_on_device_token", unique: true
t.index ["forwarding_destination_id"], name: "index_devices_on_forwarding_destination_id"
t.index ["geohash"], name: "index_devices_on_geohash"
t.index ["last_reading_at"], name: "index_devices_on_last_reading_at"
t.index ["owner_id"], name: "index_devices_on_owner_id"
Expand Down Expand Up @@ -150,6 +152,13 @@
t.index ["owner_id"], name: "index_experiments_on_owner_id"
end

create_table "forwarding_destinations", force: :cascade do |t|
t.string "name", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["name"], name: "index_forwarding_destinations_on_name", unique: true
end

create_table "friendly_id_slugs", id: :serial, force: :cascade do |t|
t.string "slug", null: false
t.integer "sluggable_id", null: false
Expand Down Expand Up @@ -346,6 +355,7 @@
add_foreign_key "api_tokens", "users", column: "owner_id"
add_foreign_key "components", "devices"
add_foreign_key "components", "sensors"
add_foreign_key "devices", "forwarding_destinations"
add_foreign_key "devices_experiments", "devices"
add_foreign_key "devices_experiments", "experiments"
add_foreign_key "devices_tags", "devices"
Expand Down