Example #1
0
        public static IMessage Parse(String[] partes, INode node)
        {
            if (partes[partes.Length - 1].EndsWith("*"))
            {
                partes[partes.Length - 1] = partes[partes.Length - 1].TrimEnd('*');
            }

            if (Convert.ToInt32(partes[2]) != node.Id)
            {
                return(null);
            }

            var evpos = Posicion.ParsearPosicionCorta(partes[4]);

            var mid = Convert.ToUInt32(partes[1]);

            var      code = partes[3];
            IMessage res;

            switch (code)
            {
            case EvCode.SpeedingTicket:
                res = new SpeedingTicket(
                    node.Id,
                    mid,
                    Posicion.ParsearPosicionCorta(partes[5]),
                    evpos,
                    Speed.KnotToKm(Convert.ToInt16(partes[7])),
                    Speed.KnotToKm(Convert.ToInt16(partes[6])),
                    null);
                break;

            case EvCode.AcceleracionBrusca:
                res = FactoryBrusca(partes, node, evpos, MessageIdentifier.AccelerationEvent, mid);
                break;

            case EvCode.FrenadaBrusca:
                res = FactoryBrusca(partes, node, evpos, MessageIdentifier.DesaccelerationEvent, mid);
                break;

            default:
                res = TranslateLegacyCode(code, partes)
                      .FactoryEvent(node.Id, mid, evpos, evpos.GetDate(), null, new List <Int64> {
                    0
                });
                break;
            }
            return(res.AddStringToSend(String.Format("REV;{0}", partes[1])));
        }
Example #2
0
        public IMessage DecodeInternal(IFrame frame)
        {
            var buffer = Encoding.ASCII.GetString(frame.Payload, 0, frame.Payload.Length);

            try
            {
                var parts = buffer.Split('@');
                if (parts.GetLength(0) > 1)
                {
                    var sum = buffer.TakeWhile(b => b != '@').Aggregate <char, byte>(0, (current, b) => (byte)(current + b));

                    if (parts[1] != String.Format("{0:X2}", sum))
                    {
                        STrace.Debug(typeof(Parser).FullName, Id, "Reporte ignorado por error de checksum");

                        if (buffer[0] == 'H' || buffer[0] == 'N' || buffer[0] == 'R')
                        {
                            return(new UserMessage(0, 0).AddStringToSend("B"));
                        }

                        var ack = String.Format("R{0};{1}", buffer.Substring(0, 2), buffer.Split(';')[1]);
                        return(new UserMessage(0, 0).AddStringToSend(ack));
                    }
                }

                if (buffer.Contains("???"))
                {
                    if (buffer[0] == 'H' || buffer[0] == 'N' || buffer[0] == 'R')
                    {
                        STrace.Debug(typeof(Parser).FullName, Id, "Reporte ignorado por corrupto");
                        return(new UserMessage(0, 0).AddStringToSend("B"));
                    }

                    var ack = String.Format("R{0};{1}", buffer.Substring(0, 2), buffer.Split(';')[1]);
                    STrace.Debug(typeof(Parser).FullName, Id, "Reporte ignorado por corrupto");
                    return(new UserMessage(0, 0).AddStringToSend(ack));
                }

                var partes = parts[0].Split(';');

                if (ParserUtils.IsInvalidDeviceId(Id))
                {
                    return((partes.Length > 1) ? new UserMessage(0, 0).AddStringToSend(String.Format("R{0};{1}", buffer.Substring(0, 2), partes[1])) : null);
                }

                var      canappend   = true;
                var      highCommand = Convert.ToChar(frame.Payload[0]);
                IMessage msg;
                switch (highCommand)
                {
                case 'A':
                    msg = EventoBotonera.Parse(partes, this, parts[0]);
                    break;

                case 'T':
                    msg = EventoTemperatura.Parse(partes, this);
                    break;

                case 'Q':
                    msg = Posicion.Parse(partes, this);
                    break;

                case 'H':
                    msg       = ParseConfigRequest(partes);
                    canappend = false;
                    break;

                case 'E':
                    msg = Convert.ToChar(frame.Payload[1]) == 'V' ? Entrante.Parse(partes, this) : Rondin.Parse(partes, this);
                    break;

                case 'V':
                    msg = Vending.Parse(partes, this);
                    break;

                case 'N':                         //solo es un nack
                case 'R':                         //solo es un ack
                    var msgid = Convert.ToUInt64(partes[1]);
                    CheckFota(msgid);
                    msg       = new UserMessage(Id, msgid);
                    canappend = false;
                    break;

                default:
                    msg = null;
                    break;
                }

                msg.ForceReplyCheckingFota(canappend, this);

                //checksum deshabilitado por que trae problemas al appendear un comando

                /*if (msg != null)
                 * {
                 *      if (parts.GetLength(0) > 1)
                 *      {
                 *              msg.AddStringToSend(String.Format("@{0:X2}", msg.GetPending().Aggregate<byte, byte>(0, (current, b) => (byte)(current + b))));
                 *      }
                 * }//*/

                return(msg);
            }
            catch
            {
                STrace.Trace(GetType().FullName, Id, String.Format("No se pudo parsear: {0}", buffer));
                return(null);
            }
        }