<commit_before>﻿using Microsoft.Azure.Devices.Client;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using W10Home.Core.Configuration;
using W10Home.Core.Queing;
using W10Home.Interfaces;

namespace W10Home.Plugin.AzureIoTHub
{
	public class AzureIoTHubDevice : IDevice
	{
		private DeviceClient deviceClient;

		private async void MessageReceiverLoop()
		{
			do
			{
				try
				{
					var message = await deviceClient.ReceiveAsync();
					if (message != null)
					{
						var reader = new StreamReader(message.BodyStream);
						var bodyString = await reader.ReadToEndAsync();
						Debug.WriteLine(bodyString);
						var messageObject = JsonConvert.DeserializeObject<QueueMessage>(bodyString);
						if (messageObject.Key == "configure")
						{

						}
						await deviceClient.CompleteAsync(message);
					}
				}
				catch (Exception ex)
				{
					Debug.WriteLine(ex.Message);
					//TODO Log
				}
			} while (true);
		}

		public async Task SendMessageToIoTHubAsync(string deviceId, string location, string key, object value)
		{
			try
			{
				string strvalue = null;
				if (value is double || value is float)
				{
					strvalue = $"{((double)value):F}";
				}
				else
				{
					strvalue = $"\"{value.ToString()}\"";
				}

				var payload =
					$"{{\"deviceid\": \"{deviceId}\", \"location\": \"{location}\", \"channelvalue\": {value}, \"channelkey\": \"{key}\", \"localtimestamp\": \"{DateTime.Now.ToUniversalTime():O}\"}}";

				var msg = new Message(Encoding.UTF8.GetBytes(payload));

				await deviceClient.SendEventAsync(msg);
				Debug.WriteLine(payload);
			}
			catch (Exception ex)
			{
				Debug.WriteLine(ex.Message);
				//TODO Log
			}
		}

		public async Task InitializeAsync(IDeviceConfiguration configuration)
		{
			try
			{
				// Instantiate the Azure IoT Hub device client
				deviceClient = DeviceClient.CreateFromConnectionString(configuration.Properties["ConnectionString"]);
				await deviceClient.SetMethodHandlerAsync("configure", HandleConfigureMethod, null);

				MessageReceiverLoop(); // launch message loop in the background
			}
			catch (Exception ex)
			{
				Debug.WriteLine(ex.Message);
				//TODO Log
			}

		}

		private async Task<MethodResponse> HandleConfigureMethod(MethodRequest methodRequest, object userContext)
		{
			Debug.WriteLine("HandleConfigureMethod called");
			return new MethodResponse(0);
		}

		public Task<IEnumerable<IChannel>> GetChannelsAsync()
		{
			throw new NotImplementedException();
		}

		public async Task Teardown()
		{
			if (deviceClient != null)
			{
				await deviceClient.CloseAsync();
				deviceClient.Dispose();
				deviceClient = null;
			}
		}
	}
}
<commit_msg>fix invalid message body deserialization<commit_after>﻿using Microsoft.Azure.Devices.Client;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using W10Home.Core.Configuration;
using W10Home.Core.Queing;
using W10Home.Interfaces;

namespace W10Home.Plugin.AzureIoTHub
{
	public class AzureIoTHubDevice : IDevice
	{
		private DeviceClient deviceClient;

		private async void MessageReceiverLoop()
		{
			do
			{
				try
				{
					var message = await deviceClient.ReceiveAsync();
					if (message != null)
					{
						var reader = new StreamReader(message.BodyStream);
						var bodyString = await reader.ReadToEndAsync();
						Debug.WriteLine(bodyString);
						var messageObject = JsonConvert.DeserializeObject<QueueMessage>(bodyString);
						if (messageObject == null) // maybe an incompatible message object -> throw away and continue
						{
							await deviceClient.CompleteAsync(message);
							continue;
						}
						if (messageObject.Key == "configure")
						{

						}
						await deviceClient.CompleteAsync(message);
					}
				}
				catch (Exception ex)
				{
					Debug.WriteLine(ex.Message);
					//TODO Log
				}
			} while (true);
		}

		public async Task SendMessageToIoTHubAsync(string deviceId, string location, string key, object value)
		{
			try
			{
				string strvalue = null;
				if (value is double || value is float)
				{
					strvalue = $"{((double)value):F}";
				}
				else
				{
					strvalue = $"\"{value.ToString()}\"";
				}

				var payload =
					$"{{\"deviceid\": \"{deviceId}\", \"location\": \"{location}\", \"channelvalue\": {value}, \"channelkey\": \"{key}\", \"localtimestamp\": \"{DateTime.Now.ToUniversalTime():O}\"}}";

				var msg = new Message(Encoding.UTF8.GetBytes(payload));

				await deviceClient.SendEventAsync(msg);
				Debug.WriteLine(payload);
			}
			catch (Exception ex)
			{
				Debug.WriteLine(ex.Message);
				//TODO Log
			}
		}

		public async Task InitializeAsync(IDeviceConfiguration configuration)
		{
			try
			{
				// Instantiate the Azure IoT Hub device client
				deviceClient = DeviceClient.CreateFromConnectionString(configuration.Properties["ConnectionString"]);
				await deviceClient.SetMethodHandlerAsync("configure", HandleConfigureMethod, null);

				MessageReceiverLoop(); // launch message loop in the background
			}
			catch (Exception ex)
			{
				Debug.WriteLine(ex.Message);
				//TODO Log
			}

		}

		private async Task<MethodResponse> HandleConfigureMethod(MethodRequest methodRequest, object userContext)
		{
			Debug.WriteLine("HandleConfigureMethod called");
			return new MethodResponse(0);
		}

		public Task<IEnumerable<IChannel>> GetChannelsAsync()
		{
			throw new NotImplementedException();
		}

		public async Task Teardown()
		{
			if (deviceClient != null)
			{
				await deviceClient.CloseAsync();
				deviceClient.Dispose();
				deviceClient = null;
			}
		}
	}
}

{'fraction_non_alphanumeric': 0.11909686315063771, 'fraction_numerical': 0.003447087211306446, 'mean_word_length': 3.327367636092468, 'pattern_counts': {'":': 10, '<': 11, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '366857', 'n_tokens_mistral': 2198, 'n_tokens_neox': 1704, 'n_words': 389}