using System;
using WebSocketSharp;
using WebSocketSharp.Server;
using Newtonsoft.Json;
using System.Timers;

using System.Collections.Generic;
using System.Runtime.Remoting.Messaging;


class Program
{
    static WebSocketServer server;
    static Timer timer;
    static int say = 0;
    static int tetik = 1;
    static void Main()
    {
        StartWebSocketServer();

        using (var ws = new WebSocket("ws://0.0.0.0:5001"))
        {
            ws.OnMessage += (sender, e) =>
            {
                try
                {
                    dynamic jsonData = JsonConvert.DeserializeObject(e.Data);
                    Console.WriteLine(jsonData);
                    string nereye = jsonData.nereye;
            

                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error while parsing JSON: " + ex.Message);
                }
            };

            ws.Connect();

           // var message = "{\"nereye\":\"outespc\",\"cihaz_id\":\"1001\",\"out\": \"out1\",\"out_value\":1,\"gecikme\":0}";
            //ws.Send(message);
            timer = new Timer();
            timer.Interval = 2000; // 2 saniyede bir çalışacak
            timer.Elapsed += TimerElapsed;
            timer.Start();

            Console.ReadKey(true);
        }
    }

    static void StartWebSocketServer()
    {
         server = new WebSocketServer("ws://0.0.0.0:5001");
        server.AddWebSocketService<MyWebSocket>("/");
        server.Start();
        Console.WriteLine("WebSocket server started on ws://0.0.0.0:5001");
    }
    static void TimerElapsed(object sender, ElapsedEventArgs e)
    {
        // Tüm bağlı istemcilere mesaj gönder

        foreach (var session in server.WebSocketServices["/"].Sessions.Sessions)
        {
            if (session.State == WebSocketState.Open)
            {
                Console.WriteLine("Gonder:");
                try
                {
                    Console.WriteLine(session.ID);
                    // Mesajı istemciye gönder
                     var message = $"{{\"nereye\":\"outespc\",\"cihaz_id\":\"1001\",\"out\": \"out{say}\",\"out_value\":{tetik},\"gecikme\":0}}";
                    session.Context.WebSocket.Send(message);

                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error while sending message to client: " + ex.Message);
                }
            }
        }
        say += 1;
        if (say > 4) {
        say= 0;
            if (tetik ==0) {
                tetik = 1;
            }
            else
            {
                tetik = 0;
            }
        }
    }

}

class MyWebSocket : WebSocketBehavior
{
    protected override void OnMessage(MessageEventArgs e)
    {
        try
        {
            dynamic jsonData = JsonConvert.DeserializeObject(e.Data);
            string nereye = jsonData.nereye;
            string sensorData = jsonData.sensorData;
            string ipAdresi = jsonData.ipAdresi;
            string cihazID = jsonData.cihazID;

            Console.WriteLine("nereye: " + nereye);
            Console.WriteLine("sensorData: " + sensorData);
            Console.WriteLine("ipAdresi: " + ipAdresi);
            Console.WriteLine("cihazID: " + cihazID);
            Console.WriteLine("jsonData: " + jsonData);
           // var message = "{\"nereye\":\"outespc\",\"cihaz_id\":\"1001\",\"out\": \"out1\",\"out_value\":1,\"gecikme\":0}";
            //Send(message);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error while parsing JSON: " + ex.Message);
        }
    }
}
