private bool m_Commit(object oDeviceData, string strCustomState) { bool result = false; GLPBase report = oDeviceData as GLPBase; string outgoingMessage = string.Empty; //to nie odpowiedz wiec, sprawdzamy połaczenie if (report == null) { byte[] arrToSend = UTF8Encoding.UTF8.GetBytes(".ttc\r\n"); try { SendToDevice(arrToSend, "tcp", true); UpdateProgress(2, 3, "Connection OK", ""); } catch { UpdateProgress(1, 3, "Waiting for connection...", ""); } } else if (report.TerminalStatus != 3) //poleczenie ok brak garmina { UpdateProgress(2, 3, "Connection OK but terminal not connected", ""); } else if (report.TerminalStatus == 3) //polaczenie i garmin ok { outgoingMessage = ".ttp " + JobID + "," + Latitude.ToString("0.0000").Replace(',', '.') + "," + Longitude.ToString("0.0000").Replace(',', '.') + ",\"" + Text + "\"\r\n"; byte[] arrToSend = UTF8Encoding.UTF8.GetBytes(outgoingMessage); try { SendToDevice(arrToSend, "tcp", true); UpdateProgress(3, 3, "Completed", ""); Thread.Sleep(2000); result = true; } catch { UpdateProgress(1, 3, "Waiting for connection...", ""); } } return(result); }
protected bool ExecuteStep(object oDeviceData) { bool result = false; if (base.StepCurrent == 1) { byte[] arrToSend = UTF8Encoding.UTF8.GetBytes(".takepic"); try { SendToDevice(arrToSend, "tcp", true); base.UpdateProgress(2, 3, "Request sent", null); } catch (GateCmdException ex) { if (ex.Message == null || !ex.Message.StartsWith("Could not find transport")) { throw; } base.UpdateProgress(1, 3, "Waiting for the device to connect", null); } } else if (base.StepCurrent == 2) { GLPBase report = oDeviceData as GLPBase; if (report.CameraStatus == 2 || report.CameraStatus == 3) { base.UpdateProgress(3, 3, "Device will send the picture soon...", null); result = true; } else if (report.CameraStatus == 1) { base.UpdateProgress(3, 3, "Picture taking failed", null); result = true; } } return(result); }
private bool m_Commit(object oDeviceData, string strCustomState) { bool result = false; GLPBase report = oDeviceData as GLPBase; string outgoingMessage = string.Empty; //to nie odpowiedz wiec, sprawdzamy połaczenie if (report == null) { byte[] arrToSend = UTF8Encoding.UTF8.GetBytes(".ttc\r\n"); try { SendToDevice(arrToSend, "tcp", true); UpdateProgress(2, 3, "Connection OK", ""); } catch { UpdateProgress(1, 3, "Waiting for connection...", ""); } } else if (report.TerminalStatus != 3) //poleczenie ok brak garmina { UpdateProgress(2, 3, "Connection OK but terminal not connected", ""); } else if (report.TerminalStatus == 3) //polaczenie i garmin ok { outgoingMessage = ".tt " + ChatTextID + ",\"" + ChatText + "\""; if (CannedResponseList.Count > 0) { int newFlag = 1; ChatReader reader = new ChatReader(); foreach (ChatResponseBag response in reader.GetAllResponses()) { uint iResponseID = (uint)response.ID; if (CannedResponseList.Contains(iResponseID)) { outgoingMessage += ",\"" + response.ResponseText + "\""; } newFlag = 0; } if (newFlag == 1) { uint[] CannedResponseID = new uint[CannedResponseList.Count]; for (int i = 0; i < CannedResponseList.Count; i++) { CannedResponseID[i] = CannedResponseList[i]; } string ConnStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString; System.Data.Odbc.OdbcConnection conn = new System.Data.Odbc.OdbcConnection(); conn.ConnectionString = ConnStr; try { conn.Open(); for (int i = 0; i < CannedResponseList.Count; i++) { System.Data.Odbc.OdbcCommand cmd = new System.Data.Odbc.OdbcCommand( @"SELECT body FROM chat_profile_message WHERE chat_profile_message_id = " + CannedResponseID[i] + ";" , conn); outgoingMessage += ",\"" + cmd.ExecuteScalar().ToString() + "\""; } } catch (Exception ex) { } finally { conn.Close(); } } } outgoingMessage += "\r\n"; byte[] arrToSend = UTF8Encoding.UTF8.GetBytes(outgoingMessage); try { SendToDevice(arrToSend, "tcp", true); UpdateProgress(3, 3, "Completed", ""); Thread.Sleep(2000); result = true; } catch { UpdateProgress(1, 3, "Waiting for connection...", ""); } } return(result); }
/// <summary> /// Data from device. TCP /// </summary> /// <param name="arrData"></param> /// <param name="iStart"></param> /// <param name="iLength"></param> public override void TranslateFromDevice(byte[] arrData, int iStart, int iLength) { m_nlog.Info(GLPParser.LogBinary("GLP", arrData)); m_parser.Write(arrData); GLPBase report = null; bool sendAck = false; while ((report = m_parser.NextReport()) != null) { if (report.sendAck) { sendAck = true; } if (!report.parseError) { if (report.loginPacket == false) { // Login if (NmeaConnection.Session == null) { try { NmeaConnection.Login("IMEI", report.DeviceID, null); } catch (Franson.Directory.AuthenticationException) { throw; } } // Pending outgoing commands. ProcessPendingCommand(report); if (report.CameraStatus == 2) { report.CameraStatus = 0; NmeaConnection.ProtocolToDevice("ACK1"); } if (report.CameraStatus == 3) //pobieranie zdjec { report.CameraStatus = 0; GLPPictureProcessor glpPictureProcessor = CameraPluginServices.TryCreatePictureProcessor(); if (glpPictureProcessor != null) { try { glpPictureProcessor.Process(report as GLPBinaryPictureData, this.NmeaConnection); NmeaConnection.ProtocolToDevice("ACK1"); } catch { NmeaConnection.ProtocolToDevice("ACK2"); //przerywamy z ponowieniem } } else { NmeaConnection.ProtocolToDevice("ACK3"); //mnie ma w ogole zainstalowanej kamery wiec przerywamy calkiem } } else if (report.TerminalData != null) { FRCMD fRCMD = null; string text = UTF8Encoding.UTF8.GetString(report.TerminalData); if (text == "GLP_UNDELIVERED\0") { text = Franson.Directory.Session.CurrentSession.Locale.Lang["GLP"].Server["GLP_UNDELIVERED"]; } if (text == "GLP_DELIVERED\0") { text = Franson.Directory.Session.CurrentSession.Locale.Lang["GLP"].Server["GLP_DELIVERED"]; } if (report.JobStatus == 100) { fRCMD = new ChangeAssignedWorkerStateCmdBuilder(CommandDirection.DeviceToGpsGate, report.JobID, NmeaConnection.Device.DeviceOwnerID, AssignedWorkerState.Active); } else if (report.JobStatus == 101) { fRCMD = new ChangeAssignedWorkerStateCmdBuilder(CommandDirection.DeviceToGpsGate, report.JobID, NmeaConnection.Device.DeviceOwnerID, AssignedWorkerState.Completed); } else if (report.JobStatus == 102) { fRCMD = new ChangeAssignedWorkerStateCmdBuilder(CommandDirection.DeviceToGpsGate, report.JobID, NmeaConnection.Device.DeviceOwnerID, AssignedWorkerState.Assigned); } else if (report.JobStatus == 103) { fRCMD = new ChangeAssignedWorkerStateCmdBuilder(CommandDirection.DeviceToGpsGate, report.JobID, NmeaConnection.Device.DeviceOwnerID, AssignedWorkerState.Assigned); } else if (report.JobStatus == 104) { fRCMD = new DeleteAssignedWorkerCmdBuilder(CommandDirection.DeviceToGpsGate, report.JobID, NmeaConnection.Device.DeviceOwnerID); } else { fRCMD = new FRCMD(null, "_ReceiveChatText", new string[] { text }); } //serwer odbiera ale nie wysyła //GpsGateClientCmd client = new GpsGateClientCmd("localhost", 30175); //client.Connect("IMEI", report.DeviceID, null); //client.CmdToServer(cmd); if (fRCMD != null) { GpsGateClientDirect client = new GpsGateClientDirect(); client.Connect(NmeaConnection.Device, this.NmeaConnection); client.CmdToServer(fRCMD); client.Disconnect(); } } else { // Report to GpsGate Framework. ToGpsGate(report.TrackPoint, report.Status); } } else { // Login if (NmeaConnection.Session == null) { NmeaConnection.Login("imei", report.DeviceID, null); } // to keep connection alive ToGpsGate(null, null); } } } if (sendAck) { NmeaConnection.ProtocolToDevice("ACK1"); } }
/// <summary> /// Next report. Or null if there is none. /// </summary> /// <returns></returns> public GLPBase NextReport() { GLPBase iBase = null; bool sendAck = false; if (m_bHeaderFound == false && m_cirBuf.UsedLength >= 14) { // Looking for header. byte[] arrPackets = m_cirBuf.Peek(0, 14); m_arrDeviceID = new byte[8]; // Device ID Array.Copy(arrPackets, m_arrDeviceID, 8); // Packet type m_iPacketType = arrPackets[8]; // Packet count m_iPacketCount = arrPackets[9]; // Firmware version BitParser bp = new BitParser(2, true, false); bp.WriteData(arrPackets, 10, 2); int build = bp.ReadIntX(6); int major = bp.ReadIntX(7); int minor = bp.ReadIntX(3); m_FirmwareVersion = minor + "." + major + "." + build; ushort ackAndLength = BitConverter.ToUInt16(arrPackets, 12); if ((ackAndLength & 0x8000) > 0) { sendAck = true; } else { sendAck = false; } m_iPacketLength = ackAndLength & (ushort)0x7FFF; if (m_cirBuf.UsedLength >= m_iPacketLength) { byte[] CRC = new byte[2]; byte[] packet = m_cirBuf.Peek(0, m_iPacketLength); GetCRC(packet, ref CRC); if (CRC[0] == packet[packet.Length - 2] && CRC[1] == packet[packet.Length - 1]) { m_bHeaderFound = true; m_iPacketsProcessed = 0; m_cirBuf.Seek(14); } else { m_bHeaderFound = false; m_cirBuf.Clear(); } } } if (m_bHeaderFound) { byte[] arrPackets = m_cirBuf.Peek(0, m_cirBuf.UsedLength); if (m_iPacketType == 0) { iBase = new GLPLogin(m_arrDeviceID, arrPackets); //iBase.AddStatus("FirmwareVersion", m_FirmwareVersion); iBase.loginPacket = true; iBase.sendAck = sendAck; } if (m_iPacketType == 1) { iBase = new GLPReport(m_arrDeviceID, arrPackets); iBase.AddStatus("FirmwareVersion", m_FirmwareVersion); m_iPacketsProcessed++; // Forward to next packet start. m_cirBuf.Seek(iBase != null ? iBase.Length : 0); iBase.sendAck = sendAck; } if (m_iPacketType == 0x20) { iBase = new GLPTerminal(m_arrDeviceID, arrPackets); m_cirBuf.Seek(iBase != null ? iBase.Length : 0); iBase.sendAck = sendAck; } if (m_iPacketType == 0x30) { iBase = new GLPBinaryPictureData(m_arrDeviceID, arrPackets); m_cirBuf.Seek(iBase != null ? iBase.Length : 0); iBase.sendAck = sendAck; } if (m_iPacketType >= 0xF0 && m_iPacketType <= 0xFF) { iBase = new GLPLogin(m_arrDeviceID, arrPackets); m_cirBuf.Seek(m_iPacketLength - 14); //bo nie wiemy jaka jest dlugosc m_bHeaderFound = false; iBase.loginPacket = true; iBase.sendAck = sendAck; return(iBase); } if (m_iPacketsProcessed >= m_iPacketCount || iBase.parseError) { // Forward to next packet start (jump CRC). if (iBase.parseError) { m_cirBuf.Clear(); } else { m_cirBuf.Seek(2); } m_bHeaderFound = false; } } return(iBase); }