public Throwable fhir(SparseArray<GlucoseRecord> records) {

    try {
      Patient p = getPatient();
      Log.d(TAG, "converting glucoserecrods to observations now");

      Device d = initializeDevice(p);
      // do conditional update here so the device is updated if needed

      String ident = getConditionalUrl(d, d.getIdentifierFirstRep());
      String a = ctx.newXmlParser().setPrettyPrint(true).encodeResourceToString(d);
      IdDt id = (IdDt) client.update().resource(d).conditionalByUrl(ident).execute().getId();
      d.setId(id);

      DeviceMetric metric = initializeDeviceMetric(d);
      metric.setId(
          client
              .create()
              .resource(metric)
              .conditionalByUrl(getConditionalUrl(metric, metric.getIdentifier()))
              .execute()
              .getId());

      for (int i = 0; i < records.size(); i++) {
        int j = records.keyAt(i);
        GlucoseRecord r = records.get(j);

        Log.d(TAG, "creating quantity");

        QuantityDt qdt = initializeQuantityDt(r);

        Observation o = initializeObservation(p, metric, r, qdt);

        Log.d(TAG, "submitting to server...");
        // client.update().resource(o).conditionalByUrl("Observation?identifier=http://...%7Cid").encodedJson().execute();
        IdDt did =
            (IdDt)
                client
                    .update()
                    .resource(o)
                    .conditionalByUrl(getConditionalUrl(o, o.getIdentifierFirstRep()))
                    .execute()
                    .getId();

        o.setId(did);

        String s = ctx.newXmlParser().setPrettyPrint(true).encodeResourceToString(o);

        Log.d(TAG, "... done");
      }
    } catch (Throwable e) {
      return e;
    }

    return null;
  }
  private Device initializeDevice(Patient p) {
    log("Initializing device");

    Device d = newDevice();
    d.setManufacturer(deviceInfo.getManufacturerName());
    d.setModel(deviceInfo.getModelNumber());
    d.setVersion(deviceInfo.getFirmwareRevision());
    NarrativeDt n = getNarrative(d);
    n.setDiv(
        n.getDiv()
            .getValueAsString()
            .substring(5, n.getDiv().getValueAsString().length() - 6)
            .concat("<strong>Serial Number: </strong>")
            .concat(deviceInfo.getSerialNumber())
            .concat("<br /><strong>Name: </strong>")
            .concat(deviceInfo.getName())
            .concat("<br /><strong>MAC Address: </strong>")
            .concat(deviceInfo.getAddress()));
    d.setText(n);
    CodeableConceptDt type = new CodeableConceptDt();
    // TODO find valid url
    type.addCoding()
        .setCode("15-102")
        .setDisplay("Glukose-Analysegerät")
        .setSystem("http://umdns.org");
    d.setType(type);

    d.setPatient(newResourceReference(p.getId()));
    d.setIdentifier(Arrays.asList(getIdentifier("/devices/", "" + deviceInfo.getSysID())));

    log("Initializing device done");

    return d;
  }
  private DeviceMetric initializeDeviceMetric(Device d) {
    log("Initializing device metric");

    DeviceMetric metric = new DeviceMetric();
    CodeableConceptDt metricType = new CodeableConceptDt();
    // TODO is this the right code and display?
    metricType
        .addCoding()
        .setCode("160020")
        .setSystem("https://rtmms.nist.gov")
        .setDisplay("MDC_CONC_GLU_GEN");
    metric.setType(metricType);
    metric.setSource(newResourceReference(d.getId()));
    metric.setText(getNarrative(metric));
    metric.setCategory(DeviceMetricCategoryEnum.MEASUREMENT);
    CodeableConceptDt unitConcept = new CodeableConceptDt();
    unitConcept
        .addCoding()
        .setDisplay("MDC_DIM_MILLI_MOLE_PER_L")
        .setSystem("https://rtmms.nist.gov")
        .setCode("266866");
    metric.setUnit(unitConcept);
    // TODO what is the identifier here??? especially the value
    metric.setIdentifier(
        new IdentifierDt().setSystem(SYSTEM_URL + "/metric/").setValue("glucosemillimolperliter"));

    log("Initializing device metric done.");
    return metric;
  }