Пример #1
0
        /// <summary>
        /// Fires on connection with the hub
        /// </summary>
        public override Task OnConnected()
        {
            using(var ctx = new TurkRContext())
            {
                var client = ctx.Clients.FirstOrDefault(c => c.ConnectionId == Context.ConnectionId);
                if(client == null)
                {
                    // Collect client info
                    var httpContext = Context.Request.GetHttpContext();
                    string ip = httpContext.Request.ServerVariables["REMOTE_ADDR"];
                    string browser = httpContext.Request.Browser.Browser + " " + httpContext.Request.Browser.Version;
                    var transport = Context.QueryString["transport"];
                    var page = Context.QueryString["page"];
                    var hubClient = Context.QueryString["client"];

                    bool isMonitor = false;
                    if(!string.IsNullOrEmpty(hubClient))
                    {
                        if (hubClient == "monitor") isMonitor = true;
                    }

                    if(isMonitor)
                    {
                        Groups.Add(Context.ConnectionId, "monitor");
                    }
                    else
                    {
                        Groups.Add(Context.ConnectionId, "client");
                    }

                    var newClient = new Client
                    {
                        ConnectionId = Context.ConnectionId,
                        IsConnected = true,
                        IsMonitor = isMonitor,
                        Ip = ip,
                        Transport = transport,
                        UserAgent = browser,
                        StartDate = DateTime.Now,
                        EndDate = DateTime.Now
                    };

                    ctx.Clients.Add(newClient);
                    ctx.SaveChanges();
                }
                else
                {
                    client.IsConnected = true;
                    client.StartDate = DateTime.Now;
                    ctx.SaveChanges();
                }
            }

            HubUpdate();
            return base.OnConnected();
        }
Пример #2
0
 /// <summary>
 /// Signup of worker to a session
 /// </summary>
 /// <param name="sessionId"></param>
 /// <param name="workerId"></param>
 private void Signup(int sessionId, string workerId)
 {
     using(var ctx = new TurkRContext())
     {
         var session = ctx.Sessions.FirstOrDefault(s => s.SessionId == sessionId);
         if(session != null)
         {
             var worker = ctx.Workers.FirstOrDefault(w => w.WorkerId == workerId);
             if(worker != null)
             {
                 var a = new Appointment { SessionId = session.SessionId, WorkerId = worker.WorkerId };
                 ctx.Appointments.Add(a);
                 ctx.SaveChanges();
             }
         }
     }
 }
Пример #3
0
 /// <summary>
 /// Signout of a worker from a session
 /// </summary>
 /// <param name="sessionId"></param>
 /// <param name="workerId"></param>
 private void Signout(int sessionId, string workerId)
 {
     using (var ctx = new TurkRContext())
     {
         var appointment = ctx.Appointments.FirstOrDefault(a => a.SessionId == sessionId & a.WorkerId == workerId);
         if(appointment != null)
         {
             ctx.Appointments.Remove(appointment);
             ctx.SaveChanges();
         }
     }
 }
Пример #4
0
        /// <summary>
        /// Create a HIT from a stored template.
        /// </summary>
        /// <param name="hitId">The Hit Id</param>
        public void CreateHit(int hitId)
        {
            using (var ctx = new TurkRContext())
            {
                var hit = ctx.Hits.FirstOrDefault(h => h.HitId == hitId);
                if (hit != null)
                {
                    SimpleClient myClient = new SimpleClient(_mturkConfig);

                    // Setup qualifications
                    List<QualificationRequirement> qualifications = new List<QualificationRequirement>();

                    // First register the HitType
                    string hitTypeId = myClient.RegisterHITType(hit.Title, hit.Description, (long)hit.AutoApprovalDelay, (long)hit.Duration, (decimal)hit.Reward, hit.Keywords, qualifications);

                    // Define the external question
                    ExternalQuestion eq = new ExternalQuestion();
                    eq.ExternalURL = "https://www.descil.ethz.ch/apps/turkr/start/" + hit.HitCode;
                    eq.FrameHeight = "800";

                    // More definitions
                    string requesterAnnotation = hit.HitCode;
                    string[] responseGroup = null;

                    // Creat the HIT
                    var amtHit = myClient.CreateHIT(hitTypeId, hit.Title, hit.Description, hit.Keywords, eq, (decimal)hit.Reward, (long)hit.Duration, (long)hit.AutoApprovalDelay, (long)hit.Lifetime, hit.Assignments, requesterAnnotation, qualifications, responseGroup);

                    hit.AmtHitId = amtHit.HITId;
                    hit.CreationTime = DateTime.UtcNow;
                    ctx.SaveChanges();
                }
            }
        }
Пример #5
0
 /// <summary>
 /// Fires on reconnection with the hub
 /// </summary>
 public override Task OnReconnected()
 {
     using (var ctx = new TurkRContext())
     {
         var client = ctx.Clients.FirstOrDefault(c => c.ConnectionId == Context.ConnectionId);
         if (client != null)
         {
             client.StartDate = DateTime.Now;
             client.IsConnected = true;
             ctx.SaveChanges();
         }
     }
     HubUpdate();
     return base.OnReconnected();
 }
Пример #6
0
 /// <summary>
 /// Fires on disconnection with the hub
 /// </summary>
 public override Task OnDisconnected()
 {
     using (var ctx = new TurkRContext())
     {
         var client = ctx.Clients.FirstOrDefault(c => c.ConnectionId == Context.ConnectionId);
         if (client != null)
         {
             if(client.IsMonitor)
             {
                 //client.EndDate = DateTime.Now;
                 //client.IsConnected = false;
                 ctx.Clients.Remove(client);
                 ctx.SaveChanges();
             }
             else
             {
                 //client.EndDate = DateTime.Now;
                 //client.IsConnected = false;
                 ctx.Clients.Remove(client);
                 ctx.SaveChanges();
             }
         }
     }
     HubUpdate();
     return base.OnDisconnected();
 }