public void AddDevice() { base.SetStatus("Adding device."); FolderBrowserDialog dlg = new FolderBrowserDialog(); dlg.ShowNewFolderButton = false; dlg.Description = "Please select a folder which contains a device to be installed."; string path = Program.ConfigMgt.Config.RecentDeviceSelectionFolder; path = ConfigHelper.GetFullPath(path); if (path != null && path.Length > 0) { dlg.SelectedPath = path; } if (dlg.ShowDialog(frmMain) == DialogResult.OK) { string folderName = dlg.SelectedPath; string path1 = ConfigHelper.GetFullPath(folderName); string path2 = ConfigHelper.EnsurePathSlash(ConfigHelper.GetFullPath(_deviceManager.DevicesFolder)); if (path1.IndexOf(path2) >= 0) { MessageBox.Show(frmMain, "Cannot install a new device from an existing folder in the Device folder. Please select another folder instead.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); goto exit; } frmMain.Cursor = Cursors.WaitCursor; GCDevice device = _deviceManager.AddDevice(folderName); frmMain.Cursor = Cursors.Default; if (device != null) { Program.Log.Write("{Device} Add device (" + device.ToString() + ") succeed : " + device.FolderPath); _deviceView.RefreshView(); _deviceView.SelectDevice(device); } else { Program.Log.Write(LogType.Warning, "{Device} Add device failed : " + GCError.LastErrorInfor); if (GCError.LastError != null) { Program.Log.Write(LogType.Error, GCError.LastError.ToString()); } MessageBox.Show(frmMain, "Add device failed.\r\n\r\n" + GCError.LastErrorInfor, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } Program.ConfigMgt.Config.RecentDeviceSelectionFolder = dlg.SelectedPath; Program.ConfigMgt.Save(); exit: base.ClearStatus(); }
private void RefreshDeviceInformation() { this.labelName.Text = this.labelType.Text = this.labelVersion.Text = this.labelDirection.Text = this.labelDescription.Text = ""; if (_sourceDevice == null) { return; } DeviceDir dir = _sourceDevice.Directory; if (dir == null) { MessageBox.Show(this, "Invalid device index file in : " + _sourceDevice.FolderPath, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); _sourceDevice = null; } if (dir != null) { this.labelName.Text = dir.Header.Name; this.labelVersion.Text = dir.Header.Version; this.labelType.Text = dir.Header.Type.ToString(); this.labelDirection.Text = dir.Header.Direction.ToString(); this.labelDescription.Text = dir.Header.Description; this.textBoxInterfaceName.Focus(); } }
public static DeviceRec CreateDeviceRec(GCDevice device) { if (device == null) { return(null); } DeviceRec rec = new DeviceRec(); rec.ID = device.DeviceID; rec.Folder = device.FolderPath; rec.IndexFile = DeviceDirManager.IndexFileName; rec.Name = device.Directory.Header.Name; rec.Type = ((int)device.Directory.Header.Type).ToString(); rec.Description = device.Directory.Header.Description; switch (device.Directory.Header.Direction) { case DirectionType.INBOUND: rec.Direction = "I"; break; case DirectionType.OUTBOUND: rec.Direction = "O"; break; case DirectionType.BIDIRECTIONAL: rec.Direction = "B"; break; } return(rec); }
public static GCDeviceCollection LoadDeviceDirectory(string devicePath) { if (!IsDevicePath(devicePath)) { return(null); } try { GCError.ClearLastError(); GCDeviceCollection dlist = new GCDeviceCollection(); string[] subFolders = Directory.GetDirectories(devicePath); foreach (string sf in subFolders) { string folder = devicePath + "\\" + sf; GCDevice device = LoadDevice(folder); dlist.Add(device); } return(dlist); } catch (Exception e) { GCError.SetLastError(e); return(null); } }
public bool DeleteDevice(GCDevice device) { GCError.ClearLastError(); //validate device if (device == null || device.DeviceID < 0) { GCError.SetLastError("Invalid device."); return(false); } //delete record from database if (!DeviceTable.DeleteDevice(device.DeviceID)) { GCError.SetLastError("Delete device from database failed."); return(false); } //delete device folder if (!FolderControl.DeleteDevice(device.FolderPath)) { GCError.SetLastError("Delete device folder " + device.FolderPath + " failed."); return(false); } return(true); }
private void RefreshInterfaceInformation() { this.labelName.Text = this.labelType.Text = this.labelDevice.Text = this.labelDirection.Text = this.labelDescription.Text = ""; if (_definedInterface == null) { return; } GCDevice device = _definedInterface.Device; DeviceDir dir = _definedInterface.Directory; if (dir == null || device == null) { MessageBox.Show(this, "Invalid device index file in interface folder : " + _definedInterface.FolderPath, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); _definedInterface = null; } this.labelName.Text = dir.Header.Name; this.labelDevice.Text = device.ToString(); this.labelType.Text = dir.Header.Type.ToString(); this.labelDirection.Text = dir.Header.Direction.ToString(); this.labelDescription.Text = dir.Header.Description; if (Program.ConfigMgt.Config.ShowConfigWhenInterfaceInstall) { ShowConfig(); } }
public void BrowseFolder() { GCDevice device = GetSelectedDevice(); if (device != null) { FolderControl.BrowseFolder(device.FolderPath); } }
public bool UpdateDevice(GCDevice device) { DeviceRec rec = DataHelper.CreateDeviceRec(device); if (rec == null) { return(false); } return(Update(rec)); }
private void _selectionPage_MoveNext(HYS.IM.UIControl.IPage me) { _sourceDevice = _selectionPage.GetSelectedDevice(); if (_sourceDevice == null) { return; } sliderPanelMain.NextPage(); _definitionPage.SetSourceDevice(_sourceDevice); }
private void RefreshDetail(GCDevice device) { this.labelName.Text = ""; this.labelType.Text = ""; this.labelVersion.Text = ""; this.labelDirection.Text = ""; this.labelDescription.Text = ""; this.listViewFile.Items.Clear(); this.listViewCommand.Items.Clear(); if (device == null) { return; } DeviceDir dir = device.Directory; if (dir == null) { MessageBox.Show(this, "Invalid device index file in : " + device.FolderPath, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } this.labelName.Text = dir.Header.Name; this.labelType.Text = dir.Header.Type.ToString(); this.labelVersion.Text = dir.Header.Version; this.labelDirection.Text = dir.Header.Direction.ToString(); this.labelDescription.Text = dir.Header.Description; int count = 1; foreach (DeviceFile f in dir.Files) { ListViewItem i = this.listViewFile.Items.Add((count++).ToString()); i.SubItems.Add(f.Location); i.SubItems.Add(f.Type.ToString()); i.SubItems.Add(f.Description); i.SubItems.Add(f.Backupable.ToString()); i.Tag = f; } count = 1; foreach (Command c in dir.Commands) { ListViewItem i = this.listViewCommand.Items.Add((count++).ToString()); i.SubItems.Add(c.Type.ToString()); i.SubItems.Add(c.Path); i.SubItems.Add(c.Argument); i.SubItems.Add(c.Description); i.Tag = c; } }
public void SelectDevice(GCDevice device) { if (device == null) { return; } foreach (ListViewItem i in this.listViewDevice.Items) { GCDevice d = i.Tag as GCDevice; if (d != null && d.DeviceID == device.DeviceID) { i.Selected = true; return; } } }
public void DeleteDevice() { GCDevice device = _deviceView.GetSelectedDevice(); if (device == null) { return; } base.SetStatus("Deleting device."); if (_interfaceMgt.HasInterface(device.DeviceID)) { MessageBox.Show(frmMain, "There are some interface(s) based on this device. Please delete the device after uninstalling these interface(s).", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { if (MessageBox.Show(frmMain, "Device can not be restored after delete. Are you sure to delete device : " + device.ToString() + " ?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { frmMain.Cursor = Cursors.WaitCursor; bool result = _deviceManager.DeleteDevice(device); frmMain.Cursor = Cursors.Default; if (result) { Program.Log.Write("{Device} Delete device (" + device.ToString() + ") succeed : " + device.FolderPath); _deviceView.RefreshView(); } else { Program.Log.Write(LogType.Warning, "{Device} Delete device (" + device.ToString() + ") failed : " + GCError.LastErrorInfor); Program.Log.Write(GCError.LastError); MessageBox.Show(frmMain, "Delete device (" + device.ToString() + ") failed.\r\n\r\n" + GCError.LastErrorInfor, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } } base.ClearStatus(); }
public override void Refresh() { GCDevice device = _deviceView.GetSelectedDevice(); _btnDeleteDevice.Enabled = IsDeviceViewSelected() && (device != null); if (_viewPanel.CurrentPage == _deviceView) { if (device == null) { base.ClearInfor(); } else { base.SetInfor("Selected device : " + device.ToString()); } } }
private void CreateUninstallScript() { string scriptFile = Application.StartupPath + "\\UninstallServices.bat"; string interfacePath = Application.StartupPath + "\\" + Program.ConfigMgt.Config.InterfaceFolder; string[] folderList = Directory.GetDirectories(interfacePath); StringBuilder sb = new StringBuilder(); foreach (string folder in folderList) { GCDevice device = FolderControl.LoadDevice(folder); if (device != null && device.Directory != null) { Command[] clist = device.Directory.Commands.FindCommands(CommandType.Stop); if (clist != null) { foreach (Command cmd in clist) { sb.Append("\"" + folder + "\\" + cmd.Path + "\"").Append(" ").AppendLine(cmd.Argument); } } clist = device.Directory.Commands.FindCommands(CommandType.Uninstall); if (clist != null) { foreach (Command cmd in clist) { sb.Append("\"" + folder + "\\" + cmd.Path + "\"").Append(" ").AppendLine(cmd.Argument); } } } string f = Path.GetFileName(folder); sb.Append("net stop ").AppendLine(f); sb.Append("sc delete ").AppendLine(f); } using (StreamWriter sw = File.CreateText(scriptFile)) { sw.Write(sb.ToString()); } Program.Log.Write("{Interface} Update uninstall script in " + scriptFile); }
public bool AddInterfaceToDatabase(GCInterface gcInterface) { //return TempFunctionHandler("Runing AddInterfaceToDatabase script..."); GCError.ClearLastError(); // validate interface if (gcInterface == null) { GCError.SetLastError("Invalid interface."); return(false); } // insert interface record into database gcInterface.Directory = null; if (!InterfaceTable.InsertInterface(gcInterface)) { GCError.SetLastError("Insert database failed."); return(false); } // get interface id gcInterface.InterfaceID = InterfaceTable.GetMaxID(); if (gcInterface.InterfaceID < 1) { GCError.SetLastError("Invalid interface ID."); return(false); } // update DeviceDir: update interface id, which will be used by interface NT service to identify itself when notifying its status to IM via windows message. GCDevice device = FolderControl.LoadDevice(gcInterface.FolderPath); device.Directory.Header.ID = gcInterface.InterfaceID.ToString(); if (!FolderControl.SaveDevice(device)) { GCError.SetLastError("Update device index file failed."); return(false); } return(true); }
public static InterfaceRec CreateInterfaceRec(GCInterface i) { if (i == null) { return(null); } GCDevice device = i.Device; DeviceDir idir = i.Directory; InterfaceRec rec = i.InterfaceRec; rec.Folder = i.FolderPath; rec.Name = idir.Header.Name; rec.DeviceID = device.DeviceID; rec.DeviceName = device.DeviceName; rec.IndexFile = DeviceDirManager.IndexFileName; rec.Type = ((int)device.Directory.Header.Type).ToString(); rec.Description = device.Directory.Header.Description; rec.EventType = idir.Header.EventType; switch (device.Directory.Header.Direction) { case DirectionType.INBOUND: rec.Direction = "I"; break; case DirectionType.OUTBOUND: rec.Direction = "O"; break; case DirectionType.BIDIRECTIONAL: rec.Direction = "B"; break; } return(rec); }
public bool HasSameDevice(GCDevice device) { DeviceRec rec = DataHelper.CreateDeviceRec(device); if (rec == null) { return(true); } string sql = "SELECT * FROM " + TableName + " WHERE DEVICE_DIRECT='" + rec.Direction + "' AND DEVICE_TYPE='" + rec.Type + "' AND DEVICE_NAME='" + rec.Name + "'"; DObjectCollection dlist = Select(sql); if (dlist == null) { return(true); //db query failed. } if (dlist.Count > 0) { return(true); } return(false); }
public GCInterface AddInterfaceToFolder(GCDevice gcDevice, string interfaceName, string interfacDesc) { GCError.ClearLastError(); // validation if (gcDevice == null || gcDevice.Directory == null) { GCError.SetLastError("Invalid device object."); return(null); } int progCount = 0; int fileCount = gcDevice.Directory.Files.Count; NotifyStart(fileCount + 2, 0, progCount++, "Create interface folder..."); // create interface folder string targetFolder = InterfacesFolder + "\\" + interfaceName; try { if (Directory.Exists(targetFolder)) { GCError.SetLastError("Folder " + targetFolder + " have already exist, please try another name."); NotifyComplete(false, ""); return(null); } if (Directory.CreateDirectory(targetFolder) == null) { GCError.SetLastError("Cannot create folder " + targetFolder + ", please try another name."); NotifyComplete(false, ""); return(null); } } catch (Exception e) { GCError.SetLastError("Cannot create folder " + targetFolder); GCError.SetLastError(e); NotifyComplete(false, ""); return(null); } // copy device files DeviceDir dir = gcDevice.Directory; string sourceFolder = gcDevice.FolderPath; string sourceFile = sourceFolder + "\\" + DeviceDirManager.IndexFileName; string targetFile = targetFolder + "\\" + DeviceDirManager.IndexFileName; try { NotifyGoing(progCount++, "Copying files... "); File.Copy(sourceFile, targetFile); foreach (DeviceFile f in dir.Files) { NotifyGoing(progCount++, "Copying files..."); sourceFile = sourceFolder + "\\" + f.Location; targetFile = targetFolder + "\\" + f.Location; // support sub folder in device dir string path = Path.GetDirectoryName(targetFile); if (!Directory.Exists(path)) { if (Directory.CreateDirectory(path) == null) { GCError.SetLastError("Cannot create folder " + path); NotifyComplete(false, ""); return(null); } } File.Copy(sourceFile, targetFile); } } catch (Exception e) { GCError.SetLastError("Cannot copy file from " + sourceFile + " to " + targetFile); GCError.SetLastError(e); NotifyComplete(false, ""); // roll back... FolderControl.DeleteDevice(targetFolder); return(null); } NotifyGoing(progCount++, "Updating files..."); // update DeviceDir: save interface name, which will be used by interface installation program GCDevice device = FolderControl.LoadDevice(targetFolder); device.Directory.Header.RefDeviceName = gcDevice.DeviceName; device.Directory.Header.RefDeviceID = gcDevice.DeviceID.ToString(); device.Directory.Header.Description = interfacDesc; device.Directory.Header.Name = interfaceName; if (!FolderControl.SaveDevice(device)) { GCError.SetLastError("Update device index file failed."); NotifyComplete(false, ""); // roll back... FolderControl.DeleteDevice(targetFolder); return(null); } // create interface object GCInterface i = new GCInterface(); i.Directory = device.Directory; i.InterfaceName = interfaceName; i.FolderPath = targetFolder; i.Device = gcDevice; NotifyComplete(true, ""); return(i); }
public GCDevice AddDevice(string folder) { GCError.ClearLastError(); // check if the folder contains a device if (!FolderControl.IsDevicePath(folder)) { GCError.SetLastError("Invalid device folder."); return(null); } // load device information from DeviceDir file GCDevice device = FolderControl.LoadDevice(folder); if (device == null) { GCError.SetLastError("Invalid device index file."); return(null); } // check whether a same device has already been installed // (if we do not do this, license control on max interface count will be useless. if (DeviceTable.HasSameDevice(device)) { GCError.SetLastError("Device (Type: " + device.Directory.Header.Type.ToString() + ", Direction: " + device.Directory.Header.Direction.ToString() + ") has already been installed."); return(null); } // insert device information into database if (!DeviceTable.InsertDevice(device)) { GCError.SetLastError("Insert database failed."); return(null); } // get device id for return; device.DeviceID = DeviceTable.GetMaxID(); if (device.DeviceID < 1) { GCError.SetLastError("Invalid device ID."); return(null); } // create device folder string folderName = DevicesFolder + "\\" + device.DeviceName; try { FolderControl.DeleteDevice(folderName); //according to defect EK_HI00045030 , 2006/11/20 Directory.CreateDirectory(folderName); } catch (Exception err) { GCError.SetLastError(err); GCError.SetLastError("Cannot create directory " + folderName); DeviceTable.DeleteDevice(device.DeviceID); return(null); } // copy device files string sourceFile = ""; string targetFile = ""; try { sourceFile = device.FolderPath + "\\" + DeviceDirManager.IndexFileName; targetFile = folderName + "\\" + DeviceDirManager.IndexFileName; File.Copy(sourceFile, targetFile); foreach (DeviceFile f in device.Directory.Files) { sourceFile = device.FolderPath + "\\" + f.Location; targetFile = folderName + "\\" + f.Location; // support sub folder in device dir string path = Path.GetDirectoryName(targetFile); if (!Directory.Exists(path)) { if (Directory.CreateDirectory(path) == null) { GCError.SetLastError("Cannot create folder " + path); return(null); } } File.Copy(sourceFile, targetFile); } } catch (Exception err) { GCError.SetLastError(err); GCError.SetLastError("Cannot copy file from " + sourceFile + " to " + targetFile); DeviceTable.DeleteDevice(device.DeviceID); return(null); } // update DeviceDir: save device id GCDevice newDevice = FolderControl.LoadDevice(folderName); newDevice.Directory.Header.ID = device.DeviceID.ToString(); if (!FolderControl.SaveDevice(newDevice)) { GCError.SetLastError("Update device index file failed."); return(null); } // update device record device.FolderPath = folderName; if (!DeviceTable.UpdateDevice(device)) { GCError.SetLastError("Update database failed."); return(null); } return(device); }
public void UpdateInterfaceByDevice() { GCDevice device = _deviceView.GetSelectedDevice(); if (device == null) { return; } base.SetStatus("Updating interfaces by device."); if (!_interfaceMgt.HasInterface(device.DeviceID)) { MessageBox.Show(frmMain, "There is no interface based on this device.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Information); goto exit; } GCInterfaceCollection iList = _interfaceMgt.QueryInterfaceList(false); GCInterfaceCollection ulist = new GCInterfaceCollection(); StringBuilder sbNameList = new StringBuilder(); foreach (GCInterface i in iList) { if (i.Device.DeviceID != device.DeviceID) { continue; } sbNameList.Append(i.InterfaceName + ","); ulist.Add(i); } if (MessageBox.Show(frmMain, "The following interface(s) will be stopped, and the executable files of the interface(s) will be over-written. Do you want to continue?\r\n\r\n" + sbNameList.ToString().TrimEnd(','), "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) { goto exit; } frmMain.Cursor = Cursors.WaitCursor; foreach (GCInterface i in ulist) { // stop interfaces Program.Log.Write(string.Format("Stopping interface {0}.", i.InterfaceName)); _interfaceMgt.StopInterface(i); // copy files (*.exe, *.dll), not including sub folder string iFolder = ConfigHelper.GetFullPath(Path.Combine(_interfaceMgt.InterfacesFolder, i.InterfaceName)); string dFolder = ConfigHelper.GetFullPath(device.FolderPath); string[] fList = Directory.GetFiles(dFolder, "*.exe"); foreach (string fName in fList) { string fn = Path.GetFileName(fName); string fromPath = Path.Combine(dFolder, fn); string toPath = Path.Combine(iFolder, fn); Program.Log.Write(string.Format("Copy {0} to {1}", fromPath, toPath)); File.Copy(fromPath, toPath, true); } fList = Directory.GetFiles(dFolder, "*.dll"); foreach (string fName in fList) { string fn = Path.GetFileName(fName); string fromPath = Path.Combine(dFolder, fn); string toPath = Path.Combine(iFolder, fn); Program.Log.Write(string.Format("Copy {0} to {1}", fromPath, toPath)); File.Copy(fromPath, toPath, true); } // modify DeviceDir (four exe from HYS to CSH.HYSIM) string dirFilePath = Path.Combine(iFolder, DeviceDirManager.IndexFileName); Program.Log.Write(string.Format("Updating file {0}.", dirFilePath)); string dirFileContent = File.ReadAllText(dirFilePath); dirFileContent = dirFileContent.Replace("HYS.Adapter.Composer.exe", "HYS.IM.Adapter.Composer.exe"); dirFileContent = dirFileContent.Replace("HYS.Adapter.Config.exe", "HYS.IM.Adapter.Config.exe"); dirFileContent = dirFileContent.Replace("HYS.Adapter.Monitor.exe", "HYS.IM.Adapter.Monitor.exe"); dirFileContent = dirFileContent.Replace("HYS.Adapter.Service.exe", "HYS.IM.Adapter.Service.exe"); File.WriteAllText(dirFilePath, dirFileContent); } frmMain.Cursor = Cursors.Default; MessageBox.Show(frmMain, "Updating interface(s) success. Please switch to the Interface View and click the Refresh button to apply the change.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); exit: base.ClearStatus(); }
public void SetSourceDevice(GCDevice device) { _sourceDevice = device; RefreshDeviceInformation(); RefreshControl(); }