Skip to content

Commit

Permalink
on / off Command added
Browse files Browse the repository at this point in the history
  • Loading branch information
buzzibaer committed Jun 3, 2023
1 parent d577d37 commit 7df17f9
Show file tree
Hide file tree
Showing 3 changed files with 234 additions and 10 deletions.
172 changes: 162 additions & 10 deletions Fan.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
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
{
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";
Expand All @@ -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<SharedData> shmem = new SharedMemory<SharedData>("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() {
Expand All @@ -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<SharedData> shmem = new SharedMemory<SharedData>("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)
Expand Down Expand Up @@ -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.

Expand All @@ -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;
}

}
}


}
1 change: 1 addition & 0 deletions FanPlugin.Wrapper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Fan.cs" />
<Compile Include="NetSharedMemory.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup />
Expand Down
71 changes: 71 additions & 0 deletions NetSharedMemory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System.IO.MemoryMappedFiles;
using System.Threading;

namespace FanPlugin.Wrapper
{

public class SharedMemory<T> 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<T>(0, out dataStruct);
return dataStruct;
}
set
{
smLock.WaitOne();
accessor.Write<T>(0, ref value);
smLock.ReleaseMutex();
}
}

// Data
private string smName;
private Mutex smLock;
private int smSize;
private bool locked;
private MemoryMappedFile mmf;
private MemoryMappedViewAccessor accessor;
}

}

0 comments on commit 7df17f9

Please sign in to comment.