public void onEventMainThread(KaaStartedEvent kaaStarted) {
   if (!mClient.isAttachedToUser()) {
     mClient.attachUser("kaa", "dummy", this);
   } else {
     mEventBus.post(new UserAttachEvent());
   }
 }
  public ClimateController(Context context, KaaClient client, EventBus eventBus) {
    mPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    mClient = client;

    mEventBus = eventBus;
    mThermostatDevice = new ThermostatDevice(mPreferences, client, eventBus);
    mEventBus.register(this);
    mClient.setAttachedListener(this);
    mClient.setDetachedListener(this);
    mClient.setEndpointAccessToken(DEFAULT_CLIMATE_CONTROL_ACCESS_TOKEN);
  }
  private void sendLog() {
    if (mKaaStarted) {

      mSentLogCount++;
      mLastLogTime = System.currentTimeMillis();

      /*
       * Create an instance of a cell monitor log record and populate it with the latest values.
       */
      CellMonitorLog cellMonitorLog = new CellMonitorLog();
      cellMonitorLog.setLogTime(mLastLogTime);
      String networkOperator = mTelephonyManager.getNetworkOperator();
      if (networkOperator == null || networkOperator.isEmpty()) {
        cellMonitorLog.setNetworkOperatorCode(UNDEFINED);
      } else {
        cellMonitorLog.setNetworkOperatorCode(
            Integer.valueOf(mTelephonyManager.getNetworkOperator()));
      }
      cellMonitorLog.setNetworkOperatorName(mTelephonyManager.getNetworkOperatorName());

      int cid = UNDEFINED;
      int lac = UNDEFINED;

      if (mCellLocation != null && mCellLocation instanceof GsmCellLocation) {
        GsmCellLocation gsmCellLocation = (GsmCellLocation) mCellLocation;
        cid = gsmCellLocation.getCid();
        lac = gsmCellLocation.getLac();
      }

      cellMonitorLog.setGsmCellId(cid);
      cellMonitorLog.setGsmLac(lac);

      int gsmSignalStrength = UNDEFINED;

      if (mSignalStrength != null) {
        gsmSignalStrength = mSignalStrength.getGsmSignalStrength();
      }
      cellMonitorLog.setSignalStrength(gsmSignalStrength);

      org.kaaproject.kaa.demo.cellmonitor.Location phoneLocation =
          new org.kaaproject.kaa.demo.cellmonitor.Location();
      if (mGpsLocation != null) {
        phoneLocation.setLatitude(mGpsLocation.getLatitude());
        phoneLocation.setLongitude(mGpsLocation.getLongitude());
      }
      cellMonitorLog.setPhoneGpsLocation(phoneLocation);

      /*
       * Pass a cell monitor log record to the Kaa client. The Kaa client will upload
       * the log record according to the defined log upload strategy.
       */
      mClient.addLogRecord(cellMonitorLog);

      mEventBus.post(new LogSent());
    }
  }
  public void pause() {
    mTelephonyManager.listen(mCellMonitorPhoneStateListener, PhoneStateListener.LISTEN_NONE);
    mLocationManager.removeUpdates(mGpsLocationListener);

    /*
     * Suspend the Kaa client. Release all network connections and application
     * resources. Suspend all the Kaa client tasks.
     */
    mClient.pause();
  }
  @Override
  public void onTerminate() {
    super.onTerminate();

    /*
     * Stop the Kaa client. Release all network connections and application
     * resources. Shut down all the Kaa client tasks.
     */
    mClient.stop();
    mKaaStarted = false;
  }
  public void resume() {
    mTelephonyManager.listen(
        mCellMonitorPhoneStateListener,
        PhoneStateListener.LISTEN_CELL_LOCATION | PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
    Criteria criteria = new Criteria();
    String bestProvider = mLocationManager.getBestProvider(criteria, false);
    mLocationManager.requestLocationUpdates(bestProvider, 0, 0, mGpsLocationListener);

    /*
     * Resume the Kaa client. Restore the Kaa client workflow. Resume all the Kaa client
     * tasks.
     */
    mClient.resume();
  }
  @Override
  public void onCreate() {
    super.onCreate();
    mEventBus = new EventBus();
    mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    mCellMonitorPhoneStateListener = new CellMonitorPhoneStateListener();

    mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String bestProvider = mLocationManager.getBestProvider(criteria, false);
    mGpsLocation = mLocationManager.getLastKnownLocation(bestProvider);
    mGpsLocationListener = new GpsLocationListener();

    /*
     * Initialize the Kaa client using the Android context.
     */
    KaaClientPlatformContext kaaClientContext = new AndroidKaaPlatformContext(this);
    mClient =
        Kaa.newClient(
            kaaClientContext,
            new SimpleKaaClientStateListener() {

              /*
               * Implement the onStarted callback to get notified as soon as
               * the Kaa client is operational.
               */
              @Override
              public void onStarted() {
                mKaaStarted = true;
                LOG.info("Kaa client started");
              }
            });

    /*
     * Define a log upload strategy used by the Kaa client for logs delivery.
     */
    mClient.setLogUploadStrategy(
        new LogUploadStrategy() {

          @Override
          public void onTimeout(LogFailoverCommand logFailoverCommand) {
            LOG.error("Unable to send logs within defined timeout!");
          }

          @Override
          public void onFailure(
              LogFailoverCommand logFailoverCommand, LogDeliveryErrorCode logDeliveryErrorCode) {
            LOG.error("Unable to send logs, error code: " + logDeliveryErrorCode);
            logFailoverCommand.retryLogUpload(10);
          }

          @Override
          public LogUploadStrategyDecision isUploadNeeded(LogStorageStatus logStorageStatus) {
            return logStorageStatus.getRecordCount() > 0
                ? LogUploadStrategyDecision.UPLOAD
                : LogUploadStrategyDecision.NOOP;
          }

          @Override
          public int getTimeout() {
            return 100;
          }

          @Override
          public long getBatchSize() {
            return 8 * 1024;
          }
        });

    /*
     * Start the Kaa client workflow.
     */
    mClient.start();
  }