class StatusController < ApplicationController
  protect_from_forgery :except => [:status_update]
  before_action :check_if_smokedetector, :only => [:status_update]

  def index
    @statuses = SmokeDetector.order('last_ping DESC').all
  end

  def status_update
    @smoke_detector.last_ping = DateTime.now
    @smoke_detector.location = params[:location]
    @smoke_detector.is_standby = params[:standby] || false

    # If an instance is manually switched to standby, we
    # don't want it to immediately kick back
    new_standby_switch = @smoke_detector.is_standby_changed? and @smoke_detector.is_standby

    @smoke_detector.save!

    respond_to do |format|
      format.json do
        if @smoke_detector.should_failover and not new_standby_switch
          @smoke_detector.update(:is_standby => false, :force_failover => false)
          render :status => 200, :json => { 'failover': true }
        else
          head 200, content_type: "text/html"
        end
      end
    end
  end
end

class StatusController < ApplicationController
  protect_from_forgery :except => [:status_update]
  before_action :check_if_smokedetector, :only => [:status_update]

  def index
    @statuses = SmokeDetector.order('last_ping DESC').all
  end

  def status_update
    @smoke_detector.last_ping = DateTime.now
    @smoke_detector.location = params[:location]
    @smoke_detector.is_standby = params[:standby] || false

    @smoke_detector.save!

    respond_to do |format|
      format.json do
        if @smoke_detector.should_failover
          @smoke_detector.update(:is_standby => false, :force_failover => false)
          render :status => 200, :json => { 'failover': true }
        else
          render :status => 200, :json => { 'failover': false }
        end
      end
    end
  end
end

Revert "Save a few bytes on every ping"
Revert "Save a few bytes on every ping"

This reverts commit a5b4e50be4189b52a6eaff1a124ad03ae7bd82d6.

Revert "Don't immediately kick back on new standby instances"

This reverts commit 3a2a636eb97c7b5199be7f3885b6552d94fb56b6.
