Example #1
0
        private async Task <string> ReadAsync(CancellationToken cancellationToken, AtlasSensor currentSensor)
        {
            Task <UInt32> loadAsyncTask;

            uint ReadBufferLength = 1024;

            // If task cancellation was requested, comply
            cancellationToken.ThrowIfCancellationRequested();

            // Set InputStreamOptions to complete the asynchronous read operation when one or more bytes is available
            currentSensor.dataReaderObject.InputStreamOptions = InputStreamOptions.Partial;

            // Create a task object to wait for data on the serialPort.InputStream
            loadAsyncTask = currentSensor.dataReaderObject.LoadAsync(ReadBufferLength).AsTask(cancellationToken);

            // Launch the task and wait
            UInt32 bytesRead = await loadAsyncTask;

            if (bytesRead > 0)
            {
                return(currentSensor.dataReaderObject.ReadString(bytesRead));
            }

            return(String.Empty);
        }
Example #2
0
 private void CloseDevice(AtlasSensor sensor)
 {
     if (sensor.serialDevice != null)
     {
         sensor.serialDevice.Dispose();
     }
     sensorsList = null;
 }
Example #3
0
        private async Task <string> WriteAsync(string command, AtlasSensor currentSensor)
        {
            Task <UInt32> storeAsyncTask;

            currentSensor.dataWriteObject.WriteString(command);

            storeAsyncTask = currentSensor.dataWriteObject.StoreAsync().AsTask();

            UInt32 bytesWritten = await storeAsyncTask;

            if (bytesWritten > 0)
            {
                return(command);
            }
            return(String.Empty);
        }
Example #4
0
        public async Task InitSensors(bool isSleepModeActivated)
        {
            if (isInitialized)
            {
                return;
            }

            var watch = Stopwatch.StartNew();

            try
            {
                string aqs = SerialDevice.GetDeviceSelector();
                var    dis = await DeviceInformation.FindAllAsync(aqs);

                logModel.AppendLog(Log.CreateLog(String.Format("Sensors found in {0} sec.", watch.ElapsedMilliseconds / 1000), Log.LogType.System));

                for (int i = 0; i < dis.Count; i++)
                {
                    try
                    {
                        if (dis[i].Id.Contains(@"\\?\FTDIBUS"))
                        {
                            var task = Task.Run(async() =>
                            {
                                try
                                {
                                    DeviceInformation entry = (DeviceInformation)dis[i];

                                    logModel.AppendLog(Log.CreateLog(String.Format("Associating device ID - {0}", entry.Id), Log.LogType.System));

                                    var serialPort = await SerialDevice.FromIdAsync(entry.Id);

                                    // Configure serial settings
                                    serialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000);
                                    serialPort.ReadTimeout  = TimeSpan.FromMilliseconds(1000);
                                    serialPort.BaudRate     = 9600;
                                    serialPort.Parity       = SerialParity.None;
                                    serialPort.StopBits     = SerialStopBitCount.One;
                                    serialPort.DataBits     = 8;
                                    serialPort.Handshake    = SerialHandshake.None;

                                    // Create cancellation token object to close I/O operations when closing the device
                                    ReadCancellationTokenSource = new CancellationTokenSource();

                                    var newSensor = new AtlasSensor()
                                    {
                                        serialDevice = serialPort
                                    };

                                    newSensor.dataWriteObject  = new DataWriter(serialPort.OutputStream);
                                    newSensor.dataReaderObject = new DataReader(serialPort.InputStream);

                                    string s         = String.Empty;
                                    string r         = String.Empty;
                                    string strResult = String.Empty;

                                    //var taskWakeUp = Task.Run(async () =>
                                    //{
                                    //    await WriteAsync(wakeupCommand, newSensor)
                                    //        .ContinueWith((a) => strResult = ReadAsync(ReadCancellationTokenSource.Token, newSensor).Result);

                                    //    await Task.Delay(1000);
                                    //});

                                    //taskWakeUp.Wait(20000);

                                    var taskStatus = Task.Run(async() =>
                                    {
                                        await WriteAsync(getStatusCommand, newSensor)
                                        .ContinueWith((are) => s = ReadAsync(ReadCancellationTokenSource.Token, newSensor).Result);
                                    });
                                    taskStatus.Wait();

                                    var taskInformation = Task.Run(async() =>
                                    {
                                        await WriteAsync(informationCommand, newSensor)
                                        .ContinueWith((are) => r = ReadAsync(ReadCancellationTokenSource.Token, newSensor).Result);
                                    });
                                    taskInformation.Wait();

                                    if (r.Contains("*OK"))
                                    {
                                        var taskAuto = Task.Run(async() =>
                                        {
                                            await WriteAsync(disableAutomaticAnswerCommand, newSensor);
                                        });
                                        taskAuto.Wait();

                                        var taskContinuous = Task.Run(async() =>
                                        {
                                            await WriteAsync(disableContinuousModeCommand, newSensor);
                                        });
                                        taskContinuous.Wait();
                                    }

                                    //if (isSleepModeActivated)
                                    //{
                                    //    var taskSleep = Task.Run(async () =>
                                    //    {
                                    //        await WriteAsync(sleepModeCommand, newSensor);
                                    //    });

                                    //    taskSleep.Wait();
                                    //}

                                    logModel.AppendLog(Log.CreateLog(String.Format("Sensor Information - {0}", r), Log.LogType.System));

                                    if (r.ToUpper().Contains("RTD"))
                                    {
                                        newSensor.sensorType = SensorTypeEnum.waterTemperature;
                                        logModel.AppendLog(Log.CreateLog("Water Temperature online", Log.LogType.Information));
                                        logModel.AppendLog(Log.CreateLog(String.Format("Water Temperature status - {0}", s), Log.LogType.System));
                                    }

                                    if (r.ToUpper().Contains("PH"))
                                    {
                                        newSensor.sensorType = SensorTypeEnum.ph;
                                        logModel.AppendLog(Log.CreateLog("PH online", Log.LogType.Information));
                                        logModel.AppendLog(Log.CreateLog(String.Format("PH status - {0}", s), Log.LogType.System));
                                    }

                                    if (r.ToUpper().Contains("ORP"))
                                    {
                                        newSensor.sensorType = SensorTypeEnum.orp;
                                        logModel.AppendLog(Log.CreateLog("ORP online", Log.LogType.Information));
                                        logModel.AppendLog(Log.CreateLog(String.Format("ORP status - {0}", s), Log.LogType.System));
                                    }

                                    if (r.ToUpper().Contains("DO"))
                                    {
                                        newSensor.sensorType = SensorTypeEnum.dissolvedOxygen;
                                        logModel.AppendLog(Log.CreateLog("Dissolved Oxygen online", Log.LogType.Information));
                                        logModel.AppendLog(Log.CreateLog(String.Format("Dissolved Oxygen status - {0}", s), Log.LogType.System));
                                    }

                                    sensorsList.Add(newSensor);
                                }

                                catch (AggregateException ex)
                                {
                                    logModel.AppendLog(Log.CreateErrorLog("Exception on Sensors Init", ex));
                                }
                            });

                            task.Wait();
                        }
                    }
                    catch (Exception ex)
                    {
                        logModel.AppendLog(Log.CreateErrorLog("Exception on Sensors Init", ex));
                    }
                }
            }
            catch (Exception ex)
            {
                logModel.AppendLog(Log.CreateErrorLog("Exception on Sensors Init", ex));
            }
            finally
            {
                isInitialized = true;

                logModel.AppendLog(Log.CreateLog(String.Format("Atlas Sensors online in {0} sec.", watch.ElapsedMilliseconds / 1000), Log.LogType.System));
                watch.Stop();
            }
        }