Skip to content

Commit

Permalink
Fix of #232
Browse files Browse the repository at this point in the history
Fix of issue, now unsetting can be done passing "nil" to appengine devices send-data

Signed-off-by: Eddy Babetto <eddy.babetto@secomind.com>
  • Loading branch information
eddbbt committed Nov 17, 2023
1 parent 93e5c6f commit 4c092bd
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions cmd/appengine/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ var devicesSendDataCmd = &cobra.Command{
Use: "send-data <device_id_or_alias> <interface_name> <path> <data>",
Short: "Sends data to a given interface path",
Long: `Sends data to a given interface path. This works both for datastream with individual and properties.
Send nil as data to unset property (if enabled)
When dealing with an aggregate, non parametric interface, path must still be provided, adhering to the
interface structure. In that case, <data> should be a JSON string which contains a key/value dictionary,
Expand Down Expand Up @@ -1079,14 +1080,22 @@ func devicesSendDataF(command *cobra.Command, args []string) error {
}

var parsedPayloadData interface{}

if err := payloadType.IsValid(); err == nil {
if parsedPayloadData, err = parseSendDataPayload(payloadData, payloadType); err != nil {
return err

if payloadData == "nil" {
parsedPayloadData = nil
} else {
if parsedPayloadData, err = parseSendDataPayload(payloadData, payloadType); err != nil {
return err
}
}

} else {
// We have to treat it as an aggregate.
aggrPayload := map[string]interface{}{}
if err := json.Unmarshal([]byte(payloadData), &aggrPayload); err != nil {
fmt.Println("A")
return err
}

Expand All @@ -1099,6 +1108,7 @@ func devicesSendDataF(command *cobra.Command, args []string) error {
fullPath := fmt.Sprintf("%s/%s", interfacePath, k)
mapping, err := interfaces.InterfaceMappingFromPath(iface, fullPath)
if err != nil {
fmt.Println("B")
return err
}

Expand Down Expand Up @@ -1148,8 +1158,15 @@ func devicesSendDataF(command *cobra.Command, args []string) error {
var sendDataCall client.AstarteRequest

if !skipRealmManagementChecks {
// We can delegate the entirety of this to astarte-go
sendDataCall, err = astarteAPIClient.SendData(realm, deviceID, deviceIdentifierType, iface, interfacePath, parsedPayloadData)

if parsedPayloadData == nil {
// We can delegate the entirety of this to astarte-go
sendDataCall, err = astarteAPIClient.UnsetProperty(realm, deviceID, deviceIdentifierType, interfaceName, interfacePath)
} else {
// We can delegate the entirety of this to astarte-go
sendDataCall, err = astarteAPIClient.SendData(realm, deviceID, deviceIdentifierType, iface, interfacePath, parsedPayloadData)
}

} else {
// Don't risk it. Use raw functions and trust the server to fail, in case.
switch interfaceTypeString {
Expand Down Expand Up @@ -1241,7 +1258,7 @@ func getProtoInterface(deviceID string, deviceIdentifierType client.DeviceIdenti
// Just a trick to trick the parser into doing the right thing.
if isParametricInterface {
iface.Mappings = []interfaces.AstarteInterfaceMapping{
interfaces.AstarteInterfaceMapping{
{
Endpoint: "/it/%{is}/parametric",
},
}
Expand Down

0 comments on commit 4c092bd

Please sign in to comment.