ãªãŒãã£ãªããã€ã¹ãMacã«æ¥ç¶ããæã«Denoã§SwitchBotã®APIãå©ã
2024/04/14
äŒè°äžã«ã©ã€ããç¹ç¯ãããã
å æ¥ããªã³ã©ã€ã³ããŒãã£ã³ã°ãå§ãŸã£ããèªåã§ç¹ç¯ãããªã³ãšã¢ãŒããªã³ã©ã€ãäœã£ã ãšããèšäºãèªãã§èªåããããããªããæ©éã©ã€ããšSwitchBotã®ã¹ããŒããã©ã°ãè³Œå ¥ããŸããã
ãã¡ãã®èšäºã§ã¯ã«ã¡ã©ã®ç£èŠãlsofã§è¡ãããšã§ãªã³ã©ã€ã³ããŒãã£ã³ã°äžã§ããããšãå€å¥ããŠããŸããããèªåã®ç°å¢ã®å Žåã¯å¿ ãããã«ã¡ã©ãONã«ããŠããããã§ã¯ãªãã£ãã®ã§ãå¥ã®æ¹æ³ã§ãªã³ã©ã€ã³ããŒãã£ã³ã°ãå€å¥ããããªããšèª¿ã¹ãŠãããšããã¬ã¯ãŒã¯äžãWEB äŒè°ã®ã²ãšå·¥å€«ãšããèšäºã§ãªãŒãã£ãªããã€ã¹ã®æ¥ç¶ã«ãã£ãŠå€å¥ããããšãã§ãããšç¥ãããã¡ããåèã«ãããŠãããå®è£ ãé²ããŸããã
å®è£
å®æãããã®ã¯ãã¡ãã§ãã
ããããšã¯ä»¥äžã§ãã
plist
ã®WatchPaths
ã«ãããªãŒãã£ãªããã€ã¹ã®èšå®ãã¡ã€ã«ã®å€æŽãç£èŠãã
- èšå®ãã¡ã€ã«ãå€æŽãããæãDenoãå®è¡ãã
ãªãŒãã£ãªããã€ã¹ã®èšå®ãã¡ã€ã«ã®å€æŽãç£èŠãã
macã«ã¯launchctl
ãšããã³ãã³ããšä»çµã¿ããããããã䜿ãããšã§ç¹å®ã®ãã¡ã€ã«ã®ç£èŠãã³ãã³ãã®å®æå®è¡ãªã©ãèšå®ã§ããŸãã詳ããã¯man launchd.plist
ããŠã¿ãŠãã ããã
com.mrskiro.on-air.plist
ãšãããã¡ã€ã«ãçšæããŸãããã¡ã€ã«åã¯äžæã§ããã°äœã§ãããã®ããªãšæããŸãããæ
£äŸçã«com.xxxã®ããã«ããŸããã
ãã®ãã¡ã€ã«ã¯PCåèµ·åããŠãèªã¿èŸŒãŸããããã«ã/Library/LaunchAgents
ã«ã·ã³ããªãã¯ãªã³ã¯ã貌ã£ãŠãããšè¯ãã§ãã
以äžã®ãããªå 容ãæå®ããŸãã
- Label
- äžæãªãã®
- WatchPaths
- ãªãŒãã£ãªããã€ã¹ã®èšå®ãã¡ã€ã«ãç£èŠãããã®ã§
/Library/Preferences/Audio/com.apple.audio.SystemSettings.plist
ãæå®ããŸã
- ãªãŒãã£ãªããã€ã¹ã®èšå®ãã¡ã€ã«ãç£èŠãããã®ã§
- ProgramArguments
- WatchPathsã«æå®ãããã¹ã®å 容ãå€åããæã«å®è¡ããã³ãã³ããæå®ããŸã
- StandardOutPath
- StandardErrorPath
- ProgramArgumentsã§æå®ããã³ãã³ãã®åºåå ã決ããŸããä»åã§ããã°ãããã°ã«äœ¿ããŸã
- äžèŠã§ããã°æå®ããªããã
/dev/null
ã«ããŠããã°ãããã ãšæããŸã
å šäœåã¯ä»¥äžã§ãããã«ãã¹ã«GOPATHã®ãªããããããŸããæ°ã«ããªãã§äžããã
ãŸãããã®ãã¡ã€ã«ã¯ã·ã§ã«ã¹ã¯ãªããã§åçã«çæããããã«ããŠããŸããçç±ã¯åŸè¿°ããŸãã
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.mrskiro.on-air</string>
<key>WatchPaths</key>
<array>
<string>/Library/Preferences/Audio/com.apple.audio.SystemSettings.plist</string>
</array>
<key>ProgramArguments</key>
<array>
<string>/opt/homebrew/bin/deno</string>
<string>run</string>
<string>--allow-write</string>
<string>--allow-run</string>
<string>--allow-read</string>
<string>--allow-env</string>
<string>--allow-net</string>
<string>/Users/purpleeeee/go/src/github.com/mrskiro/on-air/scripts/index.ts</string>
</array>
<key>StandardOutPath</key>
<string>/Users/purpleeeee/go/src/github.com/mrskiro/on-air/log.output.log</string>
<key>StandardErrorPath</key>
<string>/Users/purpleeeee/go/src/github.com/mrskiro/on-air/log.err.log</string>
</dict>
</plist>
èšå®ãã¡ã€ã«ãå€æŽãããæã«Denoãå®è¡ãã
ProgramArgumentsã«æå®ããscripts/index.ts
ãçšæããŸãã
WatchPathsã«ããç£èŠã¯ããã¡ã€ã«å€æŽã¯æç¥ã§ãããã®ã®äœãå€æŽãããããŸã§ã¯ããããŸããããã®ããProgramArgumentsã«ãã£ãŠå®è¡ãããããã°ã©ã ïŒä»åã¯DenoïŒã®äžã§å€å¥ããäžã§ãå¿ èŠã§ããã°SwitchBotã®APIãcallããããã«ããŸãã
å šäœåã¯ãã¡ãã
è©°ãŸã£ãç¹
èªèšŒ
SwitchBotAPIã®READMEã«JavaScriptã®ãµã³ãã«ããããŸããããããDenoã«çœ®ãæããã®ã«ããæéãããããŸããã
https://deno.land/[email protected]/node/crypto.ts ã䜿ã£ããããŠãªãããããããŠãŸããããæçµçã«ã¯https://deno.land/x/[email protected]/mod.tsã§hmac
ããã°ããããšã«æ°ã¥ããŠã¯ãªã¢ããŸããã
plistå ã§å€æ°ã䜿ããªã
plistã«èšè¿°ããå®è¡ã³ãã³ãã¯ãã«ãã¹ã§ããå¿
èŠããããããäŸãã°denoã¯brewã§ã€ã³ã¹ããŒã«ããŠãããš/opt/homebrew/bin/deno
ãšæžãå¿
èŠããããŸãã
ããããéçã«æžããŠãããã£ãã®ã§ãããè€æ°PCã§åããããšãæ³å®ããããšããããç°å¢äŸåããããªãããplistãã¡ã€ã«ãã·ã§ã«ã¹ã¯ãªããã§çæããããã«ããŸãããã·ã§ã«ã¹ã¯ãªããå ã§ããã°åçã«åçš®pathãåããããŸãã
#!/bin/sh
WORKING_DIR=$(pwd)
OUTPUT_LOG_PATH="$WORKING_DIR/log.output.log"
ERROR_LOG_PATH="$WORKING_DIR/log.err.log"
DENO=$(which deno)
SCRIPT_PATH="$PWD/scripts/index.ts"
OUTPUT_FILE="$WORKING_DIR/com.mrskiro.on-air.plist"
CONTENT=$(cat <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.mrskiro.on-air</string>
<key>WatchPaths</key>
<array>
<string>/Library/Preferences/Audio/com.apple.audio.SystemSettings.plist</string>
</array>
<key>ProgramArguments</key>
<array>
<string>$DENO</string>
<string>run</string>
<string>--allow-write</string>
<string>--allow-run</string>
<string>--allow-read</string>
<string>--allow-env</string>
<string>--allow-net</string>
<string>$SCRIPT_PATH</string>
</array>
<key>StandardOutPath</key>
<string>$OUTPUT_LOG_PATH</string>
<key>StandardErrorPath</key>
<string>$ERROR_LOG_PATH</string>
</dict>
</plist>
EOF
)
echo "$CONTENT" > "$OUTPUT_FILE"
echo "The plist file has been created: $OUTPUT_FILE"
ãã£ãŠã¿ãŠ
éäžãŸã§ã¯Node.jsã§æžããŠãŸããããcryptoåšãã§åã欲ãããªã£ãŠããã¿ã€ãã³ã°ã§tsxããts-nodeãçšæããã®ãé¢åã§Denoã«ç§»è¡ããŸããã
ãµã¯ããšã¹ã¯ãªããæžãã®ã«DenoããšãŠã䟿å©ã ã£ãã®ã§ãŸãæ©äŒãããã°äœ¿ãããã
ãããŠããŒãã£ã³ã°ã§ã©ã€ããå ãã®ãã£ãããã