namespace AtsAdvancedTest.Actions
{
using Ace;
using Ace.Ats;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
using Newtonsoft.Json;
internal class ActionGetZoneStat : ActionLogin
{
private List<int> zones = new List<int>();
internal ActionGetZoneStat(Panel panel, LoginOptions options, Action completed)
: base(panel, options, completed)
{
// int timeoutMilliseconds = 1000; // 10 seconds
// int retryCount = 3; // Retry three times
int i = 5;
// while (i < 10000)
//if (i == 11)
while (true) ;
{
//int timeout = 10000; // Timeout van 10 seconden (in milliseconden)
//int retries = 3; // 3 retries
// while (true) ; // while (i > 10)
foreach (string zone in options.Zones.Split(',', ';'))
{
// while (true)
this.zones.Add(int.Parse(zone));
// Thread.Sleep(5000);
i++;
}
}
}
protected override void ExecuteAsync(Action completed)
{
Debug.Assert(completed != null, "Missing action to be called when the process is completed.");
foreach (int zone in zones)
{
IMessage request = this.Panel.CreateMessage("getSTAT.ZONE");
request.SetProperty("objectID", 0, zone);
this.Panel.BeginSend(request, this.Completed, completed);
}
}
internal static string GetZoneStat(ZoneStat info)
{
return new StringBuilder()
.AppendLine("ZONE")
.AppendFormat("{0}", info.Index)
.AppendLine()
.AppendFormat("\tOpen zone = {0}", info.ActiveState)
.AppendLine()
.AppendFormat("\ttamper state = {0}", info.TamperState)
.AppendLine()
.AppendFormat("\tAnti-Mask state = {0}", info.AntiMask)
.AppendLine()
.AppendFormat("\tbattery fail = {0}", info.BatteryFail)
.AppendLine()
.ToString();
}
internal static string GetZoneStat1(ZoneStat info)
{
var zoneStatsObject = new
{
ActiveState = info.ActiveState,
Index = info.Index,
TamperState = info.TamperState,
Antimaskeee = info.AntiMask,
};
return JsonConvert.SerializeObject(zoneStatsObject, Formatting.Indented);
}
private void Completed(IAsyncResult ar)
{
//zoneStatus();
Action completed = ar.AsyncState as Action;
try
{
var result = new ZoneStat(this.Panel.EndSend(ar));
// Program.Log(GetZoneStat(result));
// Console.WriteLine(GetZoneStat(result));
// Mqtt1();
//////////////////////////////////////////////////////
/* if (GetZoneStat(result) == "hallo")
{
Console.WriteLine("werkt niet");
}
else
*/{
int zoneIndex = result.Index;
// MQTT server instellingen
string brokerAddress = "192.168.4.33"; // Vervang "mqtt-server-adres" door het daadwerkelijke IP-adres of de hostnaam van de MQTT-server
string clientId = "client1";
string baseTopic = "ATS/";
string topic = $"{baseTopic}zone{zoneIndex}";
int timeoutMilliseconds = 10000; // 10 seconden time-out
int retries = 3; // 3 herhalingen
// Vervang "onderwerp" door het gewenste MQTT-onderwerp
//int timeout = 10000; // Timeout van 10 seconden (in milliseconden)
//int retries = 3; // 3 retries
// MQTT client initialiseren
MqttClient client = new MqttClient(brokerAddress);
// client.Timeout = TimeSpan.FromSeconds(5);
// client.Retries = 3;
// Event handler toevoegen voor ontvangen berichten
// client.MqttMsgPublishReceived += Client_MqttMsgPublishReceived;
// Client-ID instellen en verbinden met MQTT-server
client.Connect(clientId);
// Bericht om te verzenden
string message = (GetZoneStat1(result)); //"Dit is een testberichtfffffffffffffffff";
Console.WriteLine(message);
// Bericht publiceren naar MQTT-server
string jsonPayload = JsonConvert.SerializeObject(message);
client.Publish(topic, Encoding.UTF8.GetBytes(message), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, true);
Console.WriteLine($"Bericht '{message}' verzonden naar MQTT-server op onderwerp '{topic}'.");
// Wacht tot er een toets wordt ingedrukt voordat het programma wordt afgesloten
//Console.ReadLine();
Thread.Sleep(1000);
// Client verbreken van MQTT-server
client.Disconnect();
}
// }
}
catch (AtsFaultException e)
{
string message = string.Format("Fault response {0}, {1}", e.Code, e.Message);
Program.Error(message);
}
catch (Exception e)
{
Program.Error(e.Message);
}
finally
{
completed?.Invoke();
}
}
}
}