Esempio n. 1
0
        public object[] ReadValue(string deviceAddress, string[] points)
        {
            var client = new Way.Lib.NetStream(this.Address, this.Port);
            var json   = Newtonsoft.Json.JsonConvert.SerializeObject(new Command()
            {
                Type          = "ReadValue",
                DeviceAddress = deviceAddress,
                Points        = points
            });

            byte[] bs = System.Text.Encoding.UTF8.GetBytes(json);
            client.Write(bs.Length);
            client.Write(bs);

            object[] values = new object[points.Length];
            for (int i = 0; i < points.Length; i++)
            {
                int            index     = client.ReadInt();
                PointValueType valueType = (PointValueType)client.ReadInt();
                if (valueType == PointValueType.Short)
                {
                    values[index] = client.ReadShort();
                }
                else if (valueType == PointValueType.Int)
                {
                    values[index] = client.ReadInt();
                }
                else if (valueType == PointValueType.Float)
                {
                    values[index] = client.ReadFloat();
                }
                else if (valueType == PointValueType.String)
                {
                    values[index] = System.Text.Encoding.UTF8.GetString(client.ReceiveDatas(client.ReadInt()));
                }
                else
                {
                    throw new Exception($"not support value type {valueType}");
                }
            }
            client.Dispose();
            return(values);
        }
Esempio n. 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="deviceAddress">设备地址</param>
        /// <param name="points">点路径</param>
        /// <param name="interval">更新时间间隔,单位:毫秒</param>
        /// <param name="onReceiveValue"></param>
        /// <param name="onError"></param>
        public Way.Lib.NetStream AddPointToWatch(string deviceAddress, string[] points, int interval, Action <string, object> onReceiveValue, Action <string> onError)
        {
            if (interval < 1000)
            {
                interval = 1000;
            }
            try
            {
                var client = new Way.Lib.NetStream(this.Address, this.Port);
                var json   = Newtonsoft.Json.JsonConvert.SerializeObject(new Command()
                {
                    Type          = "AddPointToWatch",
                    DeviceAddress = deviceAddress,
                    Points        = points,
                    Interval      = interval
                });
                byte[] bs = System.Text.Encoding.UTF8.GetBytes(json);
                client.Write(bs.Length);
                client.Write(bs);

                Task.Run(() =>
                {
                    while (true)
                    {
                        try
                        {
                            int index = client.ReadInt();
                            PointValueType valueType = (PointValueType)client.ReadInt();
                            if (valueType == PointValueType.Short)
                            {
                                onReceiveValue(points[index], client.ReadShort());
                            }
                            else if (valueType == PointValueType.Int)
                            {
                                onReceiveValue(points[index], client.ReadInt());
                            }
                            else if (valueType == PointValueType.Float)
                            {
                                onReceiveValue(points[index], client.ReadFloat());
                            }
                            else if (valueType == PointValueType.String)
                            {
                                var str = System.Text.Encoding.UTF8.GetString(client.ReceiveDatas(client.ReadInt()));
                                onReceiveValue(points[index], str);
                            }
                            else
                            {
                                throw new Exception($"not support value type {valueType}");
                            }
                        }
                        catch (Exception ex)
                        {
                            client.Close();
                            onError(ex.Message);
                            return;
                        }
                    }
                });
                return(client);
            }
            catch (Exception ex)
            {
                onError(ex.Message);
            }
            return(null);
        }