Beispiel #1
0
 private void CloseDevice(AtlasSensor sensor)
 {
     if (sensor.currentSerialPort != null)
     {
         sensor.currentSerialPort.Dispose();
     }
     sensorsList = null;
 }
Beispiel #2
0
        public void InitSensors(bool isSleepModeActivated)
        {
            if (isInitialized)
            {
                return;
            }

            var watch = Stopwatch.StartNew();

            try
            {
                string[] ports = SerialPort.GetPortNames();

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

                for (int i = 0; i < ports.Count(); i++)
                {
                    try
                    {
                        if (ports[i].Contains("ttyUSB"))
                        {
                            try
                            {
                                lg.AppendLog(Log.CreateLog(String.Format("Associating device ID - {0}", ports[i]), LogType.System));

                                var serialPort = new SerialPort(ports[i]);

                                // Configure serial settings
                                serialPort.BaudRate     = 9600;
                                serialPort.Parity       = Parity.None;
                                serialPort.StopBits     = StopBits.One;
                                serialPort.DataBits     = 8;
                                serialPort.Handshake    = Handshake.None;
                                serialPort.ReadTimeout  = 6000;
                                serialPort.WriteTimeout = 6000;

                                serialPort.Open();

                                var tsk = Task.Run(async() =>
                                {
                                    r = await SendCommand(serialPort, informationCommand);
                                });
                                tsk.Wait();

                                tsk = Task.Run(async() =>
                                {
                                    s = await SendCommand(serialPort, getStatusCommand);
                                });
                                tsk.Wait();

                                tsk = Task.Run(async() =>
                                {
                                    strResult = await SendCommand(serialPort, getContinuousModeStatus);
                                });
                                tsk.Wait();

                                if (strResult.Contains(answersContinuousModeOn))
                                {
                                    tsk = Task.Run(async() =>
                                    {
                                        strResult = await SendCommand(serialPort, disableContinuousModeCommand);
                                    });
                                    tsk.Wait();
                                }

                                tsk = Task.Run(async() =>
                                {
                                    strResult = await SendCommand(serialPort, getAutomaticAnswerStatusv1);
                                });
                                tsk.Wait();


                                if (strResult.Contains(answersAutomaticAnswerOnv1))
                                {
                                    tsk = Task.Run(async() =>
                                    {
                                        strResult = await SendCommand(serialPort, disableAutomaticAnswerCommandv1);
                                    });
                                    tsk.Wait();
                                }

                                tsk = Task.Run(async() =>
                                {
                                    strResult = await SendCommand(serialPort, getAutomaticAnswerStatusv2);
                                });
                                tsk.Wait();

                                if (strResult.Contains(answersAutomaticAnswerOnv2))
                                {
                                    tsk = Task.Run(async() =>
                                    {
                                        strResult = await SendCommand(serialPort, disableAutomaticAnswerCommandv2);
                                    });
                                    tsk.Wait();
                                }

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

                                //    taskSleep.Wait();
                                //}

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

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

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

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

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

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

                                if (r.ToUpper().Contains("EC"))
                                {
                                    newSensor.sensorType = SensorTypeEnum.EC;
                                    lg.AppendLog(Log.CreateLog("Electro-Conductivity online", LogType.Information));
                                    lg.AppendLog(Log.CreateLog(String.Format("Electro-Conductivity - {0}", s), LogType.System));
                                }

                                sensorsList.Add(newSensor);
                            }

                            catch (AggregateException ex)
                            {
                                Console.WriteLine(ex.ToString());
                                lg.AppendLog(Log.CreateErrorLog("Exception on Sensors Init", ex));
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.ToString());
                        lg.AppendLog(Log.CreateErrorLog("Exception on Sensors Init", ex));
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                lg.AppendLog(Log.CreateErrorLog("Exception on Sensors Init", ex));
            }
            finally
            {
                isInitialized = true;

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