[BF] Fixing Senml

[NF] Senml now has a configure option to setup the guid
[NF] Zway-Bot now listen on /exit
[NF] Implment searchpath for Zway-Bot (/etc/zwaybot and %appdata%/zwaybot)
This commit is contained in:
BlubbFish 2018-05-07 16:52:24 +00:00
parent affbfc79fc
commit 29caf6f6f6
27 changed files with 267 additions and 195 deletions

52
IoT/Connector/ABackend.cs Normal file
View File

@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BlubbFish.Utils.IoT.Events;
namespace BlubbFish.Utils.IoT.Connector {
public abstract class ABackend {
public enum BackendType {
Data,
User
}
public event BackendMessage MessageIncomming;
public event BackendMessage MessageSending;
public delegate void BackendMessage(Object sender, BackendEvent e);
protected Dictionary<String, String> settings;
public ABackend(Dictionary<String, String> settings) {
this.settings = settings;
}
public static ABackend GetInstance(Dictionary<String, String> settings, BackendType ty) {
if (settings.Count == 0) {
return null;
}
String object_sensor = "BlubbFish.Utils.IoT.Connector." + ty.ToString() + "." + settings["type"].ToUpperLower() + ", " + "Connector" + ty.ToString() + settings["type"].ToUpperLower();
Type t = null;
try {
t = Type.GetType(object_sensor, true);
} catch (TypeLoadException) {
Console.Error.WriteLine("Configuration: " + settings["type"] + " is not a " + ty.ToString() + "Backend");
return null;
} catch (System.IO.FileNotFoundException) {
Console.Error.WriteLine("Driver " + object_sensor + " could not load!");
return null;
}
return (ABackend)t.GetConstructor(new Type[] { typeof(Dictionary<String, String>) }).Invoke(new Object[] { settings });
}
protected void NotifyClientIncomming(BackendEvent value) {
this.MessageIncomming?.Invoke(this, value);
}
protected void NotifyClientSending(BackendEvent value) {
this.MessageSending?.Invoke(this, value);
}
public abstract void Dispose();
}
}

View File

@ -1,33 +1,10 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using BlubbFish.Utils.IoT.Events;
namespace BlubbFish.Utils.IoT.Connector { namespace BlubbFish.Utils.IoT.Connector {
public abstract class ADataBackend { public abstract class ADataBackend : ABackend {
public ADataBackend(Dictionary<String, String> settings) : base(settings) { }
public abstract event MqttMessage MessageIncomming;
public abstract event MqttMessage MessageSending;
public delegate void MqttMessage(Object sender, MqttEventArgs e);
public static ADataBackend GetInstance(Dictionary<String, String> settings) {
if(settings.Count == 0) {
return null;
}
String object_sensor = "BlubbFish.Utils.IoT.Connector.Data." + Char.ToUpper(settings["type"][0]) + settings["type"].Substring(1).ToLower()+", "+ "ConnectorData" + Char.ToUpper(settings["type"][0]) + settings["type"].Substring(1).ToLower();
Type t = null;
try {
t = Type.GetType(object_sensor, true);
} catch (TypeLoadException) {
Console.Error.WriteLine("settings.ini: " + settings["type"] + " is not a DataBackend");
return null;
} catch(System.IO.FileNotFoundException) {
Console.Error.WriteLine("Driver " + settings["type"] + " could not load!");
return null;
}
return (ADataBackend)t.GetConstructor(new Type[] { typeof(Dictionary<String, String>) }).Invoke(new Object[] { settings });
}
public abstract void Send(String topic, String data); public abstract void Send(String topic, String data);
public abstract void Dispose();
} }
} }

View File

@ -1,32 +1,11 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using BlubbFish.Utils.IoT.Events;
namespace BlubbFish.Utils.IoT.Connector { namespace BlubbFish.Utils.IoT.Connector {
public abstract class AUserBackend { public abstract class AUserBackend : ABackend {
protected Dictionary<String, String> settings; public AUserBackend(Dictionary<String, String> settings) : base(settings) {}
public abstract event TelegramMessage MessageIncomming;
public abstract event TelegramMessage MessageSending;
public delegate void TelegramMessage(Object sender, UserMessageEventArgs e);
public AUserBackend(Dictionary<String, String> settings) {
this.settings = settings;
}
public static AUserBackend GetInstance(Dictionary<String, String> settings) {
String object_sensor = "BlubbFish.Utils.IoT.Connector.User." + Char.ToUpper(settings["type"][0]) + settings["type"].Substring(1).ToLower()+", "+ "ConnectorUser" + Char.ToUpper(settings["type"][0]) + settings["type"].Substring(1).ToLower();
Type t = null;
try {
t = Type.GetType(object_sensor, true);
} catch (TypeLoadException) {
throw new ArgumentException("settings.ini: " + settings["type"] + " is not a UserBackend");
}
return (AUserBackend)t.GetConstructor(new Type[] { typeof(Dictionary<String, String>) }).Invoke(new Object[] { settings });
}
public abstract void Send(String message); public abstract void Send(String message);
public abstract void Send(String message, String[] buttons); public abstract void Send(String message, String[] buttons);
public abstract void Dispose();
} }
} }

View File

@ -45,7 +45,7 @@
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\..\..\..\Librarys\mqtt\M2Mqtt\M2Mqtt.csproj"> <ProjectReference Include="..\..\..\..\..\Librarys\mqtt\M2Mqtt\M2Mqtt_4.7.1.csproj">
<Project>{a11aef5a-b246-4fe8-8330-06db73cc8074}</Project> <Project>{a11aef5a-b246-4fe8-8330-06db73cc8074}</Project>
<Name>M2Mqtt</Name> <Name>M2Mqtt</Name>
</ProjectReference> </ProjectReference>

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
using BlubbFish.Utils.IoT.Events;
using uPLibrary.Networking.M2Mqtt; using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages; using uPLibrary.Networking.M2Mqtt.Messages;
@ -8,10 +9,7 @@ namespace BlubbFish.Utils.IoT.Connector.Data {
public class Mqtt : ADataBackend, IDisposable { public class Mqtt : ADataBackend, IDisposable {
private MqttClient client; private MqttClient client;
public override event MqttMessage MessageIncomming; public Mqtt(Dictionary<String, String> settings) : base(settings) {
public override event MqttMessage MessageSending;
public Mqtt(Dictionary<String, String> settings) {
if(settings.ContainsKey("port")) { if(settings.ContainsKey("port")) {
this.client = new MqttClient(settings["server"], Int32.Parse(settings["port"]), false, null, null, MqttSslProtocols.None); this.client = new MqttClient(settings["server"], Int32.Parse(settings["port"]), false, null, null, MqttSslProtocols.None);
} else { } else {
@ -27,12 +25,12 @@ namespace BlubbFish.Utils.IoT.Connector.Data {
} }
private void Client_MqttMsgPublishReceived(Object sender, MqttMsgPublishEventArgs e) { private void Client_MqttMsgPublishReceived(Object sender, MqttMsgPublishEventArgs e) {
this.MessageIncomming?.Invoke(this, new MqttEventArgs(Encoding.UTF8.GetString(e.Message), e.Topic)); this.NotifyClientIncomming(new DataEvent(Encoding.UTF8.GetString(e.Message), e.Topic, DateTime.Now));
} }
public override void Send(String topic, String data) { public override void Send(String topic, String data) {
this.client.Publish(topic, Encoding.UTF8.GetBytes(data)); this.client.Publish(topic, Encoding.UTF8.GetBytes(data));
this.MessageSending?.Invoke(this, new MqttEventArgs(data, topic)); this.NotifyClientSending(new DataEvent(data, topic, DateTime.Now));
} }
#region IDisposable Support #region IDisposable Support

View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BlubbFish.Utils.IoT.Events {
public class BackendEvent : EventArgs {
public BackendEvent() : base() { }
public BackendEvent(String message, Object from, DateTime date, String label) {
this.From = from;
this.Message = message;
this.Date = date;
this.Label = label;
}
public Object From { get; private set; }
public String Message { get; private set; }
public DateTime Date { get; private set; }
public String Label { get; private set; }
}
}

12
IoT/Events/DataEvent.cs Normal file
View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BlubbFish.Utils.IoT.Events {
public class DataEvent : BackendEvent {
public DataEvent(String data) : base() { }
public DataEvent(String message, String topic, DateTime date) : base(message, topic, date, "Data") { }
}
}

12
IoT/Events/UserEvent.cs Normal file
View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BlubbFish.Utils.IoT.Events {
public class UserEvent : BackendEvent {
public UserEvent() : base() { }
public UserEvent(String message, Int64 UserId, DateTime date) : base(message, UserId, date, "User") { }
}
}

19
IoT/Helper.cs Normal file
View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BlubbFish.Utils.IoT {
static class Helper {
internal static String ToUpperLower(this String s) {
if (s.Length == 0) {
return "";
}
if (s.Length == 1) {
return s.ToUpper();
}
return s[0].ToString().ToUpper() + s.Substring(1).ToLower();
}
}
}

View File

@ -4,17 +4,18 @@ using System.Linq;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading; using System.Threading;
using BlubbFish.Utils.IoT.Connector; using BlubbFish.Utils.IoT.Connector;
using BlubbFish.Utils.IoT.Events;
namespace BlubbFish.Utils.IoT.Sensor { namespace BlubbFish.Utils.IoT.JsonSensor {
public abstract class ASensor : IDisposable { public abstract class AJsonSensor : IDisposable {
protected String topic; protected String topic;
protected Int32 pollcount; protected Int32 pollcount;
private Dictionary<String, String> settings; protected Dictionary<String, String> settings;
protected ADataBackend backend; protected ABackend backend;
private Thread updateThread; private Thread pollingThread;
private Boolean pollEnabled = false; private Boolean pollEnabled = false;
public ASensor(Dictionary<String, String> settings, String name, ADataBackend backend) { public AJsonSensor(Dictionary<String, String> settings, String name, ABackend backend) {
this.GetBool = true; this.GetBool = true;
this.GetFloat = 0.0f; this.GetFloat = 0.0f;
this.GetInt = 0; this.GetInt = 0;
@ -28,8 +29,8 @@ namespace BlubbFish.Utils.IoT.Sensor {
this.pollEnabled = true; this.pollEnabled = true;
this.Polling = Int32.Parse(settings["polling"]); this.Polling = Int32.Parse(settings["polling"]);
this.pollcount = this.Polling; this.pollcount = this.Polling;
this.updateThread = new Thread(this.SensorPolling); this.pollingThread = new Thread(this.SensorPolling);
this.updateThread.Start(); this.pollingThread.Start();
} }
} }
@ -40,8 +41,8 @@ namespace BlubbFish.Utils.IoT.Sensor {
} }
} }
private void IncommingMqttMessage(Object sender, MqttEventArgs e) { private void IncommingMqttMessage(Object sender, BackendEvent e) {
if(Regex.Match(e.Topic, this.topic).Success) { if(Regex.Match(e.From.ToString(), this.topic).Success) {
if (this.UpdateValue(e)) { if (this.UpdateValue(e)) {
this.Timestamp = DateTime.Now; this.Timestamp = DateTime.Now;
this.Update?.Invoke(this, e); this.Update?.Invoke(this, e);
@ -49,29 +50,36 @@ namespace BlubbFish.Utils.IoT.Sensor {
} }
} }
public static ASensor GetInstance(ADataBackend backend, Dictionary<String, String> dictionary, String name) { public static AJsonSensor GetInstance(Dictionary<String, ABackend> backends, Dictionary<String, String> settings, String name) {
String object_sensor = "BlubbFish.Utils.IoT.Sensor." + Char.ToUpper(dictionary["type"][0]) + dictionary["type"].Substring(1).ToLower(); String object_sensor = "BlubbFish.Utils.IoT.JsonSensor." + Char.ToUpper(settings["type"][0]) + settings["type"].Substring(1).ToLower();
Type t = null; Type t = null;
try { try {
t = Type.GetType(object_sensor, true); t = Type.GetType(object_sensor, true);
} catch(TypeLoadException) { } catch(TypeLoadException) {
throw new ArgumentException("sensor.ini: " + dictionary["type"] + " is not a Sensor"); throw new ArgumentException("Sensor: " + object_sensor + " is not a Sensor");
} }
return (ASensor)t.GetConstructor(new Type[] { typeof(Dictionary<String, String>), typeof(String), typeof(ADataBackend) }).Invoke(new Object[] { dictionary, name, backend }); if(!settings.ContainsKey("backend") || !backends.ContainsKey(settings["backend"])) {
throw new ArgumentException("Backend not specified!");
}
return (AJsonSensor)t.GetConstructor(new Type[] { typeof(Dictionary<String, String>), typeof(String), typeof(ABackend) }).Invoke(new Object[] { settings, name, backends[settings["backend"]] });
} }
protected virtual void Poll() { protected virtual void Poll() {
if(this.pollcount++ >= this.Polling) { if(this.pollcount++ >= this.Polling) {
this.pollcount = 1; this.pollcount = 1;
this.backend.Send(this.topic + "/status",""); if (this.backend is ADataBackend) {
((ADataBackend)this.backend).Send(this.topic + "/get", "");
}
} }
} }
public virtual void SetBool(Boolean v) { public virtual void SetBool(Boolean v) {
this.backend.Send(this.topic + "/set", v ? "on" : "off"); if (this.backend is ADataBackend) {
((ADataBackend)this.backend).Send(this.topic + "/set", v ? "on" : "off");
}
} }
protected abstract Boolean UpdateValue(MqttEventArgs e); protected abstract Boolean UpdateValue(BackendEvent e);
public Single GetFloat { get; protected set; } public Single GetFloat { get; protected set; }
public Boolean GetBool { get; protected set; } public Boolean GetBool { get; protected set; }
@ -98,18 +106,18 @@ namespace BlubbFish.Utils.IoT.Sensor {
if(!this.disposedValue) { if(!this.disposedValue) {
if(disposing) { if(disposing) {
this.pollEnabled = false; this.pollEnabled = false;
if (this.updateThread != null && this.updateThread.ThreadState == ThreadState.Running) { if (this.pollingThread != null && this.pollingThread.ThreadState == ThreadState.Running) {
this.updateThread.Abort(); this.pollingThread.Abort();
while (this.updateThread.ThreadState != ThreadState.Aborted) { } while (this.pollingThread.ThreadState != ThreadState.Aborted) { }
} }
this.backend.MessageIncomming -= this.IncommingMqttMessage; this.backend.MessageIncomming -= this.IncommingMqttMessage;
} }
this.settings = null; this.settings = null;
this.updateThread = null; this.pollingThread = null;
this.disposedValue = true; this.disposedValue = true;
} }
} }
~ASensor() { ~AJsonSensor() {
Dispose(false); Dispose(false);
} }
public void Dispose() { public void Dispose() {

37
IoT/JsonSensor/Bosmon.cs Normal file
View File

@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BlubbFish.Utils.IoT.Connector;
using BlubbFish.Utils.IoT.Events;
using LitJson;
namespace BlubbFish.Utils.IoT.JsonSensor {
class Bosmon : AJsonSensor {
public Bosmon(Dictionary<String, String> settings, String name, ABackend backend) : base(settings, name, backend) {
}
public String Ric { get; private set; }
public String Message { get; private set; }
public String Func { get; private set; }
public DateTime Time { get; private set; }
protected override Boolean UpdateValue(BackendEvent e) {
try {
JsonData json = JsonMapper.ToObject(e.Message);
if(json.ContainsKey("TYPE_POCSAG")) {
if(this.settings["rics"].Split(';').ToList().Contains(json["Address"].ToString())) {
this.Ric = json["Address"].ToString();
this.Message = json["Msg"].ToString();
this.Func = json["Func"].ToString();
this.Time = new DateTime(Int64.Parse(json["Timestamp"].ToString()));
return true;
}
}
} catch(Exception) { }
//throw new NotImplementedException();
return false;
}
}
}

View File

@ -2,9 +2,10 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using BlubbFish.Utils.IoT.Connector; using BlubbFish.Utils.IoT.Connector;
using BlubbFish.Utils.IoT.Events;
namespace BlubbFish.Utils.IoT.Sensor { namespace BlubbFish.Utils.IoT.JsonSensor {
class Luminanz : ASensor { class Luminanz : AJsonSensor {
public Luminanz(Dictionary<String, String> settings, String name, ADataBackend backend) : base(settings, name, backend) { public Luminanz(Dictionary<String, String> settings, String name, ADataBackend backend) : base(settings, name, backend) {
this.GetBool = true; this.GetBool = true;
this.GetFloat = 0.0f; this.GetFloat = 0.0f;
@ -12,7 +13,7 @@ namespace BlubbFish.Utils.IoT.Sensor {
this.Datatypes = Types.Int; this.Datatypes = Types.Int;
} }
protected override Boolean UpdateValue(MqttEventArgs e) { protected override Boolean UpdateValue(BackendEvent e) {
this.GetInt = Int32.Parse(e.Message, new CultureInfo("en-US")); this.GetInt = Int32.Parse(e.Message, new CultureInfo("en-US"));
return true; return true;
} }

View File

@ -1,14 +1,15 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using BlubbFish.Utils.IoT.Connector; using BlubbFish.Utils.IoT.Connector;
using BlubbFish.Utils.IoT.Events;
namespace BlubbFish.Utils.IoT.Sensor { namespace BlubbFish.Utils.IoT.JsonSensor {
class Pir : ASensor { class Pir : AJsonSensor {
public Pir(Dictionary<String, String> settings, String name, ADataBackend backend) : base(settings, name, backend) { public Pir(Dictionary<String, String> settings, String name, ADataBackend backend) : base(settings, name, backend) {
this.Datatypes = Types.Bool; this.Datatypes = Types.Bool;
} }
protected override Boolean UpdateValue(MqttEventArgs e) { protected override Boolean UpdateValue(BackendEvent e) {
this.GetBool = (e.Message.ToLower() == "on") ? true : false; this.GetBool = (e.Message.ToLower() == "on") ? true : false;
return true; return true;
} }

View File

@ -2,9 +2,10 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using BlubbFish.Utils.IoT.Connector; using BlubbFish.Utils.IoT.Connector;
using BlubbFish.Utils.IoT.Events;
namespace BlubbFish.Utils.IoT.Sensor { namespace BlubbFish.Utils.IoT.JsonSensor {
class Power : ASensor { class Power : AJsonSensor {
public Power(Dictionary<String, String> settings, String name, ADataBackend backend) : base(settings, name, backend) { public Power(Dictionary<String, String> settings, String name, ADataBackend backend) : base(settings, name, backend) {
this.GetBool = true; this.GetBool = true;
this.GetFloat = 0.0f; this.GetFloat = 0.0f;
@ -12,7 +13,7 @@ namespace BlubbFish.Utils.IoT.Sensor {
this.Datatypes = Types.Float; this.Datatypes = Types.Float;
} }
protected override Boolean UpdateValue(MqttEventArgs e) { protected override Boolean UpdateValue(BackendEvent e) {
this.GetFloat = Single.Parse(e.Message, new CultureInfo("en-US")); this.GetFloat = Single.Parse(e.Message, new CultureInfo("en-US"));
return true; return true;
} }

View File

@ -1,14 +1,15 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using BlubbFish.Utils.IoT.Connector; using BlubbFish.Utils.IoT.Connector;
using BlubbFish.Utils.IoT.Events;
namespace BlubbFish.Utils.IoT.Sensor { namespace BlubbFish.Utils.IoT.JsonSensor {
class Switch : ASensor { class Switch : AJsonSensor {
public Switch(Dictionary<System.String, System.String> settings, String name, ADataBackend backend) : base(settings, name, backend) { public Switch(Dictionary<System.String, System.String> settings, String name, ADataBackend backend) : base(settings, name, backend) {
this.Datatypes = Types.Bool; this.Datatypes = Types.Bool;
} }
protected override Boolean UpdateValue(MqttEventArgs e) { protected override Boolean UpdateValue(BackendEvent e) {
this.GetBool = (e.Message.ToLower() == "on") ? true : false; this.GetBool = (e.Message.ToLower() == "on") ? true : false;
return true; return true;
} }

View File

@ -2,9 +2,10 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using BlubbFish.Utils.IoT.Connector; using BlubbFish.Utils.IoT.Connector;
using BlubbFish.Utils.IoT.Events;
namespace BlubbFish.Utils.IoT.Sensor { namespace BlubbFish.Utils.IoT.JsonSensor {
class Temperatur : ASensor { class Temperatur : AJsonSensor {
public Temperatur(Dictionary<String, String> settings, String name, ADataBackend backend) : base(settings, name, backend) { public Temperatur(Dictionary<String, String> settings, String name, ADataBackend backend) : base(settings, name, backend) {
this.GetBool = true; this.GetBool = true;
this.GetFloat = 0.0f; this.GetFloat = 0.0f;
@ -12,7 +13,7 @@ namespace BlubbFish.Utils.IoT.Sensor {
this.Datatypes = Types.Float; this.Datatypes = Types.Float;
} }
protected override Boolean UpdateValue(MqttEventArgs e) { protected override Boolean UpdateValue(BackendEvent e) {
this.GetFloat = Single.Parse(e.Message, new CultureInfo("en-US")); this.GetFloat = Single.Parse(e.Message, new CultureInfo("en-US"));
return true; return true;
} }

View File

@ -1,52 +0,0 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text.RegularExpressions;
using BlubbFish.Utils.IoT.Connector;
using LitJson;
namespace BlubbFish.Utils.IoT.Sensor {
class Flex4gridswitch : ASensor {
private String id;
public Flex4gridswitch(Dictionary<String, String> settings, String name, ADataBackend backend) : base(settings, name, backend) {
this.Datatypes = Types.Bool;
this.id = settings["id"];
}
protected override Boolean UpdateValue(MqttEventArgs e) {
CultureInfo info = new CultureInfo("de-DE");
info.NumberFormat.NumberDecimalSeparator = ".";
CultureInfo.DefaultThreadCurrentCulture = info;
CultureInfo.DefaultThreadCurrentUICulture = info;
System.Threading.Thread.CurrentThread.CurrentCulture = info;
System.Threading.Thread.CurrentThread.CurrentUICulture = info;
try {
JsonData data = JsonMapper.ToObject(e.Message);
if (data.Keys.Contains("id") && data["id"].ToString() == this.id) {
this.GetBool = data["status"].ToString() == "active" ? true : false;
return true;
}
if(data.Keys.Contains(this.id)) {
this.GetBool = data[this.id].ToString() == "active" ? true : false;
return true;
}
} catch (Exception) {
}
return false;
}
protected override void Poll() {
if (this.pollcount++ >= this.Polling) {
this.pollcount = 1;
String hid = Regex.Match(this.topic, "/flex4grid/v1/households/([^/]*)/device/state").Groups[1].Value;
this.backend.Send("/flex4grid/v1/households/" + hid + "/devices/state/request", "{\"timestamp\": \"" + DateTime.Now.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'ffffff'Z'") + "\"}");
}
}
public override void SetBool(Boolean v) {
String hid = Regex.Match(this.topic, "/flex4grid/v1/households/([^/]*)/device/state").Groups[1].Value;
this.backend.Send("/flex4grid/v1/households/" + hid + "/device/actuate", "{\"command\":\""+(v ? "ON" : "OFF") + "\",\"deviceId\":\""+ this.id + "\",\"timestamp\":\"" + DateTime.Now.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'ffffff'Z'") + "\"}");
}
}
}

View File

@ -1,34 +0,0 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using BlubbFish.Utils.IoT.Connector;
using LitJson;
namespace BlubbFish.Utils.IoT.Sensor {
class Flex4gridpower : ASensor {
private String id;
public Flex4gridpower(Dictionary<String, String> settings, String name, ADataBackend backend) : base(settings, name, backend) {
this.Datatypes = Types.Float;
this.id = settings["id"];
}
protected override Boolean UpdateValue(MqttEventArgs e) {
CultureInfo info = new CultureInfo("de-DE");
info.NumberFormat.NumberDecimalSeparator = ".";
CultureInfo.DefaultThreadCurrentCulture = info;
CultureInfo.DefaultThreadCurrentUICulture = info;
System.Threading.Thread.CurrentThread.CurrentCulture = info;
System.Threading.Thread.CurrentThread.CurrentUICulture = info;
try {
JsonData data = JsonMapper.ToObject(e.Message);
if(data["id"].ToString() == this.id) {
this.GetFloat = Single.Parse(data["power"].ToString());
return true;
}
} catch (Exception) {
}
return false;
}
}
}

View File

@ -44,18 +44,21 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Connector\ABackend.cs" />
<Compile Include="Connector\ADataBackend.cs" /> <Compile Include="Connector\ADataBackend.cs" />
<Compile Include="Connector\AUserBackend.cs" /> <Compile Include="Connector\AUserBackend.cs" />
<Compile Include="Connector\Helper.cs" /> <Compile Include="Events\BackendEvent.cs" />
<Compile Include="Events\DataEvent.cs" />
<Compile Include="Events\UserEvent.cs" />
<Compile Include="Helper.cs" />
<Compile Include="JsonSensor\Bosmon.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Sensor\ASensor.cs" /> <Compile Include="JsonSensor\AJsonSensor.cs" />
<Compile Include="Sensor\Flex4GridPower.cs" /> <Compile Include="JsonSensor\Luminanz.cs" />
<Compile Include="Sensor\Flex4GridSwitch.cs" /> <Compile Include="JsonSensor\Pir.cs" />
<Compile Include="Sensor\Luminanz.cs" /> <Compile Include="JsonSensor\Power.cs" />
<Compile Include="Sensor\Pir.cs" /> <Compile Include="JsonSensor\Switch.cs" />
<Compile Include="Sensor\Power.cs" /> <Compile Include="JsonSensor\Temperatur.cs" />
<Compile Include="Sensor\Switch.cs" />
<Compile Include="Sensor\Temperatur.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\Librarys\litjson\litjson\litjson_4.7.1.csproj"> <ProjectReference Include="..\..\Librarys\litjson\litjson\litjson_4.7.1.csproj">

Binary file not shown.

View File

@ -2,23 +2,57 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace BlubbFish.Utils namespace BlubbFish.Utils {
{
public class InIReader : IDisposable public class InIReader : IDisposable
{ {
private Dictionary<String, Dictionary<String, String>> inifile; private Dictionary<String, Dictionary<String, String>> inifile;
private FileSystemWatcher k = new FileSystemWatcher(Directory.GetCurrentDirectory(), "*.ini"); private FileSystemWatcher k;
private String filename; private String filename;
private static List<String> search_path = new List<String>() {
Directory.GetCurrentDirectory()
};
private static Dictionary<String, InIReader> instances = new Dictionary<String, InIReader>(); private static Dictionary<String, InIReader> instances = new Dictionary<String, InIReader>();
public static void SetSearchPath(List<String> directorys) {
search_path.AddRange(directorys);
}
public static Boolean ConfigExist(String filename) {
foreach (String path in search_path) {
if (File.Exists(path + Path.DirectorySeparatorChar + filename)) {
return true;
} else if (File.Exists(path + Path.DirectorySeparatorChar + filename + ".ini")) {
return true;
} else if (File.Exists(path + Path.DirectorySeparatorChar + filename + ".conf")) {
return true;
}
}
return false;
}
private InIReader(String filename) private InIReader(String filename)
{ {
this.filename = filename; foreach (String path in search_path) {
if (File.Exists(path + Path.DirectorySeparatorChar + filename)) {
this.filename = path + Path.DirectorySeparatorChar + filename;
this.k = new FileSystemWatcher(path, filename);
break;
} else if (File.Exists(path + Path.DirectorySeparatorChar + filename + ".ini")) {
this.filename = path + Path.DirectorySeparatorChar + filename + ".ini";
this.k = new FileSystemWatcher(path, filename + ".ini");
break;
} else if(File.Exists(path + Path.DirectorySeparatorChar + filename + ".conf")) {
this.filename = path + Path.DirectorySeparatorChar + filename + ".conf";
this.k = new FileSystemWatcher(path, filename + ".conf");
break;
}
}
if(this.filename == null) {
throw new ArgumentException(filename + " not found!");
}
this.k.Changed += new FileSystemEventHandler(this.ReadAgain); this.k.Changed += new FileSystemEventHandler(this.ReadAgain);
LoadFile(); LoadFile();
} }

Binary file not shown.