Bir cihazdan kayıt işlemi (örneğin giriş/çıkış ya da kullanıcı ekleme) gerçekleştirdiğimde, o cihazda kayıt işlemi başarılı bir şekilde veritabanına işleniyor. Aynı işlem sırasında diğer cihazda MQTT üzerinden mesaj geldiğini ShowMessage ile görebiliyorum, fakat o cihazda mesaj gelmesine rağmen veritabanına kayıt işlemi gerçekleşmiyor. MQTT bağlantısını anaEkran (menü) unitinde kurdum ve mesajları burada dinliyorum. Her iki cihazda da aynı kod çalışıyor ve aynı topic'e aboneyiz. Sorum ise: Mesaj diğer cihaza ulaşmasına rağmen neden veritabanına kayıt işlemi gerçekleşmiyor olabilir? Ana ekranda MQTT bağlantısı kurmamda bir sakınca var mı, yoksa farklı bir birimde mi kurmam gerekir?
var anaEkranForm: TclForm; pnlContainer: TclProPanel; lblWelcome: TclProLabel; btnLogin, btnRegister, btnMembers, btnQrOkuyucu: TclProButton; MQTT1: TclMQTT; gelenMesaj: string; action, table, id, user_id, checkin_time, checkout_time, kullanici_adi, email, password: string; subStr: string; sql: string; mesaj: string; pos1, pos2: integer; temp: string; checkin_location, checkout_location: string;
void ParseAndApplyMQTTMessage { mesaj = gelenMesaj; action = ''; table = ''; id = ''; user_id = ''; checkin_time = ''; checkout_time = ''; checkin_location = ''; checkout_location = ''; kullanici_adi = ''; email = ''; password = '';
// Basit string ayrıştırma if Pos('"action":"insert"', mesaj) > 0 action = 'insert'; else if Pos('"action":"update"', mesaj) > 0 action = 'update'; else if Pos('"action":"delete"', mesaj) > 0 action = 'delete';
if Pos('"table":"checkins"', mesaj) > 0 table = 'checkins'; else if Pos('"table":"users"', mesaj) > 0 table = 'users';
// id subStr = '"id":"'; pos1 = Pos(subStr, mesaj); if pos1 > 0 { temp = Copy(mesaj, pos1 + Length(subStr), Length(mesaj)); pos2 = Pos('"', temp); id = Copy(temp, 1, pos2 - 1); }
// user_id subStr = '"user_id":"'; pos1 = Pos(subStr, mesaj); if pos1 > 0 { temp = Copy(mesaj, pos1 + Length(subStr), Length(mesaj)); pos2 = Pos('"', temp); user_id = Copy(temp, 1, pos2 - 1); }
subStr = '"checkin_time":"'; pos1 = Pos(subStr, mesaj); if pos1 > 0 { temp = Copy(mesaj, pos1 + Length(subStr), Length(mesaj)); pos2 = Pos('"', temp); checkin_time = Copy(temp, 1, pos2 - 1); }
subStr = '"checkout_time":"'; pos1 = Pos(subStr, mesaj); if pos1 > 0 { temp = Copy(mesaj, pos1 + Length(subStr), Length(mesaj)); pos2 = Pos('"', temp); checkout_time = Copy(temp, 1, pos2 - 1); }
subStr = '"checkin_location":"'; pos1 = Pos(subStr, mesaj); if pos1 > 0 { temp = Copy(mesaj, pos1 + Length(subStr), Length(mesaj)); pos2 = Pos('"', temp); checkin_location = Copy(temp, 1, pos2 - 1); }
subStr = '"checkout_location":"'; pos1 = Pos(subStr, mesaj); if pos1 > 0 { temp = Copy(mesaj, pos1 + Length(subStr), Length(mesaj)); pos2 = Pos('"', temp); checkout_location = Copy(temp, 1, pos2 - 1); }
subStr = '"kullanici_adi":"'; pos1 = Pos(subStr, mesaj); if pos1 > 0 { temp = Copy(mesaj, pos1 + Length(subStr), Length(mesaj)); pos2 = Pos('"', temp); kullanici_adi = Copy(temp, 1, pos2 - 1); }
subStr = '"email":"'; pos1 = Pos(subStr, mesaj); if pos1 > 0 { temp = Copy(mesaj, pos1 + Length(subStr), Length(mesaj)); pos2 = Pos('"', temp); email = Copy(temp, 1, pos2 - 1); }
subStr = '"password":"'; pos1 = Pos(subStr, mesaj); if pos1 > 0 { temp = Copy(mesaj, pos1 + Length(subStr), Length(mesaj)); pos2 = Pos('"', temp); password = Copy(temp, 1, pos2 - 1); }
// SQL işlemleri if table == 'checkins' { 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; } } else if table == 'users' && action == 'insert' { try 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); except ShowMessage('[MQTT] Kullanıcı eklenemedi: ' + LastExceptionMessage); } } } 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; ShowMessage('MQTT ile gelen mesaj: ' + gelenMesaj); ParseAndApplyMQTTMessage; } }
{ anaEkranForm = TclForm.Create(Self); anaEkranForm.ClSetCaption('Hoş Geldiniz'); anaEkranForm.SetFormColor('#e0f7fa', '#a8e063', clGVertical);
pnlContainer = anaEkranForm.AddNewProPanel(anaEkranForm, 'pnlContainer'); pnlContainer.Align = alCenter; pnlContainer.Width = anaEkranForm.clWidth * 0.8; pnlContainer.Height = anaEkranForm.clHeight * 0.6; pnlContainer.clProSettings.BackgroundColor = clAlphaColor.clHexToColor('#FFFFFFDD'); pnlContainer.clProSettings.BorderWidth = 0; pnlContainer.clProSettings.RoundWidth = 30; pnlContainer.clProSettings.RoundHeight = 30; pnlContainer.SetclProSettings(pnlContainer.clProSettings);
MQTT1 = anaEkranForm.AddNewMQTTConnection(pnlContainer,'MQTT1'); MQTT1.Channel = 'clomosy/qrTakip'; MQTT1.Connect; anaEkranForm.AddNewEvent(MQTT1, tbeOnMQTTPublishReceived, 'OnMQTTPublishReceived');
lblWelcome = anaEkranForm.AddNewProLabel(pnlContainer, 'lblWelcome', 'Qr Giriş-Çıkış Takip Sistemi'); lblWelcome.Align = alTop; lblWelcome.Height = 60; lblWelcome.Margins.Top = 20; clComponent.SetupComponent(lblWelcome, '{"TextSize":24,"TextColor":"#388e3c","TextBold":"yes","TextHorizontalAlign":"center"}');
btnLogin = anaEkranForm.AddNewProButton(pnlContainer, 'btnLogin', 'GİRİŞ YAP'); btnLogin.Align = alTop; btnLogin.Height = 50; btnLogin.Margins.Top = 30; clComponent.SetupComponent(btnLogin, '{"BackgroundColor":"#ffffff","TextColor":"#43a047","BorderColor":"#43a047","BorderWidth":2,"RoundWidth":20,"RoundHeight":20,"TextSize":16}'); anaEkranForm.AddNewEvent(btnLogin, tbeOnClick, 'OnLoginClick');
btnRegister = anaEkranForm.AddNewProButton(pnlContainer, 'btnRegister', 'ÜYE OL'); btnRegister.Align = alTop; btnRegister.Height = 50; btnRegister.Margins.Top = 15; clComponent.SetupComponent(btnRegister, '{"BackgroundColor":"#ffffff","TextColor":"#43a047","BorderColor":"#43a047","BorderWidth":2,"RoundWidth":20,"RoundHeight":20,"TextSize":16}'); anaEkranForm.AddNewEvent(btnRegister, tbeOnClick, 'OnRegisterClick');
btnQrOkuyucu = anaEkranForm.AddNewProButton(pnlContainer, 'btnQrOkuyucu', 'QR KODU'); btnQrOkuyucu.Align = alTop; btnQrOkuyucu.Height = 50; btnQrOkuyucu.Margins.Top = 15; clComponent.SetupComponent(btnQrOkuyucu, '{"BackgroundColor":"#ffffff","TextColor":"#43a047","BorderColor":"#43a047","BorderWidth":2,"RoundWidth":20,"RoundHeight":20,"TextSize":16}'); anaEkranForm.AddNewEvent(btnQrOkuyucu, tbeOnClick, 'OnQrOkuyucuClick');
btnMembers = anaEkranForm.AddNewProButton(pnlContainer, 'btnMembers', 'Personeller'); btnMembers.Align = alBottom; btnMembers.Height = 40; btnMembers.Margins.Bottom = 20; clComponent.SetupComponent(btnMembers, '{"BackgroundColor":"#ffffff","TextColor":"#43a047","BorderColor":"#43a047","BorderWidth":2,"RoundWidth":20,"RoundHeight":20,"TextSize":16}'); anaEkranForm.AddNewEvent(btnMembers, tbeOnClick, 'OnMembersClick');
anaEkranForm.Run; } örneğin bir kullanıcıyı kayıt ettiğimi varsayarak uRegister unitimde üye ekleme prosedürüne eklediğim kod ise : void uyeEkle { Clomosy.DBSQLiteQuery.Sql.Text = 'INSERT INTO users (kullanici_adi, email, password) VALUES (' + QuotedStr(kullaniciAdiEdit.Text) + ', ' + QuotedStr(epostaEdit.Text) + ', ' + QuotedStr(sifreEdit.Text) + ')'; Clomosy.DBSQLiteQuery.openorExecute; ShowMessage('Üye Olundu..');
// MQTT ile diğer cihazlara bildir if (MQTT1.Connected) { jsonMesaj = '{"action":"insert","table":"users",' + '"kullanici_adi":"' + kullaniciAdiEdit.Text + '",' + '"email":"' + epostaEdit.Text + '",' + '"password":"' + sifreEdit.Text + '"}'; MQTT1.Send(jsonMesaj); }
|