private static void RaisePortsChangedIfNecessary(EventType eventType)
        {
            try
            {
                lock (_serialPorts)
                {
                    var availableSerialPorts = GetAvailablePorts();
                    switch (eventType)
                    {
                    case (EventType.Insertion):
                    {
                        var added = availableSerialPorts.Except(_serialPorts).ToArray();
                        _serialPorts = availableSerialPorts;
                        PortsChanged.Raise(null, new PortsChangedArgs(eventType, added));
                        break;
                    }

                    case (EventType.Removal):
                    {
                        var removed = _serialPorts.Except(availableSerialPorts).ToArray();
                        _serialPorts = availableSerialPorts;
                        PortsChanged.Raise(null, new PortsChangedArgs(eventType, removed));
                        break;
                    }
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }
예제 #2
0
        private static void RaisePortsChangedIfNecessary(EventType eventType)
        {
            lock (SerialPorts)
            {
                var availableSerialPorts         = GetAvailableSerialPorts();
                IEnumerable <string> uniqueItems = availableSerialPorts.Distinct <string>();

                if (eventType == EventType.Insertion)
                {
                    if (!SerialPorts.SequenceEqual(uniqueItems))
                    {
                        var added = uniqueItems.Except(SerialPorts).ToArray();
                        SerialPorts = added;
                        PortsChanged.Raise(null, new PortsChangedArgs(eventType, added));
                    }
                }
                else if (eventType == EventType.Removal)
                {
                    if (SerialPorts.SequenceEqual(uniqueItems))
                    {
                        var removed = SerialPorts.Except(uniqueItems).ToArray();
                        SerialPorts = removed;
                        PortsChanged.Raise(null, new PortsChangedArgs(eventType, removed));
                    }
                }
            }
        }
예제 #3
0
        private static void RaisePortsChangedIfNecessary(EventType eventType)
        {
            string[] availableSerialPorts;
            if (eventType == EventType.Insertion)
            {
                lock (_serialPorts)
                {
                    availableSerialPorts = GetAvailableSerialPorts();
                    if (!_serialPorts.SequenceEqual(availableSerialPorts))
                    {
                        var serialPort = availableSerialPorts.Except(_serialPorts).FirstOrDefault();
                        "Port added {0}"._DLOG(serialPort);

                        _serialPorts = availableSerialPorts;
                        PortsChanged.Raise(null, new PortsChangedArgs(eventType, serialPort));
                    }
                }
            }
            else
            {
                PortsChanged.Raise(null, new PortsChangedArgs(eventType, "No matter. We will try."));

                lock (_serialPorts)
                {
                    availableSerialPorts = GetAvailableSerialPorts();
                    if (!_serialPorts.SequenceEqual(availableSerialPorts))
                    {
                        var serialPort = _serialPorts.Except(availableSerialPorts).FirstOrDefault();
                        _serialPorts = availableSerialPorts;
                        "Port closed "._DLOG(serialPort);
                    }
                }
            }
        }
예제 #4
0
 private static void RaisePortsChangedIfNecessary(EventType eventType)
 {
     lock (_serialPorts) {
         var availableSerialPorts = GetAvailableSerialPorts();
         if (!_serialPorts.SequenceEqual(availableSerialPorts))
         {
             _serialPorts = availableSerialPorts;
             PortsChanged.Raise(null, new PortsChangedArgs(eventType, _serialPorts));
         }
     }
 }
예제 #5
0
 private void RaisePortsChangedIfNecessary(SerialPortServiceEventType eventType)
 {
     lock (_lock)
     {
         string[] availableSerialPorts = GetPortNames();
         if (!_previousSerialPorts.SequenceEqual(availableSerialPorts))
         {
             _previousSerialPorts = availableSerialPorts;
             PortsChanged?.Invoke(this, new PortsChangedArgs(eventType, _previousSerialPorts));
         }
     }
 }
예제 #6
0
 private void RaisePortsChangedIfNecessary(EventType eventType)
 {
     lock (_serialPortNames)
     {
         var availableSerialPorts = GetAvailableSerialPortNames();
         if (!_serialPortNames.SequenceEqual(availableSerialPorts))
         {
             _serialPortNames = availableSerialPorts;
             PortsChanged?.Invoke(this, new PortsChangedArgs(eventType, _serialPortNames));
         }
     }
 }
예제 #7
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()));
         }
     }
 }
예제 #8
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}"));
        }
예제 #9
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));
         }
     }
 }
예제 #10
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);
        }
예제 #11
0
        private static void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            lock ( _syncObject ) {
                try {
                    timer.Stop();
                }
                catch (ObjectDisposedException) {
                    // See the try-catch construction at timer.Start() for more information.
                    if (_Disposed)
                    {
                        throw;
                    }
                }
            }

            lock (SerialPortService.serial_ports) {
                var availableSerialPorts = GetAvailableSerialPorts();
                if (!serial_ports.SequenceEqual(availableSerialPorts))
                {
                    serial_ports = availableSerialPorts;

                    // debug info
                    if (Constants.DEBUG_STATUS == true)
                    {
                        System.Diagnostics.Debug.Write("SerialPorts:");
                        foreach (string port in serial_ports)
                        {
                            System.Diagnostics.Debug.Write("\t");
                            System.Diagnostics.Debug.Write(port);
                        }
                        System.Diagnostics.Debug.Write("\r\n\r\n");
                    }
                }

                // Always fire this event, since we now need access to other device without
                // a serial port as well (such as STM32 bootloader).
                PortsChanged.Raise(null, new PortsChangedArgs(EventType.All, serial_ports));
            }
        }
예제 #12
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);
        }