<commit_before>/**
 * Copyright (c) 2010-2013, openHAB.org and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
package org.openhab.binding.maxcube.internal.message;

import java.util.ArrayList;

import org.apache.commons.codec.binary.Base64;
import org.openhab.binding.maxcube.internal.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * The M message contains metadata about the MAX!Cube setup. 
 * 
 * @author <NAME> (<EMAIL>)
 * @since 1.4.0
 */
public final class M_Message extends Message {

	public ArrayList<RoomInformation> rooms;
	public ArrayList<DeviceInformation> devices;


	public M_Message(String raw) {
		super(raw);

		try {
			String[] tokens = this.getPayload().split(Message.DELIMETER);
			byte[] bytes = Base64.decodeBase64(tokens[2].getBytes());

			rooms = new ArrayList<RoomInformation>();
			devices = new ArrayList<DeviceInformation>();

			int roomCount = bytes[2];

			int byteOffset = 3; // start of rooms

			/* process room */

			for (int i = 0; i < roomCount; i++) {

				int position = bytes[byteOffset++];
				String name = "";

				int nameLength = (int) bytes[byteOffset++] & 0xff; 
				for (int char_idx = 0; char_idx < nameLength; char_idx++) {
					name += (char) bytes[byteOffset++];
				}

				String rfAddress = Utils.toHex(((int)bytes[byteOffset] & 0xff), ((int)bytes[byteOffset+1] & 0xff), ((int)bytes[byteOffset + 2] & 0xff));
				byteOffset += 3;

				rooms.add(new RoomInformation(position, name, rfAddress));
			}

			/* process devices */

			int deviceCount = bytes[byteOffset++];

			for (int deviceId = 0; deviceId < deviceCount; deviceId++) {
				DeviceType deviceType = DeviceType.create(bytes[byteOffset++]);

				String rfAddress = Utils.toHex(((int)bytes[byteOffset]&0xff), ((int)bytes[byteOffset+1]&0xff), ((int)bytes[byteOffset+2]&0xff));
				byteOffset += 3;

				String serialNumber = "";

				for (int i = 0; i < 10; i++) {
					serialNumber += (char) bytes[byteOffset++];
				}

				int nameLength = (int)bytes[byteOffset++] & 0xff;

				String deviceName = "";

				for (int char_idx = 0;	 char_idx < nameLength; char_idx++) {
					deviceName += (char)bytes[byteOffset++];
				}

				int roomId = (int)bytes[byteOffset++] & 0xff;

				devices.add(new DeviceInformation(deviceType, serialNumber, rfAddress, deviceName, roomId));	


			}
		}  catch (Exception e) {
			Logger logger = LoggerFactory.getLogger(M_Message.class);
			logger.info("Unknown error parsing the M Message");
			logger.info(e.getMessage());
			logger.debug(Utils.getStackTrace(e));
			logger.debug("\tRAW : {}", this.getPayload());
		}

	}

	@Override<commit_msg>Improved errorhandling for MaxCube without configuration<commit_after>/**
 * Copyright (c) 2010-2013, openHAB.org and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
package org.openhab.binding.maxcube.internal.message;

import java.util.ArrayList;

import org.apache.commons.codec.binary.Base64;
import org.openhab.binding.maxcube.internal.MaxCubeBinding;
import org.openhab.binding.maxcube.internal.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * The M message contains metadata about the MAX!Cube setup. 
 * 
 * @author <NAME> (<EMAIL>)
 * @since 1.4.0
 */
public final class M_Message extends Message {

	public ArrayList<RoomInformation> rooms;
	public ArrayList<DeviceInformation> devices;
	Logger logger = LoggerFactory.getLogger(MaxCubeBinding.class);
	

	public M_Message(String raw) {
		super(raw);

		String[] tokens = this.getPayload().split(Message.DELIMETER);

		if (tokens.length > 1) try {
			byte[] bytes = Base64.decodeBase64(tokens[2].getBytes());

			rooms = new ArrayList<RoomInformation>();
			devices = new ArrayList<DeviceInformation>();

			int roomCount = bytes[2];

			int byteOffset = 3; // start of rooms

			/* process room */

			for (int i = 0; i < roomCount; i++) {

				int position = bytes[byteOffset++];
				String name = "";

				int nameLength = (int) bytes[byteOffset++] & 0xff; 
				for (int char_idx = 0; char_idx < nameLength; char_idx++) {
					name += (char) bytes[byteOffset++];
				}

				String rfAddress = Utils.toHex(((int)bytes[byteOffset] & 0xff), ((int)bytes[byteOffset+1] & 0xff), ((int)bytes[byteOffset + 2] & 0xff));
				byteOffset += 3;

				rooms.add(new RoomInformation(position, name, rfAddress));
			}

			/* process devices */

			int deviceCount = bytes[byteOffset++];

			for (int deviceId = 0; deviceId < deviceCount; deviceId++) {
				DeviceType deviceType = DeviceType.create(bytes[byteOffset++]);

				String rfAddress = Utils.toHex(((int)bytes[byteOffset]&0xff), ((int)bytes[byteOffset+1]&0xff), ((int)bytes[byteOffset+2]&0xff));
				byteOffset += 3;

				String serialNumber = "";

				for (int i = 0; i < 10; i++) {
					serialNumber += (char) bytes[byteOffset++];
				}

				int nameLength = (int)bytes[byteOffset++] & 0xff;

				String deviceName = "";

				for (int char_idx = 0;	 char_idx < nameLength; char_idx++) {
					deviceName += (char)bytes[byteOffset++];
				}

				int roomId = (int)bytes[byteOffset++] & 0xff;

				devices.add(new DeviceInformation(deviceType, serialNumber, rfAddress, deviceName, roomId));	


			}
		}  catch (Exception e) {
			logger.info("Unknown error parsing the M Message");
			logger.info(e.getMessage());
			logger.debug(Utils.getStackTrace(e));
			logger.debug("\tRAW : {}", this.getPayload());
		}
		else {
			logger.info("No rooms defined. Configure your Max!Cube");
		} 
	}

	@Override
{'fraction_non_alphanumeric': 0.14422093419706786, 'fraction_numerical': 0.016535969996590523, 'mean_word_length': 3.9261125104953822, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2283434', 'n_tokens_mistral': 2186, 'n_tokens_neox': 1834, 'n_words': 458}