All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.davemeier82.homeautomation.hivemq.mapper.EventToDtoMapper Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2021-2022 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.github.davemeier82.homeautomation.hivemq.mapper;

import io.github.davemeier82.homeautomation.core.device.property.DeviceProperty;
import io.github.davemeier82.homeautomation.core.device.property.DevicePropertyId;
import io.github.davemeier82.homeautomation.core.event.AlarmStateChangedEvent;
import io.github.davemeier82.homeautomation.core.event.AlarmStateUpdatedEvent;
import io.github.davemeier82.homeautomation.core.event.BatteryLevelChangedEvent;
import io.github.davemeier82.homeautomation.core.event.BatteryLevelUpdatedEvent;
import io.github.davemeier82.homeautomation.core.event.CloudBaseChangedEvent;
import io.github.davemeier82.homeautomation.core.event.CloudBaseUpdatedEvent;
import io.github.davemeier82.homeautomation.core.event.Co2LevelChangedEvent;
import io.github.davemeier82.homeautomation.core.event.Co2LevelUpdatedEvent;
import io.github.davemeier82.homeautomation.core.event.DevicePropertyEvent;
import io.github.davemeier82.homeautomation.core.event.DimmingLevelChangedEvent;
import io.github.davemeier82.homeautomation.core.event.DimmingLevelUpdatedEvent;
import io.github.davemeier82.homeautomation.core.event.HumidityChangedEvent;
import io.github.davemeier82.homeautomation.core.event.HumidityUpdatedEvent;
import io.github.davemeier82.homeautomation.core.event.IlluminanceChangedEvent;
import io.github.davemeier82.homeautomation.core.event.IlluminanceUpdatedEvent;
import io.github.davemeier82.homeautomation.core.event.MotionChangedEvent;
import io.github.davemeier82.homeautomation.core.event.MotionUpdatedEvent;
import io.github.davemeier82.homeautomation.core.event.PowerChangedEvent;
import io.github.davemeier82.homeautomation.core.event.PowerUpdatedEvent;
import io.github.davemeier82.homeautomation.core.event.PressureChangedEvent;
import io.github.davemeier82.homeautomation.core.event.PressureUpdatedEvent;
import io.github.davemeier82.homeautomation.core.event.RainIntervalAmountChangedEvent;
import io.github.davemeier82.homeautomation.core.event.RainIntervalAmountUpdatedEvent;
import io.github.davemeier82.homeautomation.core.event.RainRateChangedEvent;
import io.github.davemeier82.homeautomation.core.event.RainRateUpdatedEvent;
import io.github.davemeier82.homeautomation.core.event.RainTodayAmountChangedEvent;
import io.github.davemeier82.homeautomation.core.event.RainTodayAmountUpdatedEvent;
import io.github.davemeier82.homeautomation.core.event.RelayStateChangedEvent;
import io.github.davemeier82.homeautomation.core.event.RelayStateUpdatedEvent;
import io.github.davemeier82.homeautomation.core.event.RollerPositionChangedEvent;
import io.github.davemeier82.homeautomation.core.event.RollerPositionUpdatedEvent;
import io.github.davemeier82.homeautomation.core.event.RollerStateChangedEvent;
import io.github.davemeier82.homeautomation.core.event.RollerStateUpdatedEvent;
import io.github.davemeier82.homeautomation.core.event.SmokeStateChangedEvent;
import io.github.davemeier82.homeautomation.core.event.SmokeStateUpdatedEvent;
import io.github.davemeier82.homeautomation.core.event.TemperatureChangedEvent;
import io.github.davemeier82.homeautomation.core.event.TemperatureUpdatedEvent;
import io.github.davemeier82.homeautomation.core.event.UvIndexChangedEvent;
import io.github.davemeier82.homeautomation.core.event.UvIndexUpdatedEvent;
import io.github.davemeier82.homeautomation.core.event.WindDirectionChangedEvent;
import io.github.davemeier82.homeautomation.core.event.WindDirectionUpdatedEvent;
import io.github.davemeier82.homeautomation.core.event.WindGustDirectionChangedEvent;
import io.github.davemeier82.homeautomation.core.event.WindGustDirectionUpdatedEvent;
import io.github.davemeier82.homeautomation.core.event.WindGustSpeedChangedEvent;
import io.github.davemeier82.homeautomation.core.event.WindGustSpeedUpdatedEvent;
import io.github.davemeier82.homeautomation.core.event.WindRunChangedEvent;
import io.github.davemeier82.homeautomation.core.event.WindRunUpdatedEvent;
import io.github.davemeier82.homeautomation.core.event.WindSpeedChangedEvent;
import io.github.davemeier82.homeautomation.core.event.WindSpeedUpdatedEvent;
import io.github.davemeier82.homeautomation.core.event.WindowStateChangedEvent;
import io.github.davemeier82.homeautomation.core.event.WindowStateUpdatedEvent;
import io.github.davemeier82.homeautomation.core.event.WindowTiltAngleChangedEvent;
import io.github.davemeier82.homeautomation.core.event.WindowTiltAngleUpdatedEvent;
import io.github.davemeier82.homeautomation.core.repositories.DevicePropertyRepository;
import io.github.davemeier82.homeautomation.hivemq.EventDto;

public class EventToDtoMapper {

  public final DevicePropertyRepository devicePropertyRepository;

  public EventToDtoMapper(DevicePropertyRepository devicePropertyRepository) {
    this.devicePropertyRepository = devicePropertyRepository;
  }

  // TODO replace switch
  public EventDto map(DevicePropertyEvent event) {
    return devicePropertyRepository.findByDevicePropertyId(event.getDevicePropertyId()).map(deviceProperty -> switch (event) {
      case RelayStateChangedEvent e -> toEvent(deviceProperty, e, "RelayStateChangedEvent");
      case TemperatureChangedEvent e -> toEvent(deviceProperty, e, "TemperatureChangedEvent");
      case HumidityChangedEvent e -> toEvent(deviceProperty, e, "HumidityChangedEvent");
      case DimmingLevelChangedEvent e -> toEvent(deviceProperty, e, "DimmingLevelChangedEvent");
      case IlluminanceChangedEvent e -> toEvent(deviceProperty, e, "IlluminanceChangedEvent");
      case RollerStateChangedEvent e -> toEvent(deviceProperty, e, "RollerStateChangedEvent");
      case RollerPositionChangedEvent e -> toEvent(deviceProperty, e, "RollerPositionChangedEvent");
      case BatteryLevelChangedEvent e -> toEvent(deviceProperty, e, "BatteryLevelChangedEvent");
      case PowerChangedEvent e -> toEvent(deviceProperty, e, "PowerChangedEvent");
      case WindowStateChangedEvent e -> toEvent(deviceProperty, e, "WindowStateChangedEvent");
      case MotionChangedEvent e -> toEvent(deviceProperty, e, "MotionChangedEvent");
      case SmokeStateChangedEvent e -> toEvent(deviceProperty, e, "SmokeStateChangedEvent");
      case Co2LevelChangedEvent e -> toEvent(deviceProperty, e, "Co2LevelChangedEvent");
      case AlarmStateChangedEvent e -> toEvent(deviceProperty, e, "AlarmStateChangedEvent");
      case PressureChangedEvent e -> toEvent(deviceProperty, e, "PressureChangedEvent");
      case UvIndexChangedEvent e -> toEvent(deviceProperty, e, "UvIndexChangedEvent");
      case CloudBaseChangedEvent e -> toEvent(deviceProperty, e, "CloudBaseChangedEvent");
      case WindSpeedChangedEvent e -> toEvent(deviceProperty, e, "WindSpeedChangedEvent");
      case WindGustSpeedChangedEvent e -> toEvent(deviceProperty, e, "WindGustSpeedChangedEvent");
      case WindDirectionChangedEvent e -> toEvent(deviceProperty, e, "WindDirectionChangedEvent");
      case WindGustDirectionChangedEvent e -> toEvent(deviceProperty, e, "WindGustDirectionChangedEvent");
      case WindRunChangedEvent e -> toEvent(deviceProperty, e, "WindRunChangedEvent");
      case RainIntervalAmountChangedEvent e -> toEvent(deviceProperty, e, "RainIntervalAmountChangedEvent");
      case RainTodayAmountChangedEvent e -> toEvent(deviceProperty, e, "RainTodayAmountChangedEvent");
      case RainRateChangedEvent e -> toEvent(deviceProperty, e, "RainRateChangedEvent");
      case RelayStateUpdatedEvent e -> toEvent(deviceProperty, e, "RelayStateUpdatedEvent");
      case TemperatureUpdatedEvent e -> toEvent(deviceProperty, e, "TemperatureUpdatedEvent");
      case HumidityUpdatedEvent e -> toEvent(deviceProperty, e, "HumidityUpdatedEvent");
      case DimmingLevelUpdatedEvent e -> toEvent(deviceProperty, e, "DimmingLevelUpdatedEvent");
      case IlluminanceUpdatedEvent e -> toEvent(deviceProperty, e, "IlluminanceUpdatedEvent");
      case RollerStateUpdatedEvent e -> toEvent(deviceProperty, e, "RollerStateUpdatedEvent");
      case RollerPositionUpdatedEvent e -> toEvent(deviceProperty, e, "RollerPositionUpdatedEvent");
      case BatteryLevelUpdatedEvent e -> toEvent(deviceProperty, e, "BatteryLevelUpdatedEvent");
      case PowerUpdatedEvent e -> toEvent(deviceProperty, e, "PowerUpdatedEvent");
      case WindowStateUpdatedEvent e -> toEvent(deviceProperty, e, "WindowStateUpdatedEvent");
      case MotionUpdatedEvent e -> toEvent(deviceProperty, e, "MotionUpdatedEvent");
      case SmokeStateUpdatedEvent e -> toEvent(deviceProperty, e, "SmokeStateUpdatedEvent");
      case Co2LevelUpdatedEvent e -> toEvent(deviceProperty, e, "Co2LevelUpdatedEvent");
      case AlarmStateUpdatedEvent e -> toEvent(deviceProperty, e, "AlarmStateUpdatedEvent");
      case PressureUpdatedEvent e -> toEvent(deviceProperty, e, "PressureUpdatedEvent");
      case UvIndexUpdatedEvent e -> toEvent(deviceProperty, e, "UvIndexUpdatedEvent");
      case CloudBaseUpdatedEvent e -> toEvent(deviceProperty, e, "CloudBaseUpdatedEvent");
      case WindSpeedUpdatedEvent e -> toEvent(deviceProperty, e, "WindSpeedUpdatedEvent");
      case WindGustSpeedUpdatedEvent e -> toEvent(deviceProperty, e, "WindGustSpeedUpdatedEvent");
      case WindDirectionUpdatedEvent e -> toEvent(deviceProperty, e, "WindDirectionUpdatedEvent");
      case WindGustDirectionUpdatedEvent e -> toEvent(deviceProperty, e, "WindGustDirectionUpdatedEvent");
      case WindRunUpdatedEvent e -> toEvent(deviceProperty, e, "WindRunUpdatedEvent");
      case RainIntervalAmountUpdatedEvent e -> toEvent(deviceProperty, e, "RainIntervalAmountUpdatedEvent");
      case RainTodayAmountUpdatedEvent e -> toEvent(deviceProperty, e, "RainTodayAmountUpdatedEvent");
      case RainRateUpdatedEvent e -> toEvent(deviceProperty, e, "RainRateUpdatedEvent");
      case WindowTiltAngleChangedEvent e -> toEvent(deviceProperty, e, "WindowTiltAngleChangedEvent");
      case WindowTiltAngleUpdatedEvent e -> toEvent(deviceProperty, e, "WindowTiltAngleUpdatedEvent");
      default -> null;
    }).orElseThrow(() -> new IllegalArgumentException("event not supported " + event.getClass().getSimpleName()));
  }

  private EventDto toEvent(DeviceProperty deviceProperty,
                           DevicePropertyEvent event,
                                  String propertyType
  ) {
    DevicePropertyId devicePropertyId = deviceProperty.getId();
    return new EventDto(devicePropertyId.deviceId().type().getTypeName(),
        devicePropertyId.deviceId().id(),
        propertyType,
        deviceProperty.getDisplayName(),
        devicePropertyId.id(),
        event.getNewValue(),
        event.getPreviousValue().orElse(null),
        event.getNewTimestamp(),
        event.getPreviousTimestamp().orElse(null)
    );
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy