using bizFacade;
using DoWhatPlc;
using MesServerWork;
using MesWorkMqtt;
using MQTTnet;
using MQTTnet.Client.Connecting;
using MQTTnet.Client.Disconnecting;
using MQTTnet.Client.Receiving;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace MisDataSaveDate
{
    public partial class Funtion
    {

        public Mqtt2 mqtt2;
        public void InitMqtt2(string url, string topic)
        {
            mqtt2 = new Mqtt2();
            mqtt2.MqttUrl = url;
            mqtt2.topic = topic;
            mqtt2.StartAsync();
            mqtt2.mqttClient.ConnectedHandler = new MqttClientConnectedHandlerDelegate(OnSubscriberConnected);
            mqtt2.mqttClient.DisconnectedHandler = new MqttClientDisconnectedHandlerDelegate(OnSubscriberDisconnected);
            mqtt2.mqttClient.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(OnSubscriberMessageReceived);

            MesServerWork.MIS_BASE.InitHashtable_MesServerCode();
        }
        private void OnSubscriberConnected(MqttClientConnectedEventArgs x)
        {
            var rec = x.AuthenticateResult.ResultCode.ToString();
            //label_State.Text = rec;
        }

        private void OnSubscriberDisconnected(MqttClientDisconnectedEventArgs x)
        {
            //var rec = x.AuthenticateResult.ResultCode.ToString();
            //label_State.Text = rec;
        }

        private void OnSubscriberMessageReceived(MqttApplicationMessageReceivedEventArgs x)
        {
            var rec = x.ApplicationMessage.ConvertPayloadToString();
            ThreadPool.QueueUserWorkItem(MIS_Funtion, rec);
        }
    }
}
