private async sendD2CMessageFromMultipleDevicesRepeatedly()

in src/simulator.ts [332:402]


  private async sendD2CMessageFromMultipleDevicesRepeatedly(
    deviceConnectionStrings: string[],
    template: string,
    isTemplate: boolean,
    numbers: number,
    interval: number,
  ) {
    const deviceCount = deviceConnectionStrings.length;
    const total = deviceCount * numbers;
    if (total <= 0) {
      this.output(`Invalid Operation.`);
      return;
    }
    const startTime = new Date();
    this.output(
      `Start sending messages from ${deviceCount} device(s) to IoT Hub.`,
    );
    const clients = [];
    const statuses = [];
    const ids = [];
    this.totalStatus = new SendStatus("Total", total);
    for (let i = 0; i < deviceCount; i++) {
      clients.push(
        await clientFromConnectionString(deviceConnectionStrings[i]),
      );
      statuses.push(
        new SendStatus(
          ConnectionString.parse(deviceConnectionStrings[i]).DeviceId,
          numbers,
        ),
      );
      ids.push(i);
    }
    for (let i = 0; i < numbers; i++) {
      // No await here, beacause the interval should begin as soon as it called send(), not after it sent.
      ids.map((j) => {
        // We use a template so that each time the message can be randomly generated.
        const generatedMessage = isTemplate
        ? dummyjson.parse(template)
        : template;
        this.sendD2CMessageCore(
          clients[j],
          generatedMessage,
          statuses[j],
          this.totalStatus,
        );
      });
      this.totalStatus.addSent(deviceCount);
      if (this.cancelToken) {
        break;
      }
      if (i < numbers - 1) {
        // There won"t be a delay after the last iteration.
        await this.cancellableDelay(interval);
      }
    }
    const endTime = new Date();
    this.output(
      this.cancelToken ? `User aborted.` : `All device(s) finished sending in ${(endTime.getTime() - startTime.getTime()) /
      1000} second(s).`,
    );
    while (
      !this.cancelToken &&
      await this.totalStatus.sum() !== await this.totalStatus.getTotal()
    ) {
      await this.delay(500);
    }
    this.output(
      `${await this.totalStatus.getSucceed()} succeeded, and ${await this.totalStatus.getFailed()} failed.`,
    );
  }