using Sharp7;
using Sharp7.Rx;
using Sharp7.Rx.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Reactive.Threading.Tasks;
using System.Threading.Tasks;
namespace LangGuan.Command.Extension
{
    public class PlcHelper2
    {
        public PlcHelper2() { }
        /// 
        /// 设定设备值
        /// 
        /// 
        /// 
        /// 
        /// 
        public async Task startDevice(string ip, string key, Int16 val)
        {
            try
            {
                using (var disposables = new CompositeDisposable())
                {
                    Console.WriteLine($"开始连接设备,ip:{ip},");
                    // create a new PLC
                    var plc = new Sharp7Plc(ip, 0, 1);
                    disposables.Add(plc);
                    // initialize the plc
                    await plc.InitializeAsync();
                    var t = await plc.ConnectionState.FirstAsync(c => c == ConnectionState.Connected).ToTask();
                    await plc.SetValue(key, val); // set a bit
                    var result = await plc.GetValue(key);
                    Console.WriteLine($"监控写入结果:----- {result}");
                    Console.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm ss")}----plc连接状态:{t}");
                    // Console.WriteLine($"plc连接状态:{plc.ConnectionState.FirstAsync(c => c == ConnectionState.Connected).Any()}");
                    //  await plc.SetValue("DB2.DBX0.4", true); // set a bit
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        public async Task startDevice502(string ip, string key, Int16 val)
        {
            try
            {
                using (var disposables = new CompositeDisposable())
                {
                    Console.WriteLine($"502开始连接设备,ip:{ip},");
                    // create a new PLC
                    var plc = new Sharp7Plc(ip, 0, 1,502);
                    disposables.Add(plc);
                    // initialize the plc
                    await plc.InitializeAsync();
                    var t = await plc.ConnectionState.FirstAsync(c => c == ConnectionState.Connected).ToTask();
                    await plc.SetValue(key, val); // set a bit
                    var result = await plc.GetValue(key);
                    Console.WriteLine($"502监控写入结果:----- {result}");
                    Console.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm ss")}--502--plc连接状态:{t}");
                    // Console.WriteLine($"plc连接状态:{plc.ConnectionState.FirstAsync(c => c == ConnectionState.Connected).Any()}");
                    //  await plc.SetValue("DB2.DBX0.4", true); // set a bit
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        public async Task SetVal(string ip, string key, int val)
        {
            using (var disposables = new CompositeDisposable())
            {
                // create a new PLC
                var plc = new Sharp7Plc(ip, 0, 2);
                disposables.Add(plc);
                // initialize the plc
                await plc.InitializeAsync();
                //wait for the plc to get connected
                await plc.ConnectionState
                         .FirstAsync(c => c == ConnectionState.Connected)
                         .ToTask();
                //  await plc.SetValue("DB2.DBX0.4", true); // set a bit
                await plc.SetValue(key, val); // set a bit
                                                   // var bit = await plc.GetValue("DB2.int4"); // get a bit
                // create a nofication for data change in the plc
                //var notification = plc.CreateNotification("DB1.DBX0.2", TransmissionMode.OnChange, TimeSpan.FromMilliseconds(100))
                //                      .Where(b => b) //select rising edge
                //                      .Do(_ => doStuff())
                //                      .Subscribe();
                //disposables.Add(notification);
                //wait for enter before ending the program
                // Console.ReadLine();
            }
        }
        /// 
        /// 取值
        /// 
        /// 
        /// 
        /// 
        /// 
        public async Task GetVal(string ip, string key)
        {
            using (var disposables = new CompositeDisposable())
            {
                // create a new PLC
                var plc = new Sharp7Plc(ip, 0, 2);
                disposables.Add(plc);
                // initialize the plc
                await plc.InitializeAsync();
                //wait for the plc to get connected
                await plc.ConnectionState
                         .FirstAsync(c => c == ConnectionState.Connected)
                         .ToTask();
                //  await plc.SetValue("DB2.DBX0.4", true); // set a bit
                var bit = await plc.GetValue(key); // get a bit
                return bit;
            }
        }
    }
}