async function processConnection()

in source/lambda/connection-builder/index.ts [185:279]


async function processConnection(connectionDefinition: ConnectionBuilderTypes.ConnectionDefinition): Promise<ConnectionBuilderTypes.ProcessConnectionResponse> {
  const { control, connectionName, protocol } = connectionDefinition;

  try {
    /**
     * Builds a new connection definition, publishes the connection definition to the machine throught the IoT topic,
     * and updates the DynamoDB table item.
     */
    const newConnectionDefinition = await buildConnectionDefinition(connectionName, control, protocol);

    /**
     * Currently, only supporting OPC DA and OPC UA.
     * As protocol validation check has been done in the previous step, `else` statement is for OPC UA.
     */
    if (protocol === ConnectionBuilderTypes.MachineProtocol.OPCDA) {
      await iotHandler.publishIoTTopicMessage(connectionName, IoTHandlerTypes.IotMessageTypes.JOB, newConnectionDefinition);
    } else {
      /**
       * It does not throw an error for the default as `processConnection` is expected to be executed
       * after the validation checks.
       * So, the default would be pull.
       */
      let source: any;

      switch (control) {
        /**
         * `start` expects that the source configuration has been stored in the DynamoDB table.
         * Since `start` adds the source configuration to the IoT Sitewise gateway capability configuration,
         * it should preserve the existing configuration, so it gets the source configuration from the DynamoDB table.
         */
        case ConnectionBuilderTypes.ConnectionControl.START:
          source = newConnectionDefinition.opcUa.source;
          await iotSitewiseHandler.addExistingSourceToGatewayCapabilityConfiguration(source);
          delete newConnectionDefinition.opcUa.source;

          break;
        /**
         * `stop` deletes the source configuration from the IoT Sitewise gateway capability configuration.
         * Since users could have changed configurations on the IoT Sitewise console, it preserves the configuration
         * in the DynamoDB table so when `start` happens, the source configuration can be used.
         */
        case ConnectionBuilderTypes.ConnectionControl.STOP:
          source = await iotSitewiseHandler.getGatewayCapabilityConfigurationSourceByServerName(newConnectionDefinition.opcUa.serverName);
          newConnectionDefinition.opcUa.source = source;

          await iotSitewiseHandler.deleteGatwayCapabilityConfigurationSource(newConnectionDefinition.opcUa.serverName);
          break;
        /**
         * `push` only returns the source configuration from the IoT Sitewise gateway capability configuration.
         */
        case ConnectionBuilderTypes.ConnectionControl.PUSH:
          source = await iotSitewiseHandler.getGatewayCapabilityConfigurationSourceByServerName(newConnectionDefinition.opcUa.serverName);
          await iotHandler.publishIoTTopicMessage(connectionName, IoTHandlerTypes.IotMessageTypes.INFO, {
            connectivityConfiguration: source
          });
          break;
        /**
         * By default, it's `pull`, and it returns the DynamoDB data through the IoT topic.
         */
        default:
          const opcUa = {
            ...newConnectionDefinition.opcUa
          };
          delete opcUa.source;

          await iotHandler.publishIoTTopicMessage(connectionName, IoTHandlerTypes.IotMessageTypes.INFO, opcUa);
          break;
      }
    }

    await updateConnection(newConnectionDefinition);

    if (SEND_ANONYMOUS_METRIC === 'Yes') {
      await sendAnonymousMetric({ EventType: control, protocol: protocol }, SOLUTION_UUID);
    }

    return {
      connectionName,
      control,
      result: `Success to ${control} the connection: ${connectionName}`
    };
  } catch (error) {
    logger.log(LoggingLevel.ERROR, `Error to process connection [${connectionName}] to [${control}]: `, error);

    if (error instanceof LambdaError) {
      throw error;
    } else {
      throw new LambdaError({
        message: `An error occurred while processing the connection: ${connectionName}`,
        name: 'ConnectionBuilderError',
        statusCode: 500
      });
    }
  }
}