smarthome icon indicating copy to clipboard operation
smarthome copied to clipboard

DSL serializer for items and things

Open kaikreuzer opened this issue 7 years ago • 9 comments

Some people ask whether it is possible to get item&thing DSL files from an existing setup that was done through the GUIs - this is currently clearly not possible.

To offer it, we would need a kind of serializer/exporter to DSLs. This could simply be a new console command. I am not sure, what information might be lacking the database for constructing "nice" DSL files, but in general, it should be feasible.

kaikreuzer avatar Dec 27 '17 11:12 kaikreuzer

@kaikreuzer I've created a little POC that serializes all the items to the textual form. I've used a piece of code I wrote for Home Builder and slightly modified it.

Unpack the following into your /openhab-conf/html/: exporter.zip (updated - added groupType)

And then go to http://youropenhab:8080/static/exporter/

You'll see a list of all your items nicely aligned.

Note that this is just a proof of concept. I thought of making it a part of Home Builder, but I'm not sure it fits there.

kubawolanin avatar Dec 27 '17 19:12 kubawolanin

For the sake of consistency I'd suggest doing it in a different way though: Creating EMF model instances (i.e. these EObject derrived classes we have in the model bundles) created from the database entries and then Xtext takes care of the serialization. The only drawback I see is that this actually requires having the Xtext stuff installed, but I guess we can consider this a given for such use-cases.

sjsf avatar Dec 28 '17 10:12 sjsf

The only drawback I see is that this actually requires having the Xtext stuff installed

I am not fully convinced that this is the only drawback. I am a bit concerned that we might have to dive deeply into Xtext specific stuff regarding adding formatting & layout meta-data to the EMF models in order to have the generation done in a user friendly format. Something that might be much easier to handle, if we simply serialise it ourselves. And as our aim is not a bi-directional synchronisation for the models, Xtext might be a heavy tool to do so.

I see the point that we would have an additional place to maintain if any DSL syntax changes are done, but this is something I could possibly live with as it does not occur very often.

kaikreuzer avatar Jan 02 '18 10:01 kaikreuzer

We already have it for Items. For Things it looks a bit - errr... empty. But it doesn't look too complicated.

I agree, Xtext is a heavy tool just for pretty-printing some text. Nevertheless, as long as the DSL files matter at all, Xtext is sticking around anyway. And it gives us a pretty type-safe way for all that, i.e. it nicely breaks the build if something changes.

sjsf avatar Jan 02 '18 11:01 sjsf

But it doesn't look too complicated.

Yes, the first 80% of the formatting requirements will be easy to do with it 😛

as long as the DSL files matter at all, Xtext is sticking around anyway

Who tells you that no one might come up with an alternative lightweight parser for those files?

kaikreuzer avatar Jan 02 '18 11:01 kaikreuzer

Who tells you that no one might come up with an alternative lightweight parser for those files?

Can it do the pretty-printing too? 😉 Okayokay, point taken.

sjsf avatar Jan 02 '18 12:01 sjsf

Hi, just in case someone is still interested in exporting things to config files. I recently did it with my homematic setup by parsing the json file and formatting this with jq to things. Maybe thats a solution for others too. Of course the filter rules must be adapted to the specific json file format...

pi:/srv/openhab2-userdata/jsondb$ jq -r '.[].value|"Thing \(.properties.modelId) \t \(.properties.serialNumber) \t \(.label)"' org.eclipse.smarthome.core.thing.Thing.json

juehv avatar Aug 12 '18 13:08 juehv

@kubawolanin That is awesome and exactly what I was looking for! I have one thing that I am missing though. The output is lacking the channel information. From what I see in the js code it seems that you already have a method #getChannels to add channel information, but since the json for items does not contain channel information it does not work. I guess you just stopped the proof of concept there. Since the information is available in /rest/links, I tried to load that as well and pass it in as an additional argument into #getItems, from there on to #getTextualItems and then finally to #getChannels. I wanted to contribute this, but unfortunately I am a java script noob and failed miserably to do so. Do you think it would be possible to add that?

I think in combination with what @juehv wrote to export things, this should be all the tooling one would need to get most of the ui configuration to the conf files.

At least having the channel information as well in the exporter page would solve all my problems, if you excuse my selfishness ;-)

inctom avatar Aug 15 '18 11:08 inctom

I've been struggling with how to edit items created in Simple Mode or .items -files. I also wanted a nice list of all my items, AND be able to add tags to them for Google Home support.

Triggered by @kubawolanin 's POC above...

@kaikreuzer I've created a little POC that serializes all the items to the textual form. I've used a piece of code I wrote for Home Builder and slightly modified it.

... I thought; let the script do all formatting so you can easily edit your items and then import them to OH again. It also allows for editing of all items in PaperUI regardles how they first got created.

HowTo: Move ALL items to JSON dB for easy editing in PaperUI and tagging for Google Home (and Alexa)

giPeteR avatar Jan 23 '19 11:01 giPeteR