-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command line actions via json config file
- Loading branch information
Showing
6 changed files
with
139 additions
and
3 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package actions | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"io/ioutil" | ||
"log" | ||
"os/exec" | ||
) | ||
|
||
type ActionName string | ||
|
||
type AirplayCommandLineAction struct { | ||
DeviceName string `json:"device_name"` | ||
Command string `json:"command"` | ||
CommandArgs string `json:"command_args"` | ||
ActionName ActionName `json:"action"` | ||
} | ||
|
||
type AirplayMusicActionRunner struct { | ||
Actions []*AirplayCommandLineAction `json:"actions"` | ||
} | ||
|
||
const ( | ||
ACTION_NAME_START_PLAYING ActionName = "start_playing" | ||
ACTION_NAME_END_PLAYING ActionName = "end_playing" | ||
) | ||
|
||
// func DefaultParams(service string) *QueryParam { | ||
func NewAirplayMusicActionRunner(configFilePath string) (*AirplayMusicActionRunner, error) { | ||
configBytes, err := ioutil.ReadFile(configFilePath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var parsedRunner AirplayMusicActionRunner | ||
err = json.Unmarshal(configBytes, &parsedRunner) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, action := range parsedRunner.Actions { | ||
if action.ActionName != ACTION_NAME_START_PLAYING && action.ActionName != ACTION_NAME_END_PLAYING { | ||
return nil, errors.New("Invalid action name") | ||
} | ||
} | ||
|
||
return &parsedRunner, nil | ||
} | ||
|
||
func (r *AirplayMusicActionRunner) RunActionForDeviceState(deviceName string, isPlaying bool) { | ||
for _, action := range r.Actions { | ||
if action.DeviceName == deviceName { | ||
if (isPlaying && action.ActionName == ACTION_NAME_START_PLAYING) || (!isPlaying && action.ActionName == ACTION_NAME_END_PLAYING) { | ||
log.Printf("Running command: %v %v", action.Command, action.CommandArgs) | ||
cmd := exec.Command(action.Command, action.CommandArgs) | ||
if err := cmd.Run(); err != nil { | ||
log.Printf("Error running command: %v %v", action.Command, action.CommandArgs) | ||
} | ||
} | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package actions | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
func TestActionsParseValid(t *testing.T) { | ||
workingDirectory, _ := os.Getwd() | ||
testFile := filepath.Join(workingDirectory, "../test_files/valid_actions_1.json") | ||
log.Printf("wd: %v", testFile) | ||
|
||
actions, err := NewAirplayMusicActionRunner(testFile) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if len(actions.Actions) != 2 { | ||
t.Fatal("parse error -- wrong count") | ||
} | ||
first := actions.Actions[0] | ||
if first.DeviceName != "device1" || first.Command != "echo" || first.ActionName != "start_playing" { | ||
t.Fatal("didn't parse first action") | ||
} | ||
second := actions.Actions[1] | ||
if second.DeviceName != "Stereo" || second.Command != "echo" || second.ActionName != "end_playing" { | ||
t.Fatal("didn't parse second action") | ||
} | ||
} |
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 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"actions": [ | ||
{ | ||
"device_name": "device1", | ||
"action": "start_playing", | ||
"command": "echo", | ||
"command_args": "'value to echo device 1'" | ||
}, | ||
{ | ||
"device_name": "Stereo", | ||
"action": "end_playing", | ||
"command": "echo", | ||
"command_args": "'value to echo for stereo'" | ||
} | ||
] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"actions": [ | ||
{ | ||
"device_name": "Stereo", | ||
"action": "start_playing", | ||
"command": "echo", | ||
"command_args": "'value to echo stereo start'" | ||
}, | ||
{ | ||
"device_name": "Stereo", | ||
"action": "end_playing", | ||
"command": "echo", | ||
"command_args": "'value to echo for stereo end'" | ||
} | ||
] | ||
} |