private static IMessage FactoryEvento(IFoteable node, ulong msgid, String texto, String gmid, DateTime dt) { IMessage salida; if (texto.StartsWith(CodeStr)) { STrace.Debug(typeof(FmiPacketId).FullName, node.Id, String.Format("llego mensaje predefinido: {0}", texto)); var code = (MessageIdentifier) Convert.ToUInt32(texto.Substring(CodeStr.Length, texto.IndexOf(';', CodeStr.Length) - CodeStr.Length)); salida = code.FactoryEvent(node.Id, msgid, null, dt, null, null); } else { STrace.Debug(typeof(FmiPacketId).FullName, node.Id, String.Format("llego mensaje personalizado: {0}", texto)); salida = new TextEvent(node.Id, msgid, dt) { Text = texto, }; } var dc = BaseDeviceCommand.createFrom(String.Format(Mensaje.GarminFm, "A1062500" + gmid), node, null); salida.AddStringToSend(dc.ToString(true)); // Fota.EnqueueOnTheFly(node, 0, new[] { dc }, ref salida); return(salida); }
public static BaseDeviceCommand ToTraxFM(this byte[] message, INode node) { STrace.Debug("GarminTalk", node.Id, String.Format("TX: {0}", BitConverter.ToString(message))); return(BaseDeviceCommand.createFrom( String.Format(Mensaje.GarminFm, StringUtils.ByteArrayToHexString(message, 1, message.Length - 4)), node, null)); }
public bool UnloadRoute(ulong messageId, Destination[] route, int routeId) { string cmd = null; var md = GetMessagingDevice(); switch (md) { case MessagingDevice.Garmin: var config = new StringBuilder(); config.AppendFormat("{0}{1}", BaseDeviceCommand.createFrom(">SSG160<", this, null).ToString(false), Environment.NewLine); config.Append(GarminFmi.EncodeDataDeletionFor(DataDeletionProtocolId.DeleteAllStopsOnTheClient).ToTraxFM(this, false)); foreach (var p in route) { config.Append(GarminFmi.EncodeDeleteTextmessage((UInt32)p.Code).ToTraxFM(this, false)); } config.Append(Environment.NewLine); config.Append(Fota.VirtualMessageFactory(MessageIdentifier.UnloadRouteSuccess, 0)); cmd = config.ToString(); break; case MessagingDevice.Mpx01: break; } SendMessages(cmd, messageId, md); return(true); }
public bool ReloadRoute(ulong messageId, Destination[] route, bool sort, int routeId) { string cmd = null; var md = GetMessagingDevice(); switch (md) { case MessagingDevice.Garmin: var config = new StringBuilder(); config.AppendFormat("{0}{1}", BaseDeviceCommand.createFrom(">SSG161<", this, null).ToString(false), Environment.NewLine); //config.Append(GarminFmi.EncodeDataDeletionFor(DataDeletionProtocolId.DeleteAllStopsOnTheClient).ToTraxFM(this, false)); foreach (var p in route) { var stopStr = GarminFmi.EncodeA603StopProtocol(p.Point, p.Code, p.Text).ToTraxFM(this, false); //STrace.Debug(typeof(GarminFmi).FullName, Id, "ReloadRoute: Stop ID=" + Convert.ToString(p.Code) + " - " + stopStr); config.Append(stopStr); } config.Append(Environment.NewLine); config.Append(Fota.VirtualMessageFactory(MessageIdentifier.ReloadRouteSuccess, NextSequence)); cmd = config.ToString(); break; case MessagingDevice.Mpx01: break; } SendMessages(cmd, messageId, md); return(true); }
private static String CheckPending(IFoteable parser, String pending) { var bydiff = (parser.LastSent.GetText(null) != pending); var bytime = parser.LastSent.IsExpired(); var t = new TimeElapsed(); var md = parser.DataProvider.GetDetalleDispositivo(parser.GetDeviceId(), "GTE_MESSAGING_DEVICE"); if (t.getTimeElapsed().TotalSeconds > 1) { STrace.Debug("ParserLock", parser.Id, String.Format("Peek DetalleDispositivo({0} secs)", t.getTimeElapsed().TotalSeconds.ToString())); } var dc = BaseDeviceCommand.createFrom(pending, parser, null); var isGMessage = dc.isGarminMessage(); var garminConnected = (md != null && md.Valor == MessagingDevice.Garmin) && (parser.IsGarminConnected == true); if (isGMessage && !garminConnected) { if (dc.getStatus() != BaseDeviceCommand.Attributes.Status_GarminNotConnected) { dc = UpdateFotaCommand(parser, null, BaseDeviceCommand.Attributes.Status_GarminNotConnected, UpdateFotaCommands.UpdateTries); } parser.LastSent = null; return(null); } if (bydiff || bytime) { if (bytime) { t.Restart(); dc = UpdateFotaCommand(parser, null, null, UpdateFotaCommands.UpdateTries); if (t.getTimeElapsed().TotalSeconds > 1) { STrace.Debug("ParserLock", parser.Id, String.Format("Peek CheckPending UpdateFotaCommand ({0} secs)", t.getTimeElapsed().TotalSeconds.ToString())); } } var result = (dc != null ? dc.ToString() : null); parser.LastSent = (result == null ? null : new INodeMessage(dc.MessageId ?? 0, result, DateTime.UtcNow)); return(result); } return(null); }
private void CheckLastSentAndDequeueIt(BaseDeviceCommand dc) { string LastCmd = LastSent.GetText(String.Empty); if (LastSent != null) { if (LastSent.IsExpired()) { LastSent = null; } else { var lastDC = BaseDeviceCommand.createFrom(LastCmd, this, null); var result = lastDC.isExpectedResponse(dc); if (result == DeviceCommandResponseStatus.Valid) { Fota.Dequeue(this, lastDC.MessageId); } } } }
private static String GetTipoReporte(BaseDeviceCommand c) { var cmd = c.getCommand(); if (cmd.StartsWith(Reporte.IMEIReq)) { return(Reporte.IMEIReq); } if (cmd.StartsWith(Reporte.IdReq)) { return(Reporte.IdReq); } if (cmd.StartsWith(Reporte.EventoGP)) { return(Reporte.EventoGP); } if (cmd.StartsWith(Reporte.EventoGL)) { return(Reporte.EventoGL); } return(cmd.Length < 3 ? Reporte.Nada : cmd.Substring(0, 3)); }
private String CheckConditionsBeforeSendPending(String pending) { if (!LastSent.IsOnTheFly()) { string md = GetMessagingDevice(); if (md == MessagingDevice.Garmin && BaseDeviceCommand.createFrom(pending, this, null).isGarminMessage()) { if (IsGarminConnected == null || !IsGarminConnected.Value) { // var mid = NextSequence; // pending = GarminFmi.EncodePing().ToTraxFm<String>(mid, this); // LastSent = new INodeMessage(mid, pending, DateTime.UtcNow) { IsOnTheFly = true }; // STrace.Debug(typeof(IFoteable).FullName, this.GetDeviceId(), "GARMIN PING Sent"); LastSent = null; return(null); } } else { ulong mid = ParserUtils.GetMsgIdTaip(pending); if (pending.StartsWith(Reporte.WriteGgPrefix) && !_sdSession) { //si quiero escribir en la sd tengo que abrir sesion antes, y antes tengo que pedir la password para abrir sesion STrace.Debug(typeof(Parser).FullName, Id, "Sesion de escritura en sd QUERING"); mid = NextSequence; pending = Mensaje.Factory(mid, this, Reporte.QuerySdSessionPassword.TrimStart('>')); LastSent = new INodeMessage(mid, pending, DateTime.UtcNow) { IsOnTheFly = true }; } } } return(pending); }
// TODO: unificar GTEDeviceCommand e INodeMessage si se puede private void SendPendingFota(ref IMessage msg) { string pending = Fota.Peek(this); if (String.IsNullOrEmpty(pending)) { return; } if (CheckImageSession(pending)) { return; } //procesar primero el pendiente pending = CheckConditionsBeforeSendPending(pending); if (String.IsNullOrEmpty(pending)) { return; } #region SendPending var gteDC = BaseDeviceCommand.createFrom(pending, this, null); pending = gteDC.ToString(true); msg = (msg ?? new UserMessage(Id, 0)); msg.AddStringToPostSend(pending); if (LastSent == null || LastSent.GetText(null) != pending) { LastSent = new INodeMessage((ulong)Id, pending, DateTime.UtcNow); } #endregion SendPending }
private bool CheckLastSentAndDequeueIt(String buffer, ulong msgId, ref IMessage salida) { var result = true; string LastCmd = LastSent.GetText(String.Empty); ulong LastId = LastSent.GetId(); if (buffer.StartsWith(Reporte.SdLocked)) { _sdSession = false; STrace.Debug(typeof(Parser).FullName, Id, "Sesion de escritura en sd BORRANDO_POR_LOCKED"); } else if (buffer.StartsWith(Reporte.SdPassword)) { //Debug.Assert(LastSent.IsOnTheFly() == true); if (LastCmd.StartsWith(Reporte.StartGgWriteSession) && (LastId == msgId)) { LastSent = null; _sdSession = true; STrace.Debug(typeof(Parser).FullName, Id, "Sesion de escritura en sd ABIERTA"); } else if (_sdSession) { STrace.Debug(typeof(Parser).FullName, Id, "Sesion de escritura en sd NO_MANEJADO"); } else if (LastId == msgId) { ulong mid = NextSequence; LastSent = new INodeMessage(mid, Mensaje.Factory(mid, this, String.Format("{0}{1}", Reporte.StartGgWriteSession.TrimStart('>'), buffer.Substring(8, 8))), DateTime.MinValue) { IsOnTheFly = true }; salida.AddStringToSend(LastSent.Text); STrace.Debug(typeof(Parser).FullName, Id, "Sesion de escritura en sd ABRIENDO"); } else { STrace.Debug(typeof(Parser).FullName, Id, String.Format("Sesion de escritura en sd NADA (LastSent.GetId()={0} msgId={1})", LastSent.GetId(), msgId)); } } else if (LastSent != null) { if (LastSent.IsExpired()) { LastSent = null; } else { var lastDC = BaseDeviceCommand.createFrom(LastCmd, this, null); var respStatus = lastDC.isExpectedResponse(BaseDeviceCommand.createFrom(buffer, this, null)); switch (respStatus) { case DeviceCommandResponseStatus.Valid: result = true; Fota.Dequeue(this, lastDC.MessageId ?? null); break; case DeviceCommandResponseStatus.Invalid: result = false; break; case DeviceCommandResponseStatus.Exception: Fota.RollbackLastTransaction(this, lastDC.MessageId ?? null); result = false; break; } } } return(result); }
public static BaseDeviceCommand UpdateFotaCommand(IFoteable device, ulong?msgid, string newStatus, BaseDeviceCommand newCommand, UpdateFotaCommands action) { var result = (BaseDeviceCommand)null; var file = ObtainFirstExistantReversedFile(device); if (file == null) { return(null); } using (var sr = new BackwardReader(file)) { var buffer = new StringBuilder(); var msgidfound = false; String line; while ((line = sr.ReadLine()) != null) // reads the lines from last to first { var lineOriginal = line = line.Trim(); if (IsLastSequenceMessage(line)) { continue; } if (IsVirtualMessage(line)) { break; } var newLine = new StringBuilder(); var isFirstCommand = true; // at begin of line evaluation, the next command in line is the first MatchCollection commandMatches = getMatchesAllCommandsFrom(line); var allSent = commandMatches.Count > 0; // the variable reflects if all commands in line were sent. for (var i = 0; i < commandMatches.Count; i++) { var cM = commandMatches[i]; var command = cM.Groups[0].Value; var dc = BaseDeviceCommand.createFrom(command, device, null); if (!dc.isAlreadySent()) { if (msgid == null || dc.MessageId == msgid) { allSent = allSent && (action != UpdateFotaCommands.RollbackTransaction && isFirstCommand && newStatus == BaseDeviceCommand.Attributes.Status_Sent); msgidfound = true; if (isFirstCommand) { isFirstCommand = false; if (newCommand != null) { dc = newCommand; dc.IdNum = device.GetDeviceId(); } if (action == UpdateFotaCommands.UpdateTries || action == UpdateFotaCommands.RollbackTransaction) { dc.Tries = (dc.Tries == null ? 0 : dc.Tries.Value) + 1; dc.LastTry = DateTime.Now; } if (action == UpdateFotaCommands.RollbackTransaction) { dc.setStatus(BaseDeviceCommand.Attributes.Status_Rollbacked); } else { dc.setStatus(newStatus); } } else if (action == UpdateFotaCommands.RollbackTransaction) { dc.Tries = (dc.Tries == null ? 0 : dc.Tries.Value) + 1; dc.LastTry = DateTime.Now; dc.setStatus(BaseDeviceCommand.Attributes.Status_Rollbacked); } if (result == null) // just catch first not sent message { result = dc; } } else { allSent = false; } } newLine.Append(dc.ToString()); } line = (allSent?null:newLine.ToString()); if (!String.IsNullOrEmpty(line)) { if (lineOriginal != line) { STrace.Debug(typeof(Fota).FullName, device.Id, "FOTA> LINE ORIGINAL: " + lineOriginal); STrace.Debug(typeof(Fota).FullName, device.Id, "FOTA> LINE REPLACED: " + line); } buffer.Insert(0, line + Environment.NewLine); } else { STrace.Debug(typeof(Fota).FullName, device.Id, "FOTA> LINE DELETED : " + lineOriginal); } if (msgidfound) { break; } } if (buffer.Length != 0) { sr.Write(buffer.ToString()); } if (device.Sequence != null) { sr.Write(LastSequenceFactory(device)); } sr.TruncateHere(); } if (IOUtils.FileExists(file)) { using (var sr = new BackwardReader(file)) { var allLastSequence = true; String line; while (allLastSequence && (line = sr.ReadLine()) != null) { allLastSequence = allLastSequence && IsLastSequenceMessage(line); } sr.Dispose(); if (allLastSequence) { File.Delete(file); } } } return(result); }