public void ReadData(DbConnect DbConnect) { DbDataReader Reader = null; long ReceiptId = 0; short ReceiptCode = 0; string ReceiptData = ""; // ищем запрос для квитанции DbConnect.ClearParameters(); DbConnect.AddParameter("Channel", Channel); DbConnect.AddParameter("Serial", Serial); DbConnect.AddParameter("Company", Company); DbConnect.AddParameter("Device", Device); DbConnect.Query = "SELECT id, codeop, data FROM receipt_requests WHERE sernum = @Serial AND mfdev = @Company AND typedev = @Device AND chnum = @Channel AND status = 0 LIMIT 1"; try { Reader = DbConnect.ExecuteReader(); if (Reader.Read()) { ReceiptId = Reader.GetInt64(Reader.GetOrdinal("id")); ReceiptCode = Reader.GetInt16(Reader.GetOrdinal("codeop")); ReceiptData = Reader.GetString(Reader.GetOrdinal("data")).Trim(); } Reader.Close(); } catch (Exception e) { if (Reader != null) { Reader.Close(); } throw new Exception((Properties.Settings.Default.DebugInfo ? e.Message : "Database read error")); } // отмечаем отправляемый запрос WriteData(DbConnect, 1); // читаем и кодируем запрос if (ReceiptId > 0) { Receipt = new Receipt(Company, ReceiptId, ReceiptCode, ReceiptData); Receipt.Read(DbConnect); } }
public void WriteData(DbConnect DbConnect, int Status) { if (Receipt == null) { return; } DbConnect.ClearParameters(); DbConnect.AddParameter("Id", Receipt.Id); DbConnect.AddParameter("Status", Status); DbConnect.Query = "UPDATE receipt_requests SET `status` = @Status WHERE id = @Id"; try { DbConnect.ExecuteNonQuery(); } catch (Exception e) { throw new Exception((Properties.Settings.Default.DebugInfo ? e.Message : "Database write error")); } }
public void WriteData(DbConnect DbConnect) { foreach (Block CurrentBlock in BlockList) { DbConnect.ClearParameters(); DbConnect.AddParameter("Channel", Channel); DbConnect.AddParameter("Serial", Serial); DbConnect.AddParameter("Company", Company); DbConnect.AddParameter("Device", Device); DbConnect.AddParameter("SoftVersion", SoftVersion); DbConnect.AddParameter("HardVersion", HardVersion); DbConnect.AddParameter("Imei", Imei); DbConnect.AddParameter("Phone", Phone); CurrentBlock.Write(DbConnect); } }
public void WriteTest(DbConnect DbConnect) { DbConnect.ClearParameters(); DbConnect.AddParameter("Channel", Channel); DbConnect.AddParameter("Serial", Serial); DbConnect.AddParameter("Company", Company); DbConnect.AddParameter("Device", Device); DbConnect.AddParameter("Code", 5); DbConnect.AddParameter("Data", ""); DbConnect.Query = "INSERT INTO receipt_requests (`ts`,`serNUM`, `mfDEV`, `typeDEV`, `chNUM`, `Status`, `CodeOp`, `Data`) VALUES (now(), @Serial, @Company, @Device, @Channel, 0, @Code, @Data)"; try { DbConnect.ExecuteNonQuery(); } catch (Exception e) { throw new Exception((Properties.Settings.Default.DebugInfo ? e.Message : "Database write error")); } }
public void WritePrefix(DbConnect DbConnect) { DbConnect.ClearParameters(); DbConnect.Query = "INSERT INTO prefix_reports (`session_time`,`prefix0`, `prefix1_3`, `prefix4_5`, `prefix8`, `prefix9_12`, `prefix13`, `prefix14`, `prefix15_22`, `prefix23_26`, `prefix31`, `crc_packet`, `crc_packet_ok`)" + " VALUES (now(), @Id, @TextId, @Size, @Channel, @Serial, @Company, @Device, @Imei, @Phone, @Operation, @Crc, @CorrectCrc)"; DbConnect.AddParameter("Id", Id); DbConnect.AddParameter("TextId", TextId); DbConnect.AddParameter("Size", Size); DbConnect.AddParameter("Channel", Channel); DbConnect.AddParameter("Serial", Serial); DbConnect.AddParameter("Company", Company); DbConnect.AddParameter("Device", Device); DbConnect.AddParameter("Imei", Imei); DbConnect.AddParameter("Phone", Phone); DbConnect.AddParameter("Operation", Operation); DbConnect.AddParameter("Crc", Crc.ToString("X4")); DbConnect.AddParameter("CorrectCrc", (CorrectCrc ? 1 : 0)); DbConnect.ExecuteNonQuery(); }
public void Read(DbConnect DbConnect) { DbDataReader Reader = null; // для начала заполняем нулями, после поверх реальными данными for (int i = 0; i < ChosenReceipt.Size; i++) { Data[i] = 0; } if (ChosenReceipt.Table.Length > 0) { DbConnect.ClearParameters(); DbConnect.AddParameter("Id", Id); DbConnect.Query = "SELECT * FROM " + ChosenReceipt.Table + " WHERE idpx = @Id LIMIT 1"; try { Reader = DbConnect.ExecuteReader(); if (!Reader.Read()) { Reader.Close(); Reader = null; throw new Exception("Request read error"); } } catch (Exception e) { if (Reader != null) { Reader.Close(); } throw new Exception((Properties.Settings.Default.DebugInfo ? e.Message : "Database read error")); } } foreach (Protocol.StructValue CurrentValue in ChosenReceipt.ValueList) { if (CurrentValue.Type.Length == 0 || CurrentValue.Size == 0) { // неизвестное поле throw new Exception("Unknown type: " + CurrentValue.Name); } else if (CurrentValue.Parameter.Length > 0) { // поле из основной таблицы string Parameter; // получаем строку по индексу try { Parameter = GetParameter(Parameters, int.Parse(CurrentValue.Parameter)); } catch (Exception e) { throw new Exception((Properties.Settings.Default.DebugInfo ? e.Message : "Parameter read error")); } // преобразуем к типу, кодируем, сохраняем в буфер квитанции if (CurrentValue.Type == "uint" && CurrentValue.Size == 1) { Array.Copy(System.BitConverter.GetBytes(Convert.ToByte(Parameter, CurrentValue.Base)), 0, Data, CurrentValue.Offset, CurrentValue.Size); } else if (CurrentValue.Type == "uint" && CurrentValue.Size == 2) { Array.Copy(System.BitConverter.GetBytes(Convert.ToInt16(Parameter, CurrentValue.Base)), 0, Data, CurrentValue.Offset, CurrentValue.Size); } else if (CurrentValue.Type == "uint" && CurrentValue.Size == 4) { Array.Copy(System.BitConverter.GetBytes(Convert.ToInt32(Parameter, CurrentValue.Base)), 0, Data, CurrentValue.Offset, CurrentValue.Size); } else if (CurrentValue.Type == "uint" && CurrentValue.Size == 8) { Array.Copy(System.BitConverter.GetBytes(Convert.ToInt64(Parameter, CurrentValue.Base)), 0, Data, CurrentValue.Offset, CurrentValue.Size); } else if (CurrentValue.Type == "float" && CurrentValue.Size == 4) { Array.Copy(System.BitConverter.GetBytes(Single.Parse(Parameter)), 0, Data, CurrentValue.Offset, CurrentValue.Size); } else if (CurrentValue.Type == "datetime" && CurrentValue.Size == 4) { Array.Copy(Converter.FromDateTime32(DateTime.ParseExact(Parameter, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture)), 0, Data, CurrentValue.Offset, CurrentValue.Size); } else if (CurrentValue.Type == "char" && CurrentValue.Size > 0) { if (Parameter.Length != CurrentValue.Size * 2) { throw new Exception("Error in hex value"); } Array.Copy(Converter.StringToByteArray(Parameter), 0, Data, CurrentValue.Offset, CurrentValue.Size); } else { throw new Exception("Unknown type: " + CurrentValue.Type + " of " + CurrentValue.Size.ToString() + "bytes"); } } else if (CurrentValue.Field.Length > 0 && Reader != null) { // поле из дополнительной таблицы int Ordinal; // проверяем наличие поля try { Ordinal = Reader.GetOrdinal(CurrentValue.Field); } catch (Exception e) { throw new Exception((Properties.Settings.Default.DebugInfo ? e.Message : "Field read error")); } // получаем значение, кодируем, сохраняем в буфер квитанции if (CurrentValue.Type == "uint" && CurrentValue.Size == 1) { Array.Copy(System.BitConverter.GetBytes(Reader.GetByte(Ordinal)), 0, Data, CurrentValue.Offset, CurrentValue.Size); } else if (CurrentValue.Type == "uint" && CurrentValue.Size == 2) { Array.Copy(System.BitConverter.GetBytes(Reader.GetInt16(Ordinal)), 0, Data, CurrentValue.Offset, CurrentValue.Size); } else if (CurrentValue.Type == "uint" && CurrentValue.Size == 4) { Array.Copy(System.BitConverter.GetBytes(Reader.GetInt32(Ordinal)), 0, Data, CurrentValue.Offset, CurrentValue.Size); } else if (CurrentValue.Type == "uint" && CurrentValue.Size == 8) { Array.Copy(System.BitConverter.GetBytes(Reader.GetInt64(Ordinal)), 0, Data, CurrentValue.Offset, CurrentValue.Size); } else if (CurrentValue.Type == "float" && CurrentValue.Size == 4) { Array.Copy(System.BitConverter.GetBytes(Reader.GetFloat(Ordinal)), 0, Data, CurrentValue.Offset, CurrentValue.Size); } else if (CurrentValue.Type == "datetime" && CurrentValue.Size == 4) { Array.Copy(Converter.FromDateTime32(Reader.GetDateTime(Ordinal)), 0, Data, CurrentValue.Offset, CurrentValue.Size); } else if (CurrentValue.Type == "char" && CurrentValue.Size > 0) { string str = Reader.GetString(Ordinal).Trim(); if (str.Length != CurrentValue.Size * 2) { throw new Exception("Error in hex value"); } Array.Copy(Converter.StringToByteArray(str), 0, Data, CurrentValue.Offset, CurrentValue.Size); } else if (CurrentValue.Type == "byte" && CurrentValue.Size > 0) { Reader.GetBytes(Ordinal, 0, Data, CurrentValue.Offset, CurrentValue.Size); } else { throw new Exception("Unknown type: " + CurrentValue.Type + " of " + CurrentValue.Size.ToString() + "bytes"); } } else if (CurrentValue.Field.Length > 0 && Reader == null) { // поле из дополнительной таблицы // запись не найдена - поля не заполняем } else { // неизвестное поле throw new Exception("Unknown value: " + CurrentValue.Name); } } if (Reader != null) { Reader.Close(); } }
public void Write(DbConnect DbConnect) { string FieldString = ""; string ParameterString = ""; foreach (Protocol.StructValue CurrentValue in ChosenBlock.ValueList) { // сверяем значение флага if (!CurrentValue.CheckFlag(Data, Size)) { continue; } if (CurrentValue.Field.Length == 0) { // неизвестное поле throw new Exception("Unknown field: " + CurrentValue.Name); } else if (CurrentValue.Parameter.Length > 0) { // поле из префикса if (!DbConnect.CheckParameter(CurrentValue.Parameter)) { throw new Exception("Unknown parameter: " + CurrentValue.Name); } FieldString += (FieldString.Length > 0 ? "," : "") + "`" + CurrentValue.Field + "`"; ParameterString += (ParameterString.Length > 0 ? "," : "") + "@" + CurrentValue.Parameter; } else if (CurrentValue.Type.Length > 0 && CurrentValue.Size > 0) { // поле из блока, распознаем тип и длину if (CurrentValue.Offset + CurrentValue.Size >= Size) { throw new Exception("Value is out of block limits: " + CurrentValue.Name); } if (CurrentValue.Type == "uint" && CurrentValue.Size == 1) { DbConnect.AddParameter("_" + CurrentValue.Field, Data[CurrentValue.Offset]); } else if (CurrentValue.Type == "uint" && CurrentValue.Size == 2) { DbConnect.AddParameter("_" + CurrentValue.Field, System.BitConverter.ToUInt16(Data, CurrentValue.Offset)); } else if (CurrentValue.Type == "uint" && CurrentValue.Size == 4) { DbConnect.AddParameter("_" + CurrentValue.Field, System.BitConverter.ToUInt32(Data, CurrentValue.Offset)); } else if (CurrentValue.Type == "uint" && CurrentValue.Size == 8) { DbConnect.AddParameter("_" + CurrentValue.Field, System.BitConverter.ToUInt64(Data, CurrentValue.Offset)); } else if (CurrentValue.Type == "float" && CurrentValue.Size == 4) { if (BitConverter.ToString(Data, CurrentValue.Offset, 4) == "00-00-C0-7F") { continue; } DbConnect.AddParameter("_" + CurrentValue.Field, System.BitConverter.ToSingle(Data, CurrentValue.Offset)); } else if (CurrentValue.Type == "datetime" && CurrentValue.Size == 4) { DbConnect.AddParameter("_" + CurrentValue.Field, Converter.ToDateTime32(Data, CurrentValue.Offset)); } else if (CurrentValue.Type == "char" && CurrentValue.Size > 0) { DbConnect.AddParameter("_" + CurrentValue.Field, System.BitConverter.ToString(Data, CurrentValue.Offset, CurrentValue.Size).Replace("-", string.Empty)); } else if (CurrentValue.Type == "byte" && CurrentValue.Size > 0) { byte[] arr = new byte[CurrentValue.Size]; Array.Copy(Data, CurrentValue.Offset, arr, 0, CurrentValue.Size); DbConnect.AddParameter("_" + CurrentValue.Field, arr); } else { throw new Exception("Unknown type: " + CurrentValue.Type + " of " + CurrentValue.Size.ToString() + "bytes"); } FieldString += (FieldString.Length > 0 ? "," : "") + "`" + CurrentValue.Field + "`"; ParameterString += (ParameterString.Length > 0 ? "," : "") + "@_" + CurrentValue.Field; } else { // неизвестное поле throw new Exception("Unknown value: " + CurrentValue.Name); } } if (FieldString.Length > 0) { DbConnect.Query = "INSERT INTO " + ChosenBlock.Table + " (" + FieldString + ") VALUES (" + ParameterString + ")"; try { DbConnect.ExecuteNonQuery(); } catch (Exception e) { throw new Exception((Properties.Settings.Default.DebugInfo ? e.Message : "Database write error")); } } }