All files / src/consumers ConsumerFactory.js

92.85% Statements 104/112
64.7% Branches 11/17
100% Functions 4/4
92.85% Lines 104/112

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 1131x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 33x 33x 33x 33x 3x 3x 30x 10x 10x 20x 20x 20x 33x 33x     33x 33x 33x 33x 1x 1x 1x 1x 1x 1x 1x 3x     3x 3x 10x     10x 10x 10x 10x 10x 10x     10x 10x 10x 10x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 20x 20x 20x 20x 20x 20x 20x 20x 20x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 10x 1x 1x 1x  
// @ts-check
// SPDX-License-Identifier: Zlib
// SPDX-FileCopyrightText: 2024 CERN (home.cern)
 
import MultiplexConsumer from "./MultiplexConsumer.js";
import MJPEGConsumer from "./MJPEGConsumer.js";
import HTTPConsumer from "./HTTPConsumer.js";
import { isEmpty, isNil } from "@cern/nodash";
 
/**
 * @typedef {import('../index.d.ts')} CamExpress
 */
 
/**
 * @implements {CamExpress.ConsumerFactory}
 */
class ConsumerFactory {
 
  /**
   * @param {CamExpress.Camera} cam
   * @param {CamExpress.CamService} service
   * @return {CamExpress.Consumer}
   */
  static createConsumer(cam, service) {
    let consumer;
 
    // Determine the right type of consumer
    if (!isNil(cam.multiplex)) {
      consumer = ConsumerFactory.createMultiplexConsumer(cam, service);
    }
    else if (cam.poll) {
      consumer = ConsumerFactory.createHTTPConsumer(cam, service.creds);
    }
    else {
      consumer = ConsumerFactory.createMJPEGConsumer(cam, service.creds);
    }
 
    if (isNil(consumer)) {
      throw new Error("Consumer initialisation failed");
    }
 
    return consumer;
 
  }
 
  /**
   * @param {CamExpress.Camera} cam
   * @param {CamExpress.CamService} service
   * @return {CamExpress.MultiplexConsumer}
   */
  static createMultiplexConsumer(cam, service) {
    if (isEmpty(cam.multiplex)) {
      throw new Error("No camera in the multiplex channel");
    }
 
    const references = cam.multiplex?.map((ref) => {
      if (isNil(ref.group) || isNil(ref.camera)) {
        throw new Error("No given path for the camera");
      }
 
      const group = service.groups?.[ref.group]?.cameras ?? {};
      const camera = Object.values(group).find(
        (camera) => (camera?.name === ref.camera));
 
      if (camera === undefined) {
        throw new Error(`No camera found at ${ref.group}/${ref.camera}`);
      }
 
      const duration = ref.duration;
 
      return { camera, duration };
    }) ?? [];
 
    return new MultiplexConsumer({
      service: service,
      cameras: references,
      index: 0
    });
  }
 
  /**
   * @param {CamExpress.Camera} cam
   * @param {CamExpress.Credentials} creds
   * @return {CamExpress.MJPEGConsumer}
   */
  static createMJPEGConsumer(cam, creds) {
    return new MJPEGConsumer({
      src: cam.url,
      auth: creds?.[cam.name],
      contentLength: cam.contentLength,
      headerDelimiter: cam.headerDelimiter,
      boundary: cam.boundary,
      throttle: cam.throttle
    });
  }
 
  /**
   * @param {CamExpress.Camera} cam
   * @param {CamExpress.Credentials} creds
   * @return {HTTPConsumer}
   */
  static createHTTPConsumer(cam, creds) {
 
    return new HTTPConsumer({
      src: cam.url,
      auth: creds?.[cam.name],
      throttle: cam.throttle
    });
  }
}
 
export default ConsumerFactory;