Beispiel #1
0
        //The 9575 Extreme Handset will default to use the Standard Message Header (MO Protocol 101, v.2) & Location Block.
        //There are 3 implementation options which can be selected by the Portal Provider:
        //0: Standard Message Header & Location Block (no size reduction)
        //1: Standard Message Header with Reduced Location Block
        //2: Reduced Message Header with Reduced Location Block
        public static bool TryParse(byte[] buff, out ICDMOPacket packet)
        {
            bool reslt = false;
              packet = new ICDMOPacket();

              if (ICDProtocol.Is101Packet(buff, 0))
              {
            reslt = ICDMOMessageStandardHeader.TryParse(buff, 0, out packet.header);
            if (reslt)
            {
              packet.rheader = null;
              reslt = ICDMOMessageBody.TryParse(buff, 0, packet, out packet);
            }
              }
              else if (ICDProtocol.Is102Packet(buff, 0))
              {
            reslt = ICDMOMessageReduceHeader.TryParse(buff, 0, out packet.rheader);
            if (reslt)
            {
              packet.header = null;
              reslt = ICDMOMessageBody.TryParse(buff, 0, packet, out packet);
            }
              }
              return reslt;
        }
        //public string ToInsertSQL(bool replaceComma, string TableName)
        //{
        //  string sql = "";
        //  sql = string.Format("insert into {0} (POS_FECHA, EQP_ESN, COD_EVENTO,", TableName);
        //  sql = string.Format("{0} POSICION, POS_VELOCIDAD, POS_RUMBO, DATA) VALUES (", sql);
        //  sql = string.Format("{0} '{1}', '{2}'", sql, ReportDate.ToString("yyyy-MM-dd HH:mm:ss"), IMEI);
        //  if (replaceComma)
        //    sql = string.Format("{0}, {1}, PointFromText('POINT({2} {3})')", sql, EventCode, ReportPosition.Longitude.getValue().ToString().Replace(',', '.'), ReportPosition.Latitude.getValue().ToString().Replace(',', '.'));
        //  else
        //    sql = string.Format("{0}, {1}, PointFromText('POINT({2} {3})')", sql, EventCode, ReportPosition.Longitude.getValue(), ReportPosition.Latitude.getValue());
        //  if (replaceComma)
        //    sql = string.Format("{0}, {1}, {2}", sql, Speed.ToString().Replace(',', '.'), ReportPosition.Head.ToString().Replace(',', '.'));
        //  else
        //    sql = string.Format("{0}, {1}, {2}", sql, Speed, ReportPosition.Head);
        //  sql = string.Format("{0},'{1}')", sql, JsonConvert.SerializeObject(Data));
        //  return sql;
        //}
        public static bool TryParse(byte[] messageSBD, string imei, out IridiumExtremeBinaryReport report, string packetType)
        {
            int ival;
              double dval;

              report = new IridiumExtremeBinaryReport();
              JObject json = new JObject();
              try
              {
            if (packetType == "MO")
            {
              ICDMOPacket MOpacket = new ICDMOPacket();
              ICDMOPacket.TryParse(messageSBD, out MOpacket);
              ICDMOMessageType type = ICDMOMessageType.Unknown;
              if (MOpacket.rheader != null)
              {
            report.IMEI = imei;
            report.protocolType = (int)MOpacket.rheader.protocolType;
            report.protocolVersion = (int)MOpacket.rheader.ProtocolVersionNumber;
            report.EventCode = (int)MOpacket.rheader.MessageType;
            report.type_message = packetType;
            type = MOpacket.rheader.MessageType;
              }
              else
              {
            report.IMEI = imei;
            report.protocolType = (int)MOpacket.header.protocolType;
            report.protocolVersion = (int)MOpacket.header.ProtocolVersionNumber;
            report.EventCode = (int)MOpacket.header.MessageType;
            report.type_message = packetType;
            type = MOpacket.header.MessageType;
              }
              switch (type)
              {
            case ICDMOMessageType.SwitchOn:
              report.inbox = true;
              break;
            case ICDMOMessageType.AutomaticPositionReport:
              report.inbox = true;
              report.posiciones = true;
              break;
            case ICDMOMessageType.Emergency:
              report.inbox = true;
              report.posiciones = true;
              break;
              }
              foreach (ICDDataBlock d in MOpacket.body.datablocks)
              {
            switch (d.BlockType)
            {
              case ICDBlockType.Location://automatic report position
                var location = (DataBlock_Location)d;
                report.ReportPosition = location.position;
                DateTime dt;
                try
                {
                 dt = new DateTime((int)location.GPSTimestampUTCYear, (int)location.GPSTimestampUTCMonth, (int)location.GPSTimestampUTCDay, (int)location.GPSTimestampUTCHours, (int)location.GPSTimestampUTCMinutes, (int)location.GPSTimestampUTCSeconds);
                 report.ReportDate = dt;
                }
                catch (Exception ex)
                {
                  report.ReportDate = new DateTime();//DateTime.Now;
                }
                report.Speed = 0;

                //string json5 = new JavaScriptSerializer().Serialize(location);
                //JObject json = JObject.FromObject(location);
                json["Location"] = JObject.FromObject(new {
                  Type = location.BlockType.ToString(),
                  DateTime = report.ReportDate,
                  VelocityDirection = location.VelocityDirection,
                  SpeedOverGround = location.SpeedOverGround,
                  CourseOverGround = location.CourseOverGround,
                  AltitudeSign = location.AltitudeSign,
                  Altitude = location.Altitude,
                  LatitudeAccuracyMeasurement = location.LatitudeAccuracyMeasurement,
                  LatitudeAccuracy = location.LatitudeAccuracy,
                  LongitudeAccuracyMeasurement = location.LongitudeAccuracyMeasurement,
                  LongitudeAccuracy =  location.LongitudeAccuracy,
                  AltitudeAccuracyMeasurement = location.AltitudeAccuracyMeasurement,
                  AltitudeAccuracy = location.AltitudeAccuracy,
                  IsValid = 1,
                  InsertInPosiciones = report.insertInPosiciones
                });
                json["Location"]["Position"] = JObject.FromObject(new {
                  Altitude = location.position.Altitude,
                  Longitude = location.position.Longitude,
                  Latitude = location.position.Latitude,
                  Cuadrante = location.position.Cuadrante,
                  Rumbo = location.position.Head
                });

                //DataBlock_Location fromJson = new JavaScriptSerializer().Deserialize<DataBlock_Location>(json);
                //var loc = new JavaScriptSerializer().DeserializeObject(json);
                //JObject jobject = JObject.Parse(json);//magic

                //var name = new{ Datablock = new []{ new {type = location.BlockType.ToString()}}};
                //JObject namejson = JObject.FromObject(name);
                //string json2 = string.Format("Datablock: {0} {3} : {4}{1} {5}{2}", "{", location.BlockType.ToString(), "}", "'type'", "'", "'");
                //JObject namejson = JObject.Parse(json2);
                //string a = @"{'type':"+"'"+location.BlockType.ToString()+"'"+"}";
                break;
              case ICDBlockType.FreeText:
                var freetext = (DataBlock_FreeText)d;
                json["FreeText"] = JObject.FromObject(new
                {
                  FreeText = freetext.FreeText.Text.ToString(),
                });
                break;
              case ICDBlockType.Recipient:
                var recipient = (DataBlock_Recipient)d;

                break;
              case ICDBlockType.Synchronisation:
                var synchronisation = (DataBlock_Synchronisation)d;
                break;
              case ICDBlockType.DeviceIdentifier:
                var deviceIdentifier = (DataBlock_DeviceIdentifier)d;
                break;
              case ICDBlockType.Diagnostics:
                var diagnostics = (DataBlock_Diagnostics)d;
                break;
              case ICDBlockType.Sender:
                var sender = (DataBlock_Sender)d;
                break;
              case ICDBlockType.RegionCircle:
                var regionCircle = (DataBlock_Circle)d;
                break;
              case ICDBlockType.RegionPolygon:
                var regionPolygon = (DataBlock_Polygon)d;
                break;
              case ICDBlockType.PortalStatus:
                var portalStatus = (DataBlock_PortalStatus)d;
                break;
              case ICDBlockType.PhoneSettings:
                var phoneSettings = (DataBlock_PhoneSettings)d;
                json["PhoneSettings"] = JObject.FromObject(new
                {
                  Type = phoneSettings.BlockType.ToString(),
                  DateTime = report.ReportDate,
                  ReportFrecuencyRate = phoneSettings.ReportingFrecuencyRate,
                  ReportingFrecuencyRateUnits = phoneSettings.ReportingFrecuencyRateUnits,
                  EmergencyDestination = phoneSettings.EmergencyDestination,
                  EmergencyCallRecipient = phoneSettings.EmergencyCallRecipient,
                });
                //JObject jsonPhoneSettings = JObject.FromObject(phoneSettings);
                //report.Data.Add(jsonPhoneSettings);
                break;
              case ICDBlockType.GeofenceStatus:
                var geofenceStatus = (DataBlock_GeofenceStatus)d;
                break;
              case ICDBlockType.CannedMessagesHash:
                var cannedMessageHash = (DataBlock_CannedMessagesHash)d;
                json["CannedMessageHash"] = JObject.FromObject(new
                {
                  Type = cannedMessageHash.BlockType.ToString(),
                  Hash = cannedMessageHash.Hash,
                });
                //JObject jsonCannedMessageHash = JObject.FromObject(cannedMessageHash);
                //report.Data.Add(jsonCannedMessageHash);
                //JsonConvert.SerializeObject(report.Data);
                break;
              case ICDBlockType.PortalProviderInformation:
                var portalProviderInfo = (DataBlock_PortalProviderInformation)d;
                break;
              case ICDBlockType.ReducedSizeLocation:
                var reducedSizeLocation = (DataBlock_ReducedLocation)d;
                break;
              case ICDBlockType.CannedMessageIdentifier:
                var cannedMessageIdentifier = (DataBlock_CannedMessageIdentifier)d;
                break;
              case ICDBlockType.DistributionListHash:
                var distributionList = (DataBlock_DistributionListHash)d;
                break;
              case ICDBlockType.MessageIdentifier:
                var messageIdentifier = (DataBlock_MessageIdentifier)d;
                break;
              case ICDBlockType.FirmwareHardwareVersion:
                var firmwareHardware = (DataBlock_HandsetFwHwVersion)d;
                break;
            }

              }
              report.Data = json;
            }
            else
            {
              ICDMTPacket MTpacket = new ICDMTPacket();
              ICDMTPacket.TryParse(messageSBD, out MTpacket);
              ICDMTMessageType type = ICDMTMessageType.Unknown;
              if (MTpacket.header != null)
              {
            report.IMEI = imei;//chequear si esta registrado
            report.protocolType = (int)MTpacket.header.protocolType;
            report.protocolVersion = (int)MTpacket.header.ProtocolVersionNumber;
            report.EventCode = (int)MTpacket.header.MessageType;
            type = MTpacket.header.MessageType;
              }
              else
              {
            report.IMEI = imei;
            report.protocolType = (int)MTpacket.header.protocolType;
            report.protocolVersion = (int)MTpacket.header.ProtocolVersionNumber;
            report.EventCode = (int)MTpacket.header.MessageType;
            type = MTpacket.header.MessageType;
              }

              report.sent = true;
            }
            return true;
              }
              catch (Exception ex) { }
              return false;
        }
Beispiel #3
0
        //The message body for a Mobile Originated message consists of zero or more Data Blocks. Some message types have mandatory Data Blocks. Optionally, additional data blocks may be included for any message type.
        public static bool TryParse(byte[] buff, int offset, ICDMOPacket packet1, out ICDMOPacket packet)
        {
            bool flag = false;
              bool reslt = false;
              packet = packet1;
              packet.body = new ICDMOMessageBody();
              ICDMOMessageType type = ICDMOMessageType.Unknown;
              if (packet.header != null)
            type = packet.header.MessageType;
              else if (packet.rheader != null)
            type = packet.rheader.MessageType;

              int boffset = offset;
              net.Tesacom.GeneralUtilities.BitStream stream = new net.Tesacom.GeneralUtilities.BitStream(buff);
              uint blockType = 0;
              if (packet.header != null)
              {
            // blockType = stream.GetUint(ICDMessageStandardHeader.HeaderLen, DataBlock_Location.BlockTypeLen);
            boffset = ICDMOMessageStandardHeader.HeaderLen;
              }
              else if (packet.rheader != null)
              {
            // blockType = stream.GetUint(ICDMessageReduceHeader.HeaderLen, DataBlock_ReducedLocation.BlockTypeLen);
            boffset = ICDMOMessageReduceHeader.HeaderLen;
              }
              while (boffset < buff.Length * 8 && flag == false)
              {
            blockType = stream.GetUint(boffset, ICDDataBlock.BlockTypeLen);

            switch (blockType)
            {
              case 0:
                DataBlock_Location location;
                if (DataBlock_Location.TryParse(buff, boffset, out location))
                {
                  packet.body.datablocks.Add(location);
                  boffset += DataBlock_Location.LocationLenBytes * 8;
                }
                break;
              case 0x01:
                DataBlock_FreeText freetext;
                if (DataBlock_FreeText.TryParse(buff, boffset, out freetext))
                {
                  packet.body.datablocks.Add(freetext);
                  boffset += DataBlock_FreeText.BlockTypeLen * 8;
                }
                break;
              case 0x0E:
                int isReduced = 0;
                DataBlock_ReducedLocation rlocation;
                if (DataBlock_ReducedLocation.TryParse(buff, boffset, out rlocation, isReduced))
                {
                  //DataBlock_ReducedLocation.ToBytes(isReduced, rlocation);
                  packet.body.datablocks.Add(rlocation);
                  boffset += DataBlock_ReducedLocation.ReducedLocationLenBytes * 8;
                }
                break;
              case 0x34:
                isReduced = 1;
                DataBlock_ReducedLocation rminlocation;
                if (DataBlock_ReducedLocation.TryParse(buff, ICDMOMessageReduceHeader.HeaderLen, out rminlocation, isReduced))
                {
                  packet.body.datablocks.Add(rminlocation);
                  boffset += DataBlock_ReducedLocation.ReducedLocationLenBytes * 8;
                }
                break;
              case 0x0a:
                //int isReduced = 0;
                DataBlock_PhoneSettings phonesettings;
                if (DataBlock_PhoneSettings.TryParse(buff, boffset, out phonesettings))
                {
                  //DataBlock_ReducedLocation.ToBytes(isReduced, rlocation);
                  packet.body.datablocks.Add(phonesettings);
                  boffset += phonesettings.PhoneSettingsLenBytes * 8;
                }
                break;
              case 0x0c:
                //int isReduced = 0;
                DataBlock_CannedMessagesHash cannedMessageHash;
                if (DataBlock_CannedMessagesHash.TryParse(buff, boffset, out cannedMessageHash))
                {
                  //DataBlock_ReducedLocation.ToBytes(isReduced, rlocation);
                  packet.body.datablocks.Add(cannedMessageHash);
                  boffset += DataBlock_CannedMessagesHash.CannedMessagesHashLenBytes * 8;
                }
                break;
              case 0x10:
                //int isReduced = 0;
                DataBlock_DistributionListHash distributionListHash;
                if (DataBlock_DistributionListHash.TryParse(buff, boffset, out distributionListHash))
                {
                  //DataBlock_ReducedLocation.ToBytes(isReduced, rlocation);
                  packet.body.datablocks.Add(distributionListHash);
                  boffset += DataBlock_DistributionListHash.DistributionListHashLenBytes * 8;
                }
                break;
              case 0x12:
                //int isReduced = 0;
                DataBlock_HandsetFwHwVersion handsetFirmwareHardwareVersion;
                if (DataBlock_HandsetFwHwVersion.TryParse(buff, boffset, out handsetFirmwareHardwareVersion))
                {
                  //DataBlock_ReducedLocation.ToBytes(isReduced, rlocation);
                  packet.body.datablocks.Add(handsetFirmwareHardwareVersion);
                  boffset += handsetFirmwareHardwareVersion.HandsetFwHwVersionLenBytes * 8;
                }
                break;
              case 0x03:
                while (boffset < buff.Length * 8)
                {
                  DataBlock_Synchronisation synchronisation;
                  if (DataBlock_Synchronisation.TryParse(buff, boffset, out synchronisation))
                  {
                    packet.body.datablocks.Add(synchronisation);
                    boffset += synchronisation.SynchronisationLenBytes * 8;
                  }
                }
                break;
              case 0x09:
                DataBlock_PortalStatus portalStatus;
                if (DataBlock_PortalStatus.TryParse(buff, boffset, out portalStatus))
                {
                  packet.body.datablocks.Add(portalStatus);
                  boffset += portalStatus.PortalStatusLenBytes * 8;
                }
                break;
              case 0x0d:
                DataBlock_PortalProviderInformation portalProviderInformation;
                if (DataBlock_PortalProviderInformation.TryParse(buff, boffset, out portalProviderInformation))
                {
                  packet.body.datablocks.Add(portalProviderInformation);
                  boffset += portalProviderInformation.PortalProviderInformationLenBytes * 8;
                }
                break;
              case 0x02:
                DataBlock_Recipient recipient;
                if (DataBlock_Recipient.TryParse(buff, boffset, out recipient))
                {
                  packet.body.datablocks.Add(recipient);
                  boffset += recipient.RecipientLenBytes * 8;
                }
                break;
              case 0x04:
                DataBlock_DeviceIdentifier deviceIdentifier;
                if (DataBlock_DeviceIdentifier.TryParse(buff, boffset, out deviceIdentifier))
                {
                  packet.body.datablocks.Add(deviceIdentifier);
                  boffset += deviceIdentifier.DeviceIdentifierLenBytes * 8;
                }
                break;
              case 0x05:
                DataBlock_Diagnostics diagnostics;
                if (DataBlock_Diagnostics.TryParse(buff, boffset, out diagnostics))
                {
                  packet.body.datablocks.Add(diagnostics);
                  boffset += diagnostics.DiagnosticsLenBytes * 8;
                }
                break;
              case 0x06:
                DataBlock_Sender sender;
                if (DataBlock_Sender.TryParse(buff, boffset, out sender))
                {
                  packet.body.datablocks.Add(sender);
                  boffset += sender.SenderLenBytes * 8;
                }
                break;
              case 0x07:
                DataBlock_Circle circle;
                if (DataBlock_Circle.TryParse(buff, boffset, out circle))
                {
                  packet.body.datablocks.Add(circle);
                  boffset += (int)circle.BlockLengthBytes * 8;
                }
                break;

              //POLYGON//
              case 0x08:
                DataBlock_Polygon polygon;
                if (DataBlock_Polygon.TryParse(buff, boffset, out polygon))
                {
                  packet.body.datablocks.Add(polygon);
                  boffset += (int)polygon.BlockLengthBytes * 8;
                }
                break;

              case 0x11:
                DataBlock_GeofenceStatus geofenceStatus;
                if (DataBlock_GeofenceStatus.TryParse(buff, boffset, out geofenceStatus))
                {
                  packet.body.datablocks.Add(geofenceStatus);
                  boffset += (int)geofenceStatus.BlockLengthBytes * 8;
                }
                break;
              case 0x0F:
                DataBlock_CannedMessageIdentifier cannedmessage;
                if (DataBlock_CannedMessageIdentifier.TryParse(buff, boffset, out cannedmessage))
                {
                  packet.body.datablocks.Add(cannedmessage);
                  boffset += (int)cannedmessage.BlockLengthBytes * 8;
                }
                break;
              case 0x17:
                DataBlock_MessageIdentifier messageIdentifier;
                if (DataBlock_MessageIdentifier.TryParse(buff, boffset, out messageIdentifier))
                {
                  packet.body.datablocks.Add(messageIdentifier);
                  boffset += (int)messageIdentifier.BlockLengthBytes * 8;
                }
                break;
              default:
                flag = true;
                break;
            }
              }

            /*
              case ICDMOMessageType.UserExtensions:
            break;
              case ICDMOMessageType.Reserved:
            break;
             */
              //}
              reslt = true;
              return reslt;
        }