ztalbot2000 / homebridge-cmd4

CMD4 Plugin for Homebridge - Supports ~All Accessory Types & now all Characteristics too
Apache License 2.0
149 stars 14 forks source link

[Support] Windows OS Using Shell Scripts #118

Open mitch7391 opened 2 years ago

mitch7391 commented 2 years ago

Describe Your Problem:

Hey @ztalbot2000 I hope you do not mind that I open an issue to try grab the attention of any of your Windows OS users using bash/shell scripts; I have tried on Reddit and had no bites. I really want to try and get installation instructions going for my repo.

I am looking for a user of homebridge-cmd4 that has their Homebridge server living on Windows OS and are using bash/shell scripts for cmd4. In particular, I would be interested in the following:

  1. Where are you scripts located in Windows?
  2. How does your "state_cmd" look?
  3. Anything else of note you needed to do that may be different to Raspbian/macOS.

If you feel this is not the place to ask this John, just close it out. Otherwise I will leave it open for some time and close it out if I get no bites again. I know I constantly peruse the issues of plug-ins I have installed; so I am hoping others do and see this.

crbyxwpzfl commented 2 years ago

this are some old notes I toke while I was running homebridge on windows with bash scripts. Perhaps they help.

  1. Does not matter. point to them with full path not relative path in state cmd.
  2. "state_cmd": "C:\\Users\\Desktop\\onedrive\\nodejs\\homebridge-config\\cmd4scripts\\VolumeWinCurl.bat"
  3. Line endings caused me headaches but this could have been just me. And running it as a service did not work for me. I even tried running it in Linux Subsystem maybe thats a alternative If you already got it working on Linux. I ended up running homebridge in a cmd window. see this issue

All of this info is old and can be wrong. I’m not very good in programming hope it helps a bit anyways.

    HOMEBRIDGE-SETUP
        PORTABLE INSTALL
            Get binaries from node.js
            cd to local install location then
            test node.js -v and npm -v in install dir
            use homebruidge -U "PATH TO CONFIG" to run homebridge

        WITH Node.js Installer directories then for plugins are 
            C:\Users\AppData\Roaming\npm\node_modules
            C:\Users\.homebridge\

        NPM commands
            npm outdated -g             //check for outdated plugins
            npm update -g               //Installiert alle Updates
            npm uninstall Pluginname -g //Deinstallieren eines Plugins
            npm ls                      //Installierte Plugins anzeigen

    HOMEBRIDGE-CMD4 
        DO NOT RUN AS SERVICE!

        ALWAYS SPECIFY FULL PATH IN CONFIG statecmd for running homebridge in Linux subsystem
        bash /mnt/c/Users/.homebridge/Cmd4Scripts/VOLUME.sh

        Check EOL End of line versions use Unix(LF)! not windows(CRLF) for.sh scripts

more notes on bash

        statecmd -> bash .homebridge/Cmd4Scripts/PS4.sh Get 'PS4' 'Brightness' //call script mit parametern $1 $2 $3 $4

        ACHTUNG .sh dateien mit LF (linux standard) EOL (end of line) characters speichern!! 
        NICHT mit CRLF (windows standard)

        IN VSCODE bottom right purple button
        ODER NOTEPAD++ EOL conversion

        ERROR message indicating wrong EOL endings "unexpected end of file"

        ATENTION NOT EQUIVALENT!!
        bash .../test.sh PARAMETER  if [ "$1" = "..." ]
        bash .../test.sh 'PARAMETER'    if [ "$1" = "'...'" ]
mitch7391 commented 2 years ago

@crbyxwpzfl thank you for your notes, I was hoping a user might be lurking the issues, find this and help me out :) I think in particular I was having issues with the file paths being path\to\bash\script.sh, but then the config.json requiring it in the format of path/to/bash/script.sh and figured I was doing something wrong here. I will refer to your notes and try again; thank you!

crbyxwpzfl commented 2 years ago

just found a old config of mine. yes I remember having trouble with that too
perhaps escaping the command for json is the problem
I think escaping the path like this worked when running homebridge in a cmd window on windows with batch scripts I used this tool a lot https://www.freeformatter.com/json-escape.html

            "platform": "Cmd4",
            "name": "Cmd4",
            "accessories": [
                {
                    "type": "Lightbulb",
                    "displayName": "volume",
                    "on": "FALSE",
                    "brightness": 10,
                    "stateChangeResponseTime": 1,
                    "state_cmd": "C:\\Users\\Desktop\\onedrive\\nodejs\\homebridge-config\\cmd4scripts\\VolumeWinCurl.bat"
                }

perhaps something like tihs works for bash scripts

"state_cmd": "bash \"C:\\Users\\Desktop\\onedrive\\nodejs\\homebridge-config\\cmd4scripts\\VolumeWinCurl.bat\""

asuming you can run this command in the same terminal you run homebridge in
bash "C:\Users\Desktop\onedrive\nodejs\homebridge-config\cmd4scripts\VolumeWinCurl.bat"
I escaped this command with the tool above. the output looked like this
bash \"C:\\Users\\Desktop\\onedrive\\nodejs\\homebridge-config\\cmd4scripts\\VolumeWinCurl.bat\"
and then pasted this into the "state_cmd": "<json escaped command>"

mitch7391 commented 2 years ago

Oh wow, you are a champion! This may be the missing piece of the puzzle! Thank you so much for this information; I will do a little testing on my days off and report back to you how it went.

mitch7391 commented 2 years ago

@crbyxwpzfl I think my issue might be further along with trying to use my shell script with Windows than just the "state_cmd" issue. Naturally only assuming that was the issue, after trying the json escape and still coming up short with feedback from my scripts, I decided to try a direct command in the terminal from script (I should have done this sooner...):

C:\Windows\System32>curl -s http://192.168.0.173:2025/getSystemData | jq '.aircons.ac1.zones.z01.measuredTemp'
jq: error: syntax error, unexpected INVALID_CHARACTER, expecting $end (Windows cmd shell quoting issues?) at <top-level>, line 1:
'.aircons.ac1.zones.z01.measuredTemp'
jq: 1 compile error

C:\Windows\System32>curl -s http://192.168.0.173:2025/getSystemData | jq ".aircons.ac1.zones.z01.measuredTemp"
24.2

So already there is a difference between how Raspbian/macOs uses jq vs how Windows OS does; how annoying... Which means I would need a different script for Windows OS users; or a different approach altogether.

crbyxwpzfl commented 2 years ago

Just a quick response. I’m not at my pc. I don’t know what jq is. Did you check for Unix(LF) Line endings (EOL). windows uses CRFL. Bash for example needs LF line endings.

mitch7391 commented 2 years ago

Hey @crbyxwpzfl,jq is essentially a json parsing tool; so in my repo I am using it to pull out data I need from a json data array and use it in my script.

Did you check for Unix(LF) Line endings (EOL). windows uses CRFL. Bash for example needs LF line endings.

I am not sure I understand this part sorry. I might have to do a bit of Googling; still learning as I go. I just know the json escape didn’t work for me and discovered the above instead.

crbyxwpzfl commented 2 years ago

If you edit a script in windows notepad and save it it will be saved with windows line endings CRFL. Some Programms don’t like that. I had this issue with bash scripts. You can choose to save a file with Unix line endings LF with most programming tools like notepad++ or vscode just google how to do that it’s some option usually not too difficult. just did a quick search specific to jq and found this on stack Hope it helps and isn’t a waste of time. But I remember the $end some how haha

mitch7391 commented 2 years ago

Thanks for all this effort @crbyxwpzfl, it is really appreciated. I might not get a chance to test this for some days as I am tied up in some other work at the moment; but I will let you know how I go when I do get to it.