Skip to content

Instantly share code, notes, and snippets.

@romichi
Last active January 3, 2016 00:39
Show Gist options
  • Select an option

  • Save romichi/8384388 to your computer and use it in GitHub Desktop.

Select an option

Save romichi/8384388 to your computer and use it in GitHub Desktop.
視線でマウスカーソルを操作する.右目を閉じるとEnterを押す
using System;
using System.Net.Sockets;
using System.Windows.Forms;
using TETCSharpClient;
using TETCSharpClient.Data;
using Newtonsoft.Json.Linq;
public class GazePoint : IGazeUpdateListener {
private TcpClient socket;
private System.Threading.Thread incomingThread;
private System.Timers.Timer timerHeartbeat;
public event EventHandler<ReceivedDataEventArgs> OnData;
SendKey sendKey = new SendKey();
public GazePoint() {
GazeManager.Instance.Activate(1, GazeManager.ClientMode.Push);
GazeManager.Instance.AddGazeListener(this);
}
public void OnGazeUpdate(GazeData gazeData) {
double gX = gazeData.SmoothedCoordinates.X;
double gY = gazeData.SmoothedCoordinates.Y;
}
public void OnCalibrationStateChanged(bool val) { }
public void OnScreenIndexChanged(int number) { }
public bool Connect(string host, int port) {
try {
socket = new TcpClient(host, port);
}
catch (Exception ex) {
Console.Out.WriteLine("Error connecting: " + ex.Message);
return false;
}
// Send the obligatory connect request message
string REQ_CONNECT = "{\"values\":{\"push\":true,\"version\":1},\"category\":\"tracker\",\"request\":\"set\"}";
Send(REQ_CONNECT);
// Lauch a seperate thread to parse incoming data
incomingThread = new System.Threading.Thread(ListenerLoop);
incomingThread.Start();
// Start a timer that sends a heartbeat every 250ms.
// The minimum interval required by the server can be read out
// in the response to the initial connect request.
string REQ_HEATBEAT = "{\"category\":\"heartbeat\",\"request\":null}";
timerHeartbeat = new System.Timers.Timer(300);
timerHeartbeat.Elapsed += delegate { Send(REQ_HEATBEAT); };
timerHeartbeat.Start();
return true;
}
private void Send(string message) {
if (socket != null && socket.Connected) {
System.IO.StreamWriter writer = new System.IO.StreamWriter(socket.GetStream());
writer.WriteLine(message);
writer.Flush();
}
}
private void ListenerLoop() {
System.IO.StreamReader reader = new System.IO.StreamReader(socket.GetStream());
bool isRunning = true;
while (isRunning) {
string response = string.Empty;
try {
response = reader.ReadLine();
JObject jObject = JObject.Parse(response);
Packet p = new Packet();
//p.rawData = json;
p.category = (string)jObject["category"];
p.request = (string)jObject["request"];
p.statusCode = (string)jObject["statuscode"];
JToken values = jObject.GetValue("values");
if (values != null) {
// sanitation
p.values = values.ToString().Replace("\r\n", "");
// 視線のx,y座標の取得
JObject j = JObject.Parse(p.values);
int x = (int)j["frame"]["avg"]["x"];
int y = (int)j["frame"]["avg"]["y"];
//Console.WriteLine("x:" + x + " y:" + y);
// 左目
int leftX = (int)j["frame"]["lefteye"]["raw"]["x"];
// 右目
int rightX = (int)j["frame"]["righteye"]["raw"]["x"];
// マウスの移動
if (x != 0 && y != 0) {
Cursor.Position = new System.Drawing.Point(x, y);
}
if (leftX != 0 && rightX == 0) {
Console.WriteLine("右目閉じてる");
sendKey.Send(Keys.Enter, false);
}
if (leftX == 0 && rightX != 0) {
Console.WriteLine("左目閉じてる");
}
}
// Raise event with the data
if (OnData != null)
OnData(this, new ReceivedDataEventArgs(p));
}
catch (Exception ex){
Console.Out.WriteLine("Error while reading response: " + ex.Message);
}
}
}
static void Main(string[] args) {
Console.WriteLine("Start");
GazePoint gazePoint = new GazePoint();
bool canConnect = gazePoint.Connect("localhost", 6555);
if (!canConnect) {
Console.WriteLine("接続に失敗");
return;
}
}
}
public class Packet {
public string time = DateTime.UtcNow.Ticks.ToString();
public string category = string.Empty;
public string request = string.Empty;
public string statusCode = string.Empty;
public string values = string.Empty;
public string rawData = string.Empty;
public Packet() { }
}
public class ReceivedDataEventArgs : EventArgs {
private Packet packet;
public ReceivedDataEventArgs(Packet _packet) {
this.packet = _packet;
}
public Packet Packet {
get { return packet; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment