-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from dasgarner/feature/commands
Feature/commands
- Loading branch information
Showing
27 changed files
with
1,200 additions
and
321 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace XiboClient.Logic | ||
{ | ||
[Serializable] | ||
public class Command | ||
{ | ||
public string Code; | ||
public string CommandString; | ||
public string Validation; | ||
|
||
public bool notifyStatus() | ||
{ | ||
return !string.IsNullOrEmpty(Validation); | ||
} | ||
|
||
/// <summary> | ||
/// Run the Command | ||
/// </summary> | ||
/// <returns>true on success</returns> | ||
public bool Run() | ||
{ | ||
if (string.IsNullOrEmpty(CommandString)) | ||
throw new ArgumentNullException("Command string is empty, please check your Display Profile " + Code + " command for a valid command string."); | ||
|
||
// Parse the command string to work out how we should run this command. | ||
if (CommandString.StartsWith("rs232")) | ||
{ | ||
Rs232Command rs232 = new Rs232Command(this); | ||
string line = rs232.Run(); | ||
|
||
if (notifyStatus()) | ||
{ | ||
return line == Validation; | ||
} | ||
else | ||
{ | ||
return true; | ||
} | ||
} | ||
else | ||
{ | ||
// Process with CMD | ||
using (Process process = new Process()) | ||
{ | ||
ProcessStartInfo startInfo = new ProcessStartInfo(); | ||
|
||
startInfo.CreateNoWindow = true; | ||
startInfo.WindowStyle = ProcessWindowStyle.Hidden; | ||
startInfo.FileName = "cmd.exe"; | ||
startInfo.Arguments = "/C " + CommandString; | ||
startInfo.UseShellExecute = false; | ||
|
||
if (notifyStatus()) | ||
startInfo.RedirectStandardOutput = true; | ||
|
||
process.StartInfo = startInfo; | ||
process.Start(); | ||
|
||
if (notifyStatus()) | ||
{ | ||
string line = ""; | ||
while (!process.StandardOutput.EndOfStream) | ||
{ | ||
line += process.StandardOutput.ReadLine(); | ||
} | ||
|
||
return line == Validation; | ||
} | ||
else | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Get a command from Application Settings based on its Command Code | ||
/// </summary> | ||
/// <param name="code"></param> | ||
/// <returns></returns> | ||
public static Command GetByCode(string code) | ||
{ | ||
foreach (Command command in ApplicationSettings.Default.Commands) | ||
{ | ||
if (command.Code == code) | ||
return command; | ||
} | ||
|
||
throw new KeyNotFoundException("Command Not Found"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace XiboClient.Action | ||
{ | ||
class PlayerAction | ||
{ | ||
public string action; | ||
|
||
public DateTime createdDt; | ||
public int ttl; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.IO.Ports; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace XiboClient.Logic | ||
{ | ||
public class Rs232Command | ||
{ | ||
private Command _command; | ||
|
||
private SerialPort _port; | ||
private string _toSend; | ||
|
||
public Rs232Command(Command command) | ||
{ | ||
_command = command; | ||
} | ||
|
||
/// <summary> | ||
/// Run the command | ||
/// </summary> | ||
public string Run() | ||
{ | ||
string response = ""; | ||
|
||
// Parse and configure the port | ||
parse(); | ||
|
||
// try to open the COM port | ||
if (!_port.IsOpen) | ||
_port.Open(); | ||
|
||
try | ||
{ | ||
// Write our data stream | ||
_port.Write(_toSend); | ||
|
||
// Read | ||
if (_command.notifyStatus()) | ||
{ | ||
_port.ReadTimeout = 5000; | ||
response = _port.ReadLine(); | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
Trace.WriteLine(new LogMessage("CommandRs232 - run", e.Message), LogType.Error.ToString()); | ||
} | ||
finally | ||
{ | ||
// Close the port | ||
_port.Close(); | ||
} | ||
|
||
return response; | ||
} | ||
|
||
/// <summary> | ||
/// Parses the command string | ||
/// </summary> | ||
private void parse() | ||
{ | ||
if (!_command.CommandString.StartsWith("rs232")) | ||
throw new ArgumentException("Not a RS232 command"); | ||
|
||
// Split the command string by "space" | ||
string[] command = _command.CommandString.Split('|'); | ||
|
||
// The second part of the string is our comma separated connection string | ||
// Port,Baud,Data,Parity,Stop,Flow | ||
string[] connection = command[1].Split(','); | ||
|
||
_port = new SerialPort(); | ||
_port.PortName = connection[0]; | ||
_port.BaudRate = Convert.ToInt32(connection[1]); | ||
_port.DataBits = Convert.ToInt16(connection[2]); | ||
_port.Parity = (Parity)Enum.Parse(typeof(Parity), connection[3]); | ||
_port.StopBits = (StopBits)Enum.Parse(typeof(StopBits), connection[4]); | ||
_port.Handshake = (Handshake)Enum.Parse(typeof(Handshake), connection[5]); | ||
|
||
// Get the actual command to send | ||
_toSend = command[2]; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace XiboClient.Logic | ||
{ | ||
public class ScheduleCommand | ||
{ | ||
public DateTime Date { get; set; } | ||
public String Code { get; set; } | ||
public Command Command { get; set; } | ||
public int ScheduleId { get; set; } | ||
|
||
private bool _run = false; | ||
public bool HasRun | ||
{ | ||
get | ||
{ | ||
return _run; | ||
} | ||
set | ||
{ | ||
_run = value; | ||
} | ||
} | ||
|
||
public void Run() | ||
{ | ||
bool success; | ||
|
||
try | ||
{ | ||
// Get a fresh command from settings | ||
Command = Command.GetByCode(Code); | ||
|
||
// Run the command. | ||
success = Command.Run(); | ||
} | ||
catch (Exception e) | ||
{ | ||
Trace.WriteLine(new LogMessage("CommandSchedule - Run", "Cannot start Run Command: " + e.Message), LogType.Error.ToString()); | ||
success = false; | ||
} | ||
|
||
// Notify the state of the command (success or failure) | ||
using (xmds.xmds statusXmds = new xmds.xmds()) | ||
{ | ||
statusXmds.Url = ApplicationSettings.Default.XiboClient_xmds_xmds; | ||
statusXmds.NotifyStatusAsync(ApplicationSettings.Default.ServerKey, ApplicationSettings.Default.HardwareKey, "{\"lastCommandSuccess\":" + success + "}"); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.