Skip to content

Commit

Permalink
Fixed InterCOM system with advanced syntax. Created demo on how it wo…
Browse files Browse the repository at this point in the history
…rks and how to register, listen, interact with, and send messages amongst other stations.
  • Loading branch information
Evan Langlais authored and Evan Langlais committed Feb 28, 2017
1 parent 6464816 commit 6982ceb
Show file tree
Hide file tree
Showing 8 changed files with 312 additions and 2 deletions.
27 changes: 27 additions & 0 deletions Enigma/DebugForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 23 additions & 1 deletion Enigma/DebugForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Threading.Tasks;
using System.Windows.Forms;
using MetroFramework.Forms;
using EnigmaX.Classes;

namespace Enigma
{
Expand All @@ -17,10 +18,31 @@ public DebugForm()
{
InitializeComponent();
}

private Station station;
private void Form1_Load(object sender, EventArgs e)
{
station = new Station(StationTypeDef.host, "test");

}

private void InterCom_onReceive(ICMessage message, EventArgs e)
{
XMessageBox messageBox = new XMessageBox(message.messageId + ": From " + message.fromId + " To " + message.toId, message.message, "Ok");
messageBox.showMessage();
}

private void metroButton1_Click(object sender, EventArgs e)
{
station.registerStation();
station.interCom.onReceive += InterCom_onReceive;
station.interCom.listen();
}

private void metroButton2_Click(object sender, EventArgs e)
{
if (station.Registered) {
station.interCom.send("%HOST%|-test", "This is a test dummy ping");
}
}
}
}
6 changes: 6 additions & 0 deletions Enigma/Enigma.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\EnigmaX\EnigmaX.csproj">
<Project>{9c277a65-39fe-466d-8ae9-4b3a12a5e61e}</Project>
<Name>EnigmaX</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
2 changes: 1 addition & 1 deletion EnigmaX/Classes/DBConnect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public DBConnect()

private void Initialize()
{
connection = new MySqlConnection("Server=4156.hostgator.com; database=ttech_enigma; UID=ttech_enigma; password=JaQE7eKrY4Rr");
connection = new MySqlConnection("Server=gator4156.hostgator.com; database=ttech_enigma; UID=ttech_enigma; password=JaQE7eKrY4Rr");
}

private bool OpenConnection()
Expand Down
32 changes: 32 additions & 0 deletions EnigmaX/Classes/ICMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EnigmaX.Classes
{
public class ICMessage
{

public string message;
public string fromId;
public string toId;
public int messageId;

public ICMessage(int _messageId, string _toId, string _fromId, string _message) {
message = _message;
toId = _toId;
fromId = _fromId;
messageId = _messageId;
}

public ICMessage(string _toId, string _fromId, string _message)
{
message = _message;
toId = _toId;
fromId = _fromId;
}

}
}
162 changes: 162 additions & 0 deletions EnigmaX/Classes/Intercom.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.ComponentModel;

namespace EnigmaX.Classes
{
public class Intercom
{

private string stationID;
private StationTypeDef stationType;
public event ComHandler onReceive;
public delegate void ComHandler(ICMessage message, EventArgs e);
public bool Registered = false;
public bool running = false;

public Intercom(string systemid, StationTypeDef type) {
stationID = systemid;
stationType = type;
Registered = register();
}

public bool register()
{
DBConnect db = new DBConnect();
List<Dictionary<string, string>> results = db.ReadCommand("SELECT * FROM Stations WHERE stationid = '" + stationID + "' LIMIT 1", "type");
if (results.Count == 0)
{
db.WriteCommand("INSERT INTO Stations (stationid, type) VALUES ('" + stationID + "','" + stationType.ToString() + "')");
return true;
}
else {
if (results[0]["type"].Equals(stationType.ToString()))
{
return true;
}
else {
//throw error or something
return false;
}
}
}

public void listen() {
if (Registered)
{
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += Worker_DoWork;
running = true;
worker.RunWorkerAsync();
}
}

public void stop() {
running = false;
}

public void send(string address, string message) {
string[] addresslist = parseAddresses(address);
foreach (string adr in addresslist) {
ICMessage messageObj = new ICMessage(adr, stationID, message);
rawSend(messageObj);
}
}

private void rawSend(ICMessage message) {
DBConnect db = new DBConnect();
db.WriteCommand("INSERT INTO InterCOM (fromid, toid, message) VALUES ('" + message.fromId + "', '" + message.toId + "', '" + message.message + "')");
}

private void deletePacket(string id) {
DBConnect db = new DBConnect();
db.WriteCommand("DELETE FROM InterCOM WHERE id = '" + id + "' limit 1");
}

private void Worker_DoWork(object sender, DoWorkEventArgs e)
{
Console.WriteLine("starting " + stationID + "...");
DBConnect db = new DBConnect();
while (running) {
Console.WriteLine("Checking for " + stationID + "...");
List<Dictionary<string, string>> results = db.ReadCommand("SELECT * FROM InterCOM WHERE toid = '" + stationID + "'", "id", "toid", "fromid", "message");
if (results.Count > 0) {
//we have a message addressed to us
Console.WriteLine("found one!");
for (int i = 0; i < results.Count; i++) {
ICMessage recieved = new ICMessage(Convert.ToInt16(results[i]["id"]), results[i]["toid"], results[i]["fromid"], results[i]["message"]);
deletePacket(results[i]["id"]);
onReceive(recieved, null);
}
}
Thread.Sleep(1000);
}
}

private string[] parseAddresses(string to) {
//format will be address|-address|%specialaddress%
List<string> addresses = new List<string>();
foreach (string address in to.Split('|')) {
if (!address[0].Equals('-'))
{
if (address.Contains("%"))
{
addresses.AddRange(getSpecialAddresses(address));
}
else
{
addresses.Add(address);
}
} else {
string fixedAdr = address.Substring(1);
if (fixedAdr.Contains("%"))
{
string[] addrs = getSpecialAddresses(fixedAdr);
foreach (string removeadress in addrs) {
addresses.Remove(removeadress);
}
}
else
{
addresses.Remove(fixedAdr);
}
}
}
return addresses.ToArray();
}

private string[] getSpecialAddresses(string address) {
List<string> addresses = new List<string>();
DBConnect db = new DBConnect();
List<Dictionary<string, string>> results = null;
switch (address)
{
case "%CHEF%":
results = db.ReadCommand("SELECT stationid FROM Stations WHERE type='chef'", "stationid");
break;
case "%HOST%":
results = db.ReadCommand("SELECT stationid FROM Stations WHERE type='host'", "stationid");
break;
case "%WAITER%":
results = db.ReadCommand("SELECT stationid FROM Stations WHERE type='waiter'", "stationid");
break;
case "%ADMIN%":
results = db.ReadCommand("SELECT stationid FROM Stations WHERE type='admin'", "stationid");
break;
default:
break;
}
if (results != null) {
foreach (Dictionary<string, string> row in results) {
addresses.Add(row["stationid"]);
}
}
return addresses.ToArray();
}

}
}
58 changes: 58 additions & 0 deletions EnigmaX/Classes/Station.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EnigmaX.Classes
{
public enum StationTypeDef { chef, waiter, admin, host };
public class Station
{
private string stationId = "";
private StationTypeDef stationType;
public Intercom interCom;
private bool _registered;

public string StationID {
get {
return stationId;
}
}

public bool Registered {
get {
return _registered;
}
}

public StationTypeDef StationType
{
get
{
return stationType;
}
}

public Station(StationTypeDef type, string stationid) {
stationType = type;
if (stationid.Contains("%") || stationid.Contains("-") || stationid.Contains("|"))
{
throw new Exception("StationID cannot contain %, -, or | special characters");
}
else
{
stationId = stationid;
}
}

public bool registerStation() {
interCom = new Intercom(stationId, stationType);
_registered = interCom.register();
return _registered;
}



}
}
3 changes: 3 additions & 0 deletions EnigmaX/EnigmaX.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Classes\DBConnect.cs" />
<Compile Include="Classes\ICMessage.cs" />
<Compile Include="Classes\Intercom.cs" />
<Compile Include="Classes\Station.cs" />
<Compile Include="Classes\XMessageBox.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Forms\XMessageBoxForm.cs">
Expand Down

0 comments on commit 6982ceb

Please sign in to comment.