Permalink
Browse files

Adding upload controller, the new data push universe.

  • Loading branch information...
1 parent 32c3aef commit e373ae6f796787db5585c4001959312eb675165e @ussjoin committed Dec 4, 2011
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
@@ -0,0 +1,3 @@
+// Place all the styles related to the upload controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
@@ -1,5 +1,6 @@
class UploadController < ApplicationController
require 'csv'
+ require 'date'
def upload
uploaded_io = params[:csv]
unless uploaded_io.nil?
@@ -8,11 +9,72 @@ def upload
#Skip Line 0
#Line 1, 2, 3: Concatenate into labels
arr_of_arrs.delete_at(0)
- arr_of_arrs[0].each_index {|i| arr_of_arrs[0][i] += " #{arr_of_arrs[1][i]} #{arr_of_arrs[2][i]}"}
+
+ params[:facility].strip!
+ params[:building].strip!
+ params[:systype].strip!
+ params[:system].strip!
+
+ #Company is A
+ company = Company.find_or_create_by_name("A")
+ #Get Facility from User
+ facility = Facility.find_or_create_by_name_and_company_id(params[:facility], company.id)
+
+ #Get Building from User
+ building = Building.find_or_create_by_name_and_facility_id(params[:building], facility.id)
+
+ #Get System Type from User
+ systype = SystemType.find_or_create_by_name(params[:systype])
+
+ #Get System from User
+ system = System.find_or_create_by_name_and_system_type_id_and_building_id(params[:system],
+ systype.id, building.id)
+
+ #Here's a terrible assumption (FIXME): Sensor boxes should increase on every import,
+ #Because we have absolutely no idea what they really are.
+ sensorbox = SensorBox.new(:name => 'aBox', :serial => 'cereal', :system => system)
+
+ arr_of_arrs[0].delete_at(0)
+ arr_of_arrs[1].delete_at(0)
+ arr_of_arrs[2].delete_at(0)
+
+ #Hardware types is [0]
+ hardwaretypes = arr_of_arrs[0].collect {|h|
+ hw = HardwareType.find_or_create_by_name(h)
+ }
+
+ #Unit Types is [2]-- do an "insert if not extant"
+ unittypes = arr_of_arrs[2].collect {|u|
+ unit = UnitType.find_or_create_by_name(u)
+ }
+
+ sensors = []
+ arr_of_arrs[1].each_index {|i|
+ s = arr_of_arrs[1][i]
+ sens = Sensor.find_or_create_by_name_and_sensor_box_id_and_hardware_type_id_and_unit_type_id(
+ s, sensorbox.id, hardwaretypes[i].id, unittypes[i].id
+ )
+ sensors.push(sens)
+ }
+
+
+ # arr_of_arrs[0].each_index {|i| arr_of_arrs[0][i] += " #{arr_of_arrs[1][i]} #{arr_of_arrs[2][i]}"}
@headers = arr_of_arrs.delete_at(0)
arr_of_arrs.delete_at(0)
arr_of_arrs.delete_at(0)
- @main = arr_of_arrs[1..10]
+ # @main = arr_of_arrs[1..10]
+
+
+ arr_of_arrs.each {|arr|
+ #0 is date
+ d = DateTime.parse(arr[0])
+ arr.delete_at(0)
+ arr.each_index {|i|
+ s = Input.new(:sensor_id => sensors[i].id, :when => d, :value => arr[i])
+ s.save
+ }
+ }
+
end
end
end
@@ -1,4 +1,20 @@
<%= form_tag({:action => :upload}, :multipart => true) do %>
+ Facility
+ building
+ systype
+ system
+ <%= label_tag(:facility, "Facility Name") %>
+ <%= text_field_tag(:facility) %>
+ <br />
+ <%= label_tag(:building, "Building Name") %>
+ <%= text_field_tag(:building) %>
+ <br />
+ <%= label_tag(:system, "System Name") %>
+ <%= text_field_tag(:system) %>
+ <br />
+ <%= label_tag(:systype, "System Type") %>
+ <%= text_field_tag(:systype)%>
+ <br />
<%= file_field_tag 'csv' %>
<br />
<%= submit_tag%>

0 comments on commit e373ae6

Please sign in to comment.