Eliminating driver code mess

This sounds perfect.... I think.

Gradle gradle gradle...

@chuck.schwer
You are talking about the Developer Tool? Had to look it up because I was not aware of what Gradle was. Even so... sounds like it would just be another (more advanced) IDE to work from. That would allow libraries (or equivalent).

Yes, its a tool to compile/build code projects and it can be extended using groovy. Its not an IDE it just runs through a set of tasks to build a project. Its similar to Ant and Maven. https://gradle.org/

I was actually thinking the exact same thing... Gradle can probably do this, and is a pretty good idea.

I should have looked into that before, but I am lazy so just keep copy/pasting code sections over and over. :smile:

1 Like

Sounds awesome... so far, at least. Is there an API to replace driver code in the hub from Gradle? :slight_smile:

But... is it a better solution than shared libraries? Why? Code reuse, and OOP is the foundation of everything we do, we even have the built-in Zigbee class we can use, why can't we have our own classes?

1 Like

It has been "fun" and time consuming enough for me working on learning the basics of Groovy, Hubitat, ZigBee and so on so far... Not sure I want to dive into something else at this moment. But... If it continues to be a problem and with no other solution in sight, I might look into it when I get time.

Because you can do it today. versus waiting for a feature that may never be added. Even if I told you today that its a great idea and we will implement it eventually, how long are you going to wait for that?

:smile: You might be surprised how lazy some of us can be at times.

2 Likes

No comment :wink:

I just need you guys to be slightly more lazy.. to the point where asking for this feature is too much work :stuck_out_tongue:

8 Likes

LOL!!! :smile:

+1

I haven't touched my drivers for months.
Simply because I got tired of hunting down copy-paste bugs. If you tell me you put this feature to the end of your list with the lowest possible priority, that's enough for me. I won't ask for deadlines, I promise. :slight_smile:

Someone else will. :smiley:

1 Like

Reviving this old thread just to say that I do this using a Python script which can automatically re-use code for my drivers and then push the changes to the hub. With the script configured I press Build in VSC and just wait for the hub to be updated a few seconds later. Any errors during upload are caught and displayed in the console of VSC.
It was never really developed to be used by anyone besides myself, but maybe it might be worth adapting if there's interest and someone else also wants to share the development of this tool?

1 Like

Iā€™d be super interested in seeing how you did this.

The script is on my GitHub together with my drivers for Tasmota 7.x:

As I mentioned, they were developed to be run by me alone, but could be easily made into a class and made much more reusable and useful to others. It uses eval() so it's not directly safe, but it works and I could write it quickly.

EDIT: I updated the code a bit, it is now in classes and should be useful to anyone who knows Python and wants to reuse code in their apps and drivers as well as to keep code/code snippets in multiple files and merging/reusing them for deployment. The upload/update of code function might also come in handy so there is no need for copy/paste and you can still work in the editor of your choice. I run this in Visual Studio Code, and it is like local compile/deploy. Very fast... If there truly is interest in this, I'll create a release thread...

4 Likes

I have started to develop drivers recently and I'm already very interested in your method @markus.
Being an OO programmer for over 20 years made me confirm that code duplication is the source of all evil. All the copy&paste involved in the regular driver development is hurting me badly.

1 Like

About a week into developing for HE I had enough and started to write on this thing so I understand what you mean :slight_smile: It doesn't have documentation but it's all in there, if you need specific pointers ask away :slight_smile:

1 Like

OK, that's a job for me! :wink:

Any news regarding the reuse of code (e.g. creating my own libs)?

For me something simple as an include directive would be good enough, that would be interpreted as a textual import. Example:

MyLibraryApp:
Contains some very sophisticated reusable methods... :sunglasses:

String translateToChinese(String german) {
...very very sophisticated code :grin:
}

MyFirstApp:

import MyLibraryApp
String chineseHallo = translateToChinese('Hallo')

MySecondApp:

import MyLibraryApp
String chineseGutenTag = translateToChinese('Guten Tag')

MyFirstDriver:

import MyLibraryApp
String chineseAufWiedersehen = translateToChinese('Auf Wiedersehen')

etc. pp.

1 Like

I just don't see this happening, as I understand it there's too much limitation on the sandboxed environment e.g. you can't create classes, where would you "load" your libraries to in order to be able to import them, etc.

You'd then have the same issue with "where" to host that file that needs to be Included. And driver / app management then becomes more involved because you have to upload both the driver / app file and all the "includes".

This can already be done at the source level using IDE / build tools, so doesn't really bring anything helpful to the table IMO.

I think Markus was using a complex Python build tool to build his drivers from segmented code files.

I use a more simple approach in VSCode, all my "common" code is in a separate file and I just use a VSCode task to merge that into my drivers, which are mostly just simple stubs. Ctrl-Shift-B and the drivers are built and ready for upload ....

1 Like

A "lib" would be just another app on my Hubitat.

That's an easy one:
Exactly there, where all my other apps are saved - on my Hubitat.

But that needs error-prone copy'n'paste actions...
And it's not an Hubitat only solution...

Sure but the platform isn't built like that, you can't "load" another App / Driver from code. When an App / Driver is saved in the editor it has to meet at least bare minimum framework requirements, you can't just save arbitrary code - try and save a file that doesn't -

Not sure what you mean by that, there's no copy'n'paste involved.

This is my task in VSCode.

{

    // See https://go.microsoft.com/fwlink/?LinkId=733558

    // for the documentation about the tasks.json format

    "version": "2.0.0",

    "tasks": [

        {

            "label": "build",

            "type": "shell",

            "command": "${workspaceFolder}/hubitat/build/build.cmd",

            "options": {

                "shell": {

                    "executable": "C:\\Windows\\System32\\cmd.exe",

                    "args": [

                        "/d",

                        "/c"

                    ]

                },

                "cwd": "${workspaceFolder}/hubitat/build"

            },

            "presentation": {

                "echo": true,

                "reveal": "always",

                "focus": false,

                "panel": "shared",

                "showReuseMessage": false,

                "clear": true

            },

            "group": {

                "kind": "build",

                "isDefault": true

            },

            "problemMatcher": []

        }

    ]

}

And this is a single line from my build.cmd:

type ..\"Sonoff SNZB-01 Wireless Switch.groovy" ..\"Common Functions.groovy" ..\"ZigBee Framework.groovy" > "Sonoff SNZB-01 Wireless Switch.groovy"

The output is "Sonoff SNZB-01 Wireless Switch.groovy" which is a fully ready to go driver.

I also upload them to a local web server:

"C:\Program Files (x86)\PuTTY\pscp.exe" -P 22 *.groovy user@my.local.web.server:/var/www/html/hubitat

Each driver / app has the importUrl set:

importUrl: "http://my.local.web.server/hubitat/Sonoff SNZB-01 Wireless Switch.groovy"

As I said, Ctrl-Shift-B and all my drivers are built and uploaded ..... then it's just a case of hitting "import" on one of the Hubitat hubs to update the driver / app there.

3 Likes