Class: StationsController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#honeypot

Instance Method Details

#api_firmware_versionObject

PATCH/PUT /stations/1/firmware_version.json


84
85
86
87
88
89
90
# File 'app/controllers/stations_controller.rb', line 84

def api_firmware_version
  if @station.update(params.require(:station).permit(:firmware_version, :gsm_software))
    head :ok
  else
    render json: @station.errors, status: :unprocessable_entity
  end
end

#createObject

POST /stations POST /stations.json


56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/controllers/stations_controller.rb', line 56

def create
  @station = Station.new(station_params)
  respond_to do |format|
    if @station.save
      format.html { redirect_to station_path(@station), notice: 'Station was successfully created.' }
      format.json { render action: 'show', status: :created, location: @station }
    else
      format.html { render action: 'new' }
      format.json { render json: @station.errors, status: :unprocessable_entity }
    end
  end
end

#destroyObject

DELETE /stations/1 DELETE /stations/1.json


110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/controllers/stations_controller.rb', line 110

def destroy
  @station.destroy
  expire_fragment('all_stations')
  respond_to do |format|
    if @station.destroyed?
      format.html { redirect_to stations_url }
      format.json { head :no_content, status: :ok }
    else
      format.html { redirect_to @stations, flash: "Station could not be deleted." }
      format.json { head :no_content, status: :ok }
    end
  end
end

#editObject

GET /stations/1/edit


50
51
52
# File 'app/controllers/stations_controller.rb', line 50

def edit
  @title = "Editing #{@station.name}"
end

#embedObject

GET /stations/:id/embed


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'app/controllers/stations_controller.rb', line 131

def embed
  @observation = @station.current_observation
  @observations = [@observation]
  @embed_options = {
      css: (params[:css].in?(['true', 'TRUE'])),
      type: params[:type] || 'table',
      height: params[:height] || 350,
      width: params[:width] || 500
  }

  unless @embed_options[:type].in? ['chart','table']
    @message = "Sorry buddy, I donĀ“t know how to render \"#{@embed_options[:type]}\"."
    @embed_options[:type] = 'error'
  end

  # Temporary fix to allow iframe from http://www.gotlandssurfcenter.se
  response.headers['X-Frame-Options'] = 'ALLOW-FROM http://www.gotlandssurfcenter.se'

  respond_to do |format|
    format.html { render "/stations/embeds/#{@embed_options[:type]}", layout: false }
  end
end

#findObject

Used by Ardiuno to lookup station ID


155
156
157
158
159
160
161
162
163
164
165
166
# File 'app/controllers/stations_controller.rb', line 155

def find
  @station = Station.find_by(hw_id: params[:hw_id])
  if(@station.nil?)
    respond_to do |format|
      format.json { head :not_found }
    end
  else
    respond_to do |format|
      format.json { render json: { id: @station.id }}
    end
  end
end

#indexObject

GET /stations GET /stations.json


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/controllers/stations_controller.rb', line 15

def index
  @title = "Stations"
  @last_updated = Observation.last
  if stale?(@last_updated)
    @stations = Station.all.with_observations(1)
    unless user_signed_in? && current_user.has_role?(:admin)
      @stations = @stations.visible
    end
    @stations.load
    respond_to do |format|
      format.html
      format.json { render json: @stations }
    end
  end
end

#newObject

GET /stations/new


45
46
47
# File 'app/controllers/stations_controller.rb', line 45

def new
  @station = Station.new
end

#searchObject

GET /stations/search?lat=x&lon=x&radius


125
126
127
128
# File 'app/controllers/stations_controller.rb', line 125

def search
  radius = params[:radius] || 20
  @stations = Station.all.near([params[:lat], params[:lon]], radius, units: :km)
end

#showObject

GET /stations/1 GET /stations/1.json


33
34
35
36
37
38
39
40
41
42
# File 'app/controllers/stations_controller.rb', line 33

def show
  @title = @station.name
  @observations = @station.load_observations!(10)
  if stale?(@station)
    respond_to do |format|
      format.html
      format.json { render json: @station }
    end
  end
end

#updateObject

PATCH/PUT /stations/1 PATCH/PUT /stations/1.json


71
72
73
74
75
76
77
78
79
80
81
# File 'app/controllers/stations_controller.rb', line 71

def update
  respond_to do |format|
    if @station.update(station_params)
      format.html { redirect_to @station, notice: 'Station was successfully updated.' }
      format.json { head :no_content }
    else
      format.html { render action: 'edit' }
      format.json { render json: @station.errors, status: :unprocessable_entity }
    end
  end
end

#update_balanceObject

PUT /s/:station_id


93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/controllers/stations_controller.rb', line 93

def update_balance
  sp = params.require(:s).permit(:b)
  @station.balance = sp[:b] if sp[:b].present?
  respond_to do |format|
    if @station.balance_changed? && @station.save
      format.any { render nothing: true, status: :ok }
      # check station balance after reply has been sent
      @station.check_balance
    else
      logger.error( "Someone attemped to update #{@station.name} balance with invalid data ('#{params[:s][:b]}') from #{request.remote_ip}" )
      format.any { render nothing: true, status: :unprocessable_entity }
    end
  end
end