Skip to content

Commit

Permalink
add mac support
Browse files Browse the repository at this point in the history
  • Loading branch information
abe-winter committed Dec 26, 2023
1 parent ce8d9f0 commit af07888
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 48 deletions.
49 changes: 1 addition & 48 deletions linuxwifi/linuxwifi.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
//go:build linux

// Package linuxwifi implements a wifi strength sensor
package linuxwifi

import (
"context"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/edaniels/golog"
"github.com/pkg/errors"
Expand Down Expand Up @@ -69,48 +65,5 @@ func (sensor *wifi) DoCommand(ctx context.Context, cmd map[string]interface{}) (

// Readings returns Wifi strength statistics.
func (sensor *wifi) Readings(ctx context.Context, extra map[string]interface{}) (map[string]interface{}, error) {
dump, err := os.ReadFile(sensor.path)
if err != nil {
return nil, err
}

result := make(map[string]interface{})
lines := strings.Split(strings.TrimSpace(string(dump)), "\n")
for i, line := range lines {
if i < 2 {
continue
}
iface, readings, err := sensor.readingsByInterface(line)
if err != nil {
return nil, err
}
result[iface] = readings
}

return result, nil
}

func (sensor *wifi) readingsByInterface(line string) (string, map[string]interface{}, error) {
fields := strings.Fields(line)

iface := strings.TrimRight(fields[0], ":")

link, err := strconv.ParseInt(strings.TrimRight(fields[2], "."), 10, 32)
if err != nil {
return "", nil, errors.Wrap(err, "invalid link quality reading")
}
level, err := strconv.ParseInt(strings.TrimRight(fields[3], "."), 10, 32)
if err != nil {
return "", nil, errors.Wrap(err, "invalid wifi level reading")
}
noise, err := strconv.ParseInt(fields[4], 10, 32)
if err != nil {
return "", nil, errors.Wrap(err, "invalid wifi noise reading")
}

return iface, map[string]interface{}{
"link_quality": int(link),
"level_dBm": int(level),
"noise_dBm": int(noise),
}, nil
return platformReadings(sensor.path)
}
22 changes: 22 additions & 0 deletions linuxwifi/readings_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package linuxwifi

import (
"os/exec"
"strings"
)

const airportCli string = "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport"

func platformReadings(_ string) (map[string]interface{}, error) {
out, err := exec.Command(airportCli, "-I").Output()
if err != nil {
return nil, err
}
ret := make(map[string]interface{})
for _, line := range strings.Split(string(out), "\n") {
if before, after, found := strings.Cut(strings.Trim(line, " "), ": "); found {
ret[before] = after
}
}
return ret, nil
}
56 changes: 56 additions & 0 deletions linuxwifi/readings_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package linuxwifi

import (
"os"
"strconv"
"strings"

"github.com/pkg/errors"
)

func platformReadings(path string) (map[string]interface{}, error) {
dump, err := os.ReadFile(path)
if err != nil {
return nil, err
}

result := make(map[string]interface{})
lines := strings.Split(strings.TrimSpace(string(dump)), "\n")
for i, line := range lines {
if i < 2 {
continue
}
iface, readings, err := readingsByInterface(line)
if err != nil {
return nil, err
}
result[iface] = readings
}

return result, nil
}

func readingsByInterface(line string) (string, map[string]interface{}, error) {
fields := strings.Fields(line)

iface := strings.TrimRight(fields[0], ":")

link, err := strconv.ParseInt(strings.TrimRight(fields[2], "."), 10, 32)
if err != nil {
return "", nil, errors.Wrap(err, "invalid link quality reading")
}
level, err := strconv.ParseInt(strings.TrimRight(fields[3], "."), 10, 32)
if err != nil {
return "", nil, errors.Wrap(err, "invalid wifi level reading")
}
noise, err := strconv.ParseInt(fields[4], 10, 32)
if err != nil {
return "", nil, errors.Wrap(err, "invalid wifi noise reading")
}

return iface, map[string]interface{}{
"link_quality": int(link),
"level_dBm": int(level),
"noise_dBm": int(noise),
}, nil
}

0 comments on commit af07888

Please sign in to comment.