How do I extract the time from a SmartRest Template?

I was browsing through my list of custom templates and noticed that each of template contains a time entry in the preview section of the API.

image

In my Angular program, I want to filter the measurements I am getting and place them in a 12-length array where each index equals a month of the year, so that I can then display it in a bar chart using ChartJS.

I made a model of such a data in TypeScript, and I wanted to confirm if this would translate into the object shown in the preview above.

export interface ITubeSetUsage {
    time: string;
    tubeSetType: string;
    value: number;
    unit: string;
}

If I am not mistaken, the time variable will be rendered according to the ISO format such as 2023-08-24T10:51:04.000Z

An example of a template that I am using is called tubeSetTemplate, and it appears in Cumulocity is as follows:

Am I going down the right path, or is there something else I should be doing to get the timestamp from a C8Y measurement?

Hi Lucas,

the datastructure that you would get via API (Cumulocity IoT - OpenAPI Specification) should look somehow like this:

import { IMeasurement } from '@c8y/client';

export interface ITubeSetUsage extends IMeasurement {
  type: 'tubeSet';
  tubeSet: {
    smokeEvacuationHighFlow: {
      value: number;
      unit: 'Total Count';
    };
  };
}

Check the IMeasurement interface (IMeasurement | Cumulocity Web SDK) for the time attribute.

Regards,
Tristan

Thanks, will do.

Hi Tristan,

I have modelled a new method to load the latest ModeUsage Measurements but I am having trouble extracting the time property from the response object I receive

 async loadLatestModeDatum(
    modeFragment: string,
    modeSeries: string,
  ) {
    const filter = {
      type: "mode",
      valueFragmentType: modeFragment,
      valueFragmentSeries: modeSeries,
      revert: true,
    };

    try {
      const response = await this.measurementService.list(filter);
      if (
        !response.data ||
        !has(response.data[0], `${modeFragment}.${modeSeries}`)
      ) {
        return;
      }

      const modeCountValue: number = get(
        response.data[0],
        `${modeFragment}.${modeSeries}.value`
      );
      const modeCountUnit: string = get(
        response.data[0],
        `${modeFragment}.${modeSeries}.unit`
      );
      this.modeUsageData.next({
          time: new Date().toISOString(),
          mode: "mode",
          value: modeCountValue,
          unit: modeCountUnit
      })
    } catch (error) {
      console.error(
        'Error occured while loading the latest measurement: ',
        error
      );
    }
  }

  async getTubeSetDataUsageByDate(dateFromParam: string, dateToParam: string){
    const tubeSetData = await this.inventoryService.getSupportedMeasurements(
      (filter: any) => {
        filter = {
          type: 'tubeSet',
          dateFrom: dateFromParam,
          dateTo: dateToParam,
        };
      }
    )
    return tubeSetData
  }

Hi Lucas,

I’m not sure what you are trying to achieve with the getSupportedMeasurements as it does not expect a function to be passed as parameter but as it is not actually being used any further, I guess we can ignore it…

instead of creating a new timestamp here:

      this.modeUsageData.next({
          time: new Date().toISOString(),
          mode: "mode",
          value: modeCountValue,
          unit: modeCountUnit
      })

You could just do:

      this.modeUsageData.next({
          time: response.data[0].time,
          mode: "mode",
          value: modeCountValue,
          unit: modeCountUnit
      })

Hi Tristan,

I had to slightly modify your code for it to be compatible with mine as in my interface, string is defined as a string:

      this.modeUsageData.next({
          time: response.data[0].time.toString(),
          mode: "mode",
          value: modeCountValue,
          unit: modeCountUnit
      })

But the TypeScript compiler seems happy, anyway.

The second method is to get tube set usage data from a certain period of time. I just needed the measurements, so I was unsure as to whether I should have placed getMeasurementsAndSeries or if getSupportedMeasurements would have sufficed.

I am guessing the latter would not?

Hi Lucas,

actually response.data[0].time should already be a string and I guess it is just typed incorrectly on our end as time: string | Date;.

With getSupportedMeasurements we are basically just calling this endpoint: Cumulocity IoT - OpenAPI Specification which only provides you the information regarding what kind of fragments your device supports.

getMeasurementsAndSeries does something similar and will provide you the supported fragment/series combinations of your device by combining the above mentioned endpoint with Cumulocity IoT - OpenAPI Specification

Both methods only allow/require you to filter by deviceId and do not allow filtering by a time frame.

In case you are just interested in the first value, you can also add a pageSize of 1 to your filter:

    const filter = {
      type: "mode",
      valueFragmentType: modeFragment,
      valueFragmentSeries: modeSeries,
      revert: true,
      pageSize: 1,
    };

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.