Skip to content

Commit

Permalink
Merge branch 'adjPoeMode'
Browse files Browse the repository at this point in the history
  • Loading branch information
mannkind committed Feb 22, 2024
2 parents 98dd2e5 + b1cf2ca commit 529fb13
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 66 deletions.
54 changes: 9 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,17 @@ A quick and dirty script to take action on a QNAP QSW-M2116P-2T2S.
./qnap-qsw login --host switch.lan --password BLAH
```

### Disable POE Ports
#### With Password
### Disable/Enable POE Ports
```
./qnap-qsw poeMode --host switch.lan --password BLAH --ports 1,2,3,4 --mode disabled
```
#### With Token
```
token=$(./qnap-qsw login --host switch.lan --password BLAH)
./qnap-qsw poeMode --host switch.lan --token $token --ports 1,2,3,4 --mode disabled
```

### Enable POE Ports
#### With Password
```
./qnap-qsw poeMode --host switch.lan --password BLAH --ports 1,2,3,4 --mode poe+
./qnap-qsw poeMode --host switch.lan --password BLAH --ports 19,20 --mode poe++
```
#### With Token
```
token=$(./qnap-qsw login --host switch.lan --password BLAH)
./qnap-qsw poeMode --host switch.lan --token $token --ports 1,2,3,4 --mode poe+
./qnap-qsw poeMode --host switch.lan --token $token --ports 19,20 --mode poe++
./qnap-qsw poeMode --host switch.lan --password BLAH --disable-ports 1,2,3,4
./qnap-qsw poeMode --host switch.lan --password BLAH --disable-ports 1,2 --poeplus-ports 3,4 --poeplusplus-ports 19,20
```

### Home Assistant

#### shell_commands.yaml
```
qnap_qsw: "/qnap-qsw login --host {{ host }} --password {{ password }}"
qnap_qsw_poe: "/config/qnap-qsw poeMode --host {{ host }} --token {{ token }} --ports {{ ports }} --mode {{ mode }}"
qnap_qsw_poe: "/config/qnap-qsw poeMode --host {{ host }} --password {{ password }} --disable-ports '{{ disable_ports }}' --poe-ports '{{ poe_ports }}' --poeplus-ports '{{ poeplus_ports }}' --poeplusplus-ports '{{ poeplusplus_ports }}'"
```

#### automations.yaml
Expand All @@ -57,17 +38,11 @@ qnap_qsw_poe: "/config/qnap-qsw poeMode --host {{ host }} --token {{ token }} --
seconds: 0
condition: []
action:
- service: shell_command.qnap_qsw
data:
host: switch.lan
password: <password here>
response_variable: login
- service: shell_command.qnap_qsw_poe
data:
host: switch.lan
token: "{{ login['stdout'] }}"
ports: 1,2,3,4,5,6,7,8,9,10,11,12,13,14,16,19,20
mode: disabled
password: <password here>
disable_ports: 1,2,3,4,5,6,7,8,9,10,11,12,13,14,16,19,20
mode: single
- alias: "Turn on non-essential POE ports after power outage"
description: ""
Expand All @@ -83,22 +58,11 @@ qnap_qsw_poe: "/config/qnap-qsw poeMode --host {{ host }} --token {{ token }} --
seconds: 0
condition: []
action:
- service: shell_command.qnap_qsw
data:
host: switch.lan
password: <password here>
response_variable: login
- service: shell_command.qnap_qsw_poe
data:
host: switch.lan
token: "{{ login['stdout'] }}"
ports: 1,2,3,4,5,6,7,8,9,10,11,12,13,14,16
mode: poe+
- service: shell_command.qnap_qsw_poe
data:
host: switch.lan
token: "{{ login['stdout'] }}"
ports: 19,20
mode: poe++
password: <password here>
poeplus_ports: 1,2,3,4,5,6,7,8,9,10,11,12,13,14,16
poeplusplus_ports: 19,20
mode: single
```
49 changes: 35 additions & 14 deletions cmd/poeMode.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,38 @@ var poeModeCmd = &cobra.Command{

// Set the POE interface statuses
wg := sync.WaitGroup{}
ch := make(chan error, len(poeModeOpts.Ports))
for _, port := range poeModeOpts.Ports {
portIdx := port
properties, ok := knownInterfaces[portIdx]
if !ok {
continue
}
allPortsCount := len(poeModeOpts.DisablePorts) + len(poeModeOpts.PoePorts) + len(poeModeOpts.PoePlusPorts) + len(poeModeOpts.PoePlusPlusPorts)
ch := make(chan error, allPortsCount)
portsAndModes := []struct {
ports []string
mode string
}{
{ports: poeModeOpts.DisablePorts, mode: "disabled"},
{ports: poeModeOpts.PoePorts, mode: "poe"},
{ports: poeModeOpts.PoePlusPorts, mode: "poe+"},
{ports: poeModeOpts.PoePlusPlusPorts, mode: "poe++"},
}

properties.Mode = qnap.POEModes.Unknown.FromString(poeModeOpts.Mode)
wg.Add(1)
go func() {
ch <- q.UpdatePOEInterfaces(&wg, portIdx, properties)
}()
for _, portsAndMode := range portsAndModes {
for _, port := range portsAndMode.ports {
// Skip empty ports
if port == "" {
continue
}

portIdx := port
properties, ok := knownInterfaces[portIdx]
if !ok {
continue
}

properties.Mode = qnap.POEModes.Unknown.FromString(portsAndMode.mode)
wg.Add(1)
go func() {
defer wg.Done()
ch <- q.UpdatePOEInterfaces(portIdx, properties)
}()
}
}

wg.Wait()
Expand Down Expand Up @@ -76,6 +95,8 @@ func init() {
rootCmd.AddCommand(poeModeCmd)

poeModeCmd.Flags().StringVar(&poeModeOpts.Token, "token", "", "The token representing the admin user; use this or password")
poeModeCmd.Flags().StringSliceVar(&poeModeOpts.Ports, "ports", []string{}, "The ports to modify")
poeModeCmd.Flags().StringVar(&poeModeOpts.Mode, "mode", "disable", "The mode of the ports to modify")
poeModeCmd.Flags().StringSliceVar(&poeModeOpts.DisablePorts, "disable-ports", []string{}, "The ports to disable")
poeModeCmd.Flags().StringSliceVar(&poeModeOpts.PoePorts, "poe-ports", []string{}, "The ports to enable poe")
poeModeCmd.Flags().StringSliceVar(&poeModeOpts.PoePlusPorts, "poeplus-ports", []string{}, "The ports to enable poe+")
poeModeCmd.Flags().StringSliceVar(&poeModeOpts.PoePlusPlusPorts, "poeplusplus-ports", []string{}, "The ports to poe++")
}
9 changes: 6 additions & 3 deletions cmd/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ type rootCommandOptions struct {
}

type poeModeCmdOptions struct {
Token string
Ports []string
Mode string
Token string
DisablePorts []string
PoePorts []string
PoePlusPorts []string
PoePlusPlusPorts []string
Mode string
}
5 changes: 1 addition & 4 deletions qnap/qnap.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"io"
"net/http"
"sync"
"time"
)

Expand Down Expand Up @@ -110,9 +109,7 @@ func (q *QNAP) POEInterfaces() (map[string]interfacesValueResponse, error) {
return interfaces, nil
}

func (q *QNAP) UpdatePOEInterfaces(wg *sync.WaitGroup, port string, properties interfacesValueResponse) error {
defer wg.Done()

func (q *QNAP) UpdatePOEInterfaces(port string, properties interfacesValueResponse) error {
// Setup and send the request
url := fmt.Sprintf(interfaceURL, q.host)
marshaledProperites, err := json.Marshal(properties)
Expand Down

0 comments on commit 529fb13

Please sign in to comment.