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

Introduce HTTP-bridge & migrate Shelly Plug S #2381

Merged
merged 15 commits into from
Jan 16, 2024

Conversation

sebastianasen
Copy link
Contributor

Introduce a new bridge for http connections and communication.

Currently, each implementation of a device via RESTful APIs is implemented in its own bundle and is more or less stable depending on asynchronous calls.

In this PR it is currently used by the shelly IO plug as a first test.

Co-authored-by: Michael Grill [email protected]

First test with a shelly IO plug.
Copy link

Code Coverage

Copy link

Code Coverage

Copy link

Code Coverage

@michaelgrill michaelgrill marked this pull request as ready for review November 29, 2023 13:40
@michaelgrill michaelgrill changed the title WIP: Edge: Introduce a http bridge Edge: Introduce a http bridge Nov 29, 2023
// TODO: Execute before TOPIC_CYCLE_BEFORE_PROCESS_IMAGE, like modbus bridge
case EdgeEventConstants.TOPIC_CYCLE_BEFORE_PROCESS_IMAGE -> {

this.endpoints.forEach(EndpointCountdown::decreaseCycleCount);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should decreaseCycleCount not only get called inside the loop? Difference would be if an Exception happens

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no the loop is only to handle endpoints wich have hit cycleCount 0 before that i decrease every cycle

@sfeilmeier sfeilmeier changed the title Edge: Introduce a http bridge Introduce HTTP-bridge & migrate Shelly Plug S Dec 19, 2023
use offer for adding endpoint
removed return and replaced with continue to fix not iterating over all elements
handle result and error in one method
set every channel to null on error
Copy link

Code Coverage

@sfeilmeier sfeilmeier merged commit 4aa1268 into OpenEMS:develop Jan 16, 2024
2 checks passed
@Sn0w3y
Copy link
Contributor

Sn0w3y commented Feb 6, 2024

2024-02-06T06:01:56,912 [tchQueue] ERROR [Events.Framework              ] FrameworkEvent ERROR
org.osgi.framework.ServiceException: Service factory returned null. (Component: IO.Shelly.Plus3EM (113))
	at org.apache.felix.framework.ServiceRegistrationImpl.getFactoryUnchecked(ServiceRegistrationImpl.java:385) ~[?:?]
	at org.apache.felix.framework.ServiceRegistrationImpl.getService(ServiceRegistrationImpl.java:249) ~[?:?]
	at org.apache.felix.framework.ServiceRegistry.getService(ServiceRegistry.java:362) ~[?:?]
	at org.apache.felix.framework.Felix.getService(Felix.java:3984) ~[?:?]
	at org.apache.felix.framework.BundleContextImpl.getService(BundleContextImpl.java:450) ~[?:?]
	at org.apache.felix.eventadmin.impl.handler.EventHandlerProxy.obtain(EventHandlerProxy.java:296) ~[?:?]
	at org.apache.felix.eventadmin.impl.handler.EventHandlerProxy.sendEvent(EventHandlerProxy.java:424) ~[?:?]
	at org.apache.felix.eventadmin.impl.tasks.HandlerTask.runWithoutDenylistTiming(HandlerTask.java:82) ~[?:?]
	at org.apache.felix.eventadmin.impl.tasks.SyncDeliverTasks.execute(SyncDeliverTasks.java:107) ~[?:?]
	at org.apache.felix.eventadmin.impl.handler.EventAdminImpl.sendEvent(EventAdminImpl.java:155) ~[?:?]
	at org.apache.felix.eventadmin.impl.security.EventAdminSecurityDecorator.sendEvent(EventAdminSecurityDecorator.java:96) ~[?:?]
	at io.openems.common.event.EventBuilder.send(EventBuilder.java:68) ~[?:?]
	at io.openems.edge.core.cycle.CycleWorker.forever(CycleWorker.java:159) ~[?:?]
	at io.openems.common.worker.AbstractWorker$1.run(AbstractWorker.java:156) ~[?:?]
Happens after one deactivates for example the Shelly Device

sfeilmeier pushed a commit that referenced this pull request Feb 21, 2024
- "Fixed" bug where an exception gets throw during deactivation of a component using the HTTP-Bridge (ty @Sn0w3y #2381 (comment))

now to get an instance of a `BridgeHttp` the following code needs to be added

```
@reference
private BridgeHttpFactory httpBridgeFactory;
private BridgeHttp httpBridge;

@activate
private void activate() {
    this.httpBridge = this.httpBridgeFactory.get();
}

@deactivate
private void deactivate() {
    this.httpBridgeFactory.unget(this.httpBridge);
    this.httpBridge = null;
}
```
as far as i know the exception which is happening is not intended to happen but for now to avoid unexpected behaviour because of the exception this is the prefered way to use it.

- Made CycleSubscriber thread safe to avoid concurrent modification exceptions

- Added time based subscribtions
  simple example to add a subscription with a delay of 1 minute between the calls would be
  
```
 final var delayProvider = DelayTimeProviderChain.fixedDelay(new Delay(1L, TimeUnit.MINUTES);
 this.httpBridge.subscribeTime(delayProvider, "http://127.0.0.1/status", t -> {
 	// process data
 }, t -> {
 	// handle error
 });
```
fanass-dev pushed a commit to fanass-dev/openems that referenced this pull request May 6, 2024
…#2539)

- "Fixed" bug where an exception gets throw during deactivation of a component using the HTTP-Bridge (ty @Sn0w3y OpenEMS#2381 (comment))

now to get an instance of a `BridgeHttp` the following code needs to be added

```
@reference
private BridgeHttpFactory httpBridgeFactory;
private BridgeHttp httpBridge;

@activate
private void activate() {
    this.httpBridge = this.httpBridgeFactory.get();
}

@deactivate
private void deactivate() {
    this.httpBridgeFactory.unget(this.httpBridge);
    this.httpBridge = null;
}
```
as far as i know the exception which is happening is not intended to happen but for now to avoid unexpected behaviour because of the exception this is the prefered way to use it.

- Made CycleSubscriber thread safe to avoid concurrent modification exceptions

- Added time based subscribtions
  simple example to add a subscription with a delay of 1 minute between the calls would be
  
```
 final var delayProvider = DelayTimeProviderChain.fixedDelay(new Delay(1L, TimeUnit.MINUTES);
 this.httpBridge.subscribeTime(delayProvider, "http://127.0.0.1/status", t -> {
 	// process data
 }, t -> {
 	// handle error
 });
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants