diff --git a/Fan.cs b/Fan.cs index eb86d0d..089de28 100644 --- a/Fan.cs +++ b/Fan.cs @@ -1,9 +1,6 @@ using System; -using System.Collections.Generic; -using System.Linq; +using System.IO.MemoryMappedFiles; using System.Net.Sockets; -using System.Text; -using System.Threading.Tasks; namespace FanPlugin.Wrapper { @@ -11,6 +8,7 @@ public class Fan { private static String playModelSingle = "36"; + private static String playLoop = "37"; private static String playFile = "40"; private static String DEFAULT_NO_DATA_LENTH = "abc"; private static String DEFAULT_HAS_2_DATA_LENTH = "abe"; @@ -19,12 +17,49 @@ public class Fan private static String contentDefaultValue = "00"; public static String DEFUALT_SERVER_IP = "192.168.4.1"; public static int DEFUALT_SERVER_PORT = 5233; + private static String getFileList = "38"; + private static String playlast = "33"; + private static String powerOff = "94"; + private static String powerOn = "95"; + + public struct SharedData { + public int actual { get; set; } + public int last { get; set; } + } + + + + public string playVideoWithId(String videoID) { + + + SharedMemory shmem = new SharedMemory("Shem",32); + + if (!shmem.Open()) return "fail"; + + SharedData data = new SharedData(); + + // Read from shared memory + data = shmem.Data; + + + // Change some data + data.last = data.actual; + data.actual = int.Parse(videoID); + + + // Write back to shared memory + shmem.Data = data; + + // Close shared memory + shmem.Close(); String command = "c31c" + playFile + DEFAULT_HAS_2_DATA_LENTH + intTo2Str(int.Parse(videoID)) + end; - return connect(command); + connect(command); + + return "NEW ID = " + data.actual + " Old ID = " + data.last; } public String selectSingleVideoPlaybackMode() { @@ -33,6 +68,65 @@ public String selectSingleVideoPlaybackMode() { return connect(command); } + public String selectLoopVideoPlaybackMode() + { + + String command = "c31c" + playLoop + DEFAULT_NO_DATA_LENTH + end; + return connect(command); + } + + public String getFileListFromFan() { + String command = "c31c" + getFileList + DEFAULT_NO_DATA_LENTH + end; + return connectRead(command); + } + + public String getApiVersionInfo() { + String command = "c31c" + sendAPInfo + DEFAULT_NO_DATA_LENTH + end; + return connectRead(command); + } + + public String playLastFromFan() { + + String command = "c31c" + playlast + DEFAULT_NO_DATA_LENTH + end; + return connect(command); + } + + public String sendPowerOn() + { + + String command = "c31c" + powerOn + DEFAULT_NO_DATA_LENTH + end; + return connect(command); + } + + public String sendPowerOff() + { + + String command = "c31c" + powerOff + DEFAULT_NO_DATA_LENTH + end; + return connect(command); + } + + public String playOldFromFan() { + + SharedMemory shmem = new SharedMemory("Shem", 32); + + if (!shmem.Open()) return "fail"; + + SharedData data = new SharedData(); + + // Read from shared memory + data = shmem.Data; + + // Write back to shared memory + shmem.Data = data; + + // Close shared memory + shmem.Close(); + + String command = "c31c" + playFile + DEFAULT_HAS_2_DATA_LENTH + intTo2Str(data.last) + end; + connect(command); + return connect("Old ID = " + data.last); + } + private static String intTo2Str(int i) { if (i >= 0 && i < 10) @@ -71,7 +165,7 @@ private static String connect(String message) // Send the message to the connected TcpServer. stream.Write(data, 0, data.Length); - Console.WriteLine("Sent: {0}", message); + // Console.WriteLine("Sent: {0}", message); // Receive the TcpServer.response. @@ -92,20 +186,78 @@ private static String connect(String message) } catch (ArgumentNullException e) { - Console.WriteLine("ArgumentNullException: {0}", e); + // Console.WriteLine("ArgumentNullException: {0}", e); return e.Message; } catch (SocketException e) { - Console.WriteLine("SocketException: {0}", e); + // Console.WriteLine("SocketException: {0}", e); return e.Message; } - Console.WriteLine("\n Press Enter to continue..."); - Console.Read(); + // Console.WriteLine("\n Press Enter to continue..."); + // Console.Read(); return "Command successfull"; } + + private static String connectRead(String message) + { + try + { + // Create a TcpClient. + // Note, for this client to work you need to have a TcpServer + // connected to the same address as specified by the server, port + // combination. + Int32 port = DEFUALT_SERVER_PORT; + System.Net.Sockets.TcpClient client = new TcpClient(DEFUALT_SERVER_IP, port); + + // Translate the passed message into ASCII and store it as a Byte array. + Byte[] data = System.Text.Encoding.ASCII.GetBytes(message); + + // Get a client stream for reading and writing. + // Stream stream = client.GetStream(); + + NetworkStream stream = client.GetStream(); + + // Send the message to the connected TcpServer. + stream.Write(data, 0, data.Length); + + // Console.WriteLine("Sent: {0}", message); + + // Receive the TcpServer.response. + + // Buffer to store the response bytes. + data = new Byte[256]; + + // String to store the response ASCII representation. + String responseData = String.Empty; + + // Read the first batch of the TcpServer response bytes. + Int32 bytes = stream.Read(data, 0, data.Length); + responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes); + // Console.WriteLine("Received: {0}", responseData); + + // Close everything. + stream.Close(); + client.Close(); + + return responseData; + } + catch (ArgumentNullException e) + { + // Console.WriteLine("ArgumentNullException: {0}", e); + return e.Message; + } + catch (SocketException e) + { + // Console.WriteLine("SocketException: {0}", e); + return e.Message; + } + + } } + + } diff --git a/FanPlugin.Wrapper.csproj b/FanPlugin.Wrapper.csproj index a27e2ba..f3f7d53 100644 --- a/FanPlugin.Wrapper.csproj +++ b/FanPlugin.Wrapper.csproj @@ -42,6 +42,7 @@ + diff --git a/NetSharedMemory.cs b/NetSharedMemory.cs new file mode 100644 index 0000000..02d01b9 --- /dev/null +++ b/NetSharedMemory.cs @@ -0,0 +1,71 @@ +using System.IO.MemoryMappedFiles; +using System.Threading; + +namespace FanPlugin.Wrapper +{ + + public class SharedMemory where T : struct + { + // Constructor + public SharedMemory(string name, int size) + { + smName = name; + smSize = size; + } + + // Methods + public bool Open() + { + try + { + // Create named MMF + mmf = MemoryMappedFile.CreateOrOpen(smName, smSize); + + // Create accessors to MMF + accessor = mmf.CreateViewAccessor(0, smSize, + MemoryMappedFileAccess.ReadWrite); + + // Create lock + smLock = new Mutex(true, "SM_LOCK", out locked); + } + catch + { + return false; + } + + return true; + } + + public void Close() + { + accessor.Dispose(); + mmf.Dispose(); + smLock.Close(); + } + + public T Data + { + get + { + T dataStruct; + accessor.Read(0, out dataStruct); + return dataStruct; + } + set + { + smLock.WaitOne(); + accessor.Write(0, ref value); + smLock.ReleaseMutex(); + } + } + + // Data + private string smName; + private Mutex smLock; + private int smSize; + private bool locked; + private MemoryMappedFile mmf; + private MemoryMappedViewAccessor accessor; + } + +}