Last active
October 22, 2020 22:15
-
-
Save DanEdens/a68c6f1ada9092cd22edafd66c412768 to your computer and use it in GitHub Desktop.
This method of piping commands is independent of EG, and will start it up if not already running. This is very useful if I need to restart it remotely. Events are sent to Eventghost through subscribing to topic "ghost" on my MQTT broker.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This method of piping commands is independent of EG, and will start it up if not running, | |
# This is very useful if I need to restart it remotely. | |
# Events are sent to Eventghost through subscribing to topic "ghost" on my MQTT broker | |
# First, I use Windows task scheduler to run WSL on Windows boot. | |
# > cat bootpm2onwsl.bat | |
# sleep 5 | |
# wsl pm2 start "sh ~/bin/mqtt_eg_link.sh" --watch & | |
# Since WSL has no init, This is not 100% reliable, | |
# so I have added a redundent call to "pm2 resurrect" in my .bashrc | |
# This is not the best method to init, but so far the added resurrect has had no ill effects | |
# Add the below function to your .bashrc in order to fix the interop socket. | |
# Be sure to replace the broker and port with your own. | |
################################################################################ | |
# fix_wsl2_interop() { | |
# for i in $(pstree -np -s $$ | grep -o -E '[0-9]+'); do | |
# if [[ -e "/run/WSL/${i}_interop" ]]; then | |
# export WSL_INTEROP=/run/WSL/${i}_interop | |
# fi | |
# done | |
# } | |
# export MOSTHOST='<YOUR BROKER>' | |
# export MOSTPORT='1880' | |
# export -f fix_wsl2_interop | |
# fix_wsl2_interop | |
# pm2 resurrect | |
################################################################################ | |
# For more information, see this thread. | |
# https://github.com/chenyanming/chenyanming.github.io/issues/1 | |
# Optional Pub boot message | |
d=$(date +%Y-%m-%d) | |
mosquitto_pub -h $MOSHOST -p $MOSPORT -t status/ghost/log -m "$d >> Wsl tunnel running" | |
# For piping directly into EG. Msg is sent directly as an event with no payload | |
# TODO: Check for eg.bat in path, and default to this if not found. | |
# mosquitto_sub -h $MOSHOST -p $MOSPORT -t ghost | xargs -t -I {} cmd.exe /c EventGhost.exe -e {} | |
# To get Event with payload, We need to first route it through the below batch file | |
# This uses the first argument as Event, and sends the rest of the string as payload. | |
mosquitto_sub -h $MOSHOST -p $MOSPORT -t ghost | xargs -t -I {} cmd.exe /c eg.bat {} | |
# > cat eg.bat | |
# | |
# Credit for this to https://github.com/kdschlosser, taken from a forum post | |
# Make sure to place this in your path. | |
# | |
@echo off&setlocal enabledelayedexpansion | |
# for /f "tokens=1" %%G IN (%1) DO set event=%%G | |
# set _tail=%~1 | |
# call set _tail=%%_tail:*%event% =%% | |
# set payload=%_tail% | |
# echo %event% | |
# echo %payload% | |
# for /f "useback tokens=*" %%a in ('%event%') do set event=%%~a | |
# for /f "useback tokens=*" %%a in ('%payload%') do set payload=%%~a | |
# | |
# call :l_replace "%payload%" "=" ":" | |
# | |
# call "C:\Program Files (x86)\EventGhost\EventGhost.exe" -event "%event%" "%payload%" | |
# goto end | |
# | |
# :l_replace | |
# set "payload=x%~1x" | |
# :l_replaceloop | |
# for /f "delims=%~2 tokens=1*" %%x in ("!payload!") do ( | |
# if "%%y"=="" set "payload=!payload:~1,-1!"&exit/b | |
# set "payload=%%x%~3%%y" | |
# ) | |
# goto l_replaceloop | |
# | |
# :end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment