-
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.
- Loading branch information
Showing
7 changed files
with
184 additions
and
48 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
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,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 | ||
} |
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,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"` | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.