QuarkIoE实时最新测量获取

时间:2018-03-26 06:10:23

标签: cumulocity

c8yMeasurements.latest(filter, realtime)
  .then(function (latestMeasurement) {
    vm.text = latestMeasurement.c8y_TemperatureMeasurement.T.value;
    console.log(vm.text);
  });

我尝试使用以下文档从某个设备获取最新的测量结果:http://resources.cumulocity.com/documentation/jssdk/latest/#/api/c8y.core.service:c8yMeasurements

但是,我不太确定realtime事实是如何运作的。我将realtime设置为true,但每次代理更改值时都不会更新vm.text的值。

为了解决这个问题,我在JS中使用了setInterval方法(这是非常讨厌的T T)。然后它在我的自定义应用程序中工作正常。

但是,在c8y build:plugin hellohello是我的插件的名称)并将zip文件上传到cockpit之后,无法获取该值。

请帮忙:(谢谢。

1 个答案:

答案 0 :(得分:3)

为了使其实时工作,您需要将结果包装到函数中,以便角度可以在更新模型时调用它。这是一个例子

function getLatestMeasurementForDatapoint({ fragment, series, __target }) {
  return c8yMeasurements.latest({
      fragment,
      series,
      device: _.get(__target, 'id')
    }, true)
      .then(
        (rtLatestMeasurement) => {
          return {
            value: () => _.get(rtLatestMeasurement, [fragment, series, 'value'])
          };
        }
      );
}

在控制器中,您可以调用此函数,该函数将返回包含键值

的对象
getLatestMeasurementForDatapoint(vm.datapoint)
  .then((latestMeasurement) => { vm.latestMeasurement = latestMeasurement; });

最后在你的模型中,你可以参考那个结果。请注意,value是一个函数,因此您需要像函数一样调用它。

{{vm.latestMeasurement.value()}}