private void button2_Click(object sender, EventArgs e) { string caption = "DemoAdapter of GC Gateway"; AdapterMessage am = new AdapterMessage(4, AdapterStatus.Stopped); am.PostMessage(caption); }
private void AttachAdapter() { DeviceDir dir = Program.DeviceMgt.DeviceDirInfor; if (dir == null || dir.Header == null) { return; } int adapterID = 0; try { adapterID = int.Parse(dir.Header.ID.Trim()); } catch { adapterID = -1; } amRunning = new AdapterMessage(adapterID, AdapterStatus.Running); amStopped = new AdapterMessage(adapterID, AdapterStatus.Stopped); Program.Log.Write("Messager initialized " + " (Enable=" + Enable.ToString() + ", AdapterID=" + adapterID.ToString() + ", WindowCaption=" + windowCaption + ")."); }
public override List <AdapterMessage> ProcessMessage(AdapterMessage message) { List <AdapterMessage> list = null; try { Tracer.WriteBegin(); NodeParams p = NodeParams.Extract(message); if (p.Operation == FileOperationValues.Write) { WriteFile(message, p); } else if (p.Operation == FileOperationValues.Read) { list = ReadFiles(message, p, FileShare.Read); } else if (p.Operation == FileOperationValues.ReadAndDelete) { list = ReadFiles(message, p, FileShare.None); } else // FileOperationValues.None { Tracer.WriteLine("File Operation set to 'None', so no action is done."); } } finally { Tracer.WriteEnd(); } return(list); }
private void _HandleMessage(AdapterMessage msg, bool updateWindow) { Program.Log.Write("{Adapter} Handling message (" + msg.ToString() + ") from adapter."); foreach (ListViewItem i in this.listViewInterface.Items) { GCInterface d = i.Tag as GCInterface; if (d != null && d.InterfaceID == msg.InterfaceID) { d.Status = msg.Status; if (i.SubItems.Count > 0) { i.SubItems[0].Text = msg.Status.ToString(); } Program.Log.Write("{Adapter} Handle message (" + msg.ToString() + ") from adapter succeeded."); if (updateWindow && i.Selected) { NotifySelectionChange(this.listViewInterface, EventArgs.Empty); Program.Log.Write("{Adapter} Update window by (" + msg.ToString() + ") succeeded."); } return; } } }
public override List <AdapterMessage> ProcessMessage(AdapterMessage message) { try { Tracer.WriteBegin(); NodeParams p = NodeParams.Extract(message); List <AdapterMessage> responseList = new List <AdapterMessage>(); String s = message.GetDataAsString(); if (p.IgnoreCase) { if (s.Contains(p.TextToReplace, StringComparison.InvariantCultureIgnoreCase)) { s = s.Replace(p.TextToReplace, p.ReplaceWithText, StringComparison.InvariantCultureIgnoreCase); Tracer.WriteLine($"Replaced text '{p.TextToReplace}' with '{p.ReplaceWithText}'."); AdapterMessage response = message.CloneWithNewData(s); return(new List <AdapterMessage>() { response }); } else { Tracer.WriteLine("No text to replace found..."); return(new List <AdapterMessage>() { message }); } } else { if (s.Contains(p.TextToReplace)) { s = s.Replace(p.TextToReplace, p.ReplaceWithText); Tracer.WriteLine($"Replaced text '{p.TextToReplace}' with '{p.ReplaceWithText}'."); AdapterMessage response = message.CloneWithNewData(s); return(new List <AdapterMessage>() { response }); } else { Tracer.WriteLine("No text to replace found..."); return(new List <AdapterMessage>() { message }); } } } finally { Tracer.WriteEnd(); } }
private void WriteFile(AdapterMessage message, NodeParams p) { lock (this) { string filepath = Path.Combine(p.Path, p.Filename); FileMode fileMode = FileMode.Create; if (p.AppendToExisting) { fileMode = FileMode.Append; } // Create directory if it does not exist. This enables paths like 'c:\temp\%date%' if (Directory.Exists(p.Path) == false) { Directory.CreateDirectory(p.Path); } using (FileStream fs = new FileStream(filepath, fileMode, FileAccess.Write, FileShare.None)) { // Seek to the end of the file in case we're appending to existing data fs.Seek(0, SeekOrigin.End); if (message.IsString) { using (StreamWriter sw = new StreamWriter(fs)) { sw.Write(message.GetDataAsString()); } } else { byte[] buffer = message.GetDataAsArray(); fs.Write(buffer, 0, buffer.Length); } } // Preserve the creation time of source if it is configured to do so and we're not appending if (p.PreserveCreationTime && fileMode != FileMode.Append) { string creationTime = message.Parameters.GetValueAsString("CreationTime"); if (creationTime != null) { try { DateTime time = DateTime.Parse(creationTime); System.IO.File.SetCreationTime(filepath, time); } catch (Exception ex) { // Just skip if not possible Tracer.WriteException(ex); } } } } }
public void HandleMessage(AdapterMessage msg, bool updateWindow) { if (msg == null) { return; } Program.Log.Write("{Adapter} Received message (" + msg.ToString() + ") from adapter."); AdapterMessageHandler dlg = new AdapterMessageHandler(_HandleMessage); this.Invoke(dlg, new object[] { msg, updateWindow }); }
private void HandleMessage(FileInfo fi) { if (fi != null && fi.IsReadOnly == false) { AdapterMessage msg = new AdapterMessage( File.ReadAllBytes(fi.FullName) ); AddExpandableMessageParams(msg, fi); MessageReceived(msg, fi.FullName); } }
private List <AdapterMessage> ReadFiles(AdapterMessage original, NodeParams p, FileShare fileShare) { List <AdapterMessage> list = new List <AdapterMessage>(); using (FileSystemEnumerator fse = new FileSystemEnumerator(p.Path, p.Filename, false)) { foreach (FileInfo fi in fse.Matches()) { if (fi == null) { continue; } AdapterMessage msg = null; if (!string.IsNullOrEmpty(p.FileMissingResponse) && !File.Exists(fi.FullName)) { Tracer.WriteLine($"File '{fi.FullName}' is MISSING, \r\nsetting response content to: '{p.FileMissingResponse}'"); msg = AdapterMessageFactory.CreateResponseMessage(original, p.FileMissingResponse); } else if (!string.IsNullOrEmpty(p.EmptyFileResponse) && new FileInfo(fi.FullName).Length == 0) { Tracer.WriteLine($"File '{fi.FullName}' is EMPTY, \r\nsetting response content to: {p.EmptyFileResponse}"); msg = AdapterMessageFactory.CreateResponseMessage(original, p.EmptyFileResponse); } else { Tracer.WriteLine($"Reading file from: '{fi.FullName}'"); byte[] data = System.IO.File.ReadAllBytes(fi.FullName); msg = AdapterMessageFactory.CreateResponseMessage(original, data); AddExpandableMessageParams(msg, fi); } if (p.Operation == FileOperationValues.ReadAndDelete && !fi.IsReadOnly) { DeleteFile(fi.FullName); } list.Add(msg); } } return(list); }
public override void ConsumeReceivedMessage(AdapterMessage message, object state) { try { Tracer.WriteBegin(); // Delete the file string filepath = state.ToString(); if (System.IO.File.Exists(filepath)) { Tracer.WriteLine($"Deleting file: '{filepath}'"); System.IO.File.Delete(filepath); } } finally { Tracer.WriteEnd(); } }
public override void ReturnResponse(AdapterMessage message, object state) { try { Tracer.WriteBegin(); if (m_NodeParams.IsTwoWay) { m_NodeParams = NodeParams.Extract(NodeParamList); string filepath = Path.Combine(m_NodeParams.ReplyPath, m_NodeParams.ReplyFilename); Tracer.WriteLine($"Writing response-file: '{filepath}'"); System.IO.File.WriteAllBytes(filepath, message.GetDataAsArray()); } } finally { Tracer.WriteEnd(); } }
public bool PreFilterMessage(ref Message m) { if (m.Msg == Win32Api.SW_SHOW) { this.Activate(); return(false); } AdapterMessage am = AdapterMessage.FromMessage(m); if (am == null) { return(false); } bool update = (_panelViews.CurrentPage == _viewInterface); _viewInterface.HandleMessage(am, update); return(true); }
//private int MsgID = 0xFFFF; #region IMessageFilter Members public bool PreFilterMessage(ref Message m) { //if (m.Msg != MsgID) return false; //this.Text = m.LParam.ToString(); //MessageBox.Show(m.HWnd.ToString() + "\r\n" + // m.Msg.ToString() + "\r\n" + // m.WParam.ToString() + "\r\n" + // m.LParam.ToString() + "\r\n"); AdapterMessage am = AdapterMessage.FromMessage(m); if (am == null) { return(false); } MessageBox.Show(am.ToString()); return(true); }
private void buttonCallme_Click(object sender, EventArgs e) { string caption = "DemoAdapter of GC Gateway"; //string caption = "HYS Interface Engine Manager"; ////string className = "WindowsForms10.Window.8.app.0.3b95145"; //int hwnd = Win32Api.FindWindow(null, caption); ////int hwnd = Win32Api.FindWindow(className, null); //// use caption after all... //IntPtr h = this.Handle; //IntPtr i = (IntPtr)hwnd; //MessageBox.Show(i.ToString()+" " + h.ToString()); //Win32Api.PostMessage(i, MsgID, 1, 2); ////Win32Api.SendMessage(i, MsgID, (IntPtr)1, "2"); AdapterMessage am = new AdapterMessage(123, AdapterStatus.Stopped); am.PostMessage(caption); }
private void AddExpandableMessageParams(AdapterMessage msg, FileInfo fi) { SetMessageParameter(msg, "Name", fi.Name); SetMessageParameter(msg, "FileName", fi.Name); if (fi.Extension != null && fi.Extension.Length > 0) { SetMessageParameter(msg, "FileNameWithoutExtension", fi.Name.Replace(fi.Extension, "")); SetMessageParameter(msg, "Extension", fi.Extension); } else { SetMessageParameter(msg, "FileNameWithoutExtension", ""); SetMessageParameter(msg, "Extension", ""); } SetMessageParameter(msg, "FullName", fi.FullName); SetMessageParameter(msg, "DirectoryName", fi.DirectoryName); SetMessageParameter(msg, "CreationTime", fi.CreationTime.ToString("yyyy-MM-dd HH:mm:ss.ffff")); SetMessageParameter(msg, "CreationTimeUtc", fi.CreationTimeUtc.ToString("yyyy-MM-dd HH:mm:ss.ffff")); SetMessageParameter(msg, "LastWriteTime", fi.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss.ffff")); SetMessageParameter(msg, "LastWriteTimeUtc", fi.LastWriteTimeUtc.ToString("yyyy-MM-dd HH:mm:ss.ffff")); }
public static NodeParams Extract(AdapterMessage message) { return(ExtractParams(message.NodeParamList)); }