public static byte[] getPayload(DlmsType type, byte[] data) { int offset = type.size == 0 ? getOffset(data) : 1; int size = type.size == 0 ? getSize(data) : type.size; return(helper.extensions.copyOfRange(data, offset, offset + size)); }
/// <summary> /// Retrieves the Time String representation of the element in the array of bytes </summary> /// <param name="data"> array of bytes containing data result from a dlms-get </param> /// <returns> A Strig that represents the time in the data </returns> /// <exception cref="DlmsException"> </exception> public static string getTimeString(byte[] data) { verify(data); DlmsType type = DlmsType.fromTag(data[0]); return(DlmsParser.getTimeStringValue(getPayload(type, data))); }
private static int getNumberOfItems(DlmsType type, byte[] data) { if (type.Equals(DlmsType.ARRAY) || type.Equals(DlmsType.STRUCTURE)) { return(getSize(data)); } return(0); }
/// <summary> /// Retrieves the String representation of the raw bytes. /// The string will include the type and the size of the element parsed. </summary> /// <param name="data"> array of bytes containing data result from a dlms-get </param> /// <returns> A String that represents the data </returns> /// <exception cref="DlmsException"> </exception> public static DlmsItem getDlmsItem(byte[] data) { verify(data); DlmsType type = DlmsType.fromTag(data[0]); DlmsItem item = new DlmsItem(type, getString(data)); int numberOfItems = getNumberOfItems(type, data); for (int i = 0; i < numberOfItems; ++i) { parseItems(item, getNextData(data)); } return(item); }
public static byte[] pack(DlmsType type, byte[] data) { int size = type.size == 0 ? data.Length : type.size; //TODO size > 1 byte int offset = type.size == 0 ? 1 : 0; byte[] retval = new byte[data.Length + 1 + offset]; retval[0] = type.tag; if (offset != 0) { retval[1] = (byte)data.Length; } Array.Copy(data, 0, retval, offset + 1, data.Length); return(retval); }
private static byte[] getNextData(byte[] data) { if (data == null || data.Length == 0) { return(new byte[0]); } DlmsType type = DlmsType.fromTag(data[0]); int offset; if (getNumberOfItems(type, data) == 0) { offset = type.size == 0 ? getSize(data) + getOffset(data) : type.size + 1; return(helper.extensions.copyOfRange(data, offset, data.Length)); } offset = getOffset(data); return(helper.extensions.copyOfRange(data, offset, data.Length)); }
private static byte[] parseItems(DlmsItem parent, byte[] data) { if (data.Length == 0) { return(data); } DlmsType type = DlmsType.fromTag(data[0]); DlmsItem item = new DlmsItem(type, getString(data)); parent.addChildren(item); int numberOfItems = getNumberOfItems(type, data); for (int i = 0; i < numberOfItems; ++i) { parseItems(item, getNextData(data)); } return(data); }
public static byte[] getByteValue(DlmsType type, string value) { switch (type.innerEnumValue) { case yadi.dlms.DlmsType.InnerEnum.ARRAY: case yadi.dlms.DlmsType.InnerEnum.BCD: case yadi.dlms.DlmsType.InnerEnum.BITSTRING: case yadi.dlms.DlmsType.InnerEnum.BOOLEAN: case yadi.dlms.DlmsType.InnerEnum.ENUM: case yadi.dlms.DlmsType.InnerEnum.OCTET_STRING: case yadi.dlms.DlmsType.InnerEnum.STRUCTURE: return(pack(type, hexStringToBytes(value))); case yadi.dlms.DlmsType.InnerEnum.DATE: //TODO return getDateStringValue(payload); case yadi.dlms.DlmsType.InnerEnum.DATE_TIME: //TODO return getDateTimeStringValue(payload); case yadi.dlms.DlmsType.InnerEnum.FLOAT32: //TODO return pack(type, ); case yadi.dlms.DlmsType.InnerEnum.FLOAT64: //TODO return Double.toString(ByteBuffer.wrap(payload).getDouble()); case yadi.dlms.DlmsType.InnerEnum.INT16: case yadi.dlms.DlmsType.InnerEnum.INT32: case yadi.dlms.DlmsType.InnerEnum.INT64: case yadi.dlms.DlmsType.InnerEnum.INT8: return(pack(type, getIntegerBytes(type, value))); case yadi.dlms.DlmsType.InnerEnum.STRING: return(pack(type, Encoding.ASCII.GetBytes(value))); case yadi.dlms.DlmsType.InnerEnum.TIME: //TODO return getTimeStringValue(payload); case yadi.dlms.DlmsType.InnerEnum.UINT16: case yadi.dlms.DlmsType.InnerEnum.UINT32: case yadi.dlms.DlmsType.InnerEnum.UINT64: case yadi.dlms.DlmsType.InnerEnum.UINT8: return(pack(type, getIntegerBytes(type, value))); case yadi.dlms.DlmsType.InnerEnum.UTF8_STRING: return(pack(type, Encoding.UTF8.GetBytes(value))); } throw new DlmsException(DlmsExceptionReason.NO_SUCH_TYPE); }
private static byte[] getIntegerBytes(DlmsType type, string str) { byte[] bytes = new byte[type.size]; bytes[0] = type.tag; int val = 0; try { val = int.Parse(str); } catch (Exception) { val = 0; } for (int i = 0; i < type.size; ++i) { bytes[type.size - i] = (byte)(val & 0x00FF); val = (int)((uint)val >> 8); } return(bytes); }
/// <summary> /// Retrieves the DlmsType from an array of bytes </summary> /// <param name="data"> array of bytes containing data result from a dlms-get </param> /// <returns> The DlmsType of the data </returns> /// <exception cref="DlmsException"> </exception> public static DlmsType getTypeFromRawBytes(byte[] data) { verify(data); return(DlmsType.fromTag(data[0])); }
private static string getStringValue(DlmsType type, byte[] payload) { switch (type.innerEnumValue) { case yadi.dlms.DlmsType.InnerEnum.ARRAY: return(bytesToHex(payload)); case yadi.dlms.DlmsType.InnerEnum.BCD: return(bytesToHex(payload)); case yadi.dlms.DlmsType.InnerEnum.BITSTRING: return(bytesToHex(payload)); case yadi.dlms.DlmsType.InnerEnum.BOOLEAN: return(bytesToHex(payload)); case yadi.dlms.DlmsType.InnerEnum.DATE: return(getDateStringValue(payload)); case yadi.dlms.DlmsType.InnerEnum.DATE_TIME: return(getDateTimeStringValue(payload)); case yadi.dlms.DlmsType.InnerEnum.ENUM: return(bytesToHex(payload)); case yadi.dlms.DlmsType.InnerEnum.FLOAT32: Array.Reverse(payload); return(Convert.ToString(BitConverter.ToSingle(payload, 0), new System.Globalization.CultureInfo("en-US"))); case yadi.dlms.DlmsType.InnerEnum.FLOAT64: Array.Reverse(payload); return(Convert.ToString(BitConverter.ToDouble(payload, 0), new System.Globalization.CultureInfo("en-US"))); case yadi.dlms.DlmsType.InnerEnum.INT16: Array.Reverse(payload); return(Convert.ToString(BitConverter.ToInt16(payload, 0))); case yadi.dlms.DlmsType.InnerEnum.INT32: Array.Reverse(payload); return(Convert.ToString(BitConverter.ToInt32(payload, 0))); case yadi.dlms.DlmsType.InnerEnum.INT64: Array.Reverse(payload); return(Convert.ToString(BitConverter.ToInt64(payload, 0))); case yadi.dlms.DlmsType.InnerEnum.INT8: return(Convert.ToString((sbyte)payload[0])); case yadi.dlms.DlmsType.InnerEnum.OCTET_STRING: return(bytesToHex(payload)); case yadi.dlms.DlmsType.InnerEnum.STRING: return(new String(Encoding.ASCII.GetChars(payload))); case yadi.dlms.DlmsType.InnerEnum.STRUCTURE: return(bytesToHex(payload)); case yadi.dlms.DlmsType.InnerEnum.TIME: return(getTimeStringValue(payload)); case yadi.dlms.DlmsType.InnerEnum.UINT16: Array.Reverse(payload); return(Convert.ToString(BitConverter.ToUInt16(payload, 0))); case yadi.dlms.DlmsType.InnerEnum.UINT32: Array.Reverse(payload); return(Convert.ToString(BitConverter.ToUInt32(payload, 0))); case yadi.dlms.DlmsType.InnerEnum.UINT64: Array.Reverse(payload); return(Convert.ToString(BitConverter.ToUInt64(payload, 0))); case yadi.dlms.DlmsType.InnerEnum.UINT8: return(Convert.ToString(payload[0])); case yadi.dlms.DlmsType.InnerEnum.UTF8_STRING: return(new String(Encoding.UTF8.GetChars(payload))); } throw new DlmsException(DlmsExceptionReason.NO_SUCH_TYPE); }
/// <summary> /// Retrieves the String representation of the element in the array of bytes </summary> /// <param name="type"> the DlmsType of the byte array </param> /// <param name="data"> array of bytes containing data result from a dlms-get </param> /// <returns> A Strig that represents the element in the data </returns> /// <exception cref="DlmsException"> </exception> public static string getString(DlmsType type, byte[] payload) { return(DlmsParser.getStringValue(type, payload)); }
public DlmsItem(DlmsType type, string value) { this.type = type; this.value = value; }