public GXAmiEventListener(GXAmiDataCollectorServer server, GXProxyClass pc, GXAmiClient dc) { Server = server; DC = dc; pc.OnUpdated += new PropertyUpdateEventHandler(OnUpdated); pc.OnError += new ErrorEventHandler(pc_OnError); pc.OnStateChange += new DeviceStateChangedEventHandler(pc_OnStateChange); pc.OnUpdateParameters += new UpdateparametersEventHandler(pc_OnUpdateParameters); pc.OnTrace += new TraceAddEventHandler(pc_OnTrace); }
private void HandleDevice(GXClaimedInfo info, GXProxyClass pc, ref string path, ref bool leaveConnectionOpen, GXClaimedTask taskinfo) { try { //If device template is not loaded yet. if (path == null) { path = Path.Combine(Gurux.Common.GXCommon.ApplicationDataPath, "Gurux"); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); Gurux.Common.GXFileSystemSecurity.UpdateDirectorySecurity(path); } path = Path.Combine(path, "Gurux.DeviceSuite"); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); Gurux.Common.GXFileSystemSecurity.UpdateDirectorySecurity(path); } path = Path.Combine(path, "DeviceProfiles"); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); Gurux.Common.GXFileSystemSecurity.UpdateDirectorySecurity(path); } path = Path.Combine(path, taskinfo.DeviceProfile.ToString()); //Load Device template if not loaded yet. if (!Directory.Exists(path)) { Directory.CreateDirectory(path); Gurux.Common.GXFileSystemSecurity.UpdateDirectorySecurity(path); byte[] data = DC.GetDeviceProfilesData(taskinfo.DeviceProfile); GXDeviceProfile type = pc.Import(data, path); if (!(type is GXPublishedDeviceProfile)) { File.Copy(type.Path, Path.Combine(path, taskinfo.DeviceProfile.ToString() + ".gxp"), true); } } } } catch (Exception ex) { path = null; if (Directory.Exists(path)) { Directory.Delete(path, true); } lock (info.Exceptions) { info.Exceptions.Add(taskinfo, ex); taskinfo.Task.State = TaskState.Failed; } } try { //Save executed task. This is used if error occures. pc.Connect(path, taskinfo); //Read or write device. if (taskinfo.Task.TaskType == TaskType.Read || taskinfo.Task.TaskType == TaskType.Write) { System.Diagnostics.Debug.WriteLine("DC start to read target: " + taskinfo.Task.Id.ToString() + " " + taskinfo.Task.TargetDeviceID.ToString()); pc.ExecuteTransaction(taskinfo); taskinfo.Task.State = TaskState.Succeeded; System.Diagnostics.Debug.WriteLine("DC end reading target: " + taskinfo.Task.Id.ToString() + " " + Guid.ToString()); } else if (taskinfo.Task.TaskType == TaskType.StartMonitor) { leaveConnectionOpen = true; pc.StartMonitoring(); taskinfo.Task.State = TaskState.Succeeded; } else if (taskinfo.Task.TaskType == TaskType.StopMonitor) { leaveConnectionOpen = false; pc.StopMonitoring(); taskinfo.Task.State = TaskState.Succeeded; } else { throw new Exception("Invalid task type."); } } catch (Exception ex) { lock (info.Exceptions) { info.Exceptions.Add(taskinfo, ex); taskinfo.Task.State = TaskState.Failed; } if (m_Error != null) { m_Error(this, ex); } } }