public void EnumDevicePoint(string opcServerAddress, List <string> parentPath, Action <PointInfomation> onFindPoint) { var client = new Way.Lib.NetStream(this.Address, this.Port); var json = Newtonsoft.Json.JsonConvert.SerializeObject(new Command() { Type = "EnumDevicePoint", DeviceAddress = opcServerAddress, ParentPath = parentPath, }); byte[] bs = System.Text.Encoding.UTF8.GetBytes(json); client.Write(bs.Length); client.Write(bs); while (true) { var len = client.ReadInt(); if (len < 0) { break; } bs = client.ReceiveDatas(len); string point = System.Text.Encoding.UTF8.GetString(bs); onFindPoint(Newtonsoft.Json.JsonConvert.DeserializeObject <PointInfomation>(point)); } client.Dispose(); }
public void ReadPackageTest() { Way.Lib.NetStream client = new Way.Lib.NetStream("127.0.0.1", 502); WriteValuePackage package = new WriteValuePackage(FunctionCode.WriteHoldingRegister); package.Address = 1; package.Value = -388; var cmd = package.BuildCommand(); client.Write(cmd); var result = package.ParseAnswer((len) => { return(client.ReceiveDatas(len)); }); }
public ServerInfo GetServerInfo() { var client = new Way.Lib.NetStream(this.Address, this.Port); var json = Newtonsoft.Json.JsonConvert.SerializeObject(new Command() { Type = "GetServerInfo" }); byte[] bs = System.Text.Encoding.UTF8.GetBytes(json); client.Write(bs.Length); client.Write(bs); int len = client.ReadInt(); bs = client.ReceiveDatas(len); client.Dispose(); return(Newtonsoft.Json.JsonConvert.DeserializeObject <ServerInfo>(System.Text.Encoding.UTF8.GetString(bs))); }
public bool CheckDeviceExist(string deviceAddress) { var client = new Way.Lib.NetStream(this.Address, this.Port); var json = Newtonsoft.Json.JsonConvert.SerializeObject(new Command() { Type = "CheckDeviceExist", DeviceAddress = deviceAddress }); byte[] bs = System.Text.Encoding.UTF8.GetBytes(json); client.Write(bs.Length); client.Write(bs); bool result = client.ReadBoolean(); client.Dispose(); return(result); }
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); }
/// <summary> /// 获取设备路径 /// </summary> /// <param name="proObj">属性值</param> /// <returns></returns> public string GetDeviceAddress(Dictionary <string, string> proValues) { var client = new Way.Lib.NetStream(this.Address, this.Port); var json = Newtonsoft.Json.JsonConvert.SerializeObject(new Command() { Type = "GetDeviceAddress", Values = new object[] { proValues } }); byte[] bs = System.Text.Encoding.UTF8.GetBytes(json); client.Write(bs.Length); client.Write(bs); int len = client.ReadInt(); bs = client.ReceiveDatas(len); client.Dispose(); return(System.Text.Encoding.UTF8.GetString(bs)); }
void handleSocket(Socket socket) { var client = new Way.Lib.NetStream(socket); new Thread(() => { try { var len = client.ReadInt(); byte[] bs = client.ReceiveDatas(len); Command cmd = Newtonsoft.Json.JsonConvert.DeserializeObject <Command>(System.Text.Encoding.UTF8.GetString(bs)); var handler = GetHandler(cmd.Type); handler.Handle(client, cmd); client.ReceiveDatas(1); } catch (Exception ex) { } finally { client.Dispose(); } }).Start(); }
public bool[] WriteValue(string deviceAddress, string[] points, object[] values) { var result = new bool[points.Length]; var client = new Way.Lib.NetStream(this.Address, this.Port); var json = Newtonsoft.Json.JsonConvert.SerializeObject(new Command() { Type = "WriteValue", DeviceAddress = deviceAddress, Points = points, Values = values }); byte[] bs = System.Text.Encoding.UTF8.GetBytes(json); client.Write(bs.Length); client.Write(bs); for (int i = 0; i < result.Length; i++) { result[i] = client.ReadBoolean(); } client.Dispose(); return(result); }
public abstract void Handle(Way.Lib.NetStream stream, Command command);
private async void MenuItem_cs文件还原设计模型_Click_1(object sender, RoutedEventArgs e) { MenuItem item = (MenuItem)sender; ContextMenu menu = (ContextMenu)item.Parent; var obj = (StackPanel)menu.PlacementTarget; ProjectNode projectNode = (ProjectNode)obj.Tag; using (System.Windows.Forms.OpenFileDialog f = new System.Windows.Forms.OpenFileDialog()) { f.Filter = "*.cs|*.cs"; if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK) { this.Cursor = Cursors.Wait; try { var bs = System.IO.File.ReadAllBytes(f.FileName); string url = $"POST /ImportCSFileHandler.aspx?projectid={projectNode.Project.id}"; var host = $"{Helper.WebSite}/"; host = host.Substring(host.IndexOf("://") + 3); host = host.Substring(0, host.IndexOf("/")); int port = 80; if (host.Contains(":")) { port = Convert.ToInt32(host.Split(':')[1]); host = host.Split(':')[0]; } string result = null; await Task.Run(() => { Way.Lib.NetStream client = new Way.Lib.NetStream(host, port); client.AsSSLClient(System.Security.Authentication.SslProtocols.None); System.IO.StreamWriter stream = new System.IO.StreamWriter(client); stream.WriteLine(url); stream.WriteLine($"Cookie: WayScriptRemoting={Net.RemotingClient.SessionID}"); stream.WriteLine($"Content-Type: import"); stream.WriteLine($"Content-Length: {bs.Length}"); stream.WriteLine(""); stream.Flush(); client.Write(bs, 0, bs.Length); while (true) { if (client.ReadLine().Length == 0) { break; } } result = client.ReadLine(); client.Close(); }); if (result != "ok") { Helper.ShowError(this, result); return; } DatabaseNode dbnode = (DatabaseNode)projectNode.Children.Where(m => m is TreeNode.DatabaseNode).FirstOrDefault(); dbnode.ReBindItems(); MessageBox.Show(this, "成功导入!"); } catch (Exception ex) { MessageBox.Show(this, ex.GetBaseException().Message); } finally { this.Cursor = null; } } } }
async void import() { using (System.IO.BinaryReader br = new System.IO.BinaryReader(System.IO.File.OpenRead(_filepath))) { string result = null; var tablenames = br.ReadString().ToJsonObject <List <string> >(); int[] rowCounts = br.ReadString().ToJsonObject <int[]>(); var host = $"{Helper.WebSite}/"; host = host.Substring(host.IndexOf("://") + 3); host = host.Substring(0, host.IndexOf("/")); int port = 80; if (host.Contains(":")) { port = Convert.ToInt32(host.Split(':')[1]); host = host.Split(':')[0]; } string url = $"POST /ImportTableData.aspx?dbid={m_databaseItemNode.Database.id}&clearDataFirst={(chkClearDataFirst.IsChecked == true ? 1 : 0)}"; string[] importTables = new string[list.SelectedItems.Count]; for (int i = 0; i < list.SelectedItems.Count; i++) { importTables[i] = list.SelectedItems[i].ToString(); } int totalCount = 0; for (int i = 0; i < importTables.Length; i++) { totalCount += rowCounts[tablenames.IndexOf(importTables[i])]; } await Task.Run(() => { try { Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp); socket.Connect(new System.Net.DnsEndPoint(host, port)); Way.Lib.NetStream client = new Way.Lib.NetStream(socket); System.IO.StreamWriter stream = new System.IO.StreamWriter(client); stream.WriteLine(url); stream.WriteLine($"Cookie: WayScriptRemoting={Net.RemotingClient.SessionID}"); stream.WriteLine($"Content-Type: import"); stream.WriteLine(""); stream.Flush(); System.IO.BinaryWriter bw = new System.IO.BinaryWriter(client); bw.Write(importTables.ToJsonString()); int uploaded = 0; while (true) { string tablename = br.ReadString(); if (tablename == ":end") { break; } string content = br.ReadString(); if (importTables.Contains(tablename) == false) { continue; } bw.Write(tablename); bw.Write(content); bw.Flush(); uploaded++; if (totalCount > 0) { this.Dispatcher.Invoke(() => { this.Title = $"正在导入...{(uploaded * 100) / totalCount}%"; }); } } bw.Write(":end"); bw.Flush(); var reader = new System.IO.StreamReader(client); while (true) { if (reader.ReadLine().Length == 0) { break; } } result = reader.ReadLine(); client.Close(); } catch (Exception ex) { result = ex.Message; } }); if (result != "ok") { this.IsEnabled = true; Helper.ShowError(this, result); } else { Helper.ShowMessage(this, "导入完毕!"); this.Close(); } } }
/// <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); }