Browse Source

Publish to InfluxDB

Robert Marshall 6 years ago
parent
commit
ff68ebdd80

+ 69 - 0
HAServer.InfluxDB/HAServer.InfluxDB.csproj

@@ -0,0 +1,69 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="14.0" DefaultTargets="Build" 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>{CA0624EE-133A-4D2C-A9AD-88D2265C7485}</ProjectGuid>
+    <OutputType>Library</OutputType>
+    <AppDesignerFolder>Properties</AppDesignerFolder>
+    <RootNamespace>HAServer.InfluxDB</RootNamespace>
+    <AssemblyName>HAServer.InfluxDB</AssemblyName>
+    <TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
+    <FileAlignment>512</FileAlignment>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>..\HAServer\bin\Debug\Components\</OutputPath>
+    <DefineConstants>DEBUG;TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </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>
+  </PropertyGroup>
+  <ItemGroup>
+    <Reference Include="InfluxDB.LineProtocol, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
+      <HintPath>..\packages\InfluxDB.LineProtocol.1.0.0\lib\net45\InfluxDB.LineProtocol.dll</HintPath>
+      <Private>True</Private>
+    </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="InfluxDB.cs" />
+    <Compile Include="InfluxDbException.cs" />
+    <Compile Include="ISettingsProviderInfluxDBExtensions.cs" />
+    <Compile Include="Properties\AssemblyInfo.cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <ProjectReference Include="..\HAServer.Interfaces\HAServer.Interfaces.csproj">
+      <Project>{7C70EE15-0A07-41D3-8D33-94C82A8ED299}</Project>
+      <Name>HAServer.Interfaces</Name>
+    </ProjectReference>
+  </ItemGroup>
+  <ItemGroup>
+    <None Include="packages.config" />
+  </ItemGroup>
+  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
+       Other similar extension points exist, see Microsoft.Common.targets.
+  <Target Name="BeforeBuild">
+  </Target>
+  <Target Name="AfterBuild">
+  </Target>
+  -->
+</Project>

+ 33 - 0
HAServer.InfluxDB/ISettingsProviderInfluxDBExtensions.cs

@@ -0,0 +1,33 @@
+using HAServer.Interfaces.Core;
+
+namespace HAServer.InfluxDB {
+	public static class SettingsProviderInfluxDBExtensions {
+		public static string Host(this ISettingsProvider provider) {
+			return provider.GetValue("influxHost");
+		}
+
+		public static int Port(this ISettingsProvider provider) {
+			return int.Parse(provider.GetValue("influxPort", "8086"));
+		}
+
+		public static string Database(this ISettingsProvider provider) {
+			return provider.GetValue("influxDatabase");
+		}
+
+		public static string Measurement(this ISettingsProvider provider) {
+			return provider.GetValue("influxMeasurement");
+		}
+
+		public static string Username(this ISettingsProvider provider) {
+			return provider.GetValue("influxUsername", string.Empty);
+		}
+
+		public static string Password(this ISettingsProvider provider) {
+			return provider.GetValue("influxPassword", string.Empty);
+		}
+
+		public static string Topic(this ISettingsProvider provider) {
+			return provider.GetValue("influxTopic", ".*");
+		}
+	}
+}

+ 39 - 0
HAServer.InfluxDB/InfluxDB.cs

@@ -0,0 +1,39 @@
+using System;
+using System.Collections.Generic;
+using HAServer.Interfaces.Core;
+using InfluxDB.LineProtocol.Client;
+using InfluxDB.LineProtocol.Payload;
+
+namespace HAServer.InfluxDB {
+	public class InfluxDB:IComponent, ISubscriber {
+		private readonly IMessageBus _messageBus;
+		private readonly ISettingsProvider _settingsProvider;
+		private readonly LineProtocolClient _client;
+
+		public InfluxDB(IMessageBus messageBus, ISettingsProvider settingsProvider) {
+			_messageBus = messageBus;
+			_settingsProvider = settingsProvider;
+			var uri = $"http://{_settingsProvider.Host()}:{settingsProvider.Port()}";
+			_client = new LineProtocolClient(new Uri(uri),
+											 _settingsProvider.Database(),
+											 _settingsProvider.Username(),
+											 _settingsProvider.Password());
+		}
+
+		public void Initialise() {
+			_messageBus.AddSubscriber(this, _settingsProvider.Topic());
+		}
+
+		public void ProcessMessage(string topic, string message) {
+			var data = new LineProtocolPoint(_settingsProvider.Measurement(),
+											 new Dictionary<string, object> {{"value", message}},
+											 new Dictionary<string, string> {{"topic", topic}});
+
+			var payload = new LineProtocolPayload();
+			payload.Add(data);
+			var result = _client.WriteAsync(payload).Result;
+			if (!result.Success)
+				throw new InfluxDbException(result.ErrorMessage);
+		}
+	}
+}

+ 9 - 0
HAServer.InfluxDB/InfluxDbException.cs

@@ -0,0 +1,9 @@
+using System;
+
+namespace HAServer.InfluxDB {
+	public class InfluxDbException : Exception {
+		public InfluxDbException(string resultErrorMessage):base($"InfluxDB error: {resultErrorMessage}") {
+			
+		}
+	}
+}

+ 36 - 0
HAServer.InfluxDB/Properties/AssemblyInfo.cs

@@ -0,0 +1,36 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following 
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("HAServer.InfluxDB")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("HAServer.InfluxDB")]
+[assembly: AssemblyCopyright("Copyright ©  2017")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// Setting ComVisible to false makes the types in this assembly not visible 
+// to COM components.  If you need to access a type in this assembly from 
+// COM, set the ComVisible attribute to true on that type.
+[assembly: ComVisible(false)]
+
+// The following GUID is for the ID of the typelib if this project is exposed to COM
+[assembly: Guid("ca0624ee-133a-4d2c-a9ad-88d2265c7485")]
+
+// Version information for an assembly consists of the following four values:
+//
+//      Major Version
+//      Minor Version 
+//      Build Number
+//      Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers 
+// by using the '*' as shown below:
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]

+ 4 - 0
HAServer.InfluxDB/packages.config

@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<packages>
+  <package id="InfluxDB.LineProtocol" version="1.0.0" targetFramework="net452" />
+</packages>

+ 10 - 0
HAServer.sln

@@ -14,6 +14,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HAServer.Components.Tests",
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HAServer.Tests.Integration", "HAServer.Tests.Integration\HAServer.Tests.Integration.csproj", "{C8E4C1F3-460F-4513-B9CF-4951A931AC58}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HAServer.InfluxDB", "HAServer.InfluxDB\HAServer.InfluxDB.csproj", "{CA0624EE-133A-4D2C-A9AD-88D2265C7485}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -70,6 +72,14 @@ Global
 		{C8E4C1F3-460F-4513-B9CF-4951A931AC58}.Release|Any CPU.Build.0 = Release|Any CPU
 		{C8E4C1F3-460F-4513-B9CF-4951A931AC58}.Release|x86.ActiveCfg = Release|Any CPU
 		{C8E4C1F3-460F-4513-B9CF-4951A931AC58}.Release|x86.Build.0 = Release|Any CPU
+		{CA0624EE-133A-4D2C-A9AD-88D2265C7485}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{CA0624EE-133A-4D2C-A9AD-88D2265C7485}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{CA0624EE-133A-4D2C-A9AD-88D2265C7485}.Debug|x86.ActiveCfg = Debug|Any CPU
+		{CA0624EE-133A-4D2C-A9AD-88D2265C7485}.Debug|x86.Build.0 = Debug|Any CPU
+		{CA0624EE-133A-4D2C-A9AD-88D2265C7485}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{CA0624EE-133A-4D2C-A9AD-88D2265C7485}.Release|Any CPU.Build.0 = Release|Any CPU
+		{CA0624EE-133A-4D2C-A9AD-88D2265C7485}.Release|x86.ActiveCfg = Release|Any CPU
+		{CA0624EE-133A-4D2C-A9AD-88D2265C7485}.Release|x86.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

+ 4 - 0
HAServer/app.config

@@ -9,5 +9,9 @@
 		<add key="webUiPath" value="../../WebUI" />
 		<add key="latitude" value="53.4808" />
 		<add key="longitude" value="-2.2426" />
+		<add key="influxHost" value="localhost" />
+		<add key="influxDatabase" value="HAServer" />
+		<add key="influxMeasurement" value="log" />
+		<add key="influxTopic" value="/home" />
 	</appSettings>
 </configuration>