示例#1
0
文件: Parser.cs 项目: jesumarquez/lt
        public override IMessage Decode(IFrame frame)
        {
            if (ParserUtils.IsInvalidDeviceId(Id))
            {
                return(null);
            }

            //Debug.Assert(Frame.Payload[5] == 0x77);
            //var SENDOK = Frame.Payload[7];
            //var IDLETIME = Frame.Payload[13] + Frame.Payload[14] << 8;

            var msgId       = BitConverter.ToUInt32(frame.Payload, 87);
            var tipoReporte = frame.Payload[4];

            var vel = Speed.KnotToKm(frame.Payload[26]);

            var hour8 = frame.Payload[27];

            if (hour8 < 0x80)
            {
                STrace.Trace(GetType().FullName, Id, "Descartando reporte por hora invalida");
                return(null);
            }

            var year   = Convert.ToInt32(frame.Payload[25].ToString("X2")) + ((DateTime.UtcNow.Year / 100) * 100);
            var month  = Convert.ToInt32(frame.Payload[24].ToString("X2"));
            var day    = Convert.ToInt32(frame.Payload[23].ToString("X2"));
            var minute = Convert.ToInt32(frame.Payload[28].ToString("X2"));
            var second = Convert.ToInt32(frame.Payload[29].ToString("X2"));

            var hour = Convert.ToInt32((hour8 - 0x80).ToString("X2"));
            var date = new DateTime(year, month, day, hour, minute, second, DateTimeKind.Utc);
            var lat  = (float)(BitConverter.ToInt32(frame.Payload, 30) / (-600000.0));
            var lon  = (float)(BitConverter.ToInt32(frame.Payload, 34) / (-600000.0));

            var pos = GPSPoint.Factory(date, lat, lon, vel);

            //var TIMESPEED = Frame.Payload[38];
            //var SPEED = Frame.Payload[39-49];

            var chofer = (frame.Payload[62] + frame.Payload[63] << 8).ToString("X10");

            lock (ChoferesLock)
            {
                if (Choferes.ContainsKey(Id))
                {
                    var lastChofer = Choferes[Id];
                    ProcessChoferState(pos, date, msgId, lastChofer, 1);                 //logout
                    ProcessChoferState(pos, date, msgId, chofer, 0);                     //login
                }
                else
                {
                    Choferes.Add(Id, chofer);
                }
            }

            switch (tipoReporte)
            {
            case 0x17:     //Reset del equipo completo.
                return(MessageIdentifier.DeviceShutdown.FactoryEvent(MessageIdentifier.GenericMessage, Id, msgId, pos, date, chofer, null));

            case 0x18:     //Inicio (power up) del equipo.
                return(MessageIdentifier.DeviceTurnedOn.FactoryEvent(MessageIdentifier.GenericMessage, Id, msgId, pos, date, chofer, null));

            case 0x19:     //Intento de reconexión del modulo GSM.
                return(MessageIdentifier.GsmSignalOn.FactoryEvent(MessageIdentifier.GenericMessage, Id, msgId, pos, date, chofer, null));

            case 0x25:     //Reset del modulo GPS.
                return(MessageIdentifier.GpsSignalOff.FactoryEvent(MessageIdentifier.GenericMessage, Id, msgId, pos, date, chofer, null));

            case 0x63:     //Recupero de información.//posicion encolada
            case 0x64:     //Información actual.//posicion online
            case 0x65:     //Información actual conectado a capturador de datos.//posicion online
                return(pos.ToPosition(Id, msgId));

            default:
                STrace.Debug(GetType().FullName, Id, String.Format("Llego reporte de tipo no soportado: tipo=0x{0:X2} lat={1} lon={2} date={3} chofer={4}", tipoReporte, pos.Lat, pos.Lon, date, chofer));
                return(null);
            }
        }