Skip to content

Commit

Permalink
adds devices command
Browse files Browse the repository at this point in the history
  • Loading branch information
dotvezz committed Jun 14, 2024
1 parent 487287e commit 1458b13
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 48 deletions.
9 changes: 5 additions & 4 deletions cloud/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cloud
import (
"context"
"fmt"
"net/http"
"time"

"github.com/libdyson-wg/libdyson-go/internal/generated/oapi"
Expand All @@ -25,8 +26,8 @@ func BeginLogin(email string) (challengeID uuid.UUID, err error) {
return uuid.Nil, fmt.Errorf("couldn't get user status: %w", err)
}

if resp == nil || resp.JSON200 == nil {
return uuid.Nil, fmt.Errorf("couldn't get user status: nil response")
if resp.StatusCode() != http.StatusOK {
return uuid.Nil, fmt.Errorf("couldn't get user status: http status code %d", resp.StatusCode())
}
}

Expand All @@ -39,8 +40,8 @@ func BeginLogin(email string) (challengeID uuid.UUID, err error) {
return uuid.Nil, fmt.Errorf("couldn't begin login: %w", err)
}

if resp == nil || resp.JSON200 == nil {
return uuid.Nil, fmt.Errorf("couldn't begin login: nil response")
if resp.StatusCode() != http.StatusOK {
return uuid.Nil, fmt.Errorf("couldn't begin login: http status code %d", resp.StatusCode())
}

return resp.JSON200.ChallengeId, nil
Expand Down
71 changes: 71 additions & 0 deletions cloud/devices.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package cloud

import (
"context"
"fmt"
"github.com/libdyson-wg/libdyson-go/internal/generated/oapi"
"net/http"
"os"
"time"

"github.com/libdyson-wg/libdyson-go/devices"
)

func GetDevices() ([]devices.Device, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*20)
defer cancel()
resp, err := client.GetDevicesWithResponse(ctx)
if err != nil {
return nil, fmt.Errorf("error getting devices from cloud: %w", err)
}

if resp.StatusCode() != http.StatusOK {
return nil, fmt.Errorf("error getting devices from cloud, http status code: %d", resp.StatusCode())
}

ds := make([]devices.Device, len(*resp.JSON200))
for i := 0; i < len(ds); i++ {
ds[i] = mapDevice((*resp.JSON200)[i])

resp, err := client.GetIoTInfoWithResponse(ctx, oapi.GetIoTInfoJSONRequestBody{
Serial: ds[i].Serial,
})

if err != nil {
_, _ = fmt.Fprintln(os.Stderr, "error getting IoTInfo from cloud for", ds[i].Serial)
continue
}

ds[i].CloudIOT = mapIoT(*resp.JSON200)
}

return ds, nil
}

func mapIoT(in oapi.IoTData) (out devices.CloudIOTConfig) {
out.Endpoint = in.Endpoint
out.IoTCredentials.CustomAuthorizerName = in.IoTCredentials.CustomAuthorizerName
out.IoTCredentials.ClientID = in.IoTCredentials.ClientId
out.IoTCredentials.TokenKey = in.IoTCredentials.TokenKey
out.IoTCredentials.TokenSignature = in.IoTCredentials.TokenSignature
out.IoTCredentials.TokenValue = in.IoTCredentials.TokenValue
return out
}

func mapDevice(in oapi.Device) (out devices.Device) {
out.Model = in.Model
out.Name = in.Name
out.Serial = in.SerialNumber
out.Type = in.Type
if in.Variant != nil {
out.Variant = *in.Variant
}

out.MQTT.LocalCredentials = in.ConnectedConfiguration.Mqtt.LocalBrokerCredentials
out.MQTT.TopicRoot = in.ConnectedConfiguration.Mqtt.MqttRootTopicLevel

out.Firmware.Version = in.ConnectedConfiguration.Firmware.Version
out.Firmware.AutoUpdateEnabled = in.ConnectedConfiguration.Firmware.AutoUpdateEnabled
out.Firmware.NewVersionAvailable = in.ConnectedConfiguration.Firmware.NewVersionAvailable
return out
}
21 changes: 19 additions & 2 deletions cmd/dson/cmd/devices.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package cmd

import (
"fmt"

"gopkg.in/yaml.v3"

"github.com/libdyson-wg/libdyson-go/config"

"github.com/spf13/cobra"
Expand All @@ -19,8 +23,21 @@ var devicesCmd = &cobra.Command{
}
return err
},
Run: func(cmd *cobra.Command, args []string) {
//ds, err := funcs.GetDevices
RunE: func(cmd *cobra.Command, args []string) error {
ds, err := funcs.GetDevices()
if err != nil {
return err
}

fmt.Println("Available devices:")
bs, err := yaml.Marshal(ds)
fmt.Println(string(bs))
return err
},
PostRun: func(cmd *cobra.Command, args []string) {
fmt.Println()
fmt.Println("This is sensitive information that can be used to control your devices. " +
"Please take caution before sharing the information above with anyone you do not trust.")
},
}

Expand Down
5 changes: 4 additions & 1 deletion cmd/dson/cmd/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package cmd
import (
"github.com/libdyson-wg/libdyson-go/cloud"
"github.com/libdyson-wg/libdyson-go/config"
"github.com/libdyson-wg/libdyson-go/devices"
"github.com/libdyson-wg/libdyson-go/internal/account"
"github.com/libdyson-wg/libdyson-go/internal/shell"
)

type functions struct {
Login func() error
GetDevices func() error
GetDevices func() ([]devices.Device, error)
}

var funcs functions
Expand All @@ -25,5 +26,7 @@ func init() {
cloud.SetToken,
cloud.SetServerRegion,
),
GetDevices: cloud.GetDevices,
}

}
41 changes: 41 additions & 0 deletions devices/device.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package devices

import (
"github.com/google/uuid"
)

type Device struct {
Name string `yaml:"name"`
Serial string `yaml:"serial"`
Model string `yaml:"model"`
Type string `yaml:"type"`
Variant string `yaml:"variant"`

CloudIOT CloudIOTConfig `yaml:"cloud_iot"`
MQTT MQTTConfig `yaml:"mqtt"`
Firmware FirmwareData `yaml:"firmware"`
}

type CloudIOTConfig struct {
IoTCredentials CloudIOTCredentials `yaml:"iot_credentials"`
Endpoint string `yaml:"endpoint"`
}

type CloudIOTCredentials struct {
ClientID uuid.UUID `yaml:"client_id"`
CustomAuthorizerName string `yaml:"custom_authorizer_name"`
TokenKey string `yaml:"token_key"`
TokenSignature string `yaml:"token_signature"`
TokenValue uuid.UUID `yaml:"token_value"`
}

type MQTTConfig struct {
LocalCredentials string `yaml:"local_credentials"`
TopicRoot string `yaml:"root_topic"`
}

type FirmwareData struct {
Version string `yaml:"version"`
AutoUpdateEnabled bool `yaml:"auto_update_enabled"`
NewVersionAvailable bool `yaml:"new_version_available"`
}
79 changes: 40 additions & 39 deletions internal/generated/oapi/openapi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions internal/generated/oapi/types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1458b13

Please sign in to comment.