예제 #1
0
        protected static bool _useSecure = false; //

        #endregion Fields

        #region Methods

        //Add a new device to the list of currently connected devices
        private static Device AddDevice(IWebSocketConnection conn, Request requestObject)
        {
            //Create the device
              Device device = new Device()
              {
            AccountName = requestObject.AccountName,
            ConnectionGuid = conn.ConnectionInfo.Id,
            FriendlyName = requestObject.FriendlyName,
            Id = Guid.Empty,
            IP = conn.ConnectionInfo.ClientIpAddress,
            Mode = requestObject.CurrentOperationMode
              };

              //Use the assigned id if the client is supplying one
              if (requestObject.DeviceGuid != null)
            device.Id = new Guid(requestObject.DeviceGuid); //We have a GUID so update to use that for this connection

              //Work out the if we still need a device id and if we do then make one up.
              if (device.Id == Guid.Empty) //If the device does not have a guid then request a new one
              {
            Guid generatedGuid = Guid.NewGuid();
            device.Id = generatedGuid;
              }//if

              //Now actually add the Id to the list
              _connectedDevices.TryAdd(device.Id.ToString(), device);

              //If we wish to send notifications of on / offline devices then send them now
              if (_deviceNotifications)
            NotifyDevices(true,device);

              //Return the device for use in reading
              return device;
        }
예제 #2
0
        //Process a request for a new devcie ID from a client and handle appropiately
        private static Request ProcessNewDeviceRequest(IWebSocketConnection conn, Request requestObject)
        {
            //See if we actually have this device already if so we can just retrun its Id to it
              //If Id supplied matches a record then we have a match
              var devices = _connectedDevices.Where(d => d.Key == requestObject.DeviceGuid || d.Value.ConnectionGuid == conn.ConnectionInfo.Id).ToList(); //Find this connection
              Device device = new Device(); //Device stub for response

              if (devices.Count == 0) //Check if we have the device in our lists
              {
            //Add the device to our connected devices list
            device = AddDevice(conn, requestObject);
              }//if
              else
              {
            //Return the device data we already have on record
            device = devices.FirstOrDefault().Value;
              }//else

              //Get a list of devices on this account to pass back to the caller
              //Hosts
              var hosts = _connectedDevices.Values.Where(h => h.AccountName == requestObject.AccountName && h.Mode == DeviceMode.Host && h.Id != Guid.Empty).ToList();
              //Remotes
              var remotes = _connectedDevices.Values.Where(r => r.AccountName == requestObject.AccountName && r.Mode == DeviceMode.Remote && r.Id != Guid.Empty).ToList();

              //Send back a response to this device with its GUID
              Request idResponse = new Request()
              {
            AccountName = requestObject.AccountName,
            FriendlyName = requestObject.FriendlyName,
            CurrentOperationMode = requestObject.CurrentOperationMode,
            Action = RequestAction.IdResponse,
            DeviceGuid = device.Id.ToString(),
            IncludeAllHosts = false,
            IncludeAllRemotes = false,
            RelatedHostIds = Device.GetDeviceIds(hosts),
            RelatedRemoteIds = Device.GetDeviceIds(remotes)
              };
              return idResponse;
        }
예제 #3
0
        private static void NotifyDevices(bool state, Device device)
        {
            Request notification = new Request
              {
            AccountName = device.AccountName,
            Action = state ? RequestAction.DeviceOnline : RequestAction.DeviceOffline,
            CurrentOperationMode = device.Mode,
            DeviceGuid = device.Id.ToString(),
            FriendlyName = device.FriendlyName
              };
              var interestedDevices = _connectedDevices.Where(d => d.Value.AccountName == device.AccountName).ToList();
              var interestedSockets = _connectedSockets.Where(s => (interestedDevices.Select(d => d.Value.ConnectionGuid.ToString())).Contains(s.Key)).ToList();

              //var x = interestedDevices
              //  .Join(_connectedSockets, d => d.Value.ConnectionGuid.ToString(), s => s.Key, (d, s) => s)
              //  .ToList();

              var notificationJSON = JsonConvert.SerializeObject(notification);
              //Send out
              SendMessages(interestedSockets, notificationJSON);
        }