-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed InterCOM system with advanced syntax. Created demo on how it wo…
…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
Showing
8 changed files
with
312 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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,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; | ||
} | ||
|
||
} | ||
} |
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,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(); | ||
} | ||
|
||
} | ||
} |
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,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; | ||
} | ||
|
||
|
||
|
||
} | ||
} |
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