Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move global Cycle-Time to the "Core.Cycle" Component. #1045 #1085

Merged
merged 1 commit into from
Mar 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package io.openems.common.worker;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;

import java.util.concurrent.atomic.AtomicInteger;

import org.junit.Test;

import io.openems.common.worker.AbstractCycleWorker;

public class AbstractCycleWorkerTest {

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package io.openems.common.worker;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;

import java.util.concurrent.atomic.AtomicInteger;

import org.junit.Test;

import io.openems.common.worker.AbstractWorker;

public class AbstractWorkerTest {

@Test
Expand Down
16 changes: 16 additions & 0 deletions io.openems.edge.core/src/io/openems/edge/core/cycle/Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.openems.edge.core.cycle;

import org.osgi.service.metatype.annotations.AttributeDefinition;
import org.osgi.service.metatype.annotations.ObjectClassDefinition;

@ObjectClassDefinition(//
name = "Core Cycle", //
description = "The global OpenEMS Cycle.")
@interface Config {

@AttributeDefinition(name = "Cycle-Time", description = "The duration of one global OpenEMS Cycle in [ms]")
int cycleTime() default Cycle.DEFAULT_CYCLE_TIME;

String webconsole_configurationFactory_nameHint() default "Core Cycle";

}
11 changes: 10 additions & 1 deletion io.openems.edge.core/src/io/openems/edge/core/cycle/Cycle.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package io.openems.edge.core.cycle;

import io.openems.common.channel.Level;
import io.openems.common.channel.Unit;
import io.openems.common.types.OpenemsType;
import io.openems.edge.common.channel.Doc;
import io.openems.common.channel.Level;

public interface Cycle {

public static final int DEFAULT_CYCLE_TIME = 1000; // in [ms]

public enum ChannelId implements io.openems.edge.common.channel.ChannelId {
/**
* Actual, measured Cycle-Time in [ms].
Expand Down Expand Up @@ -39,4 +41,11 @@ public Doc doc() {
}
}

/**
* Gets the duration of one global OpenEMS Cycle in [ms].
*
* @return the duration in milliseconds
*/
public int getCycleTime();

}
56 changes: 36 additions & 20 deletions io.openems.edge.core/src/io/openems/edge/core/cycle/CycleImpl.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package io.openems.edge.core.cycle;

import java.util.TreeMap;
import java.util.Comparator;
import java.util.TreeSet;

import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ConfigurationPolicy;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Modified;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.osgi.service.component.annotations.ReferencePolicyOption;
import org.osgi.service.event.EventAdmin;
import org.osgi.service.metatype.annotations.Designate;
import org.slf4j.Logger;

import io.openems.common.OpenemsConstants;
Expand All @@ -20,9 +24,11 @@
import io.openems.edge.common.sum.Sum;
import io.openems.edge.scheduler.api.Scheduler;

@Designate(ocd = Config.class, factory = false)
@Component(//
name = "Core.Cycle", //
immediate = true, //
configurationPolicy = ConfigurationPolicy.OPTIONAL, //
property = { //
"id=" + OpenemsConstants.CYCLE_ID, //
"enabled=true" //
Expand All @@ -37,36 +43,30 @@ public class CycleImpl extends AbstractOpenemsComponent implements OpenemsCompon
@Reference
protected Sum sumComponent;

@Reference
protected ComponentManager componentManager;

/**
* Holds the Schedulers and their relative cycleTime. They are sorted ascending
* by their cycleTimes.
*/
protected final TreeMap<Scheduler, Integer> schedulers = new TreeMap<Scheduler, Integer>(
(a, b) -> a.getCycleTime() - b.getCycleTime());

@Reference
protected ComponentManager componentManager;
protected final TreeSet<Scheduler> schedulers = new TreeSet<Scheduler>(Comparator.comparing(Scheduler::id));

protected int commonCycleTime = Scheduler.DEFAULT_CYCLE_TIME;
protected int maxCycles = 1;
protected int cycle = 0;
private Config config = null;;

@Reference(policy = ReferencePolicy.DYNAMIC, policyOption = ReferencePolicyOption.GREEDY, cardinality = ReferenceCardinality.MULTIPLE)
@Reference(//
policy = ReferencePolicy.DYNAMIC, //
policyOption = ReferencePolicyOption.GREEDY, //
cardinality = ReferenceCardinality.MULTIPLE, //
target = "(enabled=true)")
protected void addScheduler(Scheduler newScheduler) {
if (newScheduler.isEnabled()) {
synchronized (this.schedulers) {
this.schedulers.put(newScheduler, 1); // relativeCycleTime is going to be overwritten by
// recalculateCommonCycleTime
this.commonCycleTime = Utils.recalculateCommonCycleTime(this.schedulers);
this.maxCycles = Utils.recalculateRelativeCycleTimes(schedulers, this.commonCycleTime);
}
synchronized (this.schedulers) {
this.schedulers.add(newScheduler);
}
}

protected void removeScheduler(Scheduler scheduler) {
this.schedulers.remove(scheduler);
this.commonCycleTime = Utils.recalculateCommonCycleTime(this.schedulers);
this.maxCycles = Utils.recalculateRelativeCycleTimes(schedulers, this.commonCycleTime);
}

public CycleImpl() {
Expand All @@ -77,8 +77,9 @@ public CycleImpl() {
}

@Activate
void activate(ComponentContext context) {
void activate(ComponentContext context, Config config) {
super.activate(context, OpenemsConstants.CYCLE_ID, "Core.Cycle", true);
this.config = config;
this.worker.activate("Core.Cycle");
}

Expand All @@ -88,9 +89,24 @@ protected void deactivate() {
this.worker.deactivate();
}

@Modified
void modified(Config config) {
this.config = config;
}

@Override
protected void logWarn(Logger log, String message) {
super.logWarn(log, message);
}

@Override
public int getCycleTime() {
Config config = this.config;
if (config != null) {
return config.cycleTime();
} else {
return Cycle.DEFAULT_CYCLE_TIME;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.time.Duration;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map.Entry;

import org.osgi.service.event.Event;
import org.slf4j.Logger;
Expand All @@ -30,16 +29,11 @@ public CycleWorker(CycleImpl parent) {

@Override
protected int getCycleTime() {
return this.parent.commonCycleTime;
return this.parent.getCycleTime();
}

@Override
protected void forever() {
// handle cycle number
if (++this.parent.cycle > this.parent.maxCycles) {
this.parent.cycle = 1;
}

// Kick Operating System Watchdog
if (SDNotify.isAvailable()) {
SDNotify.sendWatchdog();
Expand Down Expand Up @@ -94,14 +88,9 @@ protected void forever() {
if (this.parent.schedulers.isEmpty()) {
this.parent.logWarn(this.log, "There are no Schedulers configured!");
} else {
for (Entry<Scheduler, Integer> entry : this.parent.schedulers.entrySet()) {
for (Scheduler scheduler : this.parent.schedulers) {
boolean schedulerHasError = false;

Scheduler scheduler = entry.getKey();
if (this.parent.cycle % entry.getValue() != 0) {
// abort if relativeCycleTime is not matching this cycle
return;
}
try {
for (Controller controller : scheduler.getControllers()) {
if (!controller.isEnabled()) {
Expand Down
90 changes: 0 additions & 90 deletions io.openems.edge.core/src/io/openems/edge/core/cycle/Utils.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@

import io.openems.common.exceptions.OpenemsError.OpenemsNamedException;
import io.openems.edge.common.channel.Doc;
import io.openems.edge.common.component.AbstractOpenemsComponent;
import io.openems.edge.common.component.ComponentManager;
import io.openems.edge.common.component.OpenemsComponent;
import io.openems.edge.controller.api.Controller;
import io.openems.edge.scheduler.api.AbstractScheduler;
import io.openems.edge.scheduler.api.Scheduler;

/**
* This Scheduler returns all existing Controllers ordered by their ID.
*/
@Designate(ocd = Config.class, factory = true)
@Component(name = "Scheduler.AllAlphabetically", immediate = true, configurationPolicy = ConfigurationPolicy.REQUIRE)
public class AllAlphabetically extends AbstractScheduler implements Scheduler, OpenemsComponent {
public class AllAlphabetically extends AbstractOpenemsComponent implements Scheduler, OpenemsComponent {

@Reference
protected ComponentManager componentManager;
Expand All @@ -32,7 +32,7 @@ public class AllAlphabetically extends AbstractScheduler implements Scheduler, O

@Activate
void activate(ComponentContext context, Config config) {
super.activate(context, config.id(), config.alias(), config.enabled(), config.cycleTime());
super.activate(context, config.id(), config.alias(), config.enabled());
this.config = config;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import org.osgi.service.metatype.annotations.AttributeDefinition;
import org.osgi.service.metatype.annotations.ObjectClassDefinition;

import io.openems.edge.scheduler.api.Scheduler;

@ObjectClassDefinition(//
name = "Scheduler All Alphabetically", //
description = "This Scheduler takes an ordered list of Component IDs. All remaining Controllers are afterwards ordered alphabetically by their ID.")
Expand All @@ -19,8 +17,6 @@
@AttributeDefinition(name = "Is enabled?", description = "Is this Component enabled?")
boolean enabled() default true;

int cycleTime() default Scheduler.DEFAULT_CYCLE_TIME;

@AttributeDefinition(name = "Controller-IDs", description = "IDs of Controllers. Controller execution is going to be sorted in the order of the IDs.")
String[] controllers_ids() default {};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@ public boolean enabled() {
return true;
}

@Override
public int cycleTime() {
return 0;
}

@Override
public String[] controllers_ids() {
return new String[] { "c3", "c2" };
Expand Down
Loading