Package com.orange.zstack.service.layer.simpleapi.parameter

Source Code of com.orange.zstack.service.layer.simpleapi.parameter.Param

package com.orange.zstack.service.layer.simpleapi.parameter;

import java.util.HashMap;

import com.orange.zstack.service.constant.DeviceState;
import com.orange.zstack.service.element.zigbee.ExtendedPanId;
import com.orange.zstack.service.element.zigbee.IEEEAddress;
import com.orange.zstack.service.element.zigbee.PanId;
import com.orange.zstack.service.element.zigbee.ShortAddress;
import com.orange.zstack.service.utils.FrameTools;

public class Param {
  public static final Param DEVICE_STATE = new Param((byte)0, DeviceState.class.getName());
  public static final Param DEVICE_IEEE_ADDRESS = new Param((byte)1, IEEEAddress.class.getName());
  public static final Param DEVICE_SHORT_ADDRESS = new Param((byte)2, ShortAddress.class.getName());
  public static final Param PARENT_SHORT_ADDRESS = new Param((byte)3, ShortAddress.class.getName());
  public static final Param PARENT_IEEE_ADDRESS = new Param((byte)4, IEEEAddress.class.getName());
  public static final Param CHANNEL = new Param((byte)5, Integer.class.getName());
  public static final Param PAN_ID = new Param((byte)6, PanId.class.getName());
  public static final Param EXT_PAN_ID = new Param((byte)7, ExtendedPanId.class.getName());

  private static HashMap validValues = new HashMap() {{
    put(new Byte(DEVICE_STATE.getValue()), DEVICE_STATE);
    put(new Byte(DEVICE_IEEE_ADDRESS.getValue()), DEVICE_IEEE_ADDRESS);
    put(new Byte(DEVICE_SHORT_ADDRESS.getValue()), DEVICE_SHORT_ADDRESS);
    put(new Byte(PARENT_SHORT_ADDRESS.getValue()), PARENT_SHORT_ADDRESS);
    put(new Byte(PARENT_IEEE_ADDRESS.getValue()), PARENT_IEEE_ADDRESS);
    put(new Byte(CHANNEL.getValue()), CHANNEL);
    put(new Byte(PAN_ID.getValue()), PAN_ID);
    put(new Byte(EXT_PAN_ID.getValue()), EXT_PAN_ID);
  }};
 
  /** parameter value */
  private byte value;
  /** response object type */
  String response;
 
  private Param(byte value, String response) {
    this.value = value;
    this.response = response;
  }
 
  /**
   * get Param from value
   * @param value the value of the Param
   * @return
   * @throws IllegalArgumentException if invalid value
   */
  private static Param getValue(byte value) throws IllegalArgumentException {
    Param param = (Param)validValues.get(new Byte(value));
   
    if (param == null) {
      throw new IllegalArgumentException("invalid Param value: " + FrameTools.byteToHexString(value));
    }
    return param;
  }

  public byte getValue() {
    return value;
  }
 
  public String toString() {
    return Byte.toString(value);
  }

  /**
   * get response object according to the entry Param
   * @param value frame containing Param in first, and result in the next bytes
   * @return DeviceState, IEEEAddress, ShortAddress, Integer, PanId, ExtendedPanId or null
   */
  public static Object getResponse(byte[] value) {
    Param param = Param.getValue(value[0]);
   
    if (param.response.equals(DeviceState.class.getName())) {
      return DeviceState.getValue(value);
    }
   
    if (param.response.equals(IEEEAddress.class.getName())) {
      return new IEEEAddress(value, 1);
    }
   
    if (param.response.equals(ShortAddress.class.getName())) {
      return new ShortAddress(value, 1);
    }
   
    if (param.response.equals(Integer.class.getName())) {
      return new Integer(FrameTools.unsignedByteToInt(value[1]));
    }
   
    if (param.response.equals(PanId.class.getName())) {
      return new PanId(value, 1);
    }
   
    if (param.response.equals(ExtendedPanId.class.getName())) {
      return new ExtendedPanId(value, 1);
    }
   
    return null;
  }
}
TOP

Related Classes of com.orange.zstack.service.layer.simpleapi.parameter.Param

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.