11using System ;
22using System . Collections . Generic ;
33using System . ComponentModel ;
4- using System . Text . Json ;
5- using CESDK ;
64using CESDK . Classes ;
75using Microsoft . AspNetCore . Builder ;
86using Microsoft . AspNetCore . Http ;
@@ -11,55 +9,34 @@ namespace Tools
119{
1210 public static class AddressListTool
1311 {
14- /// <summary>
15- /// Executes Lua code wrapped in synchronize() for GUI thread safety
16- /// </summary>
17- private static string RunSync ( string luaCode )
18- {
19- var lua = PluginContext . Lua ;
20- var wrappedCode = $ "return synchronize(function() { luaCode } end)";
21-
22- lua . DoString ( wrappedCode ) ;
23-
24- string result = "" ;
25- if ( lua . IsString ( - 1 ) )
26- result = lua . ToString ( - 1 ) ?? "" ;
27- else if ( lua . IsNumber ( - 1 ) )
28- result = lua . ToNumber ( - 1 ) . ToString ( ) ;
29-
30- lua . SetTop ( 0 ) ;
31- return result ;
32- }
33-
34- private static string Escape ( string s ) =>
35- s . Replace ( "\\ " , "\\ \\ " ) . Replace ( "'" , "\\ '" ) . Replace ( "\n " , "\\ n" ) . Replace ( "\r " , "\\ r" ) . Replace ( "\" " , "\\ \" " ) ;
36-
3712 public static void MapAddressListApi ( this WebApplication app )
3813 {
3914 // GET /api/addresslist - Get all memory records
4015 app . MapGet ( "/api/addresslist" , ( ) =>
4116 {
4217 try
4318 {
44- var json = RunSync ( @"
45- local al = getAddressList()
46- local t = {}
47- for i = 0, al.Count - 1 do
48- local r = al.getMemoryRecord(i)
49- table.insert(t, string.format(
50- '{""id"":%d,""index"":%d,""description"":""%s"",""address"":""%s"",""value"":""%s"",""active"":%s}',
51- r.ID, r.Index,
52- (r.Description or ''):gsub('""', '\\""'),
53- (r.Address or ''):gsub('""', '\\""'),
54- tostring(r.Value or ''):gsub('""', '\\""'),
55- r.Active and 'true' or 'false'
56- ))
57- end
58- return '[' .. table.concat(t, ',') .. ']'
59- " ) ;
60-
61- var records = JsonSerializer . Deserialize < JsonElement > ( string . IsNullOrEmpty ( json ) ? "[]" : json ) ;
62- return Results . Ok ( new { success = true , count = records . GetArrayLength ( ) , records } ) ;
19+ var records = CESDK . CESDK . Synchronize ( ( ) =>
20+ {
21+ var al = new AddressList ( ) ;
22+ var result = new List < object > ( ) ;
23+ for ( int i = 0 ; i < al . Count ; i ++ )
24+ {
25+ var r = al . GetMemoryRecord ( i ) ;
26+ result . Add ( new
27+ {
28+ id = r . ID ,
29+ index = r . Index ,
30+ description = r . Description ,
31+ address = r . Address ,
32+ value = r . Value ,
33+ active = r . Active
34+ } ) ;
35+ }
36+ return result ;
37+ } ) ;
38+
39+ return Results . Ok ( new { success = true , count = records . Count , records } ) ;
6340 }
6441 catch ( Exception ex )
6542 {
@@ -75,25 +52,23 @@ r.Active and 'true' or 'false'
7552 {
7653 try
7754 {
78- var desc = Escape ( request . Description ) ;
79- var addr = Escape ( request . Address ) ;
80- var varType = ( int ) request . VarType ;
81- var value = Escape ( request . Value ) ;
55+ var record = CESDK . CESDK . Synchronize ( ( ) =>
56+ {
57+ var al = new AddressList ( ) ;
58+ var r = al . CreateMemoryRecord ( ) ;
59+ r . Description = request . Description ;
60+ r . Address = request . Address ;
61+ r . VarType = request . VarType ;
62+ r . Value = request . Value ;
63+ return new
64+ {
65+ id = r . ID ,
66+ description = r . Description ,
67+ address = r . Address ,
68+ value = r . Value
69+ } ;
70+ } ) ;
8271
83- var json = RunSync ( $@ "
84- local al = getAddressList()
85- local r = al.createMemoryRecord()
86- r.Description = '{ desc } '
87- r.Address = '{ addr } '
88- r.Type = { varType }
89- r.Value = '{ value } '
90- return string.format(
91- '{{""id"":%d,""description"":""%s"",""address"":""%s"",""value"":""%s""}}',
92- r.ID, (r.Description or ''):gsub('""', '\\""'), (r.Address or ''):gsub('""', '\\""'), tostring(r.Value or ''):gsub('""', '\\""')
93- )
94- " ) ;
95-
96- var record = JsonSerializer . Deserialize < JsonElement > ( json ) ;
9772 return Results . Ok ( new { success = true , record } ) ;
9873 }
9974 catch ( Exception ex )
@@ -110,38 +85,38 @@ r.Active and 'true' or 'false'
11085 {
11186 try
11287 {
113- var findCode = GetFindCode ( request . Id , request . Index , request . Description ) ;
114- var updates = new List < string > ( ) ;
115-
116- if ( ! string . IsNullOrEmpty ( request . NewDescription ) )
117- updates . Add ( $ "r.Description = '{ Escape ( request . NewDescription ) } '") ;
118- if ( ! string . IsNullOrEmpty ( request . NewAddress ) )
119- updates . Add ( $ "r.Address = '{ Escape ( request . NewAddress ) } '") ;
120- if ( request . NewVarType . HasValue )
121- updates . Add ( $ "r.Type = { ( int ) request . NewVarType . Value } ") ;
122- if ( ! string . IsNullOrEmpty ( request . NewValue ) )
123- updates . Add ( $ "r.Value = '{ Escape ( request . NewValue ) } '") ;
124- if ( request . Active . HasValue )
125- updates . Add ( $ "r.Active = { ( request . Active . Value ? "true" : "false" ) } ") ;
126-
127- var updateCode = string . Join ( "\n " , updates ) ;
128-
129- var json = RunSync ( $@ "
130- local al = getAddressList()
131- local r = { findCode }
132- if r == nil then return '{{""error"":""not found""}}' end
133- { updateCode }
134- return string.format(
135- '{{""id"":%d,""description"":""%s"",""address"":""%s"",""value"":""%s"",""active"":%s}}',
136- r.ID, (r.Description or ''):gsub('""', '\\""'), (r.Address or ''):gsub('""', '\\""'), tostring(r.Value or ''):gsub('""', '\\""'), r.Active and 'true' or 'false'
137- )
138- " ) ;
139-
140- if ( json . Contains ( "\" error\" " ) )
88+ var result = CESDK . CESDK . Synchronize ( ( ) =>
89+ {
90+ var al = new AddressList ( ) ;
91+ var r = FindRecord ( al , request . Id , request . Index , request . Description ) ;
92+ if ( r == null )
93+ return ( object ? ) null ;
94+
95+ if ( ! string . IsNullOrEmpty ( request . NewDescription ) )
96+ r . Description = request . NewDescription ;
97+ if ( ! string . IsNullOrEmpty ( request . NewAddress ) )
98+ r . Address = request . NewAddress ;
99+ if ( request . NewVarType . HasValue )
100+ r . VarType = request . NewVarType . Value ;
101+ if ( ! string . IsNullOrEmpty ( request . NewValue ) )
102+ r . Value = request . NewValue ;
103+ if ( request . Active . HasValue )
104+ r . Active = request . Active . Value ;
105+
106+ return new
107+ {
108+ id = r . ID ,
109+ description = r . Description ,
110+ address = r . Address ,
111+ value = r . Value ,
112+ active = r . Active
113+ } ;
114+ } ) ;
115+
116+ if ( result == null )
141117 return Results . Ok ( new { success = false , error = "Record not found" } ) ;
142118
143- var record = JsonSerializer . Deserialize < JsonElement > ( json ) ;
144- return Results . Ok ( new { success = true , record } ) ;
119+ return Results . Ok ( new { success = true , record = result } ) ;
145120 }
146121 catch ( Exception ex )
147122 {
@@ -157,17 +132,18 @@ r.Active and 'true' or 'false'
157132 {
158133 try
159134 {
160- var findCode = GetFindCode ( request . Id , request . Index , request . Description ) ;
161-
162- var result = RunSync ( $@ "
163- local al = getAddressList()
164- local r = { findCode }
165- if r == nil then return 'notfound' end
166- r.destroy()
167- return 'ok'
168- " ) ;
169-
170- if ( result == "notfound" )
135+ var found = CESDK . CESDK . Synchronize ( ( ) =>
136+ {
137+ var al = new AddressList ( ) ;
138+ var r = FindRecord ( al , request . Id , request . Index , request . Description ) ;
139+ if ( r == null )
140+ return false ;
141+
142+ al . DeleteMemoryRecord ( r ) ;
143+ return true ;
144+ } ) ;
145+
146+ if ( ! found )
171147 return Results . Ok ( new { success = false , error = "Record not found" } ) ;
172148
173149 return Results . Ok ( new { success = true } ) ;
@@ -186,13 +162,11 @@ r.Active and 'true' or 'false'
186162 {
187163 try
188164 {
189- RunSync ( @"
190- local al = getAddressList()
191- for i = al.Count - 1, 0, -1 do
192- al.getMemoryRecord(i).destroy()
193- end
194- return 'ok'
195- " ) ;
165+ CESDK . CESDK . Synchronize ( ( ) =>
166+ {
167+ var al = new AddressList ( ) ;
168+ al . Clear ( ) ;
169+ } ) ;
196170 return Results . Ok ( new { success = true } ) ;
197171 }
198172 catch ( Exception ex )
@@ -205,14 +179,14 @@ r.Active and 'true' or 'false'
205179 . WithOpenApi ( ) ;
206180 }
207181
208- private static string GetFindCode ( int ? id , int ? index , string ? description )
182+ private static MemoryRecord ? FindRecord ( AddressList al , int ? id , int ? index , string ? description )
209183 {
210184 if ( id . HasValue )
211- return $ " al.getMemoryRecordByID( { id . Value } )" ;
185+ return al . GetMemoryRecordByID ( id . Value ) ;
212186 if ( index . HasValue )
213- return $ " al.getMemoryRecord( { index . Value } )" ;
187+ return al . GetMemoryRecord ( index . Value ) ;
214188 if ( ! string . IsNullOrEmpty ( description ) )
215- return $ " al.getMemoryRecordByDescription(' { Escape ( description ) } ')" ;
189+ return al . GetMemoryRecordByDescription ( description ) ;
216190
217191 throw new ArgumentException ( "Provide id, index, or description to find the record" ) ;
218192 }
0 commit comments