text: /*********************************************************************************************
   * This code was automatically generated by Digital Twin Code Generator tool 0.6.7.
   *
   * Generated Date: 20/01/14
   *********************************************************************************************/
  
  #include "pnp_device.h"
  
  // Number of Digital Twins interfaces that this device supports.
  #define DIGITALTWIN_INTERFACE_NUM 2
  
  #define ENVIRONMENTALSENSOR_INDEX 0
  
  #define DEVICEINFO_INDEX 1
  
  #define DEFAULT_SEND_TELEMETRY_INTERVAL_MS 10000
  
  #define DEVICE_CAPABILITY_MODEL_URI "urn:DotNETForDevices:SensorboxModel:1"
  
  static DIGITALTWIN_INTERFACE_CLIENT_HANDLE interfaceClientHandles[DIGITALTWIN_INTERFACE_NUM];
  static TICK_COUNTER_HANDLE tickcounter = NULL;
  static tickcounter_ms_t lastTickSend;
  
  int pnp_device_initialize(const char* connectionString, const char* trustedCert)
  {
      if ((tickcounter = tickcounter_create()) == NULL)
      {
          LogError("tickcounter_create failed");
          return -1;
      }
  
      DIGITALTWIN_DEVICE_CLIENT_LL_HANDLE digitalTwinDeviceClientHandle = NULL;
      memset(&interfaceClientHandles, 0, sizeof(interfaceClientHandles));
  
      // Initialize DigitalTwin device handle
      if ((digitalTwinDeviceClientHandle = DigitalTwinClientHelper_InitializeDeviceHandle(connectionString, false, trustedCert)) == NULL)
      {
          LogError("DigitalTwinClientHelper_InitializeDeviceHandle failed");
          return -1;
      }
  
      // Invoke to the ***Interface_Create - implemented in a separate library - to create DIGITALTWIN_INTERFACE_CLIENT_HANDLE.
      // NOTE: Other than creation and destruction, NO operations may occur on any DIGITALTWIN_INTERFACE_CLIENT_HANDLE
      // until after we've completed its registration (see DigitalTwinClientHelper_RegisterInterfacesAndWait).
  
      if ((interfaceClientHandles[ENVIRONMENTALSENSOR_INDEX] = EnvironmentalSensorInterface_Create(digitalTwinDeviceClientHandle)) == NULL)
      {
          LogError("EnvironmentalSensorInterface_Create failed");
          return -1;
      }
  
      if ((interfaceClientHandles[DEVICEINFO_INDEX] = DeviceInfoInterface_Create(digitalTwinDeviceClientHandle)) == NULL)
      {
          LogError("DeviceInfoInterface_Create failed");
          return -1;
      }
  
      // Register the interface(s) we've created with Azure IoT.  This call will block until interfaces
      // are successfully registered, we get a failure from server, or we timeout.
      if (DigitalTwinClientHelper_RegisterInterfacesAndWait(digitalTwinDeviceClientHandle, DEVICE_CAPABILITY_MODEL_URI, interfaceClientHandles, DIGITALTWIN_INTERFACE_NUM) != DIGITALTWIN_CLIENT_OK)
      {
          LogError("DigitalTwinClientHelper_RegisterInterfacesAndWait failed");
          return -1;
      }
  
      DigitalTwinClientHelper_Check();
  
      // report properties
  
      DeviceInfoInterface_Property_ReportAll();
  
      tickcounter_get_current_ms(tickcounter, &lastTickSend);
      return 0;
  }
  
  /**********************************************************************************************
   * This part of the code is manually modified. Make sure to back-up if you need to regenerate
   * the code by the Digital Twin Code Generator tool.
   * ********************************************************************************************/
  static int intervalTime = 0;
  
  void pnp_device_set_interval_time(int interval)
  {
      intervalTime = interval;
  }
  /* End of manual modifications ****************************************************************/
  
  void pnp_device_run()
  {
      tickcounter_ms_t nowTick;
      tickcounter_get_current_ms(tickcounter, &nowTick);
  
      if ((nowTick - lastTickSend) >= (intervalTime == 0 ? DEFAULT_SEND_TELEMETRY_INTERVAL_MS : intervalTime))
      {
          LogInfo("Send telemetry data to IoT Hub");
  
          EnvironmentalSensorInterface_Telemetry_SendAll();
  
          tickcounter_get_current_ms(tickcounter, &lastTickSend);
      }
      else
      {
          // Just check data from IoT Hub
          DigitalTwinClientHelper_Check();
      }
  }
  
  void pnp_device_close()
  {
      if (interfaceClientHandles[ENVIRONMENTALSENSOR_INDEX] != NULL)
      {
          EnvironmentalSensorInterface_Close(interfaceClientHandles[ENVIRONMENTALSENSOR_INDEX]);
      }
  
      if (interfaceClientHandles[DEVICEINFO_INDEX] != NULL)
      {
          DeviceInfoInterface_Close(interfaceClientHandles[DEVICEINFO_INDEX]);
      }
  
      DigitalTwinClientHelper_DeInitialize();
  }
  