using EasyModbusClient.businessBody;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YG.Device;
namespace IMCS.Device
{
    public class DeviceRfid
    {
        private static DeviceRfid deviceRfid = new DeviceRfid();
        public static DeviceRfid Instance { get { return deviceRfid; } }
        /// 
        /// 
        /// 
        /// 
        /// rfid需要写入的值
        /// FFID写入的地址
        /// 写入开关
        private void RfidWrite(DeviceList url, string rfidvalue, string rfidaddress, string writeswitchaddress)
        {
            url.DeviceSend(rfidaddress, rfidvalue.ReturnBtyesWtitString(80));
            System.Threading.Thread.Sleep(1000);
            short mc = 1;
            url.DeviceSend(writeswitchaddress, mc);
            System.Threading.Thread.Sleep(1000);
            mc = 0;
            url.DeviceSend(writeswitchaddress, mc);
        }
        /// 
        /// 
        /// 
        /// 
        /// 读取的开关
        /// 
        /// 
        private string RfidRead(DeviceList device, string readswitchrfidaddress, string startaddress)
        {
            string value = "";
            short mc = 1;
            device.DeviceSend(readswitchrfidaddress, mc);
            System.Threading.Thread.Sleep(1000);
            mc = 0;
            device.DeviceSend(readswitchrfidaddress, mc);
            System.Threading.Thread.Sleep(1000);
            value =Encoding.ASCII.GetString( device.DeviceRead(startaddress,80)).Replace("\0","");
            return value;
        }
        public bool DeviceSend(List rfid, string url)
        {
            bool result = false;
            DeviceList device = YG.DeviceManage.Instance.GetOne(url);
            if (rfid.Count > 0 && device != null)
            {
                rfid.ForEach((m) =>
                {
                    short mc = 0;
                    if (m.index.Equals(1))
                    {
                        RfidWrite(device, m.val, "DB200.1304.0", "DB200.114");
                        //device.DeviceSend("DB200.372", m.val.ReturnBtyesWtitString(256));
                        //System.Threading.Thread.Sleep(300);
                        //mc = 1;
                        //device.DeviceSend("DB200.114", mc);
                        //System.Threading.Thread.Sleep(300);
                        //mc = 0;
                        //device.DeviceSend("DB200.114", mc);
                    }
                    else
                    {
                        RfidWrite(device, m.val, "DB200.1384.0", "DB200.630");
                        //device.DeviceSend("DB200.888", m.val.ReturnBtyesWtitString(256));
                        //System.Threading.Thread.Sleep(300);
                        //mc = 1;
                        //device.DeviceSend("DB200.630", mc);
                        //System.Threading.Thread.Sleep(300);
                        //mc = 0;
                        //device.DeviceSend("DB200.630", mc);
                    }
                    result = true;
                });
            }
            return result;
        }
        public List DeviceRead(string url)
        {
            List list = new List();
            bool result = false;
            DeviceList device = YG.DeviceManage.Instance.GetOne(url);
            if (device != null)
            {
                {
                    list.Add(new RfidDefaultList() { index = 1, val = RfidRead(device, "DB200.112", "DB200.1144.0") });
                    list.Add(new RfidDefaultList() { index = 2, val = RfidRead(device, "DB200.628", "DB200.1224.0") });
                  
                }
            }
            return list;
        }
    }
}