/// <summary> /// Creates a new browser object with a set of filters. /// </summary> public AreaBrowser( ISystemContext context, ViewDescription view, NodeId referenceType, bool includeSubtypes, BrowseDirection browseDirection, QualifiedName browseName, IEnumerable <IReference> additionalReferences, bool internalOnly, AreaState area) : base( context, view, referenceType, includeSubtypes, browseDirection, browseName, additionalReferences, internalOnly) { m_stage = Stage.Begin; if (area != null) { m_area = AreaState.GetDirectory(context, area.NodeId); m_isRoot = area.IsRoot; } }
/// <summary> /// Creates a new browser object with a set of filters. /// </summary> public AreaBrowser( ISystemContext context, ViewDescription view, NodeId referenceType, bool includeSubtypes, BrowseDirection browseDirection, QualifiedName browseName, IEnumerable<IReference> additionalReferences, bool internalOnly, AreaState area) : base( context, view, referenceType, includeSubtypes, browseDirection, browseName, additionalReferences, internalOnly) { m_stage = Stage.Begin; if (area != null) { m_area = AreaState.GetDirectory(context, area.NodeId); m_isRoot = area.IsRoot; } }
/// <summary> /// Returns true if the system must be scanning to provide updates for the monitored item. /// </summary> private void StopMonitoring(ISystemContext context, MonitoredNode monitoredNode) { // find the root. NodeState root = monitoredNode.Node.GetHierarchyRoot(); // check for areas. AreaState area = root as AreaState; if (area != null) { if (m_system.MonitorArea(area, true) == 0) { RemoveNodeHierarchyFromCache(context, area); } return; } // check for controllers. ControllerState controller = root as ControllerState; if (controller != null) { if (m_system.MonitorController(controller, true) == 0) { RemoveNodeHierarchyFromCache(context, controller); } return; } }
/// <summary> /// Returns true if the system must be scanning to provide updates for the monitored item. /// </summary> private void StartMonitoring(ISystemContext context, MonitoredNode monitoredNode) { // find the root. NodeState root = monitoredNode.Node.GetHierarchyRoot(); // check for areas. AreaState area = root as AreaState; if (area != null) { m_system.MonitorArea(area, false); if (!m_cache.ContainsKey(area.NodeId)) { AddNodeHierarchyToCache(context, area); } return; } // check for controllers. ControllerState controller = root as ControllerState; if (controller != null) { m_system.MonitorController(controller, false); if (!m_cache.ContainsKey(controller.NodeId)) { AddNodeHierarchyToCache(context, controller); } return; } }
/// <summary> /// Returns the next child with the requested browse name. /// </summary> private IReference FindByBrowseName() { NodeState target = null; // check if match found previously. if (m_position == UInt32.MaxValue) { return(null); } // get the system to use. FileSystemMonitor system = SystemContext.SystemHandle as FileSystemMonitor; if (system == null) { return(null); } // browse name must be qualified by the correct namespace. if (system.NamespaceIndex != base.BrowseName.NamespaceIndex) { return(null); } // look for file. FileInfo[] files = m_area.GetFiles(Utils.Format("{0}.csv", base.BrowseName.Name)); if (files != null && files.Length > 0) { target = new ControllerState(SystemContext, files[0]); } // look for directory if (target == null) { DirectoryInfo[] directories = m_area.GetDirectories(base.BrowseName.Name, SearchOption.TopDirectoryOnly); if (directories == null || directories.Length <= 0) { return(null); } target = new AreaState(SystemContext, directories[0]); } // match found. m_position = UInt32.MaxValue; // return the requested reference type. return(new NodeStateReference(ReferenceTypeIds.Organizes, false, target)); }
/// <summary> /// Starts monitoring an area for changes. /// </summary> public int MonitorArea(AreaState area, bool stop) { lock (m_dataLock) { MonitoredObject monitoredObject = null; for (int ii = 0; ii < m_monitoredObjects.Count; ii++) { monitoredObject = m_monitoredObjects[ii]; if (Object.ReferenceEquals(area, monitoredObject.Area)) { if (stop) { monitoredObject.Refs--; if (monitoredObject.Refs == 0) { m_monitoredObjects.RemoveAt(ii); } } else { monitoredObject.Refs++; } return(monitoredObject.Refs); } } if (stop) { return(0); } StartTimer(); monitoredObject = new MonitoredObject(); monitoredObject.Area = area; monitoredObject.Refs = 1; m_monitoredObjects.Add(monitoredObject); return(monitoredObject.Refs); } }
/// <summary> /// Validates an area, creates the node and assigns node ids to all children. /// </summary> public bool ValidateArea(ISystemContext context, NodeState node) { // only need to validate once. node.OnValidate = null; DirectoryInfo directory = AreaState.GetDirectory(context, node.NodeId); if (directory == null || !directory.Exists) { return(false); } // initialize the area from the type model. node.Create(context, node.NodeId, node.BrowseName, null, false); // assign the child node ids. AssignChildNodeIds(context, node); return(true); }
/// <summary> /// Returns the next child. /// </summary> private IReference NextChild() { // check if a specific browse name is requested. if (!QualifiedName.IsNull(base.BrowseName)) { return(FindByBrowseName()); } NodeState target = null; // process directories. if (m_stage == Stage.Directories) { if (m_position < 0 || m_directories == null || m_position >= m_directories.Length) { return(null); } target = new AreaState(SystemContext, m_directories[m_position]); m_position++; } // process files. if (m_stage == Stage.Files) { if (m_position < 0 || m_files == null || m_position >= m_files.Length) { return(null); } target = new ControllerState(SystemContext, m_files[m_position]); m_position++; } return(new NodeStateReference(ReferenceTypeIds.Organizes, false, target)); }
/// <summary> /// Returns the next reference. /// </summary> /// <returns></returns> public override IReference Next() { lock (DataLock) { IReference reference = null; // enumerate pre-defined references. // always call first to ensure any pushed-back references are returned first. reference = base.Next(); if (reference != null) { return(reference); } // check that the root exists. if (m_area == null || !m_area.Exists) { return(null); } // don't start browsing huge number of references when only internal references are requested. if (InternalOnly) { return(null); } // start with directories. if (m_stage == Stage.Begin) { m_stage = Stage.Directories; m_directories = m_area.GetDirectories(); m_position = 0; } // enumerate directories. if (m_stage == Stage.Directories) { if (IsRequired(ReferenceTypeIds.Organizes, false)) { reference = NextChild(); if (reference != null) { return(reference); } } m_stage = Stage.Files; m_files = m_area.GetFiles("*.csv"); m_position = 0; } // enumerate files. if (m_stage == Stage.Files) { if (IsRequired(ReferenceTypeIds.Organizes, false)) { reference = NextChild(); if (reference != null) { return(reference); } } m_stage = Stage.Parents; m_position = 0; } // enumerate parents. if (m_stage == Stage.Parents) { if (IsRequired(ReferenceTypeIds.Organizes, true)) { if (m_isRoot) { reference = new NodeStateReference(ReferenceTypeIds.Organizes, true, ObjectIds.ObjectsFolder); } else { // create the parent area. AreaState parent = new AreaState(SystemContext, m_area.Parent); // construct the reference. reference = new NodeStateReference(ReferenceTypeIds.Organizes, true, parent); } m_stage = Stage.Done; m_position = 0; return(reference); } } // all done. return(null); } }
/// <summary> /// Creates the appropriate NodeState object from a NodeId. /// </summary> public static NodeState CreateHandleFromNodeId( ISystemContext context, NodeId nodeId, IDictionary <NodeId, NodeState> cache) { // get the system to use. FileSystemMonitor system = context.SystemHandle as FileSystemMonitor; if (system == null) { return(null); } // check for valid node id. if (nodeId == null || nodeId.NamespaceIndex != system.m_namespaceIndex || nodeId.IdType != IdType.String) { return(null); } // lookup in cache. NodeState state = null; if (cache != null) { if (cache.TryGetValue(nodeId, out state)) { return(state); } } string path = (string)nodeId.Identifier; uint baseline = (uint)Convert.ToUInt32('0'); // parse the object type id. uint objectTypeId = 0; int start = 0; for (int ii = 0; ii < path.Length; ii++) { if (path[ii] == ':') { start = ii + 1; break; } if (!Char.IsDigit(path[ii])) { return(null); } objectTypeId *= 10; objectTypeId += (uint)Convert.ToUInt32(path[ii]) - baseline; } string parentPath = path; NodeId parentId = nodeId; // check if referencing a child of the object. int end = -1; if (start < path.Length) { end = path.IndexOf(':', start); if (end >= start) { parentPath = path.Substring(0, end); parentId = new NodeId(parentPath, system.NamespaceIndex); } } // return cached value if available. if (cache != null) { if (!cache.TryGetValue(parentId, out state)) { state = null; } } // create the object instance. if (state == null) { switch (objectTypeId) { case ObjectTypes.AreaType: { string directoryPath = system.ExtractPathFromNodeId(nodeId); state = new AreaState(context, new DirectoryInfo(directoryPath)); break; } case ObjectTypes.ControllerType: { string filePath = system.ExtractPathFromNodeId(nodeId); filePath += ".csv"; state = new ControllerState(context, new FileInfo(filePath)); break; } default: { return(null); } } // update cache if provided. if (cache != null) { cache[parentId] = state; } } // nothing more to do if referencing the root. if (end < 0) { return(state); } // create the child identified by the name in the node id. string childPath = path.Substring(end + 1); // extract path of children. List <string> childNames = new List <string>(); int index = childPath.IndexOf(':'); while (index > 0) { childNames.Add(childPath.Substring(0, index)); childPath = childPath.Substring(index + 1); index = childPath.IndexOf(':'); } childNames.Add(childPath); NodeState parent = state; BaseInstanceState child = null; for (int ii = 0; ii < childNames.Count; ii++) { child = parent.CreateChild( context, new QualifiedName(childNames[ii], 0)); if (child == null) { return(null); } parent = child; if (ii == childNames.Count - 1) { child.NodeId = nodeId; if (state.ValidationRequired) { child.OnValidate = system.ValidateChild; } if (cache != null) { cache[nodeId] = child; } } } return(child); }
/// <summary> /// Returns the next reference. /// </summary> /// <returns></returns> public override IReference Next() { lock (DataLock) { IReference reference = null; // enumerate pre-defined references. // always call first to ensure any pushed-back references are returned first. reference = base.Next(); if (reference != null) { return reference; } // check that the root exists. if (m_area == null || !m_area.Exists) { return null; } // don't start browsing huge number of references when only internal references are requested. if (InternalOnly) { return null; } // start with directories. if (m_stage == Stage.Begin) { m_stage = Stage.Directories; m_directories = m_area.GetDirectories(); m_position = 0; } // enumerate directories. if (m_stage == Stage.Directories) { if (IsRequired(ReferenceTypeIds.Organizes, false)) { reference = NextChild(); if (reference != null) { return reference; } } m_stage = Stage.Files; m_files = m_area.GetFiles("*.csv"); m_position = 0; } // enumerate files. if (m_stage == Stage.Files) { if (IsRequired(ReferenceTypeIds.Organizes, false)) { reference = NextChild(); if (reference != null) { return reference; } } m_stage = Stage.Parents; m_position = 0; } // enumerate parents. if (m_stage == Stage.Parents) { if (IsRequired(ReferenceTypeIds.Organizes, true)) { if (m_isRoot) { reference = new NodeStateReference(ReferenceTypeIds.Organizes, true, ObjectIds.ObjectsFolder); } else { // create the parent area. AreaState parent = new AreaState(SystemContext, m_area.Parent); // construct the reference. reference = new NodeStateReference(ReferenceTypeIds.Organizes, true, parent); } m_stage = Stage.Done; m_position = 0; return reference; } } // all done. return null; } }
/// <summary> /// Returns the next child. /// </summary> private IReference NextChild() { // check if a specific browse name is requested. if (!QualifiedName.IsNull(base.BrowseName)) { return FindByBrowseName(); } NodeState target = null; // process directories. if (m_stage == Stage.Directories) { if (m_position < 0 || m_directories == null || m_position >= m_directories.Length) { return null; } target = new AreaState(SystemContext, m_directories[m_position]); m_position++; } // process files. if (m_stage == Stage.Files) { if (m_position < 0 || m_files == null || m_position >= m_files.Length) { return null; } target = new ControllerState(SystemContext, m_files[m_position]); m_position++; } return new NodeStateReference(ReferenceTypeIds.Organizes, false, target); }
/// <summary> /// Returns the next child with the requested browse name. /// </summary> private IReference FindByBrowseName() { NodeState target = null; // check if match found previously. if (m_position == UInt32.MaxValue) { return null; } // get the system to use. FileSystemMonitor system = SystemContext.SystemHandle as FileSystemMonitor; if (system == null) { return null; } // browse name must be qualified by the correct namespace. if (system.NamespaceIndex != base.BrowseName.NamespaceIndex) { return null; } // look for file. FileInfo[] files = m_area.GetFiles(Utils.Format("{0}.csv", base.BrowseName.Name)); if (files != null && files.Length > 0) { target = new ControllerState(SystemContext, files[0]); } // look for directory if (target == null) { DirectoryInfo[] directories = m_area.GetDirectories(base.BrowseName.Name, SearchOption.TopDirectoryOnly); if (directories == null || directories.Length <= 0) { return null; } target = new AreaState(SystemContext, directories[0]); } // match found. m_position = UInt32.MaxValue; // return the requested reference type. return new NodeStateReference(ReferenceTypeIds.Organizes, false, target); }
/// <summary> /// Starts monitoring an area for changes. /// </summary> public int MonitorArea(AreaState area, bool stop) { lock (m_dataLock) { MonitoredObject monitoredObject = null; for (int ii = 0; ii < m_monitoredObjects.Count; ii++) { monitoredObject = m_monitoredObjects[ii]; if (Object.ReferenceEquals(area, monitoredObject.Area)) { if (stop) { monitoredObject.Refs--; if (monitoredObject.Refs == 0) { m_monitoredObjects.RemoveAt(ii); } } else { monitoredObject.Refs++; } return monitoredObject.Refs; } } if (stop) { return 0; } StartTimer(); monitoredObject = new MonitoredObject(); monitoredObject.Area = area; monitoredObject.Refs = 1; m_monitoredObjects.Add(monitoredObject); return monitoredObject.Refs; } }
/// <summary> /// Creates the appropriate NodeState object from a NodeId. /// </summary> public static NodeState CreateHandleFromNodeId( ISystemContext context, NodeId nodeId, IDictionary<NodeId,NodeState> cache) { // get the system to use. FileSystemMonitor system = context.SystemHandle as FileSystemMonitor; if (system == null) { return null; } // check for valid node id. if (nodeId == null || nodeId.NamespaceIndex != system.m_namespaceIndex || nodeId.IdType != IdType.String) { return null; } // lookup in cache. NodeState state = null; if (cache != null) { if (cache.TryGetValue(nodeId, out state)) { return state; } } string path = (string)nodeId.Identifier; uint baseline = (uint)Convert.ToUInt32('0'); // parse the object type id. uint objectTypeId = 0; int start = 0; for (int ii = 0; ii < path.Length; ii++) { if (path[ii] == ':') { start = ii+1; break; } if (!Char.IsDigit(path[ii])) { return null; } objectTypeId *= 10; objectTypeId += (uint)Convert.ToUInt32(path[ii]) - baseline; } string parentPath = path; NodeId parentId = nodeId; // check if referencing a child of the object. int end = -1; if (start < path.Length) { end = path.IndexOf(':', start); if (end >= start) { parentPath = path.Substring(0, end); parentId = new NodeId(parentPath, system.NamespaceIndex); } } // return cached value if available. if (cache != null) { if (!cache.TryGetValue(parentId, out state)) { state = null; } } // create the object instance. if (state == null) { switch (objectTypeId) { case ObjectTypes.AreaType: { string directoryPath = system.ExtractPathFromNodeId(nodeId); state = new AreaState(context, new DirectoryInfo(directoryPath)); break; } case ObjectTypes.ControllerType: { string filePath = system.ExtractPathFromNodeId(nodeId); filePath += ".csv"; state = new ControllerState(context, new FileInfo(filePath)); break; } default: { return null; } } // update cache if provided. if (cache != null) { cache[parentId] = state; } } // nothing more to do if referencing the root. if (end < 0) { return state; } // create the child identified by the name in the node id. string childPath = path.Substring(end+1); // extract path of children. List<string> childNames = new List<string>(); int index = childPath.IndexOf(':'); while (index > 0) { childNames.Add(childPath.Substring(0, index)); childPath = childPath.Substring(index+1); index = childPath.IndexOf(':'); } childNames.Add(childPath); NodeState parent = state; BaseInstanceState child = null; for (int ii = 0; ii < childNames.Count; ii++) { child = parent.CreateChild( context, new QualifiedName(childNames[ii], 0)); if (child == null) { return null; } parent = child; if (ii == childNames.Count-1) { child.NodeId = nodeId; if (state.ValidationRequired) { child.OnValidate = system.ValidateChild; } if (cache != null) { cache[nodeId] = child; } } } return child; }