/**
 * Copyright (c) 2010-2014, 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.zwave.internal.converter;

import java.util.Map;

import org.openhab.binding.zwave.internal.converter.command.ZWaveCommandConverter;
import org.openhab.binding.zwave.internal.converter.state.IntegerDecimalTypeConverter;
import org.openhab.binding.zwave.internal.converter.state.IntegerOnOffTypeConverter;
import org.openhab.binding.zwave.internal.converter.state.IntegerOpenClosedTypeConverter;
import org.openhab.binding.zwave.internal.converter.state.IntegerPercentTypeConverter;
import org.openhab.binding.zwave.internal.converter.state.ZWaveStateConverter;
import org.openhab.binding.zwave.internal.protocol.SerialMessage;
import org.openhab.binding.zwave.internal.protocol.ZWaveController;
import org.openhab.binding.zwave.internal.protocol.ZWaveNode;
import org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveAlarmCommandClass;
import org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveAlarmCommandClass.AlarmType;
import org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveAlarmCommandClass.ZWaveAlarmValueEvent;
import org.openhab.binding.zwave.internal.protocol.event.ZWaveCommandClassValueEvent;
import org.openhab.core.events.EventPublisher;
import org.openhab.core.items.Item;
import org.openhab.core.types.Command;
import org.openhab.core.types.State;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/***
 * ZWaveAlarmConverter class. Converter for communication with the 
 * {@link ZWaveAlarmCommandClass}. Implements polling of the alarm 
 * status and receiving of alarm  events.
 * @author Chris Jackson
 * @since 1.6.0
 */
public class ZWaveAlarmConverter extends ZWaveCommandClassConverter<ZWaveAlarmCommandClass> {

	private static final Logger logger = LoggerFactory.getLogger(ZWaveAlarmConverter.class);
	private static final int REFRESH_INTERVAL = 0; // refresh interval in seconds for the binary switch;

	/**
	 * Constructor. Creates a new instance of the {@link ZWaveAlarmConverter} class.
	 * @param controller the {@link ZWaveController} to use for sending messages.
	 * @param eventPublisher the {@link EventPublisher} to use to publish events.
	 */
	public ZWaveAlarmConverter(ZWaveController controller, EventPublisher eventPublisher) {
		super(controller, eventPublisher);
		
		// State and commmand converters used by this converter. 
		this.addStateConverter(new IntegerDecimalTypeConverter());
		this.addStateConverter(new IntegerPercentTypeConverter());
		this.addStateConverter(new IntegerOnOffTypeConverter());
		this.addStateConverter(new IntegerOpenClosedTypeConverter());
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void executeRefresh(ZWaveNode node, 
			ZWaveAlarmCommandClass commandClass, int endpointId, Map<String,String> arguments) {
		logger.debug("Generating poll message for {} for node {} endpoint {}", commandClass.getCommandClass().getLabel(), node.getNodeId(), endpointId);
		SerialMessage serialMessage;
		String alarmType = arguments.get("alarm_type");
		
		if (alarmType != null) {
			serialMessage = node.encapsulate(commandClass.getMessage(AlarmType.getAlarmType(Integer.parseInt(alarmType))), commandClass, endpointId);
		} else {
			serialMessage = node.encapsulate(commandClass.getValueMessage(), commandClass, endpointId);
		}
			
		if (serialMessage == null) {
			logger.warn("Generating message failed for command class = {}, node = {}, endpoint = {}", commandClass.getCommandClass().getLabel(), node.getNodeId(), endpointId);
			return;
		}
		
		this.getController().sendData(serialMessage);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void handleEvent(ZWaveCommandClassValueEvent event, Item item, Map<String,String> arguments) {
		ZWaveStateConverter<?,?> converter = this.getStateConverter(item, event.getValue());
		String alarmType = arguments.get("alarm_type");
		ZWaveAlarmValueEvent alarmEvent = (ZWaveAlarmValueEvent)event;
		
		if (converter == null) {
			logger.warn("No converter found for item = {}, node = {} endpoint = {}, ignoring event.", item.getName(), event.getNodeId(), event.getEndpoint());
			return;
		}

		// Don't trigger event if this item is bound to another alarm type
		if (alarmType != null && AlarmType.getAlarmType(Integer.parseInt(alarmType)) != alarmEvent.getAlarmType())
			return;
		
		State state = converter.convertFromValueToState(event.getValue());
		this.getEventPublisher().postUpdate(item.getName(), state);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void receiveCommand(Item item, Command command, ZWaveNode node,
			ZWaveAlarmCommandClass commandClass, int endpointId, Map<String,String> arguments) {
		ZWaveCommandConverter<?,?> converter = this.getCommandConverter(command.getClass());
		
		if (converter == null) {
			logger.warn("No converter found for item = {}, node = {} endpoint = {}, ignoring command.", item.getName(), node.getNodeId(), endpointId);
			return;
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	int getRefreshInterval() {
		return REFRESH_INTERVAL;
	}
}

@@ -3077,16 +3077,25 @@
 .debug(%22
+NODE %7B%7D: 
 Generati
@@ -3120,20 +3120,9 @@
 r %7B%7D
- for node %7B%7D
+,
  end
@@ -3127,24 +3127,42 @@
 ndpoint %7B%7D%22,
+ node.getNodeId(),
  commandClas
@@ -3192,34 +3192,16 @@
 Label(),
- node.getNodeId(),
  endpoin
@@ -3619,16 +3619,25 @@
 r.warn(%22
+NODE %7B%7D: 
 Generati
@@ -3681,19 +3681,8 @@
  %7B%7D,
- node = %7B%7D,
  end
@@ -3693,16 +3693,34 @@
 t = %7B%7D%22,
+ node.getNodeId(),
  command
@@ -3754,34 +3754,16 @@
 Label(),
- node.getNodeId(),
  endpoin
@@ -4220,32 +4220,41 @@
 %09%09%09logger.warn(%22
+NODE %7B%7D: 
 No converter fou
@@ -4266,34 +4266,24 @@
 r item = %7B%7D,
- node = %7B%7D
  endpoint = 
@@ -4308,39 +4308,39 @@
 .%22, 
-item.getName(), event.getNodeId
+event.getNodeId(), item.getName
 (), 
@@ -5036,16 +5036,25 @@
 r.warn(%22
+NODE %7B%7D: 
 No conve
@@ -5082,18 +5082,8 @@
  %7B%7D,
- node = %7B%7D
  end
@@ -5118,38 +5118,38 @@
 .%22, 
-item
+node
 .getN
-ame(), node.getNodeId
+odeId(), item.getName
 (), 

Update debug messages