MQTT trigger script template

Script windmill

by dieriba.pro916 · 2/25/2025

  • Submitted by dieriba.pro916 Bun
    Created 441 days ago
    1
    /**
    2
     * General Trigger Preprocessor Function
    3
     * 
    4
     * ⚠️ IMPORTANT: This preprocessor function is ALWAYS called FIRST, before the main function.
    5
     * 
    6
     * This example assumes the script was triggered by an MQTT event, but the same 
    7
     * preprocessor can handle other trigger types by checking wm_trigger.kind.
    8
     * 
    9
     * Flow: 
    10
     * 1. Trigger event occurs (MQTT message in this example)
    11
     * 2. Preprocessor function is called FIRST
    12
     * 3. Main function is called with the return values from preprocessor
    13
     * 
    14
     * You can transform the raw trigger data here before it reaches your main function.
    15
     * Common transformations include:
    16
     * - Converting binary payload to string or JSON
    17
     * - Extracting metadata from the trigger source
    18
     * - Filtering messages
    19
     * - Adding timestamps or other context
    20
     * 
    21
     * IMPORTANT: The object you return from this preprocessor determines the parameters passed to your main function:
    22
     * - If you return {a: 1, b: 2}, then main will be called as main(a, b)
    23
     * - If you return {payload}, then main will be called as main(payload)
    24
     * 
    25
     * @param wm_trigger - Information about the trigger source (could be MQTT, HTTP, email, etc.)
    26
     * @param payload - The raw data from the trigger (type depends on the trigger kind)
    27
     * @returns An object with the arguments to pass to the main function
    28
     */
    29
    export async function preprocessor(
    30
      wm_trigger: {
    31
        kind: 'http' | 'email' | 'webhook' | 'websocket' | 'kafka' | 'nats' | 'postgres' | 'sqs' | 'mqtt',
    32
        mqtt?: {
    33
          topic: string,
    34
          retain: boolean,
    35
          pkid: number,
    36
          qos: number,
    37
          v5?: {
    38
            payload_format_indicator?: number,
    39
            topic_alias?: number,
    40
            response_topic?: string,
    41
            correlation_data?: Array<number>,
    42
            user_properties?: Array<[string, string]>,
    43
            subscription_identifiers?: Array<number>,
    44
            content_type?: string
    45
          }
    46
        }
    47
      },
    48
      /**
    49
     * Each trigger type has its own specific parameters:
    50
     * - For MQTT: 'payload' (Array of raw message bytes)
    51
     * - For SQS: 'msg' (message data)
    52
     * - Other trigger types have their own specific properties
    53
     */
    54
      payload: Array<number>,
    55
    ) {
    56
      if (wm_trigger.kind === 'mqtt' && wm_trigger.mqtt) {
    57
        // Convert Array<number> to Uint8Array for binary processing
    58
        const uint8Payload = new Uint8Array(payload);
    59
    
    
    60
        // Convert the binary payload to a string
    61
        // This assumes the binary data represents a string (e.g., UTF-8 encoded text)
    62
        const payloadAsString = new TextDecoder().decode(uint8Payload);
    63
    
    
    64
        return {
    65
          contentType: wm_trigger.mqtt.v5?.content_type,
    66
          payload: uint8Payload,
    67
          payloadAsString
    68
        }
    69
      }
    70
      else {
    71
        return {
    72
          kind: wm_trigger.kind
    73
        }
    74
      }
    75
    }
    76
    
    
    77
    /**
    78
     * Main Function - Handles Trigger Events
    79
     * 
    80
     * ⚠️ This function is called AFTER the preprocessor function.
    81
     * 
    82
     * This example assumes we're handling an MQTT trigger event where the payload is a string.
    83
     * The main function receives arguments based on what the preprocessor returned.
    84
     * 
    85
     * @param contentType - For MQTT: The content type from MQTT v5 properties if available
    86
     * @param payload - The raw binary payload from MQTT
    87
     * @param payloadAsString - The payload as a string (decoded from binary data)
    88
     */
    89
    export async function main(payload: Uint8Array, payloadAsString: string, contentType?: string) {
    90
    
    
    91
    }