Skip to content

Commit

Permalink
Merge pull request #219 from xibosignage/develop
Browse files Browse the repository at this point in the history
v3 R300
  • Loading branch information
dasgarner authored Jun 23, 2021
2 parents 89c5b89 + 4420db1 commit ce5053a
Show file tree
Hide file tree
Showing 46 changed files with 2,424 additions and 630 deletions.
162 changes: 162 additions & 0 deletions Action/Action.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/**
* Copyright (C) 2021 Xibo Signage Ltd
*
* Xibo - Digital Signage - http://www.xibo.org.uk
*
* This file is part of Xibo.
*
* Xibo is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* Xibo is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Xibo. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Xml;

namespace XiboClient.Action
{
public class Action
{
public int Id { get; set; }
public string ActionType { get; set; }
public string TriggerType { get; set; }
public string TriggerCode { get; set; }
public int WidgetId { get; set; }
public int SourceId { get; set; }
public string Source { get; set; }
public int TargetId { get; set; }
public string Target { get; set; }
public string LayoutCode { get; set; }
public bool Bubble { get; set; }
public bool IsDrawer { get; set; }

public Rect Rect { get; set; }

/// <summary>
/// Create an Action from XmlNode
/// </summary>
/// <param name="node"></param>
/// <returns></returns>
public static Action CreateFromXmlNode(XmlNode node, int top, int left, int width, int height, bool isDrawer)
{
XmlAttributeCollection attributes = node.Attributes;

return new Action
{
Id = int.Parse(attributes["id"].Value),
ActionType = attributes["actionType"]?.Value,
TriggerType = attributes["triggerType"]?.Value,
TriggerCode = attributes["triggerCode"]?.Value,
WidgetId = string.IsNullOrEmpty(attributes["widgetId"]?.Value) ? 0 : int.Parse(attributes["widgetId"].Value),
SourceId = string.IsNullOrEmpty(attributes["sourceId"]?.Value) ? 0 : int.Parse(attributes["sourceId"].Value),
Source = attributes["source"]?.Value,
TargetId = string.IsNullOrEmpty(attributes["targetId"]?.Value) ? 0 : int.Parse(attributes["targetId"].Value),
Target = attributes["target"]?.Value,
LayoutCode = attributes["layoutCode"]?.Value,
Bubble = false,
IsDrawer = isDrawer,
Rect = new Rect(left, top, width, height)
};
}

/// <summary>
/// Create a list of Actions from an XmlNodeList
/// </summary>
/// <param name="nodeList"></param>
/// <returns></returns>
public static List<Action> CreateFromXmlNodeList(XmlNodeList nodeList)
{
return CreateFromXmlNodeList(nodeList, 0, 0, 0, 0, false);
}

/// <summary>
/// Create a list of Actions from an XmlNodeList
/// </summary>
/// <param name="nodeList"></param>
/// <returns></returns>
public static List<Action> CreateFromXmlNodeList(XmlNodeList nodeList, bool isDrawer)
{
return CreateFromXmlNodeList(nodeList, 0, 0, 0, 0, isDrawer);
}

/// <summary>
/// Create a list of Actions from an XmlNodeList
/// </summary>
/// <param name="nodeList"></param>
/// <returns></returns>
public static List<Action> CreateFromXmlNodeList(XmlNodeList nodeList, int top, int left, int width, int height)
{
return CreateFromXmlNodeList(nodeList, top, left, width, height, false);
}

/// <summary>
/// Create a list of Actions from an XmlNodeList
/// </summary>
/// <param name="nodeList"></param>
/// <param name="isDrawer"></param>
/// <returns></returns>
public static List<Action> CreateFromXmlNodeList(XmlNodeList nodeList, int top, int left, int width, int height, bool isDrawer)
{
List<Action> actions = new List<Action>();
foreach (XmlNode node in nodeList)
{
try
{
actions.Add(CreateFromXmlNode(node, top, left, width, height, isDrawer));
}
catch (Exception e)
{
Trace.WriteLine(new LogMessage("Action", "CreateFromXmlNodeList: create from node failed. e = " + e.Message), LogType.Info.ToString());
}
}
return actions;
}

/// <summary>
/// Get Priority for action source
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static int PriorityForActionSource(string source)
{
switch (source)
{
case "widget":
return 1;

case "region":
return 2;

case "layout":
return 3;

default:
return 4;
}
}

/// <summary>
/// Is the point inside this action
/// </summary>
/// <param name="point"></param>
/// <returns></returns>
public bool IsPointInside(Point point)
{
return Rect.Contains(point);
}
}
}
46 changes: 40 additions & 6 deletions Action/Command.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
using System;
/**
* Copyright (C) 2021 Xibo Signage Ltd
*
* Xibo - Digital Signage - http://www.xibo.org.uk
*
* This file is part of Xibo.
*
* Xibo is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* Xibo is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Xibo. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;

namespace XiboClient.Logic
namespace XiboClient.Action
{
[Serializable]
public class Command
Expand All @@ -12,7 +32,7 @@ public class Command
public string CommandString;
public string Validation;

public bool notifyStatus()
public bool IsValidationRequired()
{
return !string.IsNullOrEmpty(Validation);
}
Expand All @@ -32,7 +52,7 @@ public bool Run()
Rs232Command rs232 = new Rs232Command(this);
string line = rs232.Run();

if (notifyStatus())
if (IsValidationRequired())
{
return line == Validation;
}
Expand All @@ -50,6 +70,20 @@ public bool Run()

return true;
}
else if (CommandString.StartsWith("http|"))
{
HttpCommand command = new HttpCommand(this);
var httpStatus = command.RunAsync();

if (IsValidationRequired())
{
return httpStatus.Result + "" == Validation;
}
else
{
return true;
}
}
else
{
// Process with CMD
Expand All @@ -63,13 +97,13 @@ public bool Run()
startInfo.Arguments = "/C " + CommandString;
startInfo.UseShellExecute = false;

if (notifyStatus())
if (IsValidationRequired())
startInfo.RedirectStandardOutput = true;

process.StartInfo = startInfo;
process.Start();

if (notifyStatus())
if (IsValidationRequired())
{
string line = "";
while (!process.StandardOutput.EndOfStream)
Expand Down
100 changes: 100 additions & 0 deletions Action/HttpCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* Copyright (C) 2021 Xibo Signage Ltd
*
* Xibo - Digital Signage - http://www.xibo.org.uk
*
* This file is part of Xibo.
*
* Xibo is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* Xibo is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Xibo. If not, see <http://www.gnu.org/licenses/>.
*/
using Flurl;
using Flurl.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XiboClient.Action
{
class HttpCommand
{
/// <summary>
/// The command
/// </summary>
private Command _command;

/// <summary>
/// Constructor
/// </summary>
/// <param name="command">The command to run</param>
public HttpCommand(Command command)
{
_command = command;
}

/// <summary>
/// Run the command
/// </summary>
/// <returns></returns>
public async Task<int> RunAsync()
{
if (!_command.CommandString.StartsWith("http"))
{
throw new ArgumentException("Not a HTTP command");
}

// Split the command string by "pipe"
string[] command = _command.CommandString.Split('|');

// The format is
// http|<url>|<content-type|application/x-www-form-urlencoded|application/json|text/plain>|<body-data>
var url = new Url(command[1]);
var contentType = command[2];
var config = JsonConvert.DeserializeObject<HttpCommandConfig>(command[3]);

if (!string.IsNullOrEmpty(config.headers))
{
url.WithHeaders(JObject.Parse(config.headers));
}

IFlurlResponse result;
switch (config.method.ToUpperInvariant())
{
case "GET":
result = await url.GetAsync();
break;

case "POST":
result = (contentType == "application/json") ? await url.PostJsonAsync(JObject.Parse(config.body)) : await url.PostStringAsync(config.body);
break;

case "PUT":
result = (contentType == "application/json") ? await url.PutJsonAsync(JObject.Parse(config.body)) : await url.PutStringAsync(config.body);
break;

case "DELETE":
result = await url.DeleteAsync();
break;

default:
throw new Exception("Unsupported method: " + config.method);
}

return result.StatusCode;
}
}
}
35 changes: 35 additions & 0 deletions Action/HttpCommandConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright (C) 2021 Xibo Signage Ltd
*
* Xibo - Digital Signage - http://www.xibo.org.uk
*
* This file is part of Xibo.
*
* Xibo is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* Xibo is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Xibo. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XiboClient.Action
{
class HttpCommandConfig
{
public string method;
public string headers;
public string body;
}
}
Loading

0 comments on commit ce5053a

Please sign in to comment.