So I tried to fill an array with data from an http request to my 3D printer running klipper (if that is of some interest)
The data I Get from the request is already in JSON format so i just tried to use the fromJSON command and got:
{
"result":{
"status":{
"virtual_sdcard":{
"progress":0.01007635121145651,
"file_position":122399,
"is_active":true,
"file_path":"/home/pi/gcode_files/Denture_bottom_w_holes_0.2mm_PLA_ENDER5PLUS_3h53m.gcode",
"file_size":12147155
},
"display_status":{
"progress":0.01007635121145651,
"message":null
},
"print_stats":{
"print_duration":228.95805019699037,
"total_duration":722.504832088016,
"filament_used":228.76148000000006,
"filename":"Denture_bottom_w_holes_0.2mm_PLA_ENDER5PLUS_3h53m.gcode",
"state":"printing",
"message":""
},
"toolhead":{
"square_corner_velocity":5.0,
"max_accel":3500.0,
"homed_axes":"xyz",
"estimated_print_time":725.5034566875,
"max_velocity":300.0,
"print_time":727.5963330477789,
"max_accel_to_decel":1750.0,
"axis_minimum":[
0.0,
0.0,
-0.5,
0.0
],
"stalls":0,
"axis_maximum":[
350.0,
350.0,
400.0,
0.0
],
"position":[
216.935,
248.203,
0.11430201849470678,
228.76148000000006
],
"extruder":"extruder"
},
"heater_bed":{
"temperature":64.91,
"power":0.6549739168566129,
"target":65.0
},
"extruder":{
"pressure_advance":0.6,
"target":225.0,
"power":0.3872956911759702,
"can_extrude":true,
"smooth_time":0.04,
"temperature":225.04
}
},
"eventtime":6145461.385067686
}
}
then converting the data i got inside my arrayback to JSON i got:
{
"result": {
"status": {
"virtual_sdcard": {
"progress": 0.0100764,
"file_position": 122399,
"is_active": 1,
"file_path": "/home/pi/gcode_files/Denture_bottom_w_holes_0.2mm_PLA_ENDER5PLUS_3h53m.gcode",
"file_size": 12147155
},
"display_status": {
"progress": 0.0100764,
"message": ""
},
"print_stats": {
"print_duration": 228.958,
"total_duration": 722.505,
"filament_used": 228.761,
"filename": "Denture_bottom_w_holes_0.2mm_PLA_ENDER5PLUS_3h53m.gcode",
"state": "printing",
"message": ""
},
"toolhead": {
"square_corner_velocity": 5,
"max_accel": 3500,
"homed_axes": "xyz",
"estimated_print_time": 725.503,
"max_velocity": 300,
"print_time": 727.596,
"max_accel_to_decel": 1750,
"axis_minimum": [0, 0, -0.5, 0],
"stalls": 0,
"axis_maximum": [350, 350, 400, 0],
"position": [216.935, 248.203, 0.114302, 228.761],
"extruder": "extruder"
},
"heater_bed": {
"temperature": 64.91,
"power": 0.654974,
"target": 65
},
"extruder": {
"pressure_advance": 0.6,
"target": 225,
"power": 0.387296,
"can_extrude": "true",
"smooth_time": 0.04,
"temperature": 225.04
}
},
"eventtime": 6.14546e+06
}
}
Now if you compare them you can see that the true from the is_active key gets sucessfully converted to a 1 as the Datatype in AppGameKit is an integer for it.
beside that it rounds the floats to the first three digids behind the comma or truncates it if there are only 0's and also creates an empty string value for the message key where originally there is null as the value so the json commands are somewhat smart.
Now Virtual Nomad on Discord suggested to create a tool that automatically creates AppGameKit Datatypes by pasting the complete json file as i asked there how i would construct it.
Here is my AppGameKit code if someone also has a 3D printer running klipper by any chance:
// Project: Restful Http Request Klipper
// Created: 22-04-15
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "Restful Http Request Klipper" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 )
Http_Init("192.168.178.203:7125", 4)
ConnectionID=Http_Request("/printer/objects/query?heater_bed&extruder&print_stats&toolhead&display_status&virtual_sdcard","")
type data
result as ResultData
endtype
type ResultData
status as StatusData
eventtime as float
endtype
type StatusData
virtual_sdcard as virtual_sdcardData
display_status as display_statusData
print_stats as print_statsData
toolhead as toolheadData
heater_bed as heater_bedData
extruder as extruderData
endtype
type virtual_sdcardData
progress as float
file_position as integer
is_active as integer
file_path as string
file_size as integer
endtype
type display_statusData
progress as float
message as string
endtype
type print_statsData
print_duration as float
total_duration as float
filament_used as float
filename as string
state as string
message as string
endtype
type toolheadData
square_corner_velocity as float
max_accel as float
homed_axes as string
estimated_print_time as float
max_velocity as float
print_time as float
max_accel_to_decel as float
axis_minimum as float[3]
stalls as integer
axis_maximum as float[3]
position as float[3]
extruder as string
endtype
type heater_bedData
temperature as float
power as float
target as float
endtype
type extruderData
pressure_advance as float
target as float
power as float
can_extrude as string
smooth_time as float
temperature as float
endtype
type Vac4Data
X as float
Y as float
Z as float
W as float
endtype
do
Response$=Http_Response(ConnectionID)
if Response$<>"0"
FileID=OpenToWrite("klipper.json")
WriteString(FileID,Response$)
CloseFile(FileID)
temp as data
temp.fromJSON(Response$)
message(Response$)
generate$=temp.toJSON()
FileID=OpenToWrite("agk.json")
WriteString(FileID,generate$)
CloseFile(FileID)
endif
Print( ScreenFPS() )
Sync()
loop
function Http_Init(Server$, Timeout#)
global Http_Server$
global Http_Timeout#
Http_Server$=Server$
Http_Timeout#=Timeout#
endfunction
function Http_Request(Path$,Post$)
ConnectionID=createHTTPConnection()
SetHTTPHost(ConnectionID,Http_Server$,0)
AddHTTPHeader(ConnectionID,"X-Api-Key", "CHANGE_THIS_TO_YOUR_OWN_API_KEY" )
SendHTTPRequestasync(ConnectionID,Path$,Post$)
endfunction ConnectionID
function Http_Response(ConnectionID)
Result=GetHTTPResponseReady(ConnectionID)
if Result=1
Response$=GetHTTPResponse(ConnectionID)
exitfunction Response$
endif
if Result<>0
CloseHTTPConnection(ConnectionID)
DeleteHTTPConnection(ConnectionID)
endif
Response$=str(Result)
endfunction Response$
function Http_RequestWithTimeout(Path$,Post$)
if GetInternetState()=1
ConnectionID=Http_Request(Path$,Post$)
Timeout#=Timer()+Http_Timeout#
repeat
Response$=Http_Response(ConnectionID)
until Response$<>"0" or Timer()>Timeout#
if Response$="0" then message("Connection Timeout")
endif
endfunction Response$