예제 #1
0
        public void AddOutputNode(PanelOutputNode node)
        {
            if (Outputs.Any(x => x.Id == node.Id))
                return;


            var output = new Output
            {
                Id = node.Id,
                Name = GenerateOutputName()
            };

            node.Settings["Name"].Value = output.Name;
            AddOutput(output);

            UpdateMeInEditor();
            UpdateMeInDb();
        }
예제 #2
0
 public Node GetOutputOwner(Output output)
 {
     lock (nodesLock)
         return nodes.FirstOrDefault(node => node.Outputs.Contains(output));
 }
예제 #3
0
 public Link GetLink(Output output, Input input)
 {
     lock (linksLock)
         return links.FirstOrDefault(x => x.InputId == input.Id && x.OutputId == output.Id);
 }
예제 #4
0
 public List<Link> GetLinksForOutput(Output output)
 {
     lock (linksLock)
         return links.Where(x => x.OutputId == output.Id).ToList();
 }
예제 #5
0
        public Link AddLink(Output output, Input input, bool writeInDb = true)
        {
            Node inputNode = GetInputOwner(input);
            Node outputNode = GetOutputOwner(output);

            if (inputNode == null || outputNode == null)
            {
                LogEngineError($"Can`t create link from [{output.Id}] to [{input.Id}]. Does not exist.");
                return null;
            }

            if (inputNode == outputNode)
            {
                LogEngineError($"Can`t create link from [{output.Id}] to [{input.Id}]. Input and output belong to the same node.");
                return null;
            }

            if (inputNode.PanelId != outputNode.PanelId)
            {
                LogEngineError($"Can`t create link from {outputNode.GetType().Name} to {inputNode.GetType().Name}. Nodes are on different panels.");
                return null;
            }


            Link link = new Link(output.Id, input.Id, inputNode.PanelId);

            //prevent two links to one input
            Link oldLink = GetLinkForInput(input);
            if (oldLink != null)
                RemoveLink(oldLink, true);

            LogEngineInfo($"New link from [{outputNode.GetType().Name}] to [{inputNode.GetType().Name}]");

            lock (linksLock)
                links.Add(link);


            if (writeInDb)
                nodesDb?.AddLink(link);

            OnNewLink?.Invoke(link);

            if (started)
                input.Value = output.Value;

            return link;
        }
예제 #6
0
        public void RemoveLink(Output output, Input input, bool writeInDb)
        {
            Link link = GetLink(output, input);

            if (link == null)
            {
                LogEngineError($"Can`t remove link from [{output.Id}] to [{input.Id}]. Does not exist.");
                return;
            }

            lock (linksLock)
                links.Remove(link);

            if (writeInDb)
                nodesDb?.RemoveLink(link.Id);

            Node inputNode = GetInputOwner(input);
            Node outputNode = GetOutputOwner(output);
            if (inputNode != null && outputNode != null)
                LogEngineInfo($"Remove link from [{outputNode.GetType().Name}] to [{inputNode.GetType().Name}]");
            else
                LogEngineInfo($"Remove link.");

            input.Value = null;
            OnRemoveLink?.Invoke(link);

        }
예제 #7
0
        public void OnOutputChange(Output output)
        {
            if (!started)
                return;

            Node node = GetOutputOwner(output);
            if (node == null)
                return;

            ShowNodeActivity(node);

            OnOutputStateUpdated?.Invoke(output);

            node.OnOutputChange(output);
        }
예제 #8
0
        public void UpdateBattery(int? batteryLevel)
        {
            Output output = Outputs.FirstOrDefault(x => x.Name == "Battery");
            if (output == null)
            {
                output = new Output { Name = "Battery" };
                output.SlotIndex = Int32.MaxValue;
                AddOutput(output);
                UpdateMeInEditor();
            }

            output.Value = batteryLevel.ToString();
            UpdateMeInDb();
        }
예제 #9
0
        public virtual void OnOutputChange(Output output)
        {
            if (options.LogOutputChanges)
                LogInfo($"{output.Name}: [{output.Value ?? "NULL"}]");

            //send state to linked nodes
            List<Link> list = engine?.GetLinksForOutput(output);
            foreach (var link in list)
            {
                Input input = engine?.GetInput(link.InputId);
                if (input != null)
                {
                    input.Value = output.Value;
                }
            }
        }
예제 #10
0
        public void RemoveOutput(Output output)
        {
            if (output == null || !Outputs.Contains(output))
            {
                LogError("Can`t remove output. Does not exist.");
                return;
            }

            var links = engine.GetLinksForOutput(output);

            engine.RemoveLinks(links,true);


            Outputs.Remove(output);
        }
예제 #11
0
        public void AddOutput(Output output)
        {
            Outputs.Add(output);

            if (engine != null)
                output.OnOutputChange += engine.OnOutputChange;
        }