-
Notifications
You must be signed in to change notification settings - Fork 6
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
Support other serial implementations #34
Comments
@wborn this makes sense. However this has not been on my priority since in the overall list for some bindings it appears is not easy/possible to avoid gnu.io and use the transport instead (for example pentair, oceanic). If the replace of gnu.io is a target for OH 4.0, I will try to follow and complete the migration also for openwebnet4j. |
also @wborn : what if |
We can probably add anything that's missing from gnu.io to the OH serial transport to make it work with all add-ons. That's how we got it to support the majority of add-ons. |
ok, perfect, I will maybe open a PR for that on openhab-core when I will try to migrate from |
@wborn if I want to start looking at this issue, which serial implementation should I look at that could replace gnu.io in OH ? |
In OH you want to use the serial transport so we can easily switch between implementations (like openhab-addons#14051). If you want to add some implementations in this repo you could add an implementation using nrjavaserial and one using purejavacomm, see also openhab/openhab-core#3632. If you need an OSGi compatible artifact you may want to use the opensmarthouse fork. If you have lots of time you could also add an implementation using jSerialComm. 🙂 |
@wborn I started looking at purejavacomm. But I found that also there |
It looks like @splatch and others use purejavacomm to workaround nrjavaserial issues, see also: https://community.openhab.org/t/oh3-x-oh4-x-alternative-java-serial-provider/128462 |
Ok. I also looked to jSerialComm and it really it looks a much more active and complete project if compared to purejavacomm. It appears it also considers the HW error case (I think it's possibile to use LISTENING_EVENT_PORT_DISCONNECTED, but I will have to test it). Also question: |
There is not much "new" things within serial APIs, so purejavacomm activity reflects activity in this area. Have a look on this part: https://github.com/Fazecast/jSerialComm/blob/master/src/main/c/Posix/SerialPort_Posix.c#L4, I would consider 2013 "recent" in terms of the serial port apis. ;-) For sure jserialcomm gives you better visibility over what happens with serial port, if you need "just" data stream any implementation will work. With regard to library design questions - try to isolate serial port work into your own basic interfaces. Then you can supply serial library with any implementation. |
I haven't used any of them myself and don't know all the limitations of each implementation. I mentioned purejavacomm because it seems to be the most commonly used alternative implementation in OH and because it may also be supported in OH with openhab/openhab-core#3632. If it is not possible or difficult to use purejavacomm as a replacement, feel free to add an implementation using jSerialComm. |
You define an interface for providers to implement serial communications in this project. It will be similar to Then you implement a provider and make it possible in your library to switch between serial port provider interfaces. Other libraries often use the Service Provider Interface (SPI) for this, see: https://www.baeldung.com/java-spi If that is too complex at first, you can also also make it possible in your library to configure the fully qualified classname of the provider. Then you can create an instance of the provider using package com.acme;
import java.lang.reflect.Constructor;
import org.junit.jupiter.api.Test;
public class ProviderTest {
interface SerialCommProvider {
String getProviderName();
}
public static class JSerialCommImpl implements SerialCommProvider {
public String getProviderName() {
return "JSerialCommImpl";
}
}
public static class OHSerialCommImpl implements SerialCommProvider {
public String getProviderName() {
return "OHSerialCommImpl";
}
}
String providerFQCN = "";
public void createProviderFQCN() throws Exception {
Class<?> providerClass = Class.forName(providerFQCN);
Constructor<?> constructor = providerClass.getConstructor();
SerialCommProvider provider = (SerialCommProvider) constructor.newInstance();
System.out.println(provider.getProviderName());
}
@Test
void createJSerialCommProviderFQCN() throws Exception {
providerFQCN = "com.acme.ProviderTest$JSerialCommImpl";
createProviderFQCN();
}
@Test
void createOHSerialCommProviderFQCN() throws Exception {
providerFQCN = "com.acme.ProviderTest$OHSerialCommImpl";
createProviderFQCN();
}
} Even simpler would be to allow for setting an When using openHAB the library would be reconfigured to use a class that implements your library interface using the OH serial transport. |
Thanks both, I will look at your suggestions. Actually my concern is more: |
It looks like also jSerialComm is also aready used in openhab-addons here: |
I think it will be there for a while. Each implementation has their own pros/cons so it is nice that OH allows for supporting multiple using the serial transport. That allows for switching between implementations whenever you run into bugs, performance, feature or platform compatibility issues. |
So in the end this is my plan:
|
It would probably be better to add the implementation using the OH serial transport to the binding in openhab-addons. That way you don't need to manage OH dependencies in your library. Nobody will use that implementation except for openHAB. Adding the implementation to the binding will also make sure it keeps working if the OH serial transport interfaces change. |
Yes sorry I did not explain it correctly, I rephrase:
Of course I want my lib to have serial implementation pluggable (offering nrjavaserial as default) but still be completely indipendent from OH |
@mvalla I think way you draw will be best. It will keep library open for standalone use and also provide a way for trouble-less integration with OH. |
Ok @wborn @splatch : I have a working branch for this lib that can use both NRJavaSerial and jSerialComm. I wrote wrappers and use Service Provider Interface (SPI) approach and works well. Also there is already another OH binding using jSerialComm: ProteusEcoMeter. |
That is great news! 🙂
The original repo is https://github.com/nyholku/purejavacomm but it seems to be very inactive. Maybe it was more active when @splatch decided to fork it?
It sounds like PureJavaComm lacks functionality that is available in both NRJavaSerial and jSerialComm. So in that case it might also be a useful addition.
I don't know why the messages got corrupt (openhab/openhab-addons#11333), but it could be that jSerialComm uses different default configuration values compared to NRJavaSerial. In the end there should only be one serial transport ( |
The 1.0.4 version was not deployed to maven central so @cdjackson pulled it into opensmarthouse where we have pipeline. |
Updated plan:
|
@wborn and @splatch : so I use the SPI approach, and provide in the openwebnet4j lib a default implementation that uses NRJavaSerial (this is a requirement for me as I want the lib to remain only dependent on NRJavaSerial JAR). When the lib is used in OH by the binding, the serial implementation provided by OH Serial Trasport will also be available. |
If your |
Done that, thanks @wborn ! So @wborn and @splatch thaks to your suggestions, I managed to write in the openwebnet binding the adapter to use OH Serial Transport. This is the class: SerialTransportAdapter As you can see I used the But now I think I finished in the OSGi/SPI swamp:
any suggestion? |
Nice progress! 🙂 If you use SPI with OSGi you need to make sure the proper bundle manifest entries are added. See: https://aries.apache.org/documentation/modules/spi-fly.html We also have a few bundles in openHAB that use such bundle manifest entries, see: |
So the suggestion is to simply add the Require-Capability binding manifest header like is done in the KNX binding pom.xml?? I don’t need to include SPI Fly jar myself nor add any other new manifest header to the openwebnet4j library (consumer), correct? sorry if this seems obvious to you but I did not fully understand all the SPI/OSGi stuff… |
Just a rough idea - why pulling openhab adapter to openwebnet4j if adapter you do is necessary only for binding? If its helpful for binding you can bake it into binding code instead of making it external dependency to the binding. This way you don't need to fight with SPI-fly/OSGi that much. |
Thanks @splatch for your comment. However I am not sure I understand it correctly:
If you can confirm the SPI mechanisms can be adjusted for OSGi by just adding some more manifest headers… if this is the case I prefer to compete this approach since I already implemented it. |
You're absolutely right about SPI loading being a trouble maker under OSGi. Since your serial transport adapter is part of binding and not library you do not have any benefit of OSGi services produced by SPI-fly.
Bypassing is a strong statement, I'd say its more a manual injection rather than implicit. Sometimes being explicit is needed and better as it makes things just simpler. I had a look on your branch and now you pass just a port name to I don't know openwebnet4j at all, hence my advice might be completely wrong from library design point of view. |
ok @splatch I got your suggestion now, and will try to inject the However this way we loose the possibility to have hot-replacement of serial port management: once the |
@wborn, @splatch : I completed this by providing both SPI ServiceLoader and injection. I also jsut submitted a new PR for openwebnet binding using injection mechanism. Tested extensibely wih my own USB serial hw and a proof-of-concept of a new transport based on jSerialComm. Updated plan:
|
It would be nice when the library supports using other serial implementations instead of
gnu.io
.That way openHAB could use its own serial transport with it, which would solve several issues (openhab/openhab-addons#7573).
This could for instance be implemented similar to the calimero-rxtx implementation (see also this comment).
See also openhab/openhab-addons#14051 for how such a mechanism can be used for using the openHAB serial transport.
The text was updated successfully, but these errors were encountered: