aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/DataStore/Tango.DataStore
diff options
context:
space:
mode:
authorRoy Ben Shabat <Roy.mail.net@gmail.com>2020-11-14 22:02:45 +0200
committerRoy Ben Shabat <Roy.mail.net@gmail.com>2020-11-14 22:02:45 +0200
commit76b22e4d05cbd8d771f678e4b5adc2dc5159afa8 (patch)
tree815892d26cbf716d9fae9e01d46109299875d94d /Software/Visual_Studio/DataStore/Tango.DataStore
parentf838a715af54ef7fc35bf9d99fee95dd8ac6533f (diff)
downloadTango-76b22e4d05cbd8d771f678e4b5adc2dc5159afa8.tar.gz
Tango-76b22e4d05cbd8d771f678e4b5adc2dc5159afa8.zip
Moved data store projects to DataStore folder.
Diffstat (limited to 'Software/Visual_Studio/DataStore/Tango.DataStore')
-rw-r--r--Software/Visual_Studio/DataStore/Tango.DataStore/DataStoreHelper.cs160
-rw-r--r--Software/Visual_Studio/DataStore/Tango.DataStore/DataStoreProtoObject.cs83
-rw-r--r--Software/Visual_Studio/DataStore/Tango.DataStore/DataType.cs22
-rw-r--r--Software/Visual_Studio/DataStore/Tango.DataStore/ExtensionMethods.cs16
-rw-r--r--Software/Visual_Studio/DataStore/Tango.DataStore/IDataStoreCollection.cs119
-rw-r--r--Software/Visual_Studio/DataStore/Tango.DataStore/IDataStoreItem.cs44
-rw-r--r--Software/Visual_Studio/DataStore/Tango.DataStore/IDataStoreManager.cs28
-rw-r--r--Software/Visual_Studio/DataStore/Tango.DataStore/Properties/AssemblyInfo.cs7
-rw-r--r--Software/Visual_Studio/DataStore/Tango.DataStore/Tango.DataStore.csproj78
-rw-r--r--Software/Visual_Studio/DataStore/Tango.DataStore/packages.config5
10 files changed, 562 insertions, 0 deletions
diff --git a/Software/Visual_Studio/DataStore/Tango.DataStore/DataStoreHelper.cs b/Software/Visual_Studio/DataStore/Tango.DataStore/DataStoreHelper.cs
new file mode 100644
index 000000000..0ca8e484e
--- /dev/null
+++ b/Software/Visual_Studio/DataStore/Tango.DataStore/DataStoreHelper.cs
@@ -0,0 +1,160 @@
+using Google.Protobuf;
+using Newtonsoft.Json.Linq;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Tango.Core.ExtensionMethods;
+using Tango.PMR;
+using Tango.PMR.Common;
+
+namespace Tango.DataStore
+{
+ /// <summary>
+ /// Contains data store helper methods.
+ /// </summary>
+ public static class DataStoreHelper
+ {
+ /// <summary>
+ /// Gets the data store data type by the specified object type.
+ /// </summary>
+ /// <param name="value">The value.</param>
+ /// <returns></returns>
+ public static DataType GetDataType(Object value)
+ {
+ return GetDataType(value.GetType());
+ }
+
+ /// <summary>
+ /// Gets data store data type by the specified .net type.
+ /// </summary>
+ /// <param name="type">The type.</param>
+ /// <returns></returns>
+ /// <exception cref="System.NotSupportedException"></exception>
+ public static DataType GetDataType(Type type)
+ {
+ if (type == typeof(int))
+ {
+ return DataType.Int32;
+ }
+ else if (type == typeof(Int64))
+ {
+ return DataType.Int32;
+ }
+ else if (type == typeof(float))
+ {
+ return DataType.Float;
+ }
+ else if (type == typeof(double))
+ {
+ return DataType.Double;
+ }
+ else if (type == typeof(String))
+ {
+ return DataType.String;
+ }
+ else if (type == typeof(bool))
+ {
+ return DataType.Boolean;
+ }
+ else if (type == typeof(byte[]))
+ {
+ return DataType.Bytes;
+ }
+ else if (type == typeof(DataStoreProtoObject))
+ {
+ return DataType.Proto;
+ }
+ else if (typeof(IMessage).IsAssignableFrom(type))
+ {
+ return DataType.Proto;
+ }
+
+ throw new NotSupportedException($"The specified type '{type.Name}' is not supported by the data store.");
+ }
+
+ /// <summary>
+ /// Formats the data store item as a string.
+ /// </summary>
+ /// <param name="item">The item.</param>
+ /// <returns></returns>
+ public static String FormatDataStoreItem(IDataStoreItem item)
+ {
+ if (item.Type == DataType.Bytes)
+ {
+ return GetByteArrayHexString((byte[])item.Value);
+ }
+ else if (item.Type == DataType.Proto)
+ {
+ return (item.Value as DataStoreProtoObject).Message.ToJsonString();
+ }
+ else
+ {
+ return item.Value.ToStringSafe();
+ }
+ }
+
+ /// <summary>
+ /// Returns a byte array string representation in hex format.
+ /// </summary>
+ /// <param name="data">The data.</param>
+ /// <returns></returns>
+ public static String GetByteArrayHexString(byte[] data)
+ {
+ StringBuilder hex = new StringBuilder();
+ foreach (byte b in data)
+ {
+ hex.Append(b.ToString("X2") + " ");
+ }
+ return hex.ToString();
+ }
+
+ /// <summary>
+ /// Parses a data store value from a string.
+ /// </summary>
+ /// <param name="type">The type.</param>
+ /// <param name="text">The string.</param>
+ /// <param name="protoMessageType">Type of the proto message (if type is Proto).</param>
+ /// <returns></returns>
+ /// <exception cref="ArgumentNullException">No PMR message type specified.</exception>
+ /// <exception cref="NotSupportedException">The specified data store type is not supported.</exception>
+ public static Object ParseDataStoreValue(DataType type, String text, MessageType? protoMessageType = null)
+ {
+ switch (type)
+ {
+ case DataType.String:
+ return text;
+ case DataType.Int32:
+ return int.Parse(text);
+ case DataType.Float:
+ return float.Parse(text);
+ case DataType.Double:
+ return double.Parse(text);
+ case DataType.Boolean:
+ return bool.Parse(text);
+ case DataType.Proto:
+ if (protoMessageType == null) throw new ArgumentNullException("No PMR message type specified.");
+ var messageType = MessageFactory.GetPMRTypeFromMessageType(protoMessageType.Value);
+ var instance = Activator.CreateInstance(messageType) as IMessage;
+ instance = instance.GetParser().ParseJson(text);
+ return DataStoreProtoObject.FromMessage(instance);
+ case DataType.Bytes:
+ string[] hexValuesSplit = text.Split(' ');
+ List<byte> bytes = new List<byte>();
+ foreach (string hex in hexValuesSplit)
+ {
+ if (hex.IsNotNullOrEmpty())
+ {
+ byte b = (byte)Convert.ToInt32(hex.Trim(), 16);
+ bytes.Add(b);
+ }
+ }
+ return bytes.ToArray();
+ }
+
+ throw new NotSupportedException("The specified data store type is not supported.");
+ }
+ }
+}
diff --git a/Software/Visual_Studio/DataStore/Tango.DataStore/DataStoreProtoObject.cs b/Software/Visual_Studio/DataStore/Tango.DataStore/DataStoreProtoObject.cs
new file mode 100644
index 000000000..5aa7c5342
--- /dev/null
+++ b/Software/Visual_Studio/DataStore/Tango.DataStore/DataStoreProtoObject.cs
@@ -0,0 +1,83 @@
+using Google.Protobuf;
+using Newtonsoft.Json;
+using Newtonsoft.Json.Linq;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Tango.Core.Bson;
+using Tango.Core.ExtensionMethods;
+using Tango.PMR;
+using Tango.PMR.Common;
+using Tango.PMR.DataStore;
+
+namespace Tango.DataStore
+{
+ public class DataStoreProtoObject
+ {
+ public MessageType MessageType { get; set; }
+ public Type Type { get; set; }
+ public byte[] Data { get; set; }
+
+
+ private IMessage _message;
+ [JsonIgnore]
+ public IMessage Message
+ {
+ get
+ {
+ if (_message == null)
+ {
+ _message = MessageFactory.ParseProtoMessage(Data, Type);
+ }
+
+ return _message;
+ }
+ private set { _message = value; }
+ }
+
+ public byte[] ToBytes()
+ {
+ return BsonConvert.Serialize<DataStoreProtoObject>(this);
+ }
+
+ public static DataStoreProtoObject FromBytes(byte[] data)
+ {
+ var instance = BsonConvert.Deserialize<DataStoreProtoObject>(data);
+ instance.Message = MessageFactory.ParseProtoMessage(instance.Data, instance.Type);
+
+ return instance;
+ }
+
+ public static DataStoreProtoObject FromMessage(IMessage message)
+ {
+ DataStoreProtoObject proto = new DataStoreProtoObject();
+ proto.Type = message.GetType();
+ proto.MessageType = MessageFactory.ParseMessageType(proto.Type.Name);
+ proto.Data = message.ToByteArray();
+ proto.Message = message;
+ return proto;
+ }
+
+ public static DataStoreProtoObject FromJObject(JObject obj)
+ {
+ return (obj.ToObject<DataStoreProtoObject>());
+ }
+
+ public static DataStoreProtoObject FromPMRDataStoreItem(DataStoreItem item)
+ {
+ DataStoreProtoObject proto = new DataStoreProtoObject();
+ proto.MessageType = item.ProtoType;
+ proto.Type = MessageFactory.GetPMRTypeFromMessageType(item.ProtoType);
+ proto.Data = item.BytesValue.ToByteArray();
+ return proto;
+ }
+
+ public override string ToString()
+ {
+ return Message?.ToJsonString();
+ }
+ }
+}
diff --git a/Software/Visual_Studio/DataStore/Tango.DataStore/DataType.cs b/Software/Visual_Studio/DataStore/Tango.DataStore/DataType.cs
new file mode 100644
index 000000000..132bff28e
--- /dev/null
+++ b/Software/Visual_Studio/DataStore/Tango.DataStore/DataType.cs
@@ -0,0 +1,22 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Tango.DataStore
+{
+ /// <summary>
+ /// Represents a data store item type.
+ /// </summary>
+ public enum DataType
+ {
+ Int32,
+ Float,
+ Double,
+ Boolean,
+ String,
+ Bytes,
+ Proto
+ }
+}
diff --git a/Software/Visual_Studio/DataStore/Tango.DataStore/ExtensionMethods.cs b/Software/Visual_Studio/DataStore/Tango.DataStore/ExtensionMethods.cs
new file mode 100644
index 000000000..acc381a61
--- /dev/null
+++ b/Software/Visual_Studio/DataStore/Tango.DataStore/ExtensionMethods.cs
@@ -0,0 +1,16 @@
+using Google.Protobuf;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Tango.DataStore;
+
+public static class ExtensionMethods
+{
+ public static DataStoreProtoObject ToDataStoreProtoObject(IMessage message)
+ {
+ return DataStoreProtoObject.FromMessage(message);
+ }
+}
+
diff --git a/Software/Visual_Studio/DataStore/Tango.DataStore/IDataStoreCollection.cs b/Software/Visual_Studio/DataStore/Tango.DataStore/IDataStoreCollection.cs
new file mode 100644
index 000000000..baca95c67
--- /dev/null
+++ b/Software/Visual_Studio/DataStore/Tango.DataStore/IDataStoreCollection.cs
@@ -0,0 +1,119 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Tango.DataStore
+{
+ /// <summary>
+ /// Represents a data store collection.
+ /// </summary>
+ public interface IDataStoreCollection
+ {
+ /// <summary>
+ /// Gets the collection name.
+ /// </summary>
+ String Name { get; }
+
+ /// <summary>
+ /// Upserts the specified key and value.
+ /// </summary>
+ /// <param name="key">The key.</param>
+ /// <param name="value">The value.</param>
+ void Put(String key, Object value);
+
+ /// <summary>
+ /// Upserts the specified key and value of type T.
+ /// </summary>
+ /// <typeparam name="T"></typeparam>
+ /// <param name="key">The key.</param>
+ /// <param name="value">The value.</param>
+ void Put<T>(String key, T value);
+
+ /// <summary>
+ /// Upserts the specified key and value.
+ /// The value must be of the specified DataType.
+ /// </summary>
+ /// <param name="key">The key.</param>
+ /// <param name="type">The type.</param>
+ /// <param name="value">The value.</param>
+ void Put(String key, DataType type, Object value);
+
+ /// <summary>
+ /// Gets the value by the specified key
+ /// </summary>
+ /// <param name="key">The key.</param>
+ /// <returns></returns>
+ Object Get(String key);
+
+ /// <summary>
+ /// Gets the value by the specified key
+ /// </summary>
+ /// <param name="key">The key.</param>
+ /// <param name="defaultValue">Will execute put when the key was not found.</param>
+ /// <returns></returns>
+ Object Get(String key, Object defaultValue);
+
+ /// <summary>
+ /// Gets the value of type T by the specified key
+ /// </summary>
+ /// <typeparam name="T"></typeparam>
+ /// <param name="key">The key.</param>
+ /// <returns></returns>
+ T Get<T>(String key);
+
+ /// <summary>
+ /// Gets the value of type T by the specified key
+ /// </summary>
+ /// <typeparam name="T"></typeparam>
+ /// <param name="key">The key.</param>
+ /// <param name="defaultValue">Will execute put when the key was not found.</param>
+ /// <returns></returns>
+ T Get<T>(String key, T defaultValue);
+
+ /// <summary>
+ /// Gets the full data store item by the specified key.
+ /// </summary>
+ /// <param name="key">The key.</param>
+ /// <returns></returns>
+ IDataStoreItem GetItem(String key);
+
+ /// <summary>
+ /// Gets the full data store item by the specified key.
+ /// </summary>
+ /// <param name="key">The key.</param>
+ /// <param name="defaultValue">Will execute put when the key was not found.</param>
+ /// <returns></returns>
+ IDataStoreItem GetItem(String key, Object defaultValue);
+
+ /// <summary>
+ /// Gets all the data store items in the collection.
+ /// </summary>
+ /// <returns></returns>
+ List<IDataStoreItem> GetAll();
+
+ /// <summary>
+ /// Gets all the data store unsynchronized items in the collection.
+ /// </summary>
+ /// <returns></returns>
+ List<IDataStoreItem> GetUnsynchronized();
+
+ /// <summary>
+ /// Deleted an item by the specified key.
+ /// </summary>
+ /// <param name="key">The key.</param>
+ void Delete(String key);
+
+ /// <summary>
+ /// Deletes all items in the collection.
+ /// </summary>
+ void DeleteAll();
+
+ /// <summary>
+ /// Returns the number of items in the collection.
+ /// </summary>
+ /// <returns></returns>
+ int Count();
+ }
+}
diff --git a/Software/Visual_Studio/DataStore/Tango.DataStore/IDataStoreItem.cs b/Software/Visual_Studio/DataStore/Tango.DataStore/IDataStoreItem.cs
new file mode 100644
index 000000000..9c03f40ee
--- /dev/null
+++ b/Software/Visual_Studio/DataStore/Tango.DataStore/IDataStoreItem.cs
@@ -0,0 +1,44 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Tango.DataStore
+{
+ /// <summary>
+ /// Represents a data store item.
+ /// </summary>
+ public interface IDataStoreItem
+ {
+ /// <summary>
+ /// Gets or sets the unique identifier (Use only for synchronization).
+ /// </summary>
+ String Guid { get; set; }
+
+ /// <summary>
+ /// Gets or sets item id.
+ /// </summary>
+ String Key { get; set; }
+
+ /// <summary>
+ /// Gets or sets the item type.
+ /// </summary>
+ DataType Type { get; set; }
+
+ /// <summary>
+ /// Gets or sets the item value.
+ /// </summary>
+ Object Value { get; set; }
+
+ /// <summary>
+ /// Gets or sets the item update UTC date.
+ /// </summary>
+ DateTime Date { get; set; }
+
+ /// <summary>
+ /// Gets or sets a value indicating whether this item is synchronized with the remote service.
+ /// </summary>
+ bool IsSynchronized { get; set; }
+ }
+}
diff --git a/Software/Visual_Studio/DataStore/Tango.DataStore/IDataStoreManager.cs b/Software/Visual_Studio/DataStore/Tango.DataStore/IDataStoreManager.cs
new file mode 100644
index 000000000..5443f12e2
--- /dev/null
+++ b/Software/Visual_Studio/DataStore/Tango.DataStore/IDataStoreManager.cs
@@ -0,0 +1,28 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Tango.DataStore
+{
+ /// <summary>
+ /// Represents a key/value data store manager.
+ /// </summary>
+ /// <seealso cref="System.IDisposable" />
+ public interface IDataStoreManager : IDisposable
+ {
+ /// <summary>
+ /// Gets the data store collection by name.
+ /// </summary>
+ /// <param name="name">The name.</param>
+ /// <returns></returns>
+ IDataStoreCollection GetCollection(String name);
+
+ /// <summary>
+ /// Gets all the collection names.
+ /// </summary>
+ /// <returns></returns>
+ List<String> GetCollectionNames();
+ }
+}
diff --git a/Software/Visual_Studio/DataStore/Tango.DataStore/Properties/AssemblyInfo.cs b/Software/Visual_Studio/DataStore/Tango.DataStore/Properties/AssemblyInfo.cs
new file mode 100644
index 000000000..8e9365e03
--- /dev/null
+++ b/Software/Visual_Studio/DataStore/Tango.DataStore/Properties/AssemblyInfo.cs
@@ -0,0 +1,7 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+[assembly: AssemblyTitle("Tango - Data Store Library")]
+[assembly: AssemblyVersion("2.0.4.1608")]
+[assembly: ComVisible(false)] \ No newline at end of file
diff --git a/Software/Visual_Studio/DataStore/Tango.DataStore/Tango.DataStore.csproj b/Software/Visual_Studio/DataStore/Tango.DataStore/Tango.DataStore.csproj
new file mode 100644
index 000000000..d75d39963
--- /dev/null
+++ b/Software/Visual_Studio/DataStore/Tango.DataStore/Tango.DataStore.csproj
@@ -0,0 +1,78 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProjectGuid>{E0364DFA-0721-4637-9D32-9D22AAC109D6}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>Tango.DataStore</RootNamespace>
+ <AssemblyName>Tango.DataStore</AssemblyName>
+ <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
+ <FileAlignment>512</FileAlignment>
+ <Deterministic>true</Deterministic>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ <DocumentationFile>bin\Debug\Tango.DataStore.xml</DocumentationFile>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ <DocumentationFile>bin\Release\Tango.DataStore.xml</DocumentationFile>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="Google.Protobuf, Version=3.4.1.0, Culture=neutral, PublicKeyToken=a7d26565bac4d604, processorArchitecture=MSIL">
+ <HintPath>..\packages\Google.Protobuf.3.4.1\lib\net45\Google.Protobuf.dll</HintPath>
+ </Reference>
+ <Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
+ <HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
+ </Reference>
+ <Reference Include="System" />
+ <Reference Include="System.Core" />
+ <Reference Include="System.Xml.Linq" />
+ <Reference Include="System.Data.DataSetExtensions" />
+ <Reference Include="Microsoft.CSharp" />
+ <Reference Include="System.Data" />
+ <Reference Include="System.Net.Http" />
+ <Reference Include="System.Xml" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="..\..\Versioning\GlobalVersionInfo.cs">
+ <Link>GlobalVersionInfo.cs</Link>
+ </Compile>
+ <Compile Include="DataStoreHelper.cs" />
+ <Compile Include="DataStoreProtoObject.cs" />
+ <Compile Include="ExtensionMethods.cs" />
+ <Compile Include="IDataStoreItem.cs" />
+ <Compile Include="DataType.cs" />
+ <Compile Include="IDataStoreCollection.cs" />
+ <Compile Include="IDataStoreManager.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <None Include="packages.config" />
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\..\Tango.Core\Tango.Core.csproj">
+ <Project>{A34EE0F0-649D-41C8-8489-B6F1CC6924EE}</Project>
+ <Name>Tango.Core</Name>
+ </ProjectReference>
+ <ProjectReference Include="..\..\Tango.PMR\Tango.PMR.csproj">
+ <Project>{e4927038-348d-4295-aaf4-861c58cb3943}</Project>
+ <Name>Tango.PMR</Name>
+ </ProjectReference>
+ </ItemGroup>
+ <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+</Project> \ No newline at end of file
diff --git a/Software/Visual_Studio/DataStore/Tango.DataStore/packages.config b/Software/Visual_Studio/DataStore/Tango.DataStore/packages.config
new file mode 100644
index 000000000..026e719c3
--- /dev/null
+++ b/Software/Visual_Studio/DataStore/Tango.DataStore/packages.config
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="utf-8"?>
+<packages>
+ <package id="Google.Protobuf" version="3.4.1" targetFramework="net461" />
+ <package id="Newtonsoft.Json" version="9.0.1" targetFramework="net461" />
+</packages> \ No newline at end of file