<commit_before>package de.loosensimnetz.iot.raspi.motor.state;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.loosensimnetz.iot.raspi.motor.Motor;
import de.loosensimnetz.iot.raspi.motor.MotorSensor;

public class MotorStateMovingDown extends MotorState {
	private final Logger logger = LoggerFactory.getLogger(getClass());

	private static final MotorStateMovingDown instance = new MotorStateMovingDown();

	protected MotorStateMovingDown() {

	}

	public static MotorStateMovingDown instance() {
		return MotorStateMovingDown.instance;
	}

	@Override
	public void update(MotorSensor sensor, long updateTime) {
		final Motor motor = sensor.getMotor();
		long timeElapsed = updateTime - sensor.getUpdateTime();
		long earliestStateChange = motor.getExpectedTimeDown().getExpectedTime()
				- motor.getExpectedTimeDown().getTolerance();
		long latestStateChange = motor.getExpectedTimeDown().getExpectedTime()
				+ motor.getExpectedTimeDown().getTolerance();

		final boolean motorMovingDown = motor.isMovingDown() && !motor.isMovingUp();
		final boolean motorMovingUp = !motor.isMovingDown() && motor.isMovingUp();
		final boolean motorStopped = !motorMovingUp && !motorMovingDown;

		if (motorStopped && timeElapsed < earliestStateChange) {
			// Motor stopped too early
			logger.info("Motor stopped too early - stop after {} ms, did not expect stop before {} ms.", timeElapsed,
					earliestStateChange);

			changeState(sensor, MotorStateError.instance(), updateTime);
			return;
		}

		if (motorStopped && timeElapsed <= latestStateChange) {
			// Motor stopped within tolerance
			logger.info(
					"Motor stopped at {} within tolerance (from {} to {}) - changing state to MotorStateStoppedDown.",
					timeElapsed, earliestStateChange, latestStateChange);

			changeState(sensor, MotorStateStoppedDown.instance(), updateTime);
			return;
		}

		if (motorMovingUp && timeElapsed >= earliestStateChange) {
			// Motor moving up within tolerance
			logger.info(
					"Motor changed direction upward at {} within tolerance (from {} to {}) - changing state to MotorStateMovingUp.",
					timeElapsed, earliestStateChange, latestStateChange);

			changeState(sensor, MotorStateMovingUp.instance(), updateTime);
			return;
		}

		if (motorMovingDown && timeElapsed <= latestStateChange) {
			// Motor is still on its way down - no state change
			return;
		}

		// Motor is in some kind of unexpected error state
		logger.info(
				"Motor is in unexpected error state. Time elapsed: {}, earliestStateChange: {}, "
						+ "latestStateChange: {}, movingUp: {}, movingDown: {}.",
				timeElapsed, earliestStateChange, latestStateChange, motorMovingUp, motorMovingDown);

		changeState(sensor, MotorStateError.instance(), updateTime);
	}

	@Override
	public StateId getStateId() {
		return StateId.MOVING_DOWN;
	}
}
<commit_msg>Bug fix<commit_after>package de.loosensimnetz.iot.raspi.motor.state;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.loosensimnetz.iot.raspi.motor.Motor;
import de.loosensimnetz.iot.raspi.motor.MotorSensor;

public class MotorStateMovingDown extends MotorState {
	private final Logger logger = LoggerFactory.getLogger(getClass());

	private static final MotorStateMovingDown instance = new MotorStateMovingDown();

	protected MotorStateMovingDown() {

	}

	public static MotorStateMovingDown instance() {
		return MotorStateMovingDown.instance;
	}

	@Override
	public void update(MotorSensor sensor, long updateTime) {
		final Motor motor = sensor.getMotor();
		long timeElapsed = updateTime - sensor.getUpdateTime();
		long earliestStateChange = motor.getExpectedTimeDown().getExpectedTime()
				- motor.getExpectedTimeDown().getTolerance();
		long latestStateChange = motor.getExpectedTimeDown().getExpectedTime()
				+ motor.getExpectedTimeDown().getTolerance();

		final boolean motorMovingDown = motor.isMovingDown() && !motor.isMovingUp();
		final boolean motorMovingUp = !motor.isMovingDown() && motor.isMovingUp();
		final boolean motorStopped = !motorMovingUp && !motorMovingDown;

		if (motorStopped && timeElapsed < earliestStateChange) {
			// Motor stopped too early
			logger.info("Motor stopped too early - stop after {} ms, did not expect stop before {} ms.", timeElapsed,
					earliestStateChange);

			changeState(sensor, MotorStateError.instance(), updateTime);
			return;
		}

		if (motorStopped) {
			// Motor stopped within tolerance
			logger.info(
					"Motor stopped at {} within tolerance (from {} to {}) - changing state to MotorStateStoppedDown.",
					timeElapsed, earliestStateChange, latestStateChange);

			changeState(sensor, MotorStateStoppedDown.instance(), updateTime);
			return;
		}

		if (motorMovingUp && timeElapsed >= earliestStateChange) {
			// Motor moving up within tolerance
			logger.info(
					"Motor changed direction upward at {} within tolerance (from {} to {}) - changing state to MotorStateMovingUp.",
					timeElapsed, earliestStateChange, latestStateChange);

			changeState(sensor, MotorStateMovingUp.instance(), updateTime);
			return;
		}

		if (motorMovingDown) {
			// Motor is still on its way down - no state change
			return;
		}

		// Motor is in some kind of unexpected error state
		logger.info(
				"Motor is in unexpected error state. Time elapsed: {}, earliestStateChange: {}, "
						+ "latestStateChange: {}, movingUp: {}, movingDown: {}.",
				timeElapsed, earliestStateChange, latestStateChange, motorMovingUp, motorMovingDown);

		changeState(sensor, MotorStateError.instance(), updateTime);
	}

	@Override
	public StateId getStateId() {
		return StateId.MOVING_DOWN;
	}
}

{'fraction_non_alphanumeric': 0.10173010380622838, 'fraction_numerical': 0.0006920415224913495, 'mean_word_length': 4.009532062391681, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4048703', 'n_tokens_mistral': 1911, 'n_tokens_neox': 1584, 'n_words': 413}