unit uConfigNILM; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, DB, ZAbstractRODataset, ZDataset, Grids, BaseGrid, AdvGrid, ExtCtrls, StdCtrls, AdvUtil, AdvObj; type TfrmNILM = class(TForm) AdvStringGrid1: TAdvStringGrid; ZReadOnlyQuery1: TZReadOnlyQuery; Panel1: TPanel; Label1: TLabel; Label2: TLabel; Label3: TLabel; Label4: TLabel; Label5: TLabel; Label6: TLabel; Label7: TLabel; Label8: TLabel; Edit1: TEdit; Edit3: TEdit; Edit4: TEdit; Edit5: TEdit; Edit6: TEdit; btnAdd: TButton; btnEdit: TButton; btnDelete: TButton; ComboBox1: TComboBox; Edit2: TEdit; Edit7: TEdit; procedure FormShow(Sender: TObject); procedure AdvStringGrid1ClickCell(Sender: TObject; ARow, ACol: Integer); procedure btnAddClick(Sender: TObject); procedure btnEditClick(Sender: TObject); procedure btnDeleteClick(Sender: TObject); private { Private declarations } select_nilm_idxNo : integer; procedure CreateTableNilmValueLog(nilm_idxno : integer); procedure DropTableNilmValueLog(nilm_idxno : integer); function CheckNILM(nilmID : integer) : Boolean; function Get_NILM_idxNo(nilmID : integer) : integer; public { Public declarations } procedure ShowNILM(); end; var frmNILM: TfrmNILM; implementation uses uMain, uDevConfig; {$R *.dfm} procedure TfrmNILM.DropTableNilmValueLog(nilm_idxno : integer); var sql : string; begin sql := ''; sql := 'DROP TABLE IF EXISTS nilm_'+format('%.5d',[nilm_idxno]); frmMain.insertSQL(sql); end; procedure TfrmNILM.CreateTableNilmValueLog(nilm_idxno : integer); var sql : string; begin sql := ''; sql := 'CREATE TABLE IF NOT EXISTS nilm_'+format('%.5d',[nilm_idxno])+' ('+ 'aNo int(11) NOT NULL AUTO_INCREMENT,'+ 'NilmID int(11) DEFAULT NULL,'+ 'pf_1 float DEFAULT NULL,'+ 'CorrectFactor_1 float DEFAULT NULL,'+ 'Vrms_1 float DEFAULT NULL,'+ 'Irms_1 float DEFAULT NULL,'+ 'Var_1 float DEFAULT NULL,'+ 'Va_1 float DEFAULT NULL,'+ 'Pwr_1 float DEFAULT NULL,'+ 'pf_2 float DEFAULT NULL,'+ 'CorrectFactor_2 float DEFAULT NULL,'+ 'Vrms_2 float DEFAULT NULL,'+ 'Irms_2 float DEFAULT NULL,'+ 'Var_2 float DEFAULT NULL,'+ 'Va_2 float DEFAULT NULL,'+ 'Pwr_2 float DEFAULT NULL,'+ 'pf_3 float DEFAULT NULL,'+ 'CorrectFactor_3 float DEFAULT NULL,'+ 'Vrms_3 float DEFAULT NULL,'+ 'Irms_3 float DEFAULT NULL,'+ 'Var_3 float DEFAULT NULL,'+ 'Va_3 float DEFAULT NULL,'+ 'Pwr_3 float DEFAULT NULL,'+ 'gX float DEFAULT NULL,'+ 'gY float DEFAULT NULL,'+ 'gZ float DEFAULT NULL,'+ 'aX float DEFAULT NULL,'+ 'aY float DEFAULT NULL,'+ 'aZ float DEFAULT NULL,'+ 'TempC float DEFAULT NULL,'+ 'lastUpdate datetime DEFAULT NULL,'+ 'UNIQUE KEY aNo (aNo)'+ ') ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci'; frmMain.insertSQL(sql); end; procedure TfrmNILM.ShowNILM(); var i, idxNo, nilmID, nilmType : integer; nilmName, S : string; srcAddr, srcMac, dstAddr, topic : string; dstPort : integer; begin select_nilm_idxNo := 0; try frmMain.NILM_ID_List.Clear; try if not frmMain.ZConnection1.Connected then frmMain.ZConnection1.Connect(); if frmMain.ZConnection1.Connected then begin ZReadOnlyQuery1.Active := false; ZReadOnlyQuery1.SQL.Clear; ZReadOnlyQuery1.SQL.Add('SELECT * FROM nilm_info'); ZReadOnlyQuery1.Open; if ZReadOnlyQuery1.RecordCount> 0 then begin ZReadOnlyQuery1.First; i := 0; frmDevConfig.cb_nilm.items.clear; AdvStringGrid1.RowCount := ZReadOnlyQuery1.RecordCount + 1; While not ZReadOnlyQuery1.Eof do begin Inc(i, 1); idxNo := ZReadOnlyQuery1.FieldByName('idxNo').asInteger; nilmID := ZReadOnlyQuery1.FieldByName('NilmID').asInteger; frmMain.NILM_ID_List.Add(nilmID); // frmMain.NILM_Map.Add(nilmID,i); nilmType := ZReadOnlyQuery1.FieldByName('nilmType').asInteger; dstPort := ZReadOnlyQuery1.FieldByName('dstPort').asInteger; srcAddr := ZReadOnlyQuery1.FieldByName('srcAddr').asString; // ½Ã¸®¾ó³Ñ¹ö·Î º¯°æ_20250905 srcMac := ZReadOnlyQuery1.FieldByName('srcMac').asString; dstAddr := ZReadOnlyQuery1.FieldByName('dstAddr').asString; topic := ZReadOnlyQuery1.FieldByName('topic').asString; nilmName := ZReadOnlyQuery1.FieldByName('nilmName').asString; AdvStringGrid1.Cells[0, i] := inttostr(idxNo); AdvStringGrid1.Cells[1, i] := inttostr(nilmID); AdvStringGrid1.Cells[2, i] := nilmName; AdvStringGrid1.Cells[3, i] := inttostr(nilmType); AdvStringGrid1.Cells[4, i] := srcAddr; // ½Ã¸®¾ó³Ñ¹ö·Î º¯°æ_20250905 AdvStringGrid1.Cells[5, i] := srcMac; AdvStringGrid1.Cells[6, i] := dstAddr; AdvStringGrid1.Cells[7, i] := inttostr(dstPort); AdvStringGrid1.Cells[8, i] := topic; frmMain.fNILM[nilmID]._TOPIC := topic; frmMain.fNILM[nilmID].idxNo := idxNo; frmMain.fNILM[nilmID]._NAME := nilmName; S := ''; S := format('%d:%s',[nilmID, nilmName]); frmDevConfig.cb_nilm.items.add(S); ZReadOnlyQuery1.Next; end; end; end; finally if frmMain.ZConnection1.Connected then frmMain.ZConnection1.Disconnect; end; except on e : exception do begin frmMain.printlog('Database Connect Error :: '+e.Message, 'DB'); end; end; end; function TfrmNILM.CheckNILM(nilmID : integer) : Boolean; begin result := true; try try if not frmMain.ZConnection1.Connected then frmMain.ZConnection1.Connect(); if frmMain.ZConnection1.Connected then begin ZReadOnlyQuery1.Active := false; ZReadOnlyQuery1.SQL.Clear; ZReadOnlyQuery1.SQL.Add('SELECT * FROM nilm_info WHERE NilmID='+inttostr(nilmID)); ZReadOnlyQuery1.Open; if ZReadOnlyQuery1.RecordCount> 0 then begin result := false; end; end; finally if frmMain.ZConnection1.Connected then frmMain.ZConnection1.Disconnect; end; except on e : exception do begin frmMain.printlog('Database Connect Error :: '+e.Message, 'DB'); end; end; end; function TfrmNILM.Get_NILM_idxNo(nilmID : integer) : integer; begin result := 0; try try if not frmMain.ZConnection1.Connected then frmMain.ZConnection1.Connect(); if frmMain.ZConnection1.Connected then begin ZReadOnlyQuery1.Active := false; ZReadOnlyQuery1.SQL.Clear; ZReadOnlyQuery1.SQL.Add('SELECT * FROM nilm_info WHERE NilmID='+inttostr(nilmID)); ZReadOnlyQuery1.Open; if ZReadOnlyQuery1.RecordCount> 0 then begin result := ZReadOnlyQuery1.FieldByName('idxNo').AsInteger; end; end; finally if frmMain.ZConnection1.Connected then frmMain.ZConnection1.Disconnect; end; except on e : exception do begin frmMain.printlog('Database Connect Error :: '+e.Message, 'DB'); end; end; end; procedure TfrmNILM.FormShow(Sender: TObject); begin ShowNILM(); end; procedure TfrmNILM.AdvStringGrid1ClickCell(Sender: TObject; ARow, ACol: Integer); var nilmType : integer; begin if ARow > 0 then begin select_nilm_idxNo := StrToIntDef(AdvStringGrid1.Cells[0, ARow], 0); Edit2.Text := IntToStr(StrToIntDef(AdvStringGrid1.Cells[1, ARow], 0)); Edit1.Text := AdvStringGrid1.Cells[2, ARow]; nilmType := StrToIntDef(AdvStringGrid1.Cells[3, ARow], 0); ComboBox1.ItemIndex := nilmType; Edit3.Text := AdvStringGrid1.Cells[4, ARow]; Edit4.Text := AdvStringGrid1.Cells[5, ARow]; Edit5.Text := AdvStringGrid1.Cells[6, ARow]; Edit7.Text := IntToStr(StrToIntDef(AdvStringGrid1.Cells[7, ARow], 0)); Edit6.Text := AdvStringGrid1.Cells[8, ARow]; end; end; procedure TfrmNILM.btnAddClick(Sender: TObject); var sql : string; nilm_idxno : integer; begin if CheckNILM(StrToInt(Edit2.Text)) then begin sql := ''; sql := 'INSERT INTO nilm_info VALUES(null, '+ ''+Edit2.Text+', '+ ''+inttostr(ComboBox1.ItemIndex)+', '+ ''''+Edit1.Text+''', '+ ''''+Edit3.Text+''', '+ ''''+Edit4.Text+''', '+ ''''+Edit5.Text+''', '+ ''+Edit7.Text+', '+ ''''+Edit6.Text+''' '+ ')'; frmMain.insertSQL(sql); sql := ''; sql := 'INSERT INTO nilm_value VALUES('+ ''+Edit2.Text+',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,now() '+ ')'; frmMain.insertSQL(sql); nilm_idxno := Get_NILM_idxNo(StrToInt(Edit2.Text)); if nilm_idxno > 0 then CreateTableNilmValueLog(nilm_idxno); ShowNILM(); select_nilm_idxNo := 0; end else begin ShowMessage('ÀÌ¹Ì Á¸ÀçÇÏ´Â NILM ID ÀÔ´Ï´Ù.'); end; end; procedure TfrmNILM.btnEditClick(Sender: TObject); var sql : string; begin if (not CheckNILM(StrToInt(Edit2.Text))) and (select_nilm_idxNo>0) then begin sql := ''; sql := 'UPDATE nilm_info SET '+ 'NilmID='+Edit2.Text+', '+ 'nilmType='+inttostr(ComboBox1.ItemIndex)+', '+ 'nilmName='''+Edit1.Text+''', '+ 'srcAddr='''+Edit3.Text+''', '+ // ½Ã¸®¾ó³Ñ¹ö·Î º¯°æ_20250905 'srcMac='''+Edit4.Text+''', '+ 'dstAddr='''+Edit5.Text+''', '+ 'dstPort='+Edit7.Text+', '+ 'topic='''+Edit6.Text+''' '+ ' WHERE idxNo='+inttostr(select_nilm_idxNo); frmMain.insertSQL(sql); ShowNILM(); select_nilm_idxNo := 0; end else begin ShowMessage('NILM ID¸¦ ãÀ» ¼ö ¾ø½À´Ï´Ù.'); end; end; procedure TfrmNILM.btnDeleteClick(Sender: TObject); var sql : string; begin if (not CheckNILM(StrToInt(Edit2.Text))) and (select_nilm_idxNo>0) then begin sql := ''; sql := 'DELETE FROM nilm_info WHERE idxNo='+inttostr(select_nilm_idxNo); frmMain.insertSQL(sql); DropTableNilmValueLog(select_nilm_idxNo); ShowNILM(); select_nilm_idxNo := 0; end else begin ShowMessage('NILM ID¸¦ ãÀ» ¼ö ¾ø½À´Ï´Ù.'); end; end; end.