| /******************************************************************************* |
| * 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.emf.transaction.ApogyCommonTransactionFacade; |
| import org.eclipse.apogy.common.io.jinput.Activator; |
| import org.eclipse.apogy.common.io.jinput.ApogyCommonIOJInputFactory; |
| import org.eclipse.apogy.common.io.jinput.ApogyCommonIOJInputPackage; |
| import org.eclipse.apogy.common.io.jinput.EComponent; |
| import org.eclipse.apogy.common.io.jinput.EComponentQualifier; |
| import org.eclipse.apogy.common.io.jinput.EControllerEnvironment; |
| import org.eclipse.apogy.core.programs.controllers.ApogyCoreProgramsControllersPackage; |
| import org.eclipse.emf.common.notify.Adapter; |
| import org.eclipse.emf.common.notify.Notification; |
| import org.eclipse.emf.common.notify.impl.AdapterImpl; |
| |
| public abstract class ControllerTriggerCustomImpl extends ControllerTriggerImpl { |
| |
| boolean busy = false; |
| |
| public EComponentQualifier getComponentQualifier() { |
| if (super.getComponentQualifier() == null) { |
| if (ApogyCommonTransactionFacade.INSTANCE.getTransactionalEditingDomain(this) == null) { |
| setComponentQualifier(ApogyCommonIOJInputFactory.eINSTANCE.createEComponentQualifier()); |
| } else { |
| ApogyCommonTransactionFacade.INSTANCE.basicSet(this, |
| ApogyCoreProgramsControllersPackage.Literals.CONTROLLER_TRIGGER__COMPONENT_QUALIFIER, |
| ApogyCommonIOJInputFactory.eINSTANCE.createEComponentQualifier()); |
| } |
| } |
| return super.getComponentQualifier(); |
| } |
| |
| @Override |
| public void setComponentQualifier(EComponentQualifier newComponentQualifier) { |
| super.setComponentQualifier(newComponentQualifier); |
| } |
| |
| |
| @Override |
| public void start() { |
| setStarted(true); |
| Activator.getEControllerEnvironment().eAdapters().add(getAdapter()); |
| } |
| |
| @Override |
| public void stop() { |
| setStarted(false); |
| Activator.getEControllerEnvironment().eAdapters().remove(getAdapter()); |
| } |
| |
| public Adapter getAdapter() { |
| Adapter adapter = super.getAdapter(); |
| if (adapter == null) { |
| adapter = new AdapterImpl() { |
| boolean oldValue = false; |
| |
| @Override |
| synchronized public void notifyChanged(Notification msg) { |
| if (getOperationCallControllerBinding().isStarted()) { |
| /** If pollingCount */ |
| if (msg.getFeatureID( |
| EControllerEnvironment.class) == ApogyCommonIOJInputPackage.ECONTROLLER_ENVIRONMENT__POLLING_COUNT) { |
| EComponent component = Activator.getEControllerEnvironment() |
| .resolveEComponent(getComponentQualifier()); |
| |
| /** If there is a component */ |
| |
| if (component != null && !ControllerTriggerCustomImpl.this.busy) { |
| ControllerTriggerCustomImpl.this.busy = true; |
| float pollData = component.getPollData(); |
| boolean latest = convert(pollData); |
| |
| |
| if (Boolean.logicalXor(this.oldValue, latest)) { |
| boolean prev = new Boolean(this.oldValue); |
| boolean now = new Boolean(latest); |
| |
| this.oldValue = new Boolean(latest); |
| update(prev, now); |
| } |
| ControllerTriggerCustomImpl.this.busy = false; |
| } |
| } |
| |
| } |
| } |
| }; |
| ApogyCommonTransactionFacade.INSTANCE.basicSet(this, |
| ApogyCoreProgramsControllersPackage.Literals.CONTROLLER_TRIGGER__ADAPTER, adapter); |
| } |
| return super.getAdapter(); |
| } |
| |
| /** |
| * Method that is called when a change on the Controller component is detected. |
| * |
| * @param oldValue The previous value of the Button. |
| * @param newValue The current value of the Button. |
| */ |
| abstract protected void update(boolean oldValue, boolean newValue); |
| |
| protected boolean convert(float value) { |
| if (value <= 0) { |
| return false; |
| } else { |
| return true; |
| } |
| } |
| |
| } // ControllerTriggerImpl |