[NF] Zway-Bot Module eingefügt

This commit is contained in:
BlubbFish 2017-12-02 18:04:43 +00:00
parent 925131a52b
commit 8771a3ce89
10 changed files with 133 additions and 45 deletions

View File

@ -1,7 +1,7 @@
using System; using System;
namespace ZwayBot { namespace ZwayBot {
public class CronEvent : EventArgs { public class CronEvent : ModulEventArgs {
public CronEvent() { public CronEvent() {
} }
@ -10,13 +10,10 @@ namespace ZwayBot {
this.Address = addr; this.Address = addr;
this.Property = prop; this.Property = prop;
this.Value = value; this.Value = value;
this.Source = "Cronjob";
} }
public String Address { get; }
public String Property { get; }
public String Value { get; }
} }
public class OvertakerEvent : EventArgs { public class OvertakerEvent : ModulEventArgs {
public OvertakerEvent() { public OvertakerEvent() {
} }
@ -25,10 +22,16 @@ namespace ZwayBot {
this.Address = addr; this.Address = addr;
this.Property = prop; this.Property = prop;
this.Value = value; this.Value = value;
this.Source = "Overtaker";
}
} }
public String Address { get; } public class ModulEventArgs : EventArgs {
public String Property { get; } public ModulEventArgs() {
public String Value { get; } }
public String Address { get; protected set; }
public String Property { get; protected set; }
public String Value { get; protected set; }
public String Source { get; protected set; }
} }
} }

View File

@ -44,5 +44,11 @@ namespace ZwayBot {
} }
return false; return false;
} }
internal static void WriteError(String text) {
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.WriteLine("ERROR: " + text);
Console.ResetColor();
}
} }
} }

20
Zway-Bot/Moduls/AModul.cs Normal file
View File

@ -0,0 +1,20 @@
using System;
using BlubbFish.IoT.Zway;
using BlubbFish.Utils;
namespace ZwayBot {
abstract class AModul {
protected ZwayController zw;
protected InIReader ini;
public AModul(ZwayController zway, InIReader settings) {
this.zw = zway;
this.ini = settings;
}
public delegate void ModulEvent(Object sender, ModulEventArgs e);
public abstract event ModulEvent Update;
public abstract void Dispose();
}
}

View File

@ -7,15 +7,12 @@ using BlubbFish.IoT.Zway;
using BlubbFish.IoT.Zway.Interfaces; using BlubbFish.IoT.Zway.Interfaces;
using BlubbFish.Utils; using BlubbFish.Utils;
namespace ZwayBot { namespace ZwayBot.Moduls {
internal class CronJob : IDisposable { internal class CronJob : AModul, IDisposable {
private ZwayController zw;
private InIReader ini;
private DateTime crontime; private DateTime crontime;
private Thread thread; private Thread thread;
public delegate void CronRunned(Object sender, CronEvent e); public override event ModulEvent Update;
public event CronRunned Update;
private Dictionary<String, String> cron_named = new Dictionary<String, String> { private Dictionary<String, String> cron_named = new Dictionary<String, String> {
{ "@yearly", "0 0 1 1 *" }, { "@yearly", "0 0 1 1 *" },
@ -26,9 +23,7 @@ namespace ZwayBot {
{ "@hourly", "0 * * * *" } { "@hourly", "0 * * * *" }
}; };
public CronJob(ZwayController zw) { public CronJob(ZwayController zway, InIReader settings) : base(zway, settings) {
this.zw = zw;
this.ini = InIReader.GetInstance("cron.ini");
this.crontime = DateTime.Now; this.crontime = DateTime.Now;
this.thread = new Thread(this.Runner); this.thread = new Thread(this.Runner);
this.thread.Start(); this.thread.Start();
@ -162,7 +157,7 @@ namespace ZwayBot {
} }
#region IDisposable Support #region IDisposable Support
private Boolean disposedValue = false; // Dient zur Erkennung redundanter Aufrufe. private Boolean disposedValue = false;
protected virtual void Dispose(Boolean disposing) { protected virtual void Dispose(Boolean disposing) {
if (!this.disposedValue) { if (!this.disposedValue) {
@ -181,7 +176,7 @@ namespace ZwayBot {
Dispose(false); Dispose(false);
} }
public void Dispose() { public override void Dispose() {
Dispose(true); Dispose(true);
GC.SuppressFinalize(this); GC.SuppressFinalize(this);
} }

View File

@ -5,18 +5,13 @@ using BlubbFish.IoT.Zway.Events;
using BlubbFish.IoT.Zway.Interfaces; using BlubbFish.IoT.Zway.Interfaces;
using BlubbFish.Utils; using BlubbFish.Utils;
namespace ZwayBot { namespace ZwayBot.Moduls {
internal class Overtaker { internal class Overtaker : AModul, IDisposable {
private ZwayController zw;
private InIReader ini;
private Dictionary<String, Dictionary<String, String>> events = new Dictionary<String, Dictionary<String, String>>(); private Dictionary<String, Dictionary<String, String>> events = new Dictionary<String, Dictionary<String, String>>();
public delegate void OvertakeValue(Object sender, OvertakerEvent e); public override event ModulEvent Update;
public event OvertakeValue Update;
public Overtaker(ZwayController zw) { public Overtaker(ZwayController zway, InIReader settings) : base(zway, settings) {
this.zw = zw;
this.ini = InIReader.GetInstance("overtaker.ini");
this.ParseIni(); this.ParseIni();
} }
@ -102,5 +97,26 @@ namespace ZwayBot {
} }
} }
} }
#region IDisposable Support
private Boolean disposedValue = false;
protected virtual void Dispose(Boolean disposing) {
if (!this.disposedValue) {
if (disposing) {
}
this.disposedValue = true;
}
}
~Overtaker() {
Dispose(false);
}
public override void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
} }
} }

View File

@ -1,16 +1,38 @@
using System; using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using BlubbFish.IoT.Zway; using BlubbFish.IoT.Zway;
using BlubbFish.Utils; using BlubbFish.Utils;
using ZwayBot.Moduls;
namespace ZwayBot { namespace ZwayBot {
class Program { class Program {
static void Main(String[] args) { static void Main(String[] args) => new Program(args);
ZwayController zw = new ZwayController(InIReader.GetInstance("settings.ini").GetSection("zway"), InIReader.GetInstance("names.ini").GetSection("names"), false);
zw.Update += ZwayDataUpate; private ZwayController zw;
CronJob cron = new CronJob(zw); private Dictionary<String, AModul> moduls = new Dictionary<String, AModul>();
cron.Update += Cron_Update;
Overtaker over = new Overtaker(zw); public Program(String[] args) {
over.Update += Over_Update; Dictionary<String, String> names;
if(File.Exists("names.ini")) {
names = InIReader.GetInstance("names.ini").GetSection("names");
} else {
names = new Dictionary<String, String>();
}
if(!File.Exists("settings.ini")) {
Helper.WriteError("No settings.ini found. Abord!");
return;
}
this.zw = new ZwayController(InIReader.GetInstance("settings.ini").GetSection("zway"), names, false);
this.zw.Update += this.ZwayDataUpate;
this.ModulLoader();
this.ModulEvents();
this.WaitForShutdown();
this.ModulDispose();
}
private void WaitForShutdown() {
while (true) { while (true) {
System.Threading.Thread.Sleep(100); System.Threading.Thread.Sleep(100);
if (Console.KeyAvailable) { if (Console.KeyAvailable) {
@ -20,19 +42,41 @@ namespace ZwayBot {
} }
} }
} }
cron.Dispose();
zw.Dispose();
} }
private static void Over_Update(Object sender, OvertakerEvent e) { private void ModulDispose() {
Console.WriteLine("OVERTAKE: " + e.Address + " set " + e.Property + " to " + e.Value); foreach (KeyValuePair<String, AModul> item in this.moduls) {
item.Value.Dispose();
Console.WriteLine("Modul entladen: " + item.Key);
}
this.zw.Dispose();
} }
private static void Cron_Update(Object sender, CronEvent e) { private void ModulEvents() {
Console.WriteLine("CRON: " + e.Address + " set " + e.Property + " to " + e.Value); foreach (KeyValuePair<String, AModul> item in this.moduls) {
item.Value.Update += this.ModulUpdate;
}
} }
private static void ZwayDataUpate(Object sender, BlubbFish.IoT.Zway.Events.DeviceUpdateEvent e) { private void ModulUpdate(Object sender, ModulEventArgs e) {
Console.WriteLine(e.Source+": " + e.Address + " set " + e.Property + " to " + e.Value);
}
private void ModulLoader() {
Assembly asm = Assembly.GetExecutingAssembly();
foreach (Type item in asm.GetTypes()) {
if(item.Namespace == "ZwayBot.Moduls") {
Type t = item;
String name = t.Name;
if (File.Exists(name.ToLower() + ".ini")) {
this.moduls.Add(name, (AModul)t.GetConstructor(new Type[] { typeof(ZwayController), typeof(InIReader) }).Invoke(new Object[] { this.zw, InIReader.GetInstance(name.ToLower() + ".ini") }));
Console.WriteLine("Load Modul " + name);
}
}
}
}
private void ZwayDataUpate(Object sender, BlubbFish.IoT.Zway.Events.DeviceUpdateEvent e) {
Console.WriteLine("-> ZW [" + e.UpdateTime + "]: " + sender.ToString()); Console.WriteLine("-> ZW [" + e.UpdateTime + "]: " + sender.ToString());
} }
} }

View File

@ -31,6 +31,9 @@
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<PropertyGroup>
<StartupObject>ZwayBot.Program</StartupObject>
</PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
@ -43,9 +46,10 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Events.cs" /> <Compile Include="Events.cs" />
<Compile Include="CronJob.cs" /> <Compile Include="Moduls\AModul.cs" />
<Compile Include="Moduls\CronJob.cs" />
<Compile Include="Helper.cs" /> <Compile Include="Helper.cs" />
<Compile Include="Overtaker.cs" /> <Compile Include="Moduls\Overtaker.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>

Binary file not shown.

Binary file not shown.

Binary file not shown.