package de.t7soft.android.t7home.roomactivity;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;
import de.t7soft.android.t7home.R;
import de.t7soft.android.t7home.database.HomeDatabaseAdapter;
import de.t7soft.android.t7home.smarthome.api.SmartHomeSession;
import de.t7soft.android.t7home.smarthome.api.devices.LogicalDevice;
import de.t7soft.android.t7home.smarthome.api.exceptions.SmartHomeSessionExpiredException;

public class RoomActuatorChangeListener implements ActuatorChangeListener {

	private static final String LOGTAG = RoomActuatorChangeListener.class.getSimpleName();

	private final Context context;
	private final String sessionId;
	private final HomeDatabaseAdapter dbAdapter;

	public RoomActuatorChangeListener(final Context context, final String sessionId, final HomeDatabaseAdapter dbAdapter) {
		this.context = context;
		this.sessionId = sessionId;
		this.dbAdapter = dbAdapter;
	}

	@Override
	public void changed(final LogicalDevice device, final String newValue) {

		final String deviceType = device.getType();
		final String deviceId = device.getDeviceId();

		if (dbAdapter.canWrite()) {
			try {
				if (deviceType.equals(LogicalDevice.Type_RoomTemperatureActuatorState)) {
					final double temperature = Double.parseDouble(newValue);
					dbAdapter.updateRoomTemperatureActuator(device, temperature);
				} else if (deviceType.equals(LogicalDevice.Type_RollerShutterActuator)) {
					final int level = Integer.parseInt(newValue);
					dbAdapter.updateRollerShutterActuator(device, level);
				} else {
					Log.w(LOGTAG, "Unkown device type: " + deviceType);
					return;
				}
			} catch (final NumberFormatException ex) {
				Log.e(LOGTAG, "Can't update value in data base.", ex);
			}
		}

		final SmartHomeSession session = new SmartHomeSession(sessionId);
		try {
			if (deviceType.equals(LogicalDevice.Type_RoomTemperatureActuatorState)) {
				session.roomTemperatureActuatorChangeState(deviceId, newValue);
				// session.refreshLogicalDeviceState();
			} else if (deviceType.equals(LogicalDevice.Type_RollerShutterActuator)) {
				session.switchRollerShutter(deviceId, newValue);
				// session.refreshLogicalDeviceState();
			} else {
				Log.w(LOGTAG, "Unkown device type: " + deviceType);
				return;
			}
		} catch (final SmartHomeSessionExpiredException e) {
			e.printStackTrace();
			final int errorMsgResId = getErrorMessage(deviceType);
			if (errorMsgResId >= 0) {
				Toast.makeText(context, errorMsgResId, Toast.LENGTH_SHORT).show();
			}
		}

	}

	private static int getErrorMessage(final String deviceType) {
		if (deviceType.equals(LogicalDevice.Type_RoomTemperatureActuator)) {
			return R.string.temperature_set_error;
		} else if (deviceType.equals(LogicalDevice.Type_RollerShutterActuator)) {
			return R.string.shutter_set_error;
		}
		return -1;
	}

}

@@ -41,16 +41,107 @@
 ivity;%0A%0A
+import java.text.DecimalFormat;%0Aimport java.text.Format;%0Aimport java.text.ParseException;%0A%0A
 import a
@@ -164,16 +164,16 @@
 ontext;%0A
-
 import a
@@ -706,16 +706,93 @@
 ame();%0A%0A
+%09private static final Format TEMPERATURE_FORMAT = new DecimalFormat(%22#.#%22);%0A%0A
 %09private
@@ -1400,24 +1400,135 @@
 orState)) %7B%0A
+%09%09%09%09%09Object parsedValue = TEMPERATURE_FORMAT.parseObject(newValue);%0A%09%09%09%09%09if (parsedValue instanceof Double) %7B%0A%09
 %09%09%09%09%09final d
@@ -1551,38 +1551,31 @@
 e = 
+(
 Double
-.
+) 
 parse
-Double(new
+d
 Value
-)
 ;%0A
+%09
 %09%09%09%09
@@ -1633,24 +1633,102 @@
 mperature);%0A
+%09%09%09%09%09%7D else %7B%0A%09%09%09%09%09%09Log.w(LOGTAG, %22Can't update value in data base.%22);%0A%09%09%09%09%09%7D%0A
 %09%09%09%09%7D else i
@@ -1992,16 +1992,114 @@
 ;%0A%09%09%09%09%7D%0A
+%09%09%09%7D catch (final ParseException ex) %7B%0A%09%09%09%09Log.e(LOGTAG, %22Can't update value in data base.%22, ex);%0A
 %09%09%09%7D cat
@@ -2354,24 +2354,146 @@
 orState)) %7B%0A
+%09%09%09%09try %7B%0A%09%09%09%09%09Object parsedValue = TEMPERATURE_FORMAT.parseObject(newValue);%0A%09%09%09%09%09if (parsedValue instanceof Double) %7B%0A%09%09
 %09%09%09%09session.
@@ -2533,35 +2533,51 @@
 e(deviceId, 
-newValue
+parsedValue.toString()
 );%0A
+%09%09
 %09%09%09%09// sessi
@@ -2600,32 +2600,206 @@
 lDeviceState();%0A
+%09%09%09%09%09%7D else %7B%0A%09%09%09%09%09%09Log.w(LOGTAG, %22Cannot change device state!%22);%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D catch (final ParseException ex) %7B%0A%09%09%09%09%09Log.e(LOGTAG, %22Cannot change device state!%22, ex);%0A%09%09%09%09%7D%0A
 %09%09%09%7D else if (de

update the temperature correct (correct paring of string value)