Skip to content

Commit

Permalink
generate service client
Browse files Browse the repository at this point in the history
  • Loading branch information
xiazhvera committed Sep 6, 2023
1 parent 8a1d7ef commit 88c40ce
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 11 deletions.
38 changes: 33 additions & 5 deletions sdk/src/main/java/software/amazon/awssdk/iot/EnumSerializer.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
/* Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
* This file is generated
*/

package software.amazon.awssdk.iot;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSerializer;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonSerializationContext;
Expand All @@ -17,15 +30,30 @@
import java.lang.reflect.Type;

/**
* Custom JSON serializer for enumerated types within IoT service models
* @param <E> the enumeration type the serializer should work with
* Class for serializing enums to and from packets
*/
public class EnumSerializer<E> implements JsonSerializer<E>, JsonDeserializer<E> {

/**
* Serializes the given enum to a JsonElement
* @param enumValue The enum to convert
* @param typeOfEnum The enum to convert type
* @param context The JsonSerializationContext to use
* @return The enum as a JsonElement
*/
public JsonElement serialize(E enumValue, Type typeOfEnum, JsonSerializationContext context) {
return new JsonPrimitive(enumValue.toString());
}

private Method fromString;

/**
* Deserializes the JsonElement to an enum
* @param json The json to convert
* @param typeOfEnum The type of enum to convert to
* @param context The JsonDeserializationContext to use
* @return The enum from the JsonElement data
*/
public E deserialize(JsonElement json, Type typeOfEnum, JsonDeserializationContext context)
throws JsonParseException {
if (fromString == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;

/**
* Factory class for converting ShadowStates to and from packet payloads
*/
public class ShadowStateFactory implements TypeAdapterFactory {

/**
* Creates a new TypeAdapter for conversion to and from packet payloads
*/
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {

Class<T> rawType = (Class<T>)type.getRawType();
Expand All @@ -24,6 +30,12 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);

return new TypeAdapter<T>() {

/**
* Writes the type to the packet payload (JsonWriter)
* @param out The JsonWriter to output the type data to
* @param shadowValue The shadow value containing the data to convert
*/
public void write(JsonWriter out, T shadowValue) throws IOException {
// Are null values present? If so, we need to process this differently
ShadowState shadow = (ShadowState)shadowValue;
Expand All @@ -46,8 +58,13 @@ public void write(JsonWriter out, T shadowValue) throws IOException {
delegate.write(out, shadowValue);
}
}
public T read(JsonReader in) throws IOException {

/**
* Reads the type from the packet payload (JsonReader)
* @param in The JsonReader containing the packet payload data
* @return The type created from the packet payload data
*/
public T read(JsonReader in) throws IOException {
T returnType = delegate.read(in);
return returnType;
}
Expand Down
47 changes: 42 additions & 5 deletions sdk/src/main/java/software/amazon/awssdk/iot/Timestamp.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
/* Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
* This file is generated
*/

package software.amazon.awssdk.iot;

import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializer;
import com.google.gson.JsonSerializationContext;
Expand All @@ -17,21 +29,46 @@
import java.util.Date;

/**
* Extension of Java date class to support Json serialization. Used in IoT service models.
* A wrapper for the java.util.Date class that allows for serializing and deseralizing to JSON
*/
public class Timestamp extends java.util.Date {
/**
* Serializer to convert Timestamp to JSON
*/
public static class Serializer implements JsonSerializer<Timestamp> {
/**
* Serializes a Timestamp to JSON
* @param src The Timestamp to convert
* @param typeOfSrc The Type to use
* @param context The JsonSerializationContext to use
* @return A JsonElement containing the Timestamp data
*/
public JsonElement serialize(Timestamp src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(src.getTime() / 1000); // convert from ms to seconds
}
}

/**
* Deserializer to convert JSON to Timestamp
*/
public static class Deserializer implements JsonDeserializer<Timestamp> {
/**
* Deserializes JSON to a Timestamp
* @param json The JsonElement containing the Timestamp
* @param typeOfT The Type to use
* @param context The JsonDeserializationContext to use
* @return A Timestamp containing the data in the JsonElement
*/
public Timestamp deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
return new Timestamp(new Date(json.getAsJsonPrimitive().getAsLong() * 1000));
}
}

/**
* Timestamp constructor
* @param date The date to use
*/
public Timestamp(Date date) {
super(date.getTime());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
import software.amazon.awssdk.crt.mqtt.QualityOfService;
import software.amazon.awssdk.crt.mqtt.MqttException;
import software.amazon.awssdk.crt.mqtt.MqttMessage;
import software.amazon.awssdk.crt.mqtt5.Mqtt5Client;

import software.amazon.awssdk.iot.Timestamp;
import software.amazon.awssdk.iot.EnumSerializer;


import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

Expand All @@ -54,6 +56,21 @@ public IotIdentityClient(MqttClientConnection connection) {
this.connection = connection;
}

/**
* Constructs a new IotIdentityClient from a mqtt5 client
* @param mqtt5Client The mqtt5 client to use
*/
public IotIdentityClient(Mqtt5Client mqtt5Client) throws MqttException{
try
{
this.connection = mqtt5Client.NewConnection();
}
catch(MqttException ex)
{
throw new MqttException("Failed to setup service client: " + ex.getMessage());
}
}

private Gson getGson() {
GsonBuilder gson = new GsonBuilder();
gson.disableHtmlEscaping();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import software.amazon.awssdk.crt.mqtt.QualityOfService;
import software.amazon.awssdk.crt.mqtt.MqttException;
import software.amazon.awssdk.crt.mqtt.MqttMessage;
import software.amazon.awssdk.crt.mqtt5.Mqtt5Client;

import software.amazon.awssdk.iot.Timestamp;
import software.amazon.awssdk.iot.EnumSerializer;
Expand Down Expand Up @@ -67,6 +68,21 @@ public IotJobsClient(MqttClientConnection connection) {
this.connection = connection;
}

/**
* Constructs a new IotJobsClient from a mqtt5 client
* @param mqtt5Client The mqtt5 client to use
*/
public IotJobsClient(Mqtt5Client mqtt5Client) throws MqttException{
try
{
this.connection = mqtt5Client.NewConnection();
}
catch(MqttException ex)
{
throw new MqttException("Failed to setup service client: " + ex.getMessage());
}
}

private Gson getGson() {
GsonBuilder gson = new GsonBuilder();
gson.disableHtmlEscaping();
Expand Down Expand Up @@ -286,6 +302,7 @@ public CompletableFuture<Integer> SubscribeToDescribeJobExecutionRejected(
}

/**
*
*
* Once subscribed, `handler` is invoked each time a message matching
* the `topic` is received. It is possible for such messages to arrive before
Expand Down Expand Up @@ -328,6 +345,7 @@ public CompletableFuture<Integer> SubscribeToNextJobExecutionChangedEvents(
}

/**
*
*
* Once subscribed, `handler` is invoked each time a message matching
* the `topic` is received. It is possible for such messages to arrive before
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import software.amazon.awssdk.crt.mqtt.QualityOfService;
import software.amazon.awssdk.crt.mqtt.MqttException;
import software.amazon.awssdk.crt.mqtt.MqttMessage;
import software.amazon.awssdk.crt.mqtt5.Mqtt5Client;

import software.amazon.awssdk.iot.Timestamp;
import software.amazon.awssdk.iot.EnumSerializer;
Expand Down Expand Up @@ -73,6 +74,21 @@ public IotShadowClient(MqttClientConnection connection) {
this.connection = connection;
}

/**
* Constructs a new IotShadowClient from a mqtt5 client
* @param mqtt5Client The mqtt5 client to use
*/
public IotShadowClient(Mqtt5Client mqtt5Client) throws MqttException{
try
{
this.connection = mqtt5Client.NewConnection();
}
catch(MqttException ex)
{
throw new MqttException("Failed to setup service client: " + ex.getMessage());
}
}

private Gson getGson() {
GsonBuilder gson = new GsonBuilder();
gson.disableHtmlEscaping();
Expand Down

0 comments on commit 88c40ce

Please sign in to comment.