move Mosquitto to ConnectorDataMosquitto
This commit is contained in:
commit
d676b08c76
54
ConnectorDataMosquitto/ConnectorDataMosquitto.csproj
Normal file
54
ConnectorDataMosquitto/ConnectorDataMosquitto.csproj
Normal file
@ -0,0 +1,54 @@
|
||||
<?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>{39235FAD-BA9D-4B51-82FC-6969967BEAE9}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>BlubbFish.Utils.IoT.Connector.Data</RootNamespace>
|
||||
<AssemblyName>ConnectorDataMosquitto</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.7.1</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<TargetFrameworkProfile />
|
||||
</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>
|
||||
</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="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="Mosquitto.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\Utils-IoT.csproj">
|
||||
<Project>{b870e4d5-6806-4a0b-b233-8907eedc5afc}</Project>
|
||||
<Name>Utils-IoT</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
125
ConnectorDataMosquitto/Mosquitto.cs
Normal file
125
ConnectorDataMosquitto/Mosquitto.cs
Normal file
@ -0,0 +1,125 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace BlubbFish.Utils.IoT.Connector.Data {
|
||||
public class Mosquitto : ADataBackend, IDisposable {
|
||||
private Process p;
|
||||
private String message;
|
||||
|
||||
public override event MqttMessage MessageIncomming;
|
||||
public override event MqttMessage MessageSending;
|
||||
|
||||
public Mosquitto(Dictionary<String, String> mqtt_settings) {
|
||||
this.settings = mqtt_settings;
|
||||
//mosquitto_sub --cafile ca.pem --cert cert.pem --key cert.key -h swb.broker.flex4grid.eu -p 8883 -t "#" -v -d
|
||||
this.message = "";
|
||||
this.p = new Process();
|
||||
this.p.StartInfo.FileName = "mosquitto_sub";
|
||||
String topic = "#";
|
||||
String args = "-h " + this.settings["server"]+" ";
|
||||
if(this.settings.ContainsKey("port")) {
|
||||
args += "-p "+ this.settings["port"]+" ";
|
||||
}
|
||||
if (this.settings.ContainsKey("cafile")) {
|
||||
args += "--cafile " + this.settings["cafile"] + " ";
|
||||
}
|
||||
if (this.settings.ContainsKey("cert")) {
|
||||
args += "--cert " + this.settings["cert"] + " ";
|
||||
}
|
||||
if (this.settings.ContainsKey("key")) {
|
||||
args += "--key " + this.settings["key"] + " ";
|
||||
}
|
||||
if(this.settings.ContainsKey("topic")) {
|
||||
topic = this.settings["topic"];
|
||||
}
|
||||
this.p.StartInfo.Arguments = args+"-t \""+ topic + "\" -v -d";
|
||||
this.p.StartInfo.CreateNoWindow = true;
|
||||
this.p.StartInfo.UseShellExecute = false;
|
||||
this.p.StartInfo.RedirectStandardOutput = true;
|
||||
this.p.StartInfo.RedirectStandardError = true;
|
||||
this.p.OutputDataReceived += this.P_OutputDataReceived;
|
||||
this.p.ErrorDataReceived += this.P_ErrorDataReceived;
|
||||
this.p.Start();
|
||||
this.p.BeginOutputReadLine();
|
||||
|
||||
}
|
||||
|
||||
public override void Send(String topic, String data) {
|
||||
Process send = new Process();
|
||||
send.StartInfo.FileName = "mosquitto_pub";
|
||||
String args = "-h " + this.settings["server"] + " ";
|
||||
if (this.settings.ContainsKey("port")) {
|
||||
args += "-p " + this.settings["port"] + " ";
|
||||
}
|
||||
if (this.settings.ContainsKey("cafile")) {
|
||||
args += "--cafile " + this.settings["cafile"] + " ";
|
||||
}
|
||||
if (this.settings.ContainsKey("cert")) {
|
||||
args += "--cert " + this.settings["cert"] + " ";
|
||||
}
|
||||
if (this.settings.ContainsKey("key")) {
|
||||
args += "--key " + this.settings["key"] + " ";
|
||||
}
|
||||
send.StartInfo.Arguments = args + "-m \""+data.Replace("\"","\\\"")+"\" -t \""+topic+"\" -d";
|
||||
send.StartInfo.CreateNoWindow = true;
|
||||
send.StartInfo.UseShellExecute = false;
|
||||
send.StartInfo.RedirectStandardOutput = true;
|
||||
send.StartInfo.RedirectStandardError = true;
|
||||
send.Start();
|
||||
send.WaitForExit();
|
||||
MessageSending?.Invoke(this, new MqttEventArgs(data, topic));
|
||||
}
|
||||
|
||||
private void P_ErrorDataReceived(Object sender, DataReceivedEventArgs e) {
|
||||
if (e.Data != null) {
|
||||
throw new NotImplementedException(e.Data);
|
||||
}
|
||||
}
|
||||
|
||||
private void P_OutputDataReceived(Object sender, DataReceivedEventArgs e) {
|
||||
if (e.Data != null) {
|
||||
if (e.Data.StartsWith("Client mosqsub")) {
|
||||
if (this.message != "" && this.message.IndexOf(" received PUBLISH ") > 0) {
|
||||
MatchCollection matches = (new Regex("^Client mosqsub[\\|/].*received PUBLISH \\(.*,.*,.*,.*, '(.*)'.*\\)\\)\n[^ ]* (.*)$", RegexOptions.IgnoreCase | RegexOptions.Singleline)).Matches(this.message);
|
||||
String topic = matches[0].Groups[1].Value;
|
||||
String message = matches[0].Groups[2].Value.Trim();
|
||||
this.MessageIncomming?.Invoke(this, new MqttEventArgs(message, topic));
|
||||
}
|
||||
this.message = e.Data + "\n";
|
||||
} else {
|
||||
this.message += e.Data + "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#region IDisposable Support
|
||||
private Boolean disposedValue = false; // Dient zur Erkennung redundanter Aufrufe.
|
||||
private readonly Dictionary<String, String> settings;
|
||||
|
||||
protected virtual void Dispose(Boolean disposing) {
|
||||
if (!this.disposedValue) {
|
||||
if (disposing) {
|
||||
this.p.CancelOutputRead();
|
||||
if (!this.p.HasExited) {
|
||||
this.p.Kill();
|
||||
}
|
||||
this.p.Close();
|
||||
}
|
||||
this.p = null;
|
||||
this.disposedValue = true;
|
||||
}
|
||||
}
|
||||
|
||||
~Mosquitto() {
|
||||
Dispose(false);
|
||||
}
|
||||
|
||||
public override void Dispose() {
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
36
ConnectorDataMosquitto/Properties/AssemblyInfo.cs
Normal file
36
ConnectorDataMosquitto/Properties/AssemblyInfo.cs
Normal file
@ -0,0 +1,36 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// Allgemeine Informationen über eine Assembly werden über die folgenden
|
||||
// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern,
|
||||
// die einer Assembly zugeordnet sind.
|
||||
[assembly: AssemblyTitle("ConnectorDataMosquitto")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("ConnectorDataMosquitto")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2017")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Durch Festlegen von ComVisible auf FALSE werden die Typen in dieser Assembly
|
||||
// für COM-Komponenten unsichtbar. Wenn Sie auf einen Typ in dieser Assembly von
|
||||
// COM aus zugreifen müssen, sollten Sie das ComVisible-Attribut für diesen Typ auf "True" festlegen.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird
|
||||
[assembly: Guid("39235fad-ba9d-4b51-82fc-6969967beae9")]
|
||||
|
||||
// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten:
|
||||
//
|
||||
// Hauptversion
|
||||
// Nebenversion
|
||||
// Buildnummer
|
||||
// Revision
|
||||
//
|
||||
// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden,
|
||||
// indem Sie "*" wie unten gezeigt eingeben:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
Loading…
Reference in New Issue
Block a user