[NF] Zway-Bot Module eingefügt
This commit is contained in:
parent
925131a52b
commit
8771a3ce89
@ -1,7 +1,7 @@
|
||||
using System;
|
||||
|
||||
namespace ZwayBot {
|
||||
public class CronEvent : EventArgs {
|
||||
public class CronEvent : ModulEventArgs {
|
||||
|
||||
public CronEvent() {
|
||||
}
|
||||
@ -10,13 +10,10 @@ namespace ZwayBot {
|
||||
this.Address = addr;
|
||||
this.Property = prop;
|
||||
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() {
|
||||
}
|
||||
@ -25,10 +22,16 @@ namespace ZwayBot {
|
||||
this.Address = addr;
|
||||
this.Property = prop;
|
||||
this.Value = value;
|
||||
this.Source = "Overtaker";
|
||||
}
|
||||
}
|
||||
|
||||
public String Address { get; }
|
||||
public String Property { get; }
|
||||
public String Value { get; }
|
||||
public class ModulEventArgs : EventArgs {
|
||||
public ModulEventArgs() {
|
||||
}
|
||||
public String Address { get; protected set; }
|
||||
public String Property { get; protected set; }
|
||||
public String Value { get; protected set; }
|
||||
public String Source { get; protected set; }
|
||||
}
|
||||
}
|
||||
|
@ -44,5 +44,11 @@ namespace ZwayBot {
|
||||
}
|
||||
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
20
Zway-Bot/Moduls/AModul.cs
Normal 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();
|
||||
}
|
||||
}
|
@ -7,15 +7,12 @@ using BlubbFish.IoT.Zway;
|
||||
using BlubbFish.IoT.Zway.Interfaces;
|
||||
using BlubbFish.Utils;
|
||||
|
||||
namespace ZwayBot {
|
||||
internal class CronJob : IDisposable {
|
||||
private ZwayController zw;
|
||||
private InIReader ini;
|
||||
namespace ZwayBot.Moduls {
|
||||
internal class CronJob : AModul, IDisposable {
|
||||
private DateTime crontime;
|
||||
private Thread thread;
|
||||
|
||||
public delegate void CronRunned(Object sender, CronEvent e);
|
||||
public event CronRunned Update;
|
||||
public override event ModulEvent Update;
|
||||
|
||||
private Dictionary<String, String> cron_named = new Dictionary<String, String> {
|
||||
{ "@yearly", "0 0 1 1 *" },
|
||||
@ -26,9 +23,7 @@ namespace ZwayBot {
|
||||
{ "@hourly", "0 * * * *" }
|
||||
};
|
||||
|
||||
public CronJob(ZwayController zw) {
|
||||
this.zw = zw;
|
||||
this.ini = InIReader.GetInstance("cron.ini");
|
||||
public CronJob(ZwayController zway, InIReader settings) : base(zway, settings) {
|
||||
this.crontime = DateTime.Now;
|
||||
this.thread = new Thread(this.Runner);
|
||||
this.thread.Start();
|
||||
@ -162,7 +157,7 @@ namespace ZwayBot {
|
||||
}
|
||||
|
||||
#region IDisposable Support
|
||||
private Boolean disposedValue = false; // Dient zur Erkennung redundanter Aufrufe.
|
||||
private Boolean disposedValue = false;
|
||||
|
||||
protected virtual void Dispose(Boolean disposing) {
|
||||
if (!this.disposedValue) {
|
||||
@ -181,7 +176,7 @@ namespace ZwayBot {
|
||||
Dispose(false);
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
public override void Dispose() {
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
@ -5,18 +5,13 @@ using BlubbFish.IoT.Zway.Events;
|
||||
using BlubbFish.IoT.Zway.Interfaces;
|
||||
using BlubbFish.Utils;
|
||||
|
||||
namespace ZwayBot {
|
||||
internal class Overtaker {
|
||||
private ZwayController zw;
|
||||
private InIReader ini;
|
||||
namespace ZwayBot.Moduls {
|
||||
internal class Overtaker : AModul, IDisposable {
|
||||
private Dictionary<String, Dictionary<String, String>> events = new Dictionary<String, Dictionary<String, String>>();
|
||||
|
||||
public delegate void OvertakeValue(Object sender, OvertakerEvent e);
|
||||
public event OvertakeValue Update;
|
||||
public override event ModulEvent Update;
|
||||
|
||||
public Overtaker(ZwayController zw) {
|
||||
this.zw = zw;
|
||||
this.ini = InIReader.GetInstance("overtaker.ini");
|
||||
public Overtaker(ZwayController zway, InIReader settings) : base(zway, settings) {
|
||||
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
|
||||
}
|
||||
}
|
@ -1,16 +1,38 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using BlubbFish.IoT.Zway;
|
||||
using BlubbFish.Utils;
|
||||
using ZwayBot.Moduls;
|
||||
|
||||
namespace ZwayBot {
|
||||
class Program {
|
||||
static void Main(String[] args) {
|
||||
ZwayController zw = new ZwayController(InIReader.GetInstance("settings.ini").GetSection("zway"), InIReader.GetInstance("names.ini").GetSection("names"), false);
|
||||
zw.Update += ZwayDataUpate;
|
||||
CronJob cron = new CronJob(zw);
|
||||
cron.Update += Cron_Update;
|
||||
Overtaker over = new Overtaker(zw);
|
||||
over.Update += Over_Update;
|
||||
static void Main(String[] args) => new Program(args);
|
||||
|
||||
private ZwayController zw;
|
||||
private Dictionary<String, AModul> moduls = new Dictionary<String, AModul>();
|
||||
|
||||
public Program(String[] args) {
|
||||
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) {
|
||||
System.Threading.Thread.Sleep(100);
|
||||
if (Console.KeyAvailable) {
|
||||
@ -20,19 +42,41 @@ namespace ZwayBot {
|
||||
}
|
||||
}
|
||||
}
|
||||
cron.Dispose();
|
||||
zw.Dispose();
|
||||
}
|
||||
|
||||
private static void Over_Update(Object sender, OvertakerEvent e) {
|
||||
Console.WriteLine("OVERTAKE: " + e.Address + " set " + e.Property + " to " + e.Value);
|
||||
private void ModulDispose() {
|
||||
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) {
|
||||
Console.WriteLine("CRON: " + e.Address + " set " + e.Property + " to " + e.Value);
|
||||
private void ModulEvents() {
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
@ -31,6 +31,9 @@
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<StartupObject>ZwayBot.Program</StartupObject>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
@ -43,9 +46,10 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Events.cs" />
|
||||
<Compile Include="CronJob.cs" />
|
||||
<Compile Include="Moduls\AModul.cs" />
|
||||
<Compile Include="Moduls\CronJob.cs" />
|
||||
<Compile Include="Helper.cs" />
|
||||
<Compile Include="Overtaker.cs" />
|
||||
<Compile Include="Moduls\Overtaker.cs" />
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue
Block a user