Sayfayı Yazdır | Pencereyi Kapat

MQTT belirtilen anahtar bulunamadı problemi

Nereden Yazdırıldığı: Clomosy | Forum
Kategori: Genel Programlama
Forum Adı: Genel İşlemler
Forum Tanımlaması: TRObject dili ile programlama yaparken karşılaşılan genel işlemler
URL: https://forum.clomosy.com.tr/forum_posts.asp?TID=1069
Tarih: 12 Temmuz 2025 Saat 23:49
Program Versiyonu: Web Wiz Forums 12.07 - https://www.webwizforums.com


Konu: MQTT belirtilen anahtar bulunamadı problemi
Mesajı Yazan: Mahmutdmr
Konu: MQTT belirtilen anahtar bulunamadı problemi
Mesaj Tarihi: 08 Temmuz 2025 Saat 18:35
kayıt işlemi id'siz mqtt ile diğer cihaza kayıt işlemini gerçekleştiriyor ancak giriş-çıkış zamanları ve lokasyon bilgilerini diğer cihaza yansıtamıyorum. Aldığım hata ise "belirtilen anahtar bulunamadı "oluyor ilk olarak veri tablomu daha sonrasında MQTT bağlantısı olan sayfada ki prosedürleri ve  giriş-çıkış işlemlerinin olduğu sayfa da ki prosedürleri sizinle paylaşacağım  sorunun id den kaynaklandığını düşünüyorum ama ne yaptıysam olmadı yardımcı olursanız sevinirim 

uses
anaEkran;
var
  database, password: String;

{
  clomosy.rununit('anaEkran');
  database = Clomosy.AppFilesPath + 'database1.db3';
  password = '';

  try
    Clomosy.DBSQLiteConnect(database, password);

    // USERS tablosu
    Clomosy.DBSQLiteQuery.Sql.Text = 'CREATE TABLE IF NOT EXISTS users (' +
      'id INTEGER PRIMARY KEY AUTOINCREMENT, ' +
      'email TEXT NOT NULL UNIQUE, ' +
      'password TEXT NOT NULL, ' +
      'kullanici_adi TEXT)';
    Clomosy.DBSQLiteQuery.OpenorExecute;

    // CHECKINS tablosu
    Clomosy.DBSQLiteQuery.Sql.Text = 'CREATE TABLE IF NOT EXISTS checkins (' +
      'id INTEGER PRIMARY KEY AUTOINCREMENT, ' +
      'user_id INTEGER, ' +
      'checkin_time TEXT, ' +
      'checkout_time TEXT, ' +
      'checkin_location TEXT, ' +
      'checkout_location TEXT)';
    Clomosy.DBSQLiteQuery.OpenorExecute;

    // ADMINS tablosu
    Clomosy.DBSQLiteQuery.Sql.Text = 'CREATE TABLE IF NOT EXISTS admins (' +
      'id INTEGER PRIMARY KEY AUTOINCREMENT, ' +
      'email TEXT NOT NULL UNIQUE, ' +
      'password TEXT NOT NULL)';
    Clomosy.DBSQLiteQuery.OpenorExecute;

    // Admin hesabı ekleniyor (sadece ilk çalıştırmada eklenir)
    Clomosy.DBSQLiteQuery.Sql.Text = 'INSERT OR IGNORE INTO admins (email, password) VALUES ("[email protected]", "123456")';
    Clomosy.DBSQLiteQuery.OpenorExecute;

    ShowMessage('Tablolar başarıyla oluşturuldu');
  except
    ShowMessage('Veritabanı oluşturulamadı: '+LastExceptionMessage);
  }
}











void ParseAndApplyMQTTMessage
{
  // Gelen mesajı ekrana yazdır
  ShowMessage('GELEN MQTT MESAJI: ' + gelenMesaj);

  // Önce action ve table çek
  action = Clomosy.CLParseJSON(gelenMesaj, 'action');
  table  = Clomosy.CLParseJSON(gelenMesaj, 'table');

  // Sadece tabloya göre ilgili alanları çek
  if table == 'users'
  {
    kullanici_adi = Clomosy.CLParseJSON(gelenMesaj, 'kullanici_adi');
    email         = Clomosy.CLParseJSON(gelenMesaj, 'email');
    password      = Clomosy.CLParseJSON(gelenMesaj, 'password');

    if action == 'insert'
    {
      sql = 'INSERT INTO users (kullanici_adi, email, password) VALUES (' +
            QuotedStr(kullanici_adi) + ', ' + QuotedStr(email) + ', ' + QuotedStr(password) + ')';
      Clomosy.DBSQLiteQuery.Sql.Text = sql;
      Clomosy.DBSQLiteQuery.OpenOrExecute;
      ShowMessage('[MQTT] Kullanıcı eklendi: ' + kullanici_adi + ' / ' + email);
    }
    // update, delete işlemleri için de alanları ekle
  }
  else if table == 'checkins'
  {
    id                = Clomosy.CLParseJSON(gelenMesaj, 'id');
    user_id           = Clomosy.CLParseJSON(gelenMesaj, 'user_id');
    checkin_time      = Clomosy.CLParseJSON(gelenMesaj, 'checkin_time');
    checkout_time     = Clomosy.CLParseJSON(gelenMesaj, 'checkout_time');
    checkin_location  = Clomosy.CLParseJSON(gelenMesaj, 'checkin_location');
    checkout_location = Clomosy.CLParseJSON(gelenMesaj, 'checkout_location');

    if action == 'insert'
    {
      sql = 'INSERT INTO checkins (user_id, checkin_time, checkin_location) VALUES (' +
            QuotedStr(user_id) + ', ' + QuotedStr(checkin_time) + ', ' + QuotedStr(checkin_location) + ')';
      Clomosy.DBSQLiteQuery.Sql.Text = sql;
      Clomosy.DBSQLiteQuery.OpenOrExecute;
    }
    else if action == 'update'
    {
      sql = 'UPDATE checkins SET checkout_time = ' + QuotedStr(checkout_time) +
            ', checkout_location = ' + QuotedStr(checkout_location) +
            ' WHERE id = ' + QuotedStr(id);
      Clomosy.DBSQLiteQuery.Sql.Text = sql;
      Clomosy.DBSQLiteQuery.OpenOrExecute;
    }
    else if action == 'delete'
    {
      sql = 'DELETE FROM checkins WHERE id = ' + QuotedStr(id);
      Clomosy.DBSQLiteQuery.Sql.Text = sql;
      Clomosy.DBSQLiteQuery.OpenOrExecute;
    }
  }
}
void OnLoginClick;
{
  Clomosy.RunUnit('uLogin');
}

void OnRegisterClick;
{
  Clomosy.RunUnit('uRegister');
}

void OnMembersClick;
{
  Clomosy.RunUnit('uAdminLog');
}

void OnQrOkuyucuClick;
{
  if (Clomosy.PlatformIsMobile)
    ShowMessage('Yalnızca sorumlu ekranda çıkar');
  else
    Clomosy.RunUnit('uQrKodu');
}

void OnMQTTPublishReceived
{
  if (MQTT1.ReceivedAlright)
  {
    gelenMesaj = MQTT1.ReceivedMessage;
    ParseAndApplyMQTTMessage;
  }
}













void KonumGirisClicked {
  Clomosy.GetCurrentLocation;
  latitude  = clGetStringTo(Clomosy.LocationValue, '|');
  longitude = clGetStringAfter(Clomosy.LocationValue, '|');
  konumGirisEdit.Text = latitude + '|' + longitude;
}


void KonumCikisClicked {
  Clomosy.GetCurrentLocation;
  latitude  = clGetStringTo(Clomosy.LocationValue, '|');
  longitude = clGetStringAfter(Clomosy.LocationValue, '|');
  konumCikisEdit.Text = latitude + '|' + longitude;
}


void GirisQrClicked {
  qrForm.CallBarcodeReader(girisEdit);
}


void GirisKaydet {
  aktifKullaniciId = Clomosy.GlobalVariableInteger;
  qrSaat = girisEdit.Text;
  konum = konumGirisEdit.Text;


  if qrSaat == '' {
    ShowMessage('QR kodu bilgisi alınmadı!');
    Exit;
  }
  if konum == '' {
    ShowMessage('Konum bilgisi alınmadı!');
    Exit;
  }

  Clomosy.DBSQLiteQuery.SQL.Text =
    'SELECT id FROM checkins ' +
    'WHERE user_id = ' + IntToStr(aktifKullaniciId) +
    ' AND (checkout_time IS NULL OR checkout_time = '''') ';
  Clomosy.DBSQLiteQuery.OpenOrExecute;

  if Clomosy.DBSQLiteQuery.Found {
    ShowMessage('Daha önce yapılmış bir giriş var, çıkış yapılmadan tekrar giriş yapılamaz!');
    Exit;
  }

  Clomosy.DBSQLiteQuery.SQL.Text =
    'INSERT INTO checkins (user_id, checkin_time, checkin_location) VALUES (' +
    IntToStr(aktifKullaniciId) + ', ' + QuotedStr(qrSaat) + ', ' +
    QuotedStr(konum) + ')';
  Clomosy.DBSQLiteQuery.OpenOrExecute;

  ShowMessage('Giriş ve konum başarıyla kaydedildi!');
  girisEdit.Text = '';
  konumGirisEdit.Text = '';

  if (MQTT1.Connected) {
    jsonMesaj = '{"action":"insert","table":"checkins",' +
      '"user_id":"' + IntToStr(aktifKullaniciId) + '",' +
      '"checkin_time":"' + qrSaat + '",' +
      '"checkin_location":"' + konum + '"}';
    MQTT1.Send(jsonMesaj);
  }
}


void CikisQrClicked {
  qrForm.CallBarcodeReader(cikisEdit);
}

void CikisKaydet {
  aktifKullaniciId = Clomosy.GlobalVariableInteger;
  qrSaat = cikisEdit.Text;
  konum = konumCikisEdit.Text;


  if qrSaat == '' {
    ShowMessage('QR kodu bilgisi alınmadı!');
    Exit;
  }
  if konum == '' {
    ShowMessage('Konum bilgisi alınmadı!');
    Exit;
  }

  Clomosy.DBSQLiteQuery.SQL.Text =
    'SELECT id FROM checkins ' +
    'WHERE user_id = ' + IntToStr(aktifKullaniciId) +
    ' AND (checkout_time IS NULL OR checkout_time = '''') ' +
    'ORDER BY checkin_time ASC LIMIT 1';
  Clomosy.DBSQLiteQuery.OpenOrExecute;

  if (not Clomosy.DBSQLiteQuery.Found) {
    ShowMessage('Önce giriş yapılmamış ya da çıkış zaten kaydedilmiş!');
    Exit;
  }

  kayitId = Clomosy.DBSQLiteQuery.FieldByName('id').AsInteger;

  Clomosy.DBSQLiteQuery.SQL.Text =
    'UPDATE checkins SET checkout_time = ' + QuotedStr(qrSaat) +
    ', checkout_location = ' + QuotedStr(konum) +
    ' WHERE id = ' + IntToStr(kayitId) +
    ' AND (checkout_time IS NULL OR checkout_time = '''')';
  Clomosy.DBSQLiteQuery.OpenOrExecute;

  ShowMessage('Çıkış ve konum başarıyla kaydedildi!');
  cikisEdit.Text = '';
  konumCikisEdit.Text = '';

  if (MQTT1.Connected) {
    jsonMesaj = '{"action":"update","table":"checkins",' +
      '"id":"' + IntToStr(kayitId) + '",' +
      '"checkout_time":"' + qrSaat + '",' +
      '"checkout_location":"' + konum + '"}';
    MQTT1.Send(jsonMesaj);
  }
}





Cevaplar:
Mesajı Yazan: ztashia
Mesaj Tarihi: 09 Temmuz 2025 Saat 13:41
Merhabalar,
mqtt mesajini kontrol ederken checkin yaparken checkout time ve location almaya calisiyorsunuz fakat o field'lari gondermediginizde anahtar bulunamadi dönüyor aynı olay tam tersi checkout yaparkende geçerli. Çözüm olarak aşağıda gönderirken her field'ı gönderdim.

if (MQTT1.Connected) {
    jsonMesaj = '{"action":"insert","table":"checkins",' +
      '"user_id":"' + IntToStr(aktifKullaniciId) + '",' +
      '"checkin_time":"' + qrSaat + '",' +
      '"checkout_time":"",' +
      '"checkout_location":"",' +
      '"checkin_location":"' + konum + '"}';
    MQTT1.Send(jsonMesaj);
  }

  if (MQTT1.Connected) {
    jsonMesaj = '{"action":"update","table":"checkins",' +
      '"id":"' + IntToStr(kayitId) + '",' +
      '"checkout_time":"' + qrSaat + '",' +
      '"checkin_time":"",' +
      '"checkin_location":"",' +
      '"checkout_location":"' + konum + '"}';
    MQTT1.Send(jsonMesaj);
  }



Sayfayı Yazdır | Pencereyi Kapat

Forum Software by Web Wiz Forums® version 12.07 - https://www.webwizforums.com
Copyright ©2001-2024 Web Wiz Ltd. - https://www.webwiz.net