blob: 9b2ca076eea1f4fd468fd172713e542f299b7c6b [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2018 Agence spatiale canadienne / Canadian Space Agency
* 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
*
* Contributors:
* Pierre Allard,
* Regent L'Archeveque - initial API and implementation
*
* SPDX-License-Identifier: EPL-1.0
*******************************************************************************/
package org.eclipse.apogy.core.programs.controllers.impl;
import org.eclipse.apogy.common.io.jinput.EComponent;
import org.eclipse.apogy.common.io.jinput.EComponentQualifier;
import org.eclipse.apogy.common.io.jinput.impl.EControllerEnvironmentCustomImpl;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.util.EList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ControllerStateTriggerCustomImpl extends ControllerStateTriggerImpl {
private static final Logger Logger = LoggerFactory.getLogger(ControllerStateTriggerImpl.class);
private boolean run = false;
private Thread thread = null;
@Override
public void setRepeatPeriod(long newRepeatPeriod) {
if (newRepeatPeriod == -1) {
stopThread();
}
super.setRepeatPeriod(newRepeatPeriod);
}
@Override
public void setComponentQualifier(EComponentQualifier newComponentQualifier) {
super.setComponentQualifier(newComponentQualifier);
/** Check to see if we should start the update */
EComponent component = org.eclipse.apogy.common.io.jinput.Activator.getEControllerEnvironment()
.resolveEComponent(newComponentQualifier);
if (component != null) {
boolean buttonStatus = convert(component.getPollData());
update(!buttonStatus, buttonStatus);
}
}
@Override
public void stop() {
stopThread();
super.stop();
}
/**
* Converts the poll data to a boolean value.
*/
@Override
protected boolean convert(float value) {
if (value == 0) {
return false;
} else {
return true;
}
}
/**
* Starts the thread that will update the operationCallControllerBindings
*/
protected void startThread() {
String message = null;
if (getName() != null) {
message = getName() + " : is now Enabled.";
} else {
message = "is now Enabled.";
}
this.run = true;
Logger.info(message);
this.thread = new Thread() {
@Override
public void run() {
while (ControllerStateTriggerCustomImpl.this.run) {
try {
// Updates
getOperationCallControllerBinding().update();
// Wait
if (getRepeatPeriod() >= EControllerEnvironmentCustomImpl.MILLIS_BEFORE_NEXT_POLLING) {
Thread.sleep(getRepeatPeriod());
} else {
Thread.sleep(EControllerEnvironmentCustomImpl.MILLIS_BEFORE_NEXT_POLLING);
}
} catch (Throwable t) {
Logger.error(t.getMessage(), t);
}
}
}
};
this.thread.start();
}
/**
* Stops the thread
*/
protected void stopThread() {
this.run = false;
String message = null;
if (getName() != null) {
message = getName() + " : is now Disabled.";
} else {
message = "is now Disabled.";
}
Logger.info(message);
}
@Override
protected void update(boolean oldValue, boolean newValue) {
if (isStarted()) {
if (newValue == isEnablingValue()) {
startThread();
} else {
stopThread();
}
}
}
@Override
public EList<Adapter> eAdapters() {
return super.eAdapters();
}
} // ControllerStateTriggerImpl