Class: DatasController

Inherits:
ApplicationController show all
Defined in:
app/controllers/datas_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#render_not_found_response

Instance Method Details

#createObject


52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/controllers/datas_controller.rb', line 52

def create
  logger.info params
  return unless sensor_id = get_sensor_id(params[:data_type])
  record = DataRecord.new( device_id: params[:device_id], timestamp: DateTime.now, sensor_id: sensor_id.id, data: params[:data] )
  begin
    record.save
  rescue ActiveRecord::InvalidForeignKey => exception
    render json: { status: 'ERROR', message: 'This device does not exist', data: exception.message }, status: :not_found
  else
    check_record_success(record)
  end
end

#destroyObject


65
66
67
68
69
# File 'app/controllers/datas_controller.rb', line 65

def destroy
  record = DataRecord.find(params[:id])
  record.destroy
  render json: { status: 'SUCCESS', message: 'Record removed', data: record }, status: :ok
end

#indexObject


5
6
7
8
# File 'app/controllers/datas_controller.rb', line 5

def index
  records = DataRecord.all
  render json: { status: 'SUCCESS', message: 'All current records in DB', data: records }, status: :ok
end

#showObject


10
11
12
13
14
15
16
17
# File 'app/controllers/datas_controller.rb', line 10

def show
  records = DataRecord.where(device_id: params[:device_id])
  check_record_blank(
    records, 
    'Current records for requested device', 
    'No record found for requested device' 
  )
end

#show_dateObject


29
30
31
32
33
34
35
36
37
38
# File 'app/controllers/datas_controller.rb', line 29

def show_date
  return unless sensor_id = get_sensor_id(params[:data_type])
  date = Date.strptime(params[:date], '%d-%m-%Y')
  records = DataRecord.where(device_id: params[:device_id], sensor_id: sensor_id, timestamp: (date.midnight)..(date.midnight+1.day))
  check_record_blank(
    records,
    'Current records for requested device with this type and for requested day',
    'No record found for requested device with this type and for requested day'
  )
end

#show_rangeObject


40
41
42
43
44
45
46
47
48
49
50
# File 'app/controllers/datas_controller.rb', line 40

def show_range
  return unless sensor_id = get_sensor_id(params[:data_type])
  start_date = Date.strptime(params[:start_date], '%d-%m-%Y')
  end_date = Date.strptime(params[:end_date], '%d-%m-%Y')
  records = DataRecord.where(device_id: params[:device_id], sensor_id: sensor_id, timestamp: (start_date.midnight)..(end_date.midnight+1.day))
  check_record_blank(
    records,
    'Current records for requested device with this type and for requested day',
    'No record found for requested device with this type and for requested day'
  )
end

#show_typeObject


19
20
21
22
23
24
25
26
27
# File 'app/controllers/datas_controller.rb', line 19

def show_type
  return unless sensor_id = get_sensor_id(params[:data_type])
  records = DataRecord.where(device_id: params[:device_id], sensor_id: sensor_id)
  check_record_blank(
    records,
    'Current records for requested device with this type',
    'No record found for requested device with this type'
  )
end