예제 #1
0
파일: GarminFmi.cs 프로젝트: jesumarquez/lt
        private static IMessage DecodeGarminFmi(byte[] text, ulong msgId, Parser node)
        {
            byte packetId = text[1];
            //byte packetSize = text[2];
            ulong msgid = 0;

            //bool needsAck = true;

            var response = "";

            switch (packetId)
            {
            case PacketIdCode.Ack:
                byte packetIdAcked = text[3];
                STrace.Debug(typeof(GarminFmi).FullName, node.GetDeviceId(),
                             "GARMIN ACK FOR PACKET ID: 0x" + StringUtils.ByteToHexString(packetIdAcked));
                //needsAck = false;
                break;

            case PacketIdCode.UnitIdEsn:
                var unitIdEsn  = BitConverter.ToUInt32(text, 3);
                var otherStuff = BitConverter.ToUInt32(text, 7);
                STrace.Debug(typeof(GarminFmi).FullName, node.GetDeviceId(),
                             "GARMIN PACKET ID: UnitId/ESN (Command Response)");
                STrace.Debug(typeof(GarminFmi).FullName, node.GetDeviceId(),
                             "GARMIN UNIT ID/ESN: " + unitIdEsn.ToString("#0"));
                STrace.Debug(typeof(GarminFmi).FullName, node.GetDeviceId(),
                             "GARMIN OTHER STUFF: " + otherStuff.ToString("#0"));
                break;

            case PacketIdCode.DateTimeData:
                STrace.Debug(typeof(GarminFmi).FullName, node.GetDeviceId(), "GARMIN PACKET ID: DateTime Data");
                break;

            case PacketIdCode.FleetManagementPacket:
                ushort fmiPacketId    = BitConverter.ToUInt16(text, 3);
                string hexFmiPacketId = StringUtils.UInt16ToHexString(fmiPacketId);

                STrace.Debug("GarminTalk", node.Id, String.Format("RX: {0}", BitConverter.ToString(text)));

                switch (fmiPacketId)
                {
                case FmiPacketId.CsProductIdData:
                case FmiPacketId.CsProductSupportData:
                case FmiPacketId.CsUnicodeSupportRequest:
                    STrace.Debug(typeof(GarminFmi).FullName, node.Id,
                                 "La función FMI Packet ID " + hexFmiPacketId + " no esta implementada.");
                    break;

                case FmiPacketId.CsTextMessageAcknowledgement:
                    // when a canned response list text message is replied with a response, this acknowledgement is received with the response selected.
                {
                    var dt         = text.ToDateTime(5);
                    var idSize     = Convert.ToByte(text[9]);
                    var id         = BitConverter.ToUInt32(text, 13);
                    var responseId = BitConverter.ToUInt32(text, 29);

                    STrace.Debug(typeof(GarminFmi).FullName, node.GetDeviceId(),
                                 String.Format("GarminTextMessageCannedResponse: {0}",
                                               BitConverter.ToString(text)));
                    STrace.Debug(typeof(GarminFmi).FullName, node.GetDeviceId(),
                                 String.Format(
                                     "GarminTextMessageCannedResponse: dt={0}, IdSize={1}, Id={2}, ResponseId={3}",
                                     dt, idSize, id, responseId));
                    STrace.Debug(typeof(GarminFmi).FullName, node.GetDeviceId(),
                                 String.Format("GarminTextMessageCannedResponse: {0}", response));

                    var salida = (IMessage)MessageIdentifier.GarminTextMessageCannedResponse
                                 .FactoryEvent(node.Id, (UInt32)msgid, null, DateTime.UtcNow, null,
                                               new List <Int64> {
                            node.GetDeviceId(), id, responseId
                        });


                    salida.AddStringToSend(EncodeTextMessageAckReceipt(id).ToTraxFM(node).ToString(true));

                    var bcdArr = new[] { EncodeDeleteTextmessage(id).ToTraxFM(node) };
                    STrace.Debug(typeof(GarminFmi).FullName, node.Id, "StopResponse: Stop ID=" + Convert.ToString(id) + " - " + EncodeDeleteTextmessage(id).ToTraxFM(node, false));
                    Fota.EnqueueOnTheFly(node, 0, bcdArr, ref salida);

                    return(salida);
                }

                case FmiPacketId.CsTextMessageOpenClient2Server:
                {
                    msgid = node.NextSequence;             // por si hace falta lo tengo a mano
                    var dt       = text.ToDateTime(5);
                    var strText1 = Encoding.ASCII.GetString(text, 13, text[2] - 11);
                    var strText2 = StringUtils.ByteArrayToHexString(text, 9, 4);
                    STrace.Debug(typeof(GarminFmi).FullName, node.GetDeviceId(),
                                 "GARMIN A607 Client to Server Text Message (" + hexFmiPacketId + "): ID=" +
                                 msgid + ", Text=" + strText1);

                    return(FactoryEvento(node, msgid, strText1, strText2, dt));
                }

                case FmiPacketId.CsA607Client2ServerTextMessage:
                {
                    //Parser.EnsureMessagingDeviceIsGarmin(node);
                    var dt = text.ToDateTime(5);
                    msgid = BitConverter.ToUInt32(text, 17);
                    var strText1 = Encoding.ASCII.GetString(text, 41, text[2] - 39);
                    var strText2 = StringUtils.ByteArrayToHexString(text, 17, 4);
                    STrace.Debug(typeof(GarminFmi).FullName, node.GetDeviceId(),
                                 "GARMIN A607 Client to Server Text Message (" + hexFmiPacketId + "): ID=" +
                                 msgid + ", Text=" + strText1);

                    return(FactoryEvento(node, msgid, strText1, strText2, dt));
                }

                case FmiPacketId.CsCannedResponseListReceipt:
                    //STrace.Debug(typeof (GarminFmi).FullName, node.GetDeviceId(), "GARMIN PACKET ID: CANNED_RESPONSE_LIST_RECEIPT (NOT IMPLEMENTED)");
                    STrace.Trace(typeof(GarminFmi).FullName, node.GetDeviceId(),
                                 "GARMIN PACKET ID: CANNED_RESPONSE_LIST_RECEIPT:" +
                                 StringUtils.ByteArrayToHexString(text, 0, text.Length));
                    break;

                case FmiPacketId.CsTextMessageReceiptA604OpenServer2Client:
                {
                    Parser.EnsureMessagingDeviceIsGarmin(node);
                    var a604TmoprDatetime   = text.ToDateTime(5);
                    var a604TmoprIdsize     = (int)text[9];
                    var a604TmoprResultCode = (ushort)(text[10] == 0 ? 0 : 1);
                    var a604TmoprUid        = (ulong)0;
                    if (a604TmoprIdsize > 0)
                    {
                        a604TmoprUid = BitConverter.ToUInt32(text, 13);
                    }
                    return(FactoryEvento(node, packetId, a604TmoprUid, a604TmoprDatetime,
                                         a604TmoprResultCode));
                }

                case FmiPacketId.CsSetCannedResponseReceipt:
                case FmiPacketId.CsDeleteCannedResponseReceipt:
                    break;

                case FmiPacketId.CsRequestCannedResponseListRefresh:
//                            node.ReloadMessages(0);
                    break;

                case FmiPacketId.CsTextMessageStatus:
                    break;

                case FmiPacketId.CsSetCannedMessageReceipt:
                    // Sucede cuando se envian los mensajes predeterminados

/*                            STrace.Debug(typeof(GarminFmi).FullName, node.Id, "La función FMI Packet ID " + hexFMIPacketId +
 *                             " no esta implementada."); */
                    break;

                case FmiPacketId.CsDeleteCannedMessageReceipt:
                    // Sucede cuando se envian los mensajes predeterminados y se borran los mensajes no utilizados

/*                            STrace.Debug(typeof(GarminFmi).FullName, node.Id, "La función FMI Packet ID " + hexFMIPacketId +
 *                             " no esta implementada."); */
                    break;

                case FmiPacketId.CsRefreshCannedMessageList:
                case FmiPacketId.CsLongTextMessageReceiptA611LongServer2Client:
                case FmiPacketId.CsSortStopListAcknowledgement:
                case FmiPacketId.CsCreateWaypointReceipt:
                case FmiPacketId.WaypointDeleted:
                case FmiPacketId.CsDeleteWaypointByCategoryReceipt:
                case FmiPacketId.CsCreateWaypointByCategoryReceipt:
                    STrace.Debug(typeof(GarminFmi).FullName, node.Id,
                                 "La función FMI Packet ID " + hexFmiPacketId + " no esta implementada.");
                    break;

                case FmiPacketId.CsEtaData:
                {
                    msgid = BitConverter.ToUInt32(text, 5);
                    var dt         = text.ToDateTime(9);
                    var distanceTo = BitConverter.ToUInt32(text, 13);
                    var pos_lat    = text.ScToDegrees(15);
                    var pos_long   = text.ScToDegrees(17);
                    STrace.Debug(typeof(GarminFmi).FullName, node.GetDeviceId(),
                                 String.Format("GARMIN ETA DATA ({0}): ID={1}, LAT/LONG={2}/{3}, ETA={4}", hexFmiPacketId, msgid, pos_lat, pos_long,
                                               dt.ToString()));

                    setETAReceivedAt(node.Id, DateTime.UtcNow);
                    setETAEstimated(node.Id, dt);
                    setETADistanceTo(node.Id, distanceTo);

                    var salida = (IMessage)MessageIdentifier.GarminETA.FactoryEvent(node.Id, 0, null, DateTime.UtcNow, null, null);
                    salida.AddStringToSend(EncodeEtaDataReceipt((UInt32)msgid).ToTraxFM(node).ToString(true));

                    return(salida);
                }

                case FmiPacketId.CsStopStatus:
                {
                    var uid = BitConverter.ToUInt32(text, 5);
                    var ss  = BitConverter.ToUInt16(text, 9);
                    STrace.Debug(typeof(GarminFmi).FullName, node.GetDeviceId(),
                                 "GARMIN STOP STATUS (0x" + hexFmiPacketId + "): ID=" + uid);

                    var salida = (IMessage)TranslateStopStatus((StopStatusValue)ss, node.Id)
                                 .FactoryEvent(node.Id, 0, null, DateTime.UtcNow, null, new List <Int64> {
                            (int)uid
                        });

                    STrace.Debug(typeof(GarminFmi).FullName, node.Id, "StopStatus: Stop ID=" + Convert.ToString(uid) + " - " + EncodeStopStatusReceipt(uid).ToTraxFM(node, false));

                    salida.AddStringToSend(EncodeStopStatusReceipt(uid).ToTraxFM(node).ToString(true));

                    return(salida);
                }

                case FmiPacketId.CsUserInterfaceTextReceipt:
                    STrace.Debug(typeof(GarminFmi).FullName, node.GetDeviceId(),
                                 "GARMIN USER INTERFACE TEXT RECEIPT (0x" + hexFmiPacketId + ")");
                    // NOTE:    everytime UserInterfaceText is changed,
                    //          we receive this event and the gateway doesnt expect to do nothing by now.
                    break;

                case FmiPacketId.CsMessageThrottlingResponse:
                    break;

                case FmiPacketId.CsMessageThrottlingQueryResponse:
                case FmiPacketId.PingCommunicationStatus:
                    STrace.Debug(typeof(GarminFmi).FullName, node.Id, "La función FMI Packet ID " + hexFmiPacketId + " no esta implementada.");
                    //needsAck = false;
                    break;

                case FmiPacketId.PingCommunicationStatusResponse:
                    node.IsGarminConnected = true;
                    STrace.Debug(typeof(GarminFmi).FullName, node.GetDeviceId(), "GARMIN PONG received");
                    //needsAck = false;
                    break;

                case FmiPacketId.CsGpiFileStartReceipt:
                case FmiPacketId.CsGpiPacketReceipt:
                case FmiPacketId.CsGpiFileEndReceipt:
                case FmiPacketId.CsGpiFileInformation:
                case FmiPacketId.CsSetDriverStatusListItemReceipt:
                case FmiPacketId.CsDeleteDriverStatusListItemReceipt:
                case FmiPacketId.CsDriverStatusListRefresh:
                    STrace.Debug(typeof(GarminFmi).FullName, node.Id, "La función FMI Packet ID " + hexFmiPacketId + " no esta implementada.");
                    break;

                case FmiPacketId.DriverIdUpdate:
                {
                    msgid = node.NextSequence;
                    var changeId   = BitConverter.ToUInt32(text, 5);
                    var changeTime = text.ToDateTime(9);
                    var driverId   = text.GetString(13);
                    var rfid       = GetRfidFromDriverId(driverId);
                    STrace.Debug(typeof(GarminFmi).FullName, node.GetDeviceId(),
                                 "GARMIN DRIVER ID UPDATE (" + hexFmiPacketId + "): ID=" + driverId +
                                 ", RFID=" + rfid);


                    var salida = (IMessage)MessageIdentifierX.FactoryRfid(node.Id, msgid, null, changeTime, rfid, 0);

                    salida.AddStringToSend(EncodeDriverIdReceipt(changeId).ToTraxFM(node).ToString(true));

                    return(salida);
                }

                case FmiPacketId.DriverIdReceipt:
                    STrace.Debug(typeof(GarminFmi).FullName, node.Id, "La función FMI Packet ID " + hexFmiPacketId + " no esta implementada.");
                    break;

                case FmiPacketId.ScA607DriverIdUpdate:
                {
                    msgid = node.NextSequence;
                    var changeId   = BitConverter.ToUInt32(text, 5);
                    var changeTime = text.ToDateTime(9);
                    var driverId   = text.GetString(17);
                    var rfid       = GetRfidFromDriverId(driverId);

                    STrace.Debug(typeof(GarminFmi).FullName, node.GetDeviceId(),
                                 "GARMIN DRIVER ID UPDATE (" + hexFmiPacketId + "): ID=" + driverId +
                                 ", RFID=" + rfid);


                    var salida = (IMessage)MessageIdentifierX.FactoryRfid(node.Id, msgid, null, changeTime, rfid, 0);
                    salida.AddStringToSend(EncodeDriverIdReceipt(changeId).ToTraxFM(node).ToString(true));
                    return(salida);
                }

                case FmiPacketId.DriverStatusUpdate:
                case FmiPacketId.DriverStatusReceipt:
                case FmiPacketId.A607DriverStatusUpdate:
                case FmiPacketId.CsFmiSafeModeReceipt:
                case FmiPacketId.CsSpeedLimitAlertSetupReceipt:
                case FmiPacketId.CsSpeedLimitAlert:
                    STrace.Debug(typeof(GarminFmi).FullName, node.Id, "La función FMI Packet ID " + hexFmiPacketId + " no esta implementada.");
                    break;
                }
                break;

            case PacketIdCode.NAck:
                node.LastSent = null;
                STrace.Debug(typeof(GarminFmi).FullName, node.GetDeviceId(), "GARMIN PACKET ID: NACK (NOT IMPLEMENTED)");
                //needsAck = false;
                break;

            case PacketIdCode.PvtData:
                msgid = node.NextSequence;
                STrace.Debug(typeof(GarminFmi).FullName, node.GetDeviceId(), "GARMIN PACKET ID: PVT DATA");
                STrace.Debug(typeof(GarminFmi).FullName, node.GetDeviceId(), "GARMIN PVTDATA:" + msgid);
                //needsAck = false;
                break;

            case PacketIdCode.StreetPilotStopMessage:
                STrace.Debug(typeof(GarminFmi).FullName, node.GetDeviceId(), "GARMIN PACKET ID: STREET PILOT STOP MESSAGE");
                break;

            case PacketIdCode.StreetPilotTextMessage:
                STrace.Debug(typeof(GarminFmi).FullName, node.GetDeviceId(), "GARMIN PACKET ID: STREET PILOT TEXT MESSAGE");
                break;
            }

            return(null);

            //if (needsAck)
            //{
            //    var salida = (IMessage) new UserMessage(node.Id, 0);
            //    var cmd = BaseDeviceCommand.createFrom(String.Format(Mensaje.GarminFm, GetHexAckFor(packetId)), node, DateTime.UtcNow.AddSeconds(30)).ToString(true);
            //    salida.AddStringToSend(cmd);
            //    return salida;
            //}

            //return null;
        }