   alias   Farmbot.{Context, Token}
   use     Farmbot.DebugLog, name: MqttClient
   use     GenMQTT
-  @max_thottle_msgs 6
 
   @doc """
     Starts a mqtt client.

  def init({%Context{} = context, %Token{} = token}) do
    Logger.info ">> Starting mqtt!", type: :busy
    Process.flag(:trap_exit, true)
    {:ok, %{token: token, context: context, cs_nodes: [], throttle: 0}}
  end

  def on_connect(%{token: token, context: context} = state) do
    GenMQTT.subscribe(self(), [{bot_topic(token), 0}])
    Process.send_after(self(), :throttle_timeout, 1000)

    fn ->
      Process.sleep(10)

    {:ok, %{state | context: ctx, cs_nodes: new_nodes}}
  end

  def handle_cast({:status, %Ser{} = ser}, state) do
    # debug_log "Got bot status."
    json = Poison.encode!(ser)
    GenMQTT.publish(self(), status_topic(state.token), json, 0, false)
    {:ok, state}
  end

