private DeviceModel.DeviceModelMessageSchema GetDeviceModelMessageSchema() { var schema = new DeviceModel.DeviceModelMessageSchema() { Name = "chiller-sensors", Format = DeviceModel.DeviceModelMessageSchemaFormat.JSON, Fields = new Dictionary <string, DeviceModel.DeviceModelMessageSchemaType>() { { "cargotemperature", DeviceModel.DeviceModelMessageSchemaType.Double } } }; return(schema); }
public async Task SendMessageAsync(string message, DeviceModel.DeviceModelMessageSchema schema) { var eventMessage = new Message(Encoding.UTF8.GetBytes(message)); eventMessage.Properties.Add(CREATION_TIME_PROPERTY, DateTimeOffset.UtcNow.ToString(DATE_FORMAT)); eventMessage.Properties.Add(MESSAGE_SCHEMA_PROPERTY, schema.Name); eventMessage.Properties.Add(CONTENT_PROPERTY, "JSON"); await this.SendRawMessageAsync(eventMessage); this.log.Debug("SendMessageAsync for device", () => new { this.deviceId }); }
/// <summary>Map a service model to the corresponding API model</summary> public DeviceModelMessageSchemaApiModel(DeviceModel.DeviceModelMessageSchema schema) : this() { if (schema == null) { return; } this.Name = schema.Name; this.Format = schema.Format.ToString(); foreach (var field in schema.Fields) { this.Fields.Add(field.Key, field.Value.ToString()); } }
private async Task SendProtobufMessageAsync(string message, DeviceModel.DeviceModelMessageSchema schema) { var eventMessage = default(Message); string className = schema.ClassName; Type type = System.Reflection.Assembly.GetExecutingAssembly().GetType(schema.ClassName, false); if (type != null) { object jsonObj = JsonConvert.DeserializeObject(message, type); MethodInfo methodInfo = Utilities.GetExtensionMethod("imessage", "Google.Protobuf", "ToByteArray"); if (methodInfo != null) { object result = methodInfo.Invoke(jsonObj, new object[] { jsonObj }); if (result != null) { byte[] byteArray = result as byte[]; eventMessage = new Message(byteArray); eventMessage.Properties.Add(CLASSNAME_PROPERTY, schema.ClassName); } else { throw new InvalidDataException("Json message transformation to byte array yielded null"); } } else { throw new ResourceNotFoundException($"Method: ToByteArray not found in {schema.ClassName}"); } } else { throw new ResourceNotFoundException($"Type: {schema.ClassName} not found"); } eventMessage.Properties.Add(CREATION_TIME_PROPERTY, DateTimeOffset.UtcNow.ToString(DATE_FORMAT)); eventMessage.Properties.Add(MESSAGE_SCHEMA_PROPERTY, schema.Name); eventMessage.Properties.Add(CONTENT_PROPERTY, schema.Format.ToString()); eventMessage.MessageSchema = schema.Name; eventMessage.CreationTimeUtc = DateTime.UtcNow; this.log.Debug("Sending message from device", () => new { this.deviceId, Schema = schema.Name }); await this.SendRawMessageAsync(eventMessage); }
private async Task SendJsonMessageAsync(string message, DeviceModel.DeviceModelMessageSchema schema) { var eventMessage = default(Message); eventMessage = new Message(Encoding.UTF8.GetBytes(message)); eventMessage.ContentType = "application/json"; eventMessage.ContentEncoding = "utf-8"; eventMessage.Properties.Add(CREATION_TIME_PROPERTY, DateTimeOffset.UtcNow.ToString(DATE_FORMAT)); eventMessage.Properties.Add(MESSAGE_SCHEMA_PROPERTY, schema.Name); eventMessage.Properties.Add(CONTENT_PROPERTY, schema.Format.ToString()); eventMessage.MessageSchema = schema.Name; eventMessage.CreationTimeUtc = DateTime.UtcNow; this.log.Debug("Sending message from device", () => new { this.deviceId, Schema = schema.Name }); await this.SendRawMessageAsync(eventMessage); }
public async Task SendMessageAsync(string message, DeviceModel.DeviceModelMessageSchema schema) { switch (schema.Format) { case DeviceModel.DeviceModelMessageSchemaFormat.JSON: await this.SendJsonMessageAsync(message, schema); break; case DeviceModel.DeviceModelMessageSchemaFormat.Protobuf: await this.SendProtobufMessageAsync(message, schema); break; default: throw new UnknownMessageFormatException($"Message format {schema.Format.ToString()} is invalid. Check the Telemetry format against the permitted values Binary, Text, Json, Protobuf"); } }
// Map service model to API model public static DeviceModelTelemetryMessageSchema FromServiceModel(DeviceModel.DeviceModelMessageSchema value) { if (value == null) { return(null); } var result = new DeviceModelTelemetryMessageSchema { Name = value.Name, Format = value.Format.ToString() }; foreach (var field in value.Fields) { result.Fields.Add(field.Key, field.Value.ToString()); } return(result); }