コード例 #1
0
        /// <summary>
        /// After connection, PhidgetServer and PhidgetClient must call AfterConnect.
        /// Put them in cache
        /// If of type PhidgetServer
        /// Signal the new PhidgetServer connection to all PhidgetClient.
        /// If of type PhidgetClient
        /// Join it the PhidgetClient group.
        /// </summary>
        /// <param name="signalRClient">PhidgetClient or PhidgetServer</param>
        public void AfterConnect(SignalRClientDetails signalRClient)
        {
            MyTrace(string.Format("AfterConnect({0})", signalRClient.MachineName));

            // Proceed SignalRClient initilization began on client side and cache it in a list.
            var caller = new SignalRClientDetails
            {
                ConnectionId = Context.ConnectionId,
                IPAddress = signalRClient.IPAddress,
                MachineName = signalRClient.MachineName,
            };
            switch (signalRClient.SignalRClientTypeConnected)
            {
                case SignalRClientType.PhidgetServer:
                    // A PhidgetServer had PhidgetModule attached to USB port.
                    caller.PhidgetModuleTypeAttached = signalRClient.PhidgetModuleTypeAttached;
                    // Add signalRClient to ConnectedPhidgetServers list.
                    lock (ConnectedPhidgetServers)
                    {
                        ConnectedPhidgetServers.Add(caller);
                    }
                    // Signal the new PhidgetServer connection to all PhidgetClient.
                    MyTrace(string.Format("onNewPhidgetServerConnect: {0} / {1}", SignalRClientType.PhidgetClient, caller.MachineName));
                    Clients.Group(SignalRClientType.PhidgetClient).onNewPhidgetServerConnect(caller);
                    break;
                case SignalRClientType.PhidgetClient:
                    // Add signalRClient to ConnectedPhidgetClients list.
                    lock (ConnectedPhidgetClients)
                    {
                        ConnectedPhidgetClients.Add(caller);
                    }
                    // Join signalRClient to "PhidgetClient" group.
                    JoinPhidgetClientGroup();
                    // Send the ConnectedPhidgetServers list to this new PhidgetClient.
                    if (ConnectedPhidgetServers.Count != 0)
                    {
                        MyTrace(string.Format("showPhidgetServerList: {0}", ConnectedPhidgetServers[0].MachineName));
                        Clients.Caller.showPhidgetServerList(ConnectedPhidgetServers);
                    }
                    break;
                default:
                    break;
            }
        }
コード例 #2
0
        /// <summary>
        /// Called by PhidgetServer to signal changes to all PhidgetClient attached to him.
        /// </summary>
        /// <param name="phidgetServer"></param>
        /// <param name="data"></param>
        public void SendToPhidgetClients(SignalRClientDetails phidgetServer, dynamic data)
        {
            MyTrace(string.Format("SendToPhidgetClients({0},{1})", phidgetServer.MachineName, data.ToString()));

            // Retrieve PhidgetServer object.
            //var fromPhidgetServer = ConnectedPhidgetServers.Find(pc => pc.ConnectionId == phidgetServerID);
            // Send data to the group.
            MyTrace(string.Format("show: {0} / {1}", phidgetServer.MachineName, data.ToString()));
            Clients.Group(phidgetServer.MachineName).show(data);
        }
コード例 #3
0
        /// <summary>
        /// TODO: If convenient, remove the PhidgetClient caller from phidgetServerSelectedOld group.
        /// Join the caller to the phidgetServerSelected Group.
        /// Ask to phidgetServerSelected to push data to the caller.
        /// </summary>
        /// <param name="phidgetServerSelected"></param>
        public void OnPhidgetServerSelected(SignalRClientDetails phidgetServerSelected)
        {
            MyTrace(string.Format("OnPhidgetServerSelected({0})", phidgetServerSelected.MachineName));

            // Find caller PhidgetClient object.
            var phidgetClientIndex = ConnectedPhidgetClients.FindIndex(cpc => cpc.ConnectionId == Context.ConnectionId);
            // If caller is yet in a PhidgetServer group, remove it from that group.
            if (ConnectedPhidgetClients[phidgetClientIndex].PhidgetServerGroupAttachement != null)
            {
                // We remove the PhidgetClient caller from it's old phidgetServer group.
                LeavePhidgetServerGroup(ConnectedPhidgetClients[phidgetClientIndex].PhidgetServerGroupAttachement);
                // Remove PhidgetServer group for the caller.
                ConnectedPhidgetClients[phidgetClientIndex].PhidgetServerGroupAttachement = null;
                MyTrace(string.Format("LeavePhidgetServerGroup: {0} / {1}", phidgetServerSelected.MachineName, Context.ConnectionId));

            }
            // Store PhidgetServer group for the caller.
            ConnectedPhidgetClients[phidgetClientIndex].PhidgetServerGroupAttachement = phidgetServerSelected.MachineName;
            // TODO: We join the caller to phidgetServerSelected group.
            JoinPhidgetServerGroup(phidgetServerSelected.MachineName);
            // Ask to phidgetServerSelected to push data to the caller.
            // We call phidgetServerSelected get method to retreive data.
            // get must then call SendToPhidgetClient to push data to this PhidgetClient.
            MyTrace(string.Format("get: {0} / {1}", phidgetServerSelected.ConnectionId, Context.ConnectionId));
            Clients.Client(phidgetServerSelected.ConnectionId).get(Context.ConnectionId);
        }
コード例 #4
0
        /// <summary>
        /// PhidgetClient call SetByPhidgetClient to set phidgetServer outputs.
        /// PhidgetServer must signal changes to all PhidgetClient attached to him.
        /// </summary>
        public void SetByPhidgetClient(SignalRClientDetails phidgetServer, dynamic data)
        {
            MyTrace(string.Format("SetByPhidgetClient({0},{1})", phidgetServer, data.ToString()));

            // PhidgetMiddlewareServer call phidgetServer set method to set phidgetServer outputs.
            // PhidgetServer must then call SendToPhidgetClients to signal changes to all PhidgetClient attached to him.
            MyTrace(string.Format("set: {0} / {1}", phidgetServer.ConnectionId, data.ToString()));
            Clients.Client(phidgetServer.ConnectionId).set(data);
        }
コード例 #5
0
        /// <summary>
        /// Init PhidgetServer Informations.
        /// </summary>
        private void InitPhidgetServerInformations()
        {
            Console.WriteLine("InitPhidgetServerInformations");

            _phidgetServerDetails = new SignalRClientDetails
            {
                MachineName = System.Environment.MachineName,
                IPAddress = GetMyIpAddress(),
                SignalRClientTypeConnected = SignalRClientType.PhidgetServer,
                PhidgetModuleTypeAttached = _phidgetModuleType,
            };
        }