Skip to content

Commit

Permalink
Allow storage of more than one temperature sensor at a time
Browse files Browse the repository at this point in the history
  • Loading branch information
justinbburris committed Apr 21, 2013
1 parent e788f34 commit 7965a87
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 106 deletions.
5 changes: 0 additions & 5 deletions app/controllers/arduinos_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ def new
end
end

# GET /arduinos/1/edit
def edit
@arduino = Arduino.find(params[:id])
end

# POST /arduinos
# POST /arduinos.json
def create
Expand Down
42 changes: 33 additions & 9 deletions app/controllers/sensor_controller.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,42 @@
class SensorController < ApplicationController

def temperature
arduino = Arduino.find_by_mac_address(params[:arduino])
sensor = params[:sensor]
arduino = Arduino.find_by_mac_address(params[:arduino])
data = params[:sensor].slice(:capture_time, :data, :unit)

temperature = new TemperatureSensor(
sensor.slice(:capture_time, :data, :unit)
.merge({arduino_id: arduino.id})
)
data.merge!({arduino_id: arduino.id})

if temperature.save
render json: temperature
sensor = TemperatureSensor.new(data)

if sensor.save
render json: sensor
else
render json: temperature.errors.messages
render json: sensor.errors.messages
end
end

def temperatures
arduino = Arduino.find_by_mac_address(params[:arduino])
sensors = []

params[:sensors].each do |sensor_data|
data = sensor_data.slice(:capture_time, :data, :unit)
data.merge!({arduino_id: arduino.id})

sensor = TemperatureSensor.new(data)

if sensor.valid?
sensors << sensor
else
render json: [sensor.errors.messages, params[:sensors]]

return
end
end

sensors.each { |sensor| sensor.save }

render json: sensors
end

end
4 changes: 4 additions & 0 deletions app/models/temperature_sensor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ def valid_temperature_type
errors.add(:unit, "Unit must be of type : #{TEMPERATURE_TYPES.join(', ')}" )
end
end

def as_json(options = {})
super(only: [:capture_time, :data, :unit, :created_at])
end
end
43 changes: 20 additions & 23 deletions app/views/arduinos/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
<%= form_for(@arduino) do |f| %>
<% if @arduino.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@arduino.errors.count, "error") %> prohibited this arduino from being saved:</h2>

<ul>
<% @arduino.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<%= form_for @arduino, :html => { :class => 'form-horizontal' } do |f| %>
<div class="control-group">
<%= f.label :name, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :name, :class => 'text_field' %>
</div>
<% end %>

<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :mac_address %><br />
<%= f.text_field :mac_address %>
<div class="control-group">
<%= f.label :mac_address, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :mac_address, :class => 'text_field' %>
</div>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
<div class="control-group">
<%= f.label :email, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :email, :class => 'text_field' %>
</div>
</div>
<div class="actions">
<%= f.submit %>

<div class="form-actions">
<%= f.submit nil, :class => 'btn btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
arduinos_path, :class => 'btn' %>
</div>
<% end %>
63 changes: 38 additions & 25 deletions app/views/arduinos/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,27 +1,40 @@
<h1>Listing arduinos</h1>

<table>
<tr>
<th>Name</th>
<th>Mac address</th>
<th>Email</th>
<th></th>
<th></th>
<th></th>
</tr>

<% @arduinos.each do |arduino| %>
<tr>
<td><%= arduino.name %></td>
<td><%= arduino.mac_address %></td>
<td><%= arduino.email %></td>
<td><%= link_to 'Show', arduino %></td>
<td><%= link_to 'Edit', edit_arduino_path(arduino) %></td>
<td><%= link_to 'Destroy', arduino, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
<%- model_class = Arduino -%>
<div class="page-header">
<h1><%=t '.title', :default => model_class.model_name.human.pluralize %></h1>
</div>
<table class="table table-striped">
<thead>
<tr>
<th><%= model_class.human_attribute_name(:id) %></th>
<th><%= model_class.human_attribute_name(:name) %></th>
<th><%= model_class.human_attribute_name(:mac_address) %></th>
<th><%= model_class.human_attribute_name(:email) %></th>
<th><%= model_class.human_attribute_name(:created_at) %></th>
<th><%=t '.actions', :default => t("helpers.actions") %></th>
</tr>
</thead>
<tbody>
<% @arduinos.each do |arduino| %>
<tr>
<td><%= link_to arduino.id, arduino_path(arduino) %></td>
<td><%= arduino.name %></td>
<td><%= arduino.mac_address %></td>
<td><%= arduino.email %></td>
<td><%=l arduino.created_at %></td>
<td>
<%= link_to t('.edit', :default => t("helpers.links.edit")),
edit_arduino_path(arduino), :class => 'btn btn-mini' %>
<%= link_to t('.destroy', :default => t("helpers.links.destroy")),
arduino_path(arduino),
:method => :delete,
:data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) },
:class => 'btn btn-mini btn-danger' %>
</td>
</tr>
<% end %>
</tbody>
</table>

<br />

<%= link_to 'New Arduino', new_arduino_path %>
<%= link_to t('.new', :default => t("helpers.links.new")),
new_arduino_path,
:class => 'btn btn-primary' %>
10 changes: 5 additions & 5 deletions app/views/arduinos/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<h1>New arduino</h1>

<%= render 'form' %>

<%= link_to 'Back', arduinos_path %>
<%- model_class = Arduino -%>
<div class="page-header">
<h1><%=t '.title', :default => [:'helpers.titles.new', 'New %{model}'], :model => model_class.model_name.human %></h1>
</div>
<%= render :partial => 'form' %>
41 changes: 23 additions & 18 deletions app/views/arduinos/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
<p id="notice"><%= notice %></p>
<%- model_class = Arduino -%>
<div class="page-header">
<h1><%=t '.title', :default => model_class.model_name.human %></h1>
</div>

<p>
<b>Name:</b>
<%= @arduino.name %>
</p>
<dl class="dl-horizontal">
<dt><strong><%= model_class.human_attribute_name(:name) %>:</strong></dt>
<dd><%= @arduino.name %></dd>
<dt><strong><%= model_class.human_attribute_name(:mac_address) %>:</strong></dt>
<dd><%= @arduino.mac_address %></dd>
<dt><strong><%= model_class.human_attribute_name(:email) %>:</strong></dt>
<dd><%= @arduino.email %></dd>
</dl>

<p>
<b>Mac address:</b>
<%= @arduino.mac_address %>
</p>

<p>
<b>Email:</b>
<%= @arduino.email %>
</p>


<%= link_to 'Edit', edit_arduino_path(@arduino) %> |
<%= link_to 'Back', arduinos_path %>
<div class="form-actions">
<%= link_to t('.back', :default => t("helpers.links.back")),
arduinos_path, :class => 'btn' %>
<%= link_to t('.edit', :default => t("helpers.links.edit")),
edit_arduino_path(@arduino), :class => 'btn' %>
<%= link_to t('.destroy', :default => t("helpers.links.destroy")),
arduino_path(@arduino),
:method => 'delete',
:data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) },
:class => 'btn btn-danger' %>
</div>
18 changes: 2 additions & 16 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,10 @@
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="#">Ardusat</a>
<%= link_to "Ardusat", :root, class: 'brand' -%>
<div class="container-fluid nav-collapse">
<ul class="nav">
<li><%= link_to "Link1", "/path1" %></li>
<li><%= link_to "Link2", "/path2" %></li>
<li><%= link_to "Link3", "/path3" %></li>
<li><%= link_to "Arduinos", arduinos_path %></li>
</ul>
</div><!--/.nav-collapse -->
</div>
Expand All @@ -58,20 +56,8 @@

<div class="container-fluid">
<div class="row-fluid">
<div class="span3">
<div class="well sidebar-nav">
<ul class="nav nav-list">
<li class="nav-header">Sidebar</li>
<li><%= link_to "Link1", "/path1" %></li>
<li><%= link_to "Link2", "/path2" %></li>
<li><%= link_to "Link3", "/path3" %></li>
</ul>
</div><!--/.well -->
</div><!--/span-->
<div class="span9">
<%= bootstrap_flash %>
<%= yield %>
</div>
</div><!--/row-->

<footer>
Expand Down
10 changes: 5 additions & 5 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
Ardusat::Application.routes.draw do resources :main, only: ['index']

resources :sensor, only: ['create'] do
collection {
post :temperature
post :temperatures
}
resources :arduinos

namespace :sensor, only: ['create'] do
post :temperature
post :temperatures
end

# The priority is based upon order of creation:
Expand Down

0 comments on commit 7965a87

Please sign in to comment.