MainWindow.xaml.cs 10.1 KB
Newer Older
1 2 3 4
using Cdy.Tag;
using System;
using System.Collections.Generic;
using System.ComponentModel;
cdy816's avatar
cdy816 已提交
5
using System.Diagnostics;
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SpiderDriverDemo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window,INotifyPropertyChanged
    {
        private SpiderDriver.ClientApi.DriverProxy driverProxy = new SpiderDriver.ClientApi.DriverProxy();
        private Dictionary<int,Tuple<string,byte>> mAllId = new Dictionary<int, Tuple<string, byte>>();
        private Thread mScanThread;
        private int mCount = 0;

        public event PropertyChangedEventHandler PropertyChanged;

        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        private void Start_Click(object sender, RoutedEventArgs e)
        {
            driverProxy = new SpiderDriver.ClientApi.DriverProxy();
            driverProxy.Connect(this.ipt.Text, int.Parse(portt.Text));
cdy816's avatar
cdy816 已提交
44 45 46 47 48 49
            driverProxy.ValueChanged = new SpiderDriver.ClientApi.DriverProxy.ProcessDataPushDelegate((values) => { 
                foreach(var vv in values)
                {
                    Debug.Print(vv.Key + "," + vv.Value.ToString());
                }
            });
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
            mScanThread = new Thread(ThreadPro);
            mScanThread.IsBackground = true;
            mScanThread.Start();
        }

        /// <summary>
        /// 
        /// </summary>
        private void ThreadPro()
        {
            while(true)
            {
                if (!driverProxy.IsLogin)
                {
                    if(driverProxy.IsConnected)
                    driverProxy.Login("Admin", "Admin");
                    if (driverProxy.IsLogin)
                    {
                        ReadAllIds();
                        driverProxy.AppendRegistorDataChangedCallBack(mAllId.Keys.ToList());
                    }
                }
                else
                {
                    ProcessSetTagValue();
                }
                Thread.Sleep(1000);
            }
        }

        private string mCountValue;

        /// <summary>
        /// 
        /// </summary>
        public string CountValue
        {
            get
            {
                return mCountValue;
            }
            set
            {
                if (mCountValue != value)
                {
                    mCountValue = value;
                    OnPropertyChanged("CountValue");
                }
            }
        }

        private string mSimValue;

        /// <summary>
        /// 
        /// </summary>
        public string SimValue
        {
            get
            {
                return mSimValue;
            }
            set
            {
                if (mSimValue != value)
                {
                    mSimValue = value;
                    OnPropertyChanged("SimValue");
                }
            }
        }

        private string mCosValue;


        /// <summary>
        /// 
        /// </summary>
        public string CosValue
        {
            get
            {
                return mCosValue;
            }
            set
            {
                if (mCosValue != value)
                {
                    mCosValue = value;
                    OnPropertyChanged("CosValue");
                }
            }
        }

        private string mBoolValue;

        /// <summary>
        /// 
        /// </summary>
        public string BoolValue
        {
            get
            {
                return mBoolValue;
            }
            set
            {
                if (mBoolValue != value)
                {
                    mBoolValue = value;
                    OnPropertyChanged("BoolValue");
                }
            }
        }

        private string mDateTimeValue;

        /// <summary>
        /// 
        /// </summary>
        public string DateTimeValue
        {
            get
            {
                return mDateTimeValue;
            }
            set
            {
                if (mDateTimeValue != value)
                {
                    mDateTimeValue = value;
                    OnPropertyChanged("DateTimeValue");
                }
            }
        }



        protected void OnPropertyChanged(string name)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }

        /// <summary>
        /// 
        /// </summary>
        private void ReadAllIds()
        {
            mAllId = driverProxy.QueryAllTagIdAndNames();
cdy816's avatar
cdy816 已提交
199 200 201 202 203 204 205

            var tagName = mAllId.First().Value.Item1;
            var ids = driverProxy.QueryTagId(new List<string>() { tagName });
            if(ids.Count>0)
            {
                //test
            }
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
        }

        private void ProcessSetTagValue()
        {
            mCount++;
            if (mCount > 3600) mCount = 0;
            double sin = Math.Sin(mCount / 180.0 * Math.PI);
            double cos = Math.Cos(mCount / 180.0 * Math.PI);
            bool bval = mCount % 300 == 0;
            byte btmp = (byte)(mCount % 256);
            DateTime dnow = DateTime.Now;

            CountValue = mCount.ToString();
            SimValue = sin.ToString("f4");
            CosValue = cos.ToString("f4");
            BoolValue = bval.ToString();
            DateTimeValue = dnow.ToString();

            var values = new Dictionary<int, Tuple<Cdy.Tag.TagType, object>>();
            foreach(var vv in mAllId)
            {
                switch ((TagType)vv.Value.Item2)
                {
                    case TagType.Double:
                        
                        values.Add(vv.Key, new Tuple<TagType, object>((TagType)vv.Value.Item2, sin));
                        break;
                    case TagType.Bool:
                        values.Add(vv.Key, new Tuple<TagType, object>((TagType)vv.Value.Item2, bval));
                        break;
                    case TagType.Byte:
                        values.Add(vv.Key, new Tuple<TagType, object>((TagType)vv.Value.Item2, btmp));
                        break;
                    case TagType.DateTime:
                        values.Add(vv.Key, new Tuple<TagType, object>((TagType)vv.Value.Item2, dnow));
                        break;
                    case TagType.Float:
                        values.Add(vv.Key, new Tuple<TagType, object>((TagType)vv.Value.Item2, cos));
                        break;
                    case TagType.Int:
                        values.Add(vv.Key, new Tuple<TagType, object>((TagType)vv.Value.Item2, mCount));
                        break;
                    case TagType.Long:
                        values.Add(vv.Key, new Tuple<TagType, object>((TagType)vv.Value.Item2, mCount));
                        break;
                    case TagType.UInt:
                        values.Add(vv.Key, new Tuple<TagType, object>((TagType)vv.Value.Item2, mCount));
                        break;
                    case TagType.ULong:
                        values.Add(vv.Key, new Tuple<TagType, object>((TagType)vv.Value.Item2, mCount));
                        break;
                    case TagType.UShort:
                        values.Add(vv.Key, new Tuple<TagType, object>((TagType)vv.Value.Item2, mCount));
                        break;
                    case TagType.Short:
                        values.Add(vv.Key, new Tuple<TagType, object>((TagType)vv.Value.Item2, mCount));
                        break;
                    case TagType.IntPoint:
                        values.Add(vv.Key, new Tuple<TagType, object>((TagType)vv.Value.Item2, new IntPointData( mCount,mCount)));
                        break;
                    case TagType.UIntPoint:
                        values.Add(vv.Key, new Tuple<TagType, object>((TagType)vv.Value.Item2, new UIntPointData(mCount, mCount)));
                        break;
                    case TagType.IntPoint3:
                        values.Add(vv.Key, new Tuple<TagType, object>((TagType)vv.Value.Item2, new IntPoint3Data(mCount, mCount, mCount)));
                        break;
                    case TagType.UIntPoint3:
                        values.Add(vv.Key, new Tuple<TagType, object>((TagType)vv.Value.Item2, new UIntPoint3Data(mCount, mCount, mCount)));
                        break;
                    case TagType.LongPoint:
                        values.Add(vv.Key, new Tuple<TagType, object>((TagType)vv.Value.Item2, new LongPointData(mCount, mCount)));
                        break;
                    case TagType.ULongPoint:
                        values.Add(vv.Key, new Tuple<TagType, object>((TagType)vv.Value.Item2, new ULongPointData(mCount, mCount)));
                        break;
                    case TagType.LongPoint3:
                        values.Add(vv.Key, new Tuple<TagType, object>((TagType)vv.Value.Item2, new LongPoint3Data(mCount, mCount, mCount)));
                        break;
                    case TagType.ULongPoint3:
                        values.Add(vv.Key, new Tuple<TagType, object>((TagType)vv.Value.Item2, new ULongPoint3Data(mCount, mCount, mCount)));
                        break;
                }

            }
            driverProxy.SetTagValue(values);
        }
cdy816's avatar
cdy816 已提交
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void hisDataWrite_Click(object sender, RoutedEventArgs e)
        {
            List<TagValue> vals = new List<TagValue>();
            DateTime dt = DateTime.Now.AddSeconds(-100);
            Random rd = new Random((int)dt.Ticks);
            for(int i=0;i<100;i++)
            {
                vals.Add(new TagValue() { Quality = 0, Time = dt, Value = rd.NextDouble() });
            }

            driverProxy.SetTagHisValue(0, TagType.Double, vals);
        }
310 311
    }
}