langcode: en
status: true
dependencies:
  enforced:
    module:
      - farm_migrate
      - data_stream
      - data_stream_notification
      - farm_sensor
      - farm_sensor_listener
id: farm_migrate_sensor_listener_notifications
label: 'Listener sensor notifications'
migration_group: farm_migrate_sensor_data
migration_tags:
  - 'Drupal 7'
  - 'farmOS 1.x'
class: Drupal\migrate\Plugin\Migration
field_plugin_method: null
cck_plugin_method: null
source:
  plugin: d7_sensor_asset
  sensor_type: 'listener'
  constants:
    NOTIFICATION_ID_PREFIX: 'sensor'
    NOTIFICATION_LABEL_SEPARATOR: ': '
destination:
  plugin: 'entity:data_stream_notification'
process:
  # Do not create a notification if there are no configured notifications.
  # In practice there will only be one notification, but it is stored as an array.
  _notifications:
    - plugin: extract
      source: sensor_settings
      default: false
      index:
        - notifications
    - plugin: skip_on_empty
      method: row
  # Save the first notification value name as a pseudo field.
  _value_name:
    - plugin: extract
      source: '@_notifications'
      default: false
      index:
        - 0
        - name
    - plugin: skip_on_empty
      method: row
  # Build an id of the format sensor_{asset_id}_{value name}
  id:
    plugin: concat
    delimiter: '_'
    source:
      - constants/NOTIFICATION_ID_PREFIX
      - id
      - '@_value_name'
  # Build a label of the format {Sensor name}: {value name}
  label:
    plugin: concat
    source:
      - name
      - constants/NOTIFICATION_LABEL_SEPARATOR
      - '@_value_name'
  # Lookup the data stream to reference.
  # Use the sensor asset id and notification value name as source ids.
  data_stream:
    - plugin: migration_lookup
      migration: farm_migrate_sensor_listener_data_streams
      no_stub: true
      source_ids:
        farm_migrate_sensor_listener_data_streams:
          - id
          - '@_value_name'
    - plugin: skip_on_empty
      method: row
  # Configure default values.
  # Only send an email the first time a threshold is crossed.
  activation_threshold:
    plugin: default_value
    default_value: 1
  deactivation_threshold:
    plugin: default_value
    default_value: 1
  condition_operator:
    plugin: default_value
    default_value: or
  delivery_interval:
    plugin: default_value
    default_value: 0
  # Build numeric condition.
  condition:
    plugin: sub_process
    source: '@_notifications'
    process:
      type:
        plugin: default_value
        default_value: numeric
      negagte:
        plugin: default_value
        default_value: false
      condition:
        - plugin: get
          source: condition
        - plugin: skip_on_empty
          method: row
      threshold:
        - plugin: get
          source: threshold
        - plugin: skip_on_empty
          method: row
  # Build email delivery.
  delivery:
    plugin: sub_process
    source: '@_notifications'
    process:
      type:
        plugin: default_value
        default_value: email
      email:
        - plugin: get
          source: email
        - plugin: skip_on_empty
          method: row
        - plugin: explode
          delimiter: ','
migration_dependencies:
  required:
    - farm_migrate_sensor_listener_data_streams
  optional: {  }
