コード例 #1
0
 private void RaisePortsChangedIfNecessary(SerialPortServiceEventType eventType)
 {
     lock (_lock)
     {
         string[] availableSerialPorts = GetPortNames();
         if (!_previousSerialPorts.SequenceEqual(availableSerialPorts))
         {
             _previousSerialPorts = availableSerialPorts;
             PortsChanged?.Invoke(this, new PortsChangedArgs(eventType, _previousSerialPorts));
         }
     }
 }
コード例 #2
0
 private void RaisePortsChangedIfNecessary(EventType eventType)
 {
     lock (_serialPortNames)
     {
         var availableSerialPorts = GetAvailableSerialPortNames();
         if (!_serialPortNames.SequenceEqual(availableSerialPorts))
         {
             _serialPortNames = availableSerialPorts;
             PortsChanged?.Invoke(this, new PortsChangedArgs(eventType, _serialPortNames));
         }
     }
 }
コード例 #3
0
 private static void RaisePortsChangedIfNecessary(EventType eventType)
 {
     lock (_serialPorts)
     {
         var availableSerialPorts = GetAvailableSerialPorts();
         if (!_serialPorts.SequenceEqual(availableSerialPorts))
         {
             var changes = eventType == EventType.Removal ? _serialPorts.Except(availableSerialPorts) : availableSerialPorts.Except(_serialPorts);
             _serialPorts = availableSerialPorts;
             PortsChanged?.Invoke(null, new PortsChangedArgs(eventType, _serialPorts, changes.ToArray()));
         }
     }
 }
コード例 #4
0
        public async Task Connect(string userName, string password, CancellationToken token)
        {
            if (ConnectionClosed.IsCancellationRequested || webSocket != null)
            {
                throw new Exception("Connection is already closed/opened");
            }

            Trace.WriteLine(Invariant($"Logging to {DeviceIP} with {sessionId}"));

            var            postUrl        = new Uri($"http://{DeviceIP}/login.cgi");
            string         formDataString = $"username={WebUtility.UrlEncode(userName)}&password={WebUtility.UrlEncode(password)}";
            HttpWebRequest request        = CreateFormWebRequest(postUrl, formDataString, "POST");

            string response = await ProcessRequest(request, token).ConfigureAwait(false);

            if (response.Contains("Invalid credentials"))
            {
                throw new Exception("Invalid Credentails.");
            }
            else if (response.Length > 0)
            {
                string errorMessage = Invariant($"Login Failed with {response}");
                throw new Exception(errorMessage);
            }

            Trace.TraceInformation(Invariant($"Logged to {DeviceIP} with {sessionId}"));
            await UpdateAllSensorData(token);

            List <SensorData> changed = new List <SensorData>();

            foreach (var data in GetSensorData())
            {
                changed.Add(data.Value.Clone());
            }
            PortsChanged?.Invoke(this, changed);

            var socketsUrl = $"ws://{DeviceIP}:7681/?c={sessionId}";

            webSocket = new WebSocket(socketsUrl, "mfi-protocol");
            webSocket.ReceiveBufferSize = 64 * 1024; // 64k
            webSocket.Opened           += WebSocket_Opened;
            webSocket.MessageReceived  += WebSocket_MessageReceived;
            webSocket.Error            += WebSocket_Error;
            webSocket.Closed           += WebSocket_Closed;
            webSocket.Open();
            Trace.WriteLine(Invariant($"Created WebSocket to {DeviceIP}"));
        }
コード例 #5
0
 private static void RaisePortsChangedIfNecessary(EventType eventType)
 {
     lock (_serialPorts) {
         var availableSerialPorts = GetAvailableSerialPorts();
         if (eventType == EventType.Insertion)
         {
             var added = availableSerialPorts.Except(_serialPorts).ToArray();
             _serialPorts = availableSerialPorts;
             PortsChanged?.Invoke(null, new PortsChangedArgs(eventType, added));
         }
         else if (eventType == EventType.Removal)
         {
             var removed = _serialPorts.Except(availableSerialPorts).ToArray();
             _serialPorts = availableSerialPorts;
             PortsChanged?.Invoke(null, new PortsChangedArgs(eventType, removed));
         }
     }
 }
コード例 #6
0
        private void CheckForNewPortsAsync()
        {
            IEnumerable <string> ports = SerialPort.GetPortNames().OrderBy(s => s);

            foreach (string comPort in ComPorts.ToList())
            {
                if (!ports.Contains(comPort))
                {
                    ComPorts.Remove(comPort);
                }
            }

            foreach (var port in ports.ToList())
            {
                if (!ComPorts.Contains(port))
                {
                    AddPort(port);
                }
            }
            PortsChanged?.Invoke(this, null);
        }
コード例 #7
0
        private void UpdateDelta(IEnumerable <SensorData> sensors)
        {
            List <SensorData> changed = new List <SensorData>();

            sensorDataMapLock.EnterWriteLock();
            try
            {
                foreach (var data in sensors)
                {
                    if (sensorDataMap.TryGetValue(data.Port, out var originalData))
                    {
                        originalData.ApplyDelta(data);
                        changed.Add(data.Clone());
                    }
                }
            }
            finally
            {
                sensorDataMapLock.ExitWriteLock();
            }

            PortsChanged?.Invoke(this, changed);
        }