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

com.geotab.model.serialization.DeviceDeserializer Maven / Gradle / Ivy

/*
 *
 * 2020 Copyright (C) Geotab Inc. All rights reserved.
 */

package com.geotab.model.serialization;

import static com.geotab.model.entity.device.Device.HISTORIC_SERIAL_NUMBER;
import static com.geotab.model.entity.device.Device.UNTRACKED_ASSET_PRODUCT_ID;
import static com.geotab.model.enumeration.DeviceTypeNameConstants.CUSTOM_DEVICE;
import static com.geotab.model.enumeration.DeviceTypeNameConstants.CUSTOM_VEHICLE_DEVICE;
import static com.geotab.model.enumeration.DeviceTypeNameConstants.GO2;
import static com.geotab.model.enumeration.DeviceTypeNameConstants.GO3;
import static com.geotab.model.enumeration.DeviceTypeNameConstants.GO4;
import static com.geotab.model.enumeration.DeviceTypeNameConstants.GO4V3;
import static com.geotab.model.enumeration.DeviceTypeNameConstants.GO5;
import static com.geotab.model.enumeration.DeviceTypeNameConstants.GO6;
import static com.geotab.model.enumeration.DeviceTypeNameConstants.GO7;
import static com.geotab.model.enumeration.DeviceTypeNameConstants.GO8;
import static com.geotab.model.enumeration.DeviceTypeNameConstants.GO9;
import static com.geotab.model.enumeration.DeviceTypeNameConstants.GO_DRIVE_DEVICE;
import static com.geotab.model.enumeration.DeviceTypeNameConstants.NONE;
import static com.geotab.model.enumeration.DeviceTypeNameConstants.OLD_GEOTAB;
import static com.geotab.model.serialization.ApiCustomDeserializerModifier.defaultDeserialize;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.geotab.model.entity.device.CustomDevice;
import com.geotab.model.entity.device.CustomVehicleDevice;
import com.geotab.model.entity.device.Device;
import com.geotab.model.entity.device.Go4v3;
import com.geotab.model.entity.device.Go5;
import com.geotab.model.entity.device.Go6;
import com.geotab.model.entity.device.Go7;
import com.geotab.model.entity.device.Go8;
import com.geotab.model.entity.device.Go9;
import com.geotab.model.entity.device.GoDriveDevice;
import com.geotab.model.entity.device.GoLegacy;
import com.geotab.model.entity.device.NoDevice;
import com.geotab.model.entity.device.UntrackedAsset;
import com.geotab.model.enumeration.DeviceType;
import java.io.IOException;
import org.apache.commons.lang3.StringUtils;

public class DeviceDeserializer extends JsonDeserializer {

  private static final String DEVICE_TYPE_ATTRIBUTE_NAME = "deviceType";
  private static final String SERIAL_NUMBER_ATTRIBUTE_NAME = "serialNumber";
  private static final String PRODUCT_ID_ATTRIBUTE_NAME = "productId";

  @Override
  public Device deserialize(JsonParser jsonParser, DeserializationContext context)
      throws IOException {
    ObjectCodec parserCodec = jsonParser.getCodec();
    JsonNode node = parserCodec.readTree(jsonParser);

    if (node.isTextual()) {
      return NoDevice.getInstance();
    }

    if (node.isObject()) {
      if (hasDeviceType(node)) {
        return deserializeDeviceByDeviceType(parserCodec, context, node);
      }
      return deserializeDeviceBySerialNumberAndProductId(parserCodec, context, node);
    }

    return null;
  }

  private boolean hasDeviceType(JsonNode node) {
    return node.get(DEVICE_TYPE_ATTRIBUTE_NAME) != null
        && !node.get(DEVICE_TYPE_ATTRIBUTE_NAME).isNull()
        && DeviceType.findByName(node.get(DEVICE_TYPE_ATTRIBUTE_NAME).textValue()) != null;
  }

  private Device deserializeDeviceByDeviceType(ObjectCodec parserCodec,
      DeserializationContext context, JsonNode node) throws IOException {
    String deviceType = node.get(DEVICE_TYPE_ATTRIBUTE_NAME).asText();

    switch (deviceType) {
      case OLD_GEOTAB:
      case GO2:
      case GO3:
      case GO4:
        return defaultDeserialize(parserCodec, context, node, GoLegacy.class);
      case GO4V3:
        return defaultDeserialize(parserCodec, context, node, Go4v3.class);
      case GO5:
        return defaultDeserialize(parserCodec, context, node, Go5.class);
      case GO6:
        return defaultDeserialize(parserCodec, context, node, Go6.class);
      case GO7:
        return defaultDeserialize(parserCodec, context, node, Go7.class);
      case GO8:
        return defaultDeserialize(parserCodec, context, node, Go8.class);
      case GO9:
        return defaultDeserialize(parserCodec, context, node, Go9.class);
      case GO_DRIVE_DEVICE:
        return defaultDeserialize(parserCodec, context, node, GoDriveDevice.class);
      case CUSTOM_DEVICE:
        return defaultDeserialize(parserCodec, context, node, CustomDevice.class);
      case CUSTOM_VEHICLE_DEVICE:
        return defaultDeserialize(parserCodec, context, node, CustomVehicleDevice.class);
      case NONE:
        return defaultDeserialize(parserCodec, context, node, UntrackedAsset.class);
      default:
        return defaultDeserialize(parserCodec, context, node, Device.class);
    }
  }

  private Device deserializeDeviceBySerialNumberAndProductId(ObjectCodec parserCodec,
      DeserializationContext context,
      JsonNode node) throws IOException {

    if (node.get(SERIAL_NUMBER_ATTRIBUTE_NAME) == null
        || node.get(SERIAL_NUMBER_ATTRIBUTE_NAME).isNull()
        || !node.get(SERIAL_NUMBER_ATTRIBUTE_NAME).isTextual()) {
      return defaultDeserialize(parserCodec, context, node, Device.class);
    }

    int productId;
    String serialNumber = node.get(SERIAL_NUMBER_ATTRIBUTE_NAME).asText();

    if (StringUtils.isEmpty(serialNumber)) {
      productId = UNTRACKED_ASSET_PRODUCT_ID;
    } else if (HISTORIC_SERIAL_NUMBER.equals(serialNumber)
        && node.get(PRODUCT_ID_ATTRIBUTE_NAME) != null
        && !node.get(PRODUCT_ID_ATTRIBUTE_NAME).isNull()) {
      productId = node.get(PRODUCT_ID_ATTRIBUTE_NAME).asInt();
    } else {
      productId = Device.productIdFromSerialNumber(serialNumber);
    }

    Class deviceType = Device
        .fromDeviceType(Device.deviceTypeFromProductId(productId));
    if (deviceType == null) {
      return null;
    }

    Device device = defaultDeserialize(parserCodec, context, node, deviceType);
    if (device != null && device.getProductId() == null) {
      device.setProductId(productId);
    }

    return device;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy