git_bsmd/AIS/bsmd.AISService/AIS/SerialDataHandler.cs

73 lines
2.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using log4net;
namespace bsmd.AISService.AIS
{
public class SerialDataHandler
{
private Serial_IO serial_IO;
private AIS_Decoder decoder;
private static ILog _log = LogManager.GetLogger(typeof(SerialDataHandler));
public SerialDataHandler(Serial_IO io, AIS_Decoder decoder)
{
this.serial_IO = io;
this.decoder = decoder;
this.serial_IO.LineRead += new Serial_IO.LineReadHandler(serial_IO_LineRead);
}
public Serial_IO Serial_IO
{
get { return this.serial_IO; }
}
public AIS_Decoder AIS_Decoder
{
get { return this.decoder; }
}
public bool Start(ref string message)
{
return this.serial_IO.Open(ref message);
}
public void Stop()
{
this.serial_IO.Close();
}
protected void serial_IO_LineRead(string data)
{
NMEA.Status nmea_Status = NMEA.Status.OK;
if (data == null || data.Length == 0) return;
NMEA decodedSentence = NMEA.Decode(data, ref nmea_Status);
if (decodedSentence != null)
{
if (decodedSentence is NMEA_AIS_Sentence)
{
NMEA_AIS_Sentence aisSentence = decodedSentence as NMEA_AIS_Sentence;
this.decoder.Decode(aisSentence.AIS_Message, aisSentence.Msg_Sentence_Nr,
aisSentence.Total_Sentence_Nr, aisSentence.Seq_Message_Ident, this.Serial_IO.StationName);
}
}
else
{
_log.Info("Serial data handler: NMEA decoder returned null sentence");
}
}
public override string ToString()
{
return string.Format("Serial AIS Receiver {0} on {1}",
this.serial_IO.StationName,
this.serial_IO.ComPort);
}
}
}