forked from jangabrielsson/EventRunner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fibaroapiHC3plug.lua
798 lines (745 loc) · 28.1 KB
/
fibaroapiHC3plug.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
VERSION = "0.7"
local idech3 = ID("HC3.copyHC3")
local idech31 = ID("HC3.sdkHC3")
local idech33 = ID("HC3.uploadHC3")
local idech33b = ID("HC3.backupHC3")
local idech33d = ID("HC3.downloadRsrcHC3")
local ide_deploy = ID("HC3.deployQA")
local idem = ID("HC3.web")
local idet = ID("HC3.templ")
local lfs = require("lfs")
local sdkFile = "fibaroapiHC3.lua"
local urlEmu = "http://127.0.0.1:6872/web/main"
local urlHC3Help = "https://forum.fibaro.com/topic/49488-sdk-for-remote-and-offline-hc3-development/"
local urlHC3QAHelp = "https://manuals.fibaro.com/home-center-3-quick-apps/"
local urlHC3ScHelp = "https://manuals.fibaro.com/home-center-3-lua-scenes/"
local urlHC3MQTTHelp = "https://manuals.fibaro.com/knowledge-base-browse/hc3-quick-apps-mqtt-client/"
local urlHC3WSHelp = "https://manuals.fibaro.com/knowledge-base-browse/hc3-quick-apps-websocket-client/"
local urlHC3ChildHelp = "https://manuals.fibaro.com/knowledge-base-browse/hc3-quick-apps-managing-child-devices/"
local function launchEmulator() wx.wxLaunchDefaultBrowser(urlEmu, 0) end
local function openURL(url) wx.wxLaunchDefaultBrowser(url, 0) end
ide:Print("Fibaro plugin version: "..VERSION)
sdkFile = ide.config.hc3api or sdkFile
ide:Print("Fibaro api file:"..sdkFile)
local function printf(fmt,...) ide:Print(string.format(fmt,...)) end
local ps = ide.osname == "Windows" and "\\" or "/"
local assetDir = os.getenv("HC3EMU")
if not assetDir then
assetDir = (os.getenv('HOME') or '')..ps..".zbstudio"..ps.."hc3emu"..ps
end
printf("Asset directory set to '%s'",assetDir)
local function readLuaFile(name)
local p = assetDir..name
local f = io.open(p,"r")
if f then
local stat,res = pcall(function()
local d = f:read("*all")
f:close()
return load(d)()
end)
if stat then return res
else printf("Error reading %s - %s",p,res) end
else print("Missing '%s', ignoring",name) end
end
local function checkAndReport(t,name)
local n = 0;
for _,_ in pairs(t or {}) do n=n+1 end
printf("%s %s",n,name)
end
local codeTemplates = readLuaFile("hc3CodeTemplates.lua")
local downLoads = readLuaFile("hc3Downloads.lua")
if codeTemplates then checkAndReport(codeTemplates.templates,"code templates") end
if downLoads then checkAndReport((downLoads or {}).files,"download links") end
local function exePath(self, version)
local version = tostring(version or ""):gsub('%.','')
local mainpath = ide:GetRootPath()
local macExe = mainpath..([[bin/lua.app/Contents/MacOS/lua%s]]):format(version)
return (ide.config.path['lua'..version]
or (ide.osname == "Windows" and mainpath..([[bin\lua%s.exe]]):format(version))
or (ide.osname == "Unix" and mainpath..([[bin/linux/%s/lua%s]]):format(ide.osarch, version))
or (wx.wxFileExists(macExe) and macExe or mainpath..([[bin/lua%s]]):format(version))),
ide.config.path['lua'..version] ~= nil
end
local function callFibaroAPIHC3(cmd,endMessage)
local version = "5.3"
-- modify LUA_CPATH and LUA_PATH to work with other Lua versions
local envcpath = "LUA_CPATH"
local envlpath = "LUA_PATH"
if version then
local env = "PATH_"..string.gsub(version, '%.', '_')
if os.getenv("LUA_C"..env) then envcpath = "LUA_C"..env end
if os.getenv("LUA_"..env) then envlpath = "LUA_"..env end
end
local cpath = os.getenv(envcpath)
if version and cpath then
-- adjust references to /clibs/ folders to point to version-specific ones
local cpath = os.getenv(envcpath)
local clibs = string.format('/clibs%s/', version):gsub('%.','')
if not cpath:find(clibs, 1, true) then cpath = cpath:gsub('/clibs/', clibs) end
wx.wxSetEnv(envcpath, cpath)
end
local lpath = version and (not iscustom) and os.getenv(envlpath)
if lpath then
-- add oslibs libraries when LUA_PATH_5_x variables are set to allow debugging to work
wx.wxSetEnv(envlpath, lpath..';'..ide.oslibs)
end
local wfilename = ide:MergePath(ide.config.path.projectdir, sdkFile)
local ep = exePath(nil,version)
local pid = CommandLineRun( --tooutput,nohide,stringcallback,uid,endcallback)
ep.." "..wfilename.." "..cmd, -- command
ide.config.path.projectdir, -- working dir
true, -- nohide
false, --
nil, -- stringcallback
nil, -- uid
function() ide:Print(endMessage) end
)
if (rundebug or version) and cpath then wx.wxSetEnv(envcpath, cpath) end
if lpath then wx.wxSetEnv(envlpath, lpath) end
return pid
end
-------- Commandline commands ---------
local function launchHC3Copy()
ide:Print("Copying and creating DB from HC3...")
callFibaroAPIHC3("-downloaddb","Copying done!")
end
local function downloadFile(file)
ide:Print("Downloading latest "..file)
callFibaroAPIHC3("-downloadfile "..file,"Download done! - "..file)
end
local function uploadResource()
ide:Print("Uploading to HC3...")
local ed = ide:GetEditor()
if not ed then return end -- all editor tabs are closed
local file = ide:GetDocument(ed):GetFilePath()
callFibaroAPIHC3("-push "..file,"Upload done!")
end
local function downloadResource()
ide:Print("Downloading from HC3...")
local editor = ide:GetEditor()
local length, curpos = editor:GetLength(), editor:GetCurrentPos()
local ssel, esel = editor:GetSelection()
local rsrc = editor:GetTextRange(ssel, esel)
if rsrc and rsrc~="" then
callFibaroAPIHC3("-pull "..rsrc,"Download done!")
end
end
local function deployQA()
ide:Print("Deploying QuickApp...")
local ed = ide:GetEditor()
if not ed then return end -- all editor tabs are closed
local file = ide:GetDocument(ed):GetFilePath()
callFibaroAPIHC3("-deploy "..file,"Deploy done!")
end
local function backupResources()
ide:Print("Backing up resources from HC3...")
callFibaroAPIHC3("-backup","Backup done!")
end
---------- API completion and tooltips --------------
local api = {
fibaro = {
type = "lib",
childs = {
call = {
description =
[[Executes an action on the device.
Parameters:
id – identifier of the device on which we want to execute the action. id can be a list of identifiers.
action_name – the name of the action we want to execute.
arguments – list of arguments that the action accepts.
Examples:
Execute “turnOn” action on device with id 30.]],
type = "function",args = "(id,action_name,...)",returns = "()",
},
get = {
description =
[[Gets value of device property and the date it was last modified.
Parameters:
id – device ID for which we want to retrieve the property value.
property_name – name of the property which value we want to retrieve.
Action returns: {value, last modification time}.
Examples:
Get the value and the date of the last modification for the value property for device 54
local value, modificationTime = fibaro.get(54, "value")]],
type = "function",args = "(id,property_name)",returns = "(value,last modification time)",
},
debug = {
description =
[[Displaying debug level text message in the debugging console.
Parameters:
tag – message tag, can be used to filter messages
message – the content of the message which shows up in the debug window
Examples:
Display “Test message” in a debug window with the tag “TestTag”:
fibaro.debug("TestTag", "Test message")]],
type = "function", args= "(tag,message)",returns = "()",
},
warning = {
description =
[[Displaying warning level text message in the debugging console. Parameters:
tag – message tag, can be used to filter messages
message – the content of the message which shows up in the debug window
Examples:
Display “Test message” in a debug window with the tag “TestTag”:
fibaro.warning("TestTag", "Test message")]],
type = "function", args= "(tag,message)",returns = "()",
},
trace = {
description =
[[Displaying trace level text message in the debugging console.
Parameters:
tag – message tag, can be used to filter messages
message – the content of the message which shows up in the debug window
Examples:
Display “Test message” in a debug window with the tag “TestTag”:
fibaro.trace("TestTag", "Test message")]],
type = "function", args= "(tag,message)",returns = "()",
},
error = {
description =
[[Displaying error level text message in the debugging console.
Parameters:
tag – message tag, can be used to filter messages
message – the content of the message which shows up in the debug window
Examples:
Display “Test message” in a debug window with the tag “TestTag”:
fibaro.error("TestTag", "Test message")]],
type = "function", args= "(tag,message)",returns = "()",
},
getType = {
description =
[[Gets the device type.
Parameters:
id – device ID for which we want to retrieve its type.
The action returns the device type.
Examples:
Retrive type of device 54
local type = fibaro.getType(54)]],
type = "function", args= "(id)",returns = "(string)",
},
getValue = {
description =
[[Gets the value of the given property.
Parameters:
id – device ID for which we want to retrieve the property value.
property_name – name of the property which value we want to retrieve.
Action returns only value of the property.
Examples:
Retrieving the value of the value property for device id 54
local value = fibaro.getValue(54, "value")]],
type = "function", args= "(id,property_name)",returns = "(value)",
},
getName = {
description =
[[Gets the device name.
Parameters:
id – device ID for which we want to retrieve its name.
The action returns the device name.
Examples:
Retrive name of device 54
local name = fibaro.getName(54)]],
type = "function", args= "(id)",returns = "(string)",
},
getGlobalVariable = {
description =
[[Get the value of the global variable.
Parameters:
variable_name – name of variable to get the value of
Examples:
Get the value of variable testVariable.
local value = fibaro.getGlobalVariable("testVariable")]],
type = "function", args= "(variable_name)",returns = "(string)",
},
setGlobalVariable = {
description =
[[Set the value of the global variable.
Parameters:
variable_name – the name of the variable we want to update
variable_value – the new value for the variable
Examples:
Change the value of testVariable to testValue.
fibaro.setGlobalVariable("testVariable", "testValue")]],
type = "function", args= "(variable_name,variable_value)",returns = "()",
},
getRoomName = {
description =
[[Gets the room name.
Parameters:
id – room ID for which we want to retrieve name.
The action returns room name.
Examples:
Retrive name for room 219
local roomName = fibaro.getRoomName(219)]],
type = "function", args= "(id)",returns = "(string)",
},
getRoomID = {
description =
[[Gets the ID of the room to which the device is assigned.
Parameters:
id – device ID for which we want to retrieve its room ID.
The action returns the ID of the room to which the device is assigned.
Examples:
Retrieve ID of the room for device 54
local roomId = fibaro.getRoomID(54)]],
type = "function", args= "(id)",returns = "(number)",
},
getRoomNameByDeviceID = {
description =
[[Gets the name of the room to which the device is assigned.
Parameters:
id – device ID for which we want to retrieve its room name.
The action returns room name.
Examples:
Retrieve name of the room for device 54
local roomName = fibaro.getRoomNameByDeviceID(54)]],
type = "function", args= "(id)",returns = "(string)",
},
getSectionID = {
description =
[[Gets the ID of the section to which the device is assigned.
Parameters:
id – device ID for which we want to retrieve its section ID.
The action returns the ID of the section to which the device is assigned.
Examples:
Retrieve ID of the section for device 54
local sectionId = fibaro.getSectionID(54)]],
type = "function", args= "(id)",returns = "(number)",
},
getIds = {
description =
[[Returns a list of device IDs for given devices.
devices – accepts list of device objects.
Examples:
local devices = fibaro.getAllDeviceIds()
local devicesId = fibaro.getIds(devices)]],
type = "function", args= "(devices)",
},
getAllDeviceIds = {
description =
[[Returns a list of objects of all devices.
Examples:
local devices = fibaro.getAllDeviceIds()]],
type = "function", args= "()",returns = "(table)",
},
getDevicesID = {
description =
[[Returns a list of device IDs that match the given filters.
Parameters:
filter – the way we want to filter the devices]],
type = "function", args= "(filter)",returns = "(table)",
},
scene = {
description =
[[Executes given action on a scene.
Parameters:
action – the action we want to perform on scenes, one of the following:
"execute" – uruchomienie sceny
"kill" – zatrzymanie sceny
scenes_ids – list of scenes IDs to perform the action on
Examples:
Execute scenes with IDs 1, 2, 3.
fibaro.scene("execute", {1, 2, 3})
Stop scenes with IDs 1, 2, 3
fibaro.scene("kill", {1, 2, 3})]],
type = "function", args= "(action,scenes_ids)",returns = "()",
},
profile = {
description =
[[Executes specified action on the user profile.
Parameters:
profile_id – ID of the profile on which we want to perform the action
action – the action we want to execute on the profile. Currently, only "activateProfile" action is available (activating the profile)
Examples:
Activate profile with ID 1.
fibaro.profile(1, "activateProfile")]],
type = "function", args= "(profile_id,action)",returns = "()",
},
callGroupAction = {
description =
[[Executes an action on the devices.
Parameters:
action_name – the name of the action we want to execute.
arguments – list of arguments that the function accepts and device filter.
The action returns a list of devices on which it was performed.]],
type = "function", args= "(action_name,arguments)",returns = "(table)",
},
alert = {
description =
[[Send custom notification.
alert_type – one of the notification types: “email”, “sms” or “push”
user_ids – list of user identifiers to send the notification to
notification_content – content of the notification to send
Examples:
Send notification with “Test notification” content using email to users with id 2, 3 and 4.
fibaro.alert("email", {2,3,4}, "Test notification")]],
type = "function", args= "(alert_type,user_ids,notification_content)",returns = "()",
},
alarm = {
description =
[[Executes specified action on one partition.
Parameters:
partition_id – partition on which we want to execute the action (optional),
action – the action we want to execute. Available actions: “arm”, “disarm”
Examples:
Arm partition no. 1:
fibaro.alarm(1, "arm")]],
type = "function", args= "(partition_id,action)",returns = "()",
},
setTimeout = {
description =
[[Performs the function asynchronously with a delay. Parameters:
delay – delay in milliseconds after which the specified function will be performed,
function – function, which will be executed with the delay.
Examples:
Execute function with 30s delay which turns on the device (ID 40) and sets profile 1 as active.
fibaro.setTimeout(30000, function()
fibaro.call(40, "turnOn")
fibaro.profile("activateProfile", 1)
end)]],
type = "function", args= "(delay,function)",returns = "()",
},
emitCustomEvent = {
description =
[[Emitting Custom Event with the specified name.
Parameters:
event_name – the name of the event we want to emit
Examples:
Emitting the event with the name “TestEvent”.
fibaro.emitCustomEvent("TestEvent")]],
type = "function", args= "(event_name)",returns = "()",
},
wakeUpDeadDevice = {
description =
[[Wake up a device.
Parameters:
id – device ID to wake up.
Examples:
Wake up device 54
fibaro.wakeUpDeadDevice(54)]],
type = "function", args= "(id)",returns = "()",
},
sleep = {
description = "Pause the scene/QuickApp for <milliseconds>",
type = "function", args= "(milliseconds)",returns = "()",
},
}
},
api = {
type = "lib",
childs = {
get = {
description = "Get resource using geteway API",
type = "function", args= "(uri)",returns="(response data, response HTTP code)",
},
put = {
description = "Send PUT method request to the geteway API",
type = "function", args= "(uri,data)",returns="(response data, response HTTP code)",
},
post = {
description = "Send POST method request to the geteway API",
type = "function", args= "(uri,data)",returns="(response data, response HTTP code)",
},
delete = {
description = "Delete resource using geteway API",
type = "function", args= "(uri)",returns="(response data, response HTTP code)",
},
}
},
json = {
type = "lib",
childs = {
encode = {
description =
[[Convert Lua table to the text represented in JSON format.
Parameters:
table – Lua table
Examples:
Display sourceTrigger in the debug console.
fibaro.debug("", json.encode(sourceTrigger))]],
type = "function", args= "(table)",returns="(string)",
},
decode = {
description =
[[Convert the text represented in JSON format to Lua table.
Parameters:
text – data in JSON format
Examples:
local tmp = "{\"test\":11}"
local array = json.decode(tmp)]],
type = "function", args= "(text)",returns="(table)",
},
}
},
net = {
type = "lib",
childs = {
HTTPClient = {
description =[[...]],
type = "function", args= "(url,options)",returns="(http object). QuickApp only.",
},
TCPSocket = {
description = [[....]],
type = "function", args= "(ipaddress)",returns="(socket object). QuickApp only.",
},
}
},
setTimeout = {
type = "function",args="(function,milliseconds)",returns="(reference)",
description = "Schedules a function to run <milliseconds> from now. QuickApp only.",
},
clearTimeout = {
type = "function",args="(reference)",returns="()",
description = "Cancels a function scheduled with setTimeout. QuickApp only.",
},
setInterval = {
type = "function",args="(function,milliseconds)",returns="(reference)",
description = "Schedules a function to run every <milliseconds> interval. QuickApp only.",
},
clearInterval = {
type = "function",args="(reference)",returns="(reference)",
description = "Cancels a function scheduled with setInterval. QuickApp only.",
},
QuickApp = {
type = "class",
childs = {
onInit = {
type="method",args="()",returns="()",
description="",
}
}
},
self = {
type = "class",
childs = {
setVariable = {
type="method",args="(variable,value)",returns="()",
description="QuickApp only.",
},
getVariable = {
type="method",args="(variable)",returns="(value)",
description="QuickApp only.",
},
debug = {
type="method",args="(strings)",returns="()",
description="QuickApp only.",
},
updateView = {
type="method",args="(element,type,value)",returns="()",
description="QuickApp only.",
},
updateProperty = {
type="method",args="(property,value)",returns="()",
description="QuickApp only.",
},
}
}
}
local function getWebContent(url)
local https = require ("ssl.https")
local ltn12 = require("ltn12")
local socket = require("socket")
local http = require("socket.http")
local resp = {}
local req = { method = "GET", url = url, headers = { ["Content-Length"]=0 }, sink = ltn12.sink.table(resp) }
local res,status,headers = https.request(req)
if status >= 200 and status < 300 then resp = table.concat(resp) end
return resp, status
end
local function downloadContent(fdef)
local ps = ide.osname == "Windows" and "\\" or "/"
local stat,res = pcall(function()
if fdef.url then
printf("Downloading %s",fdef.url)
local src,code = getWebContent(fdef.url)
local f = io.open(ide.config.path.projectdir..ps..fdef.path,"w+")
f:write(src)
printf("Writing %s",fdef.path)
f:close()
elseif fdef.urldir then
lfs.mkdir(ide.config.path.projectdir..ps..fdef.pathdir)
for fn,p in pairs(fdef.files) do
printf("Downloading %s",fn)
local src,code = getWebContent(fdef.urldir..fn)
local f = io.open(ide.config.path.projectdir..ps..fdef.pathdir..ps..p,"w+")
printf("Writing %s",fdef.pathdir..ps..p)
f:write(src)
f:close()
end
end
end)
if not stat then printf("Error downloading %s - %s",fdef.url or fdef.urldir,res) end
end
local function sendCommandToEmulator(cmd)
local https = require ("ssl.https")
local ltn12 = require("ltn12")
local socket = require("socket")
local http = require("socket.http")
local url = "http://127.0.0.1:6872/web/"
local resp = {}
local req = { method = "GET", url = url, headers = { ["Content-Length"]=0 }, sink = ltn12.sink.table(resp) }
local res,status,headers = http.request(req)
return res,status
end
local interpreter = {
name = [[HC3 emulator]],
description = [[Lua 5.3 for HC3]],
api = {"baselib", "HC3"},
luaversion = version or '5.3',
fexepath = exePath,
frun = function(self,wfilename,rundebug)
local exe = self:fexepath(version or "")
local filepath = wfilename:GetFullPath()
local fibaroName = sdkFile
local projectPath = ide:GetProject()
if projectPath then fibaroName = projectPath..fibaroName end
local tmplua = wx.wxFileName()
tmplua:AssignTempFileName(".")
tmpluafile = tmplua:GetFullPath()
local flua = io.open(tmpluafile, "w")
if not flua then
DisplayOutput("Can't open temporary file '"..filepath.."' for writing\n")
return
end
flua:write("local md = require('mobdebug')\n")
flua:write("md.basedir([[projectPath]])\n")
flua:write("md.start()\n")
flua:write("if dofile and hc3_emulator==nil then\n")
flua:write(" hc3_emulator = { loadconfigs=true }\n")
flua:write((" dofile([[%s]])\n"):format(sdkFile))
flua:write("end\n")
flua:write(("hc3_emulator.loadQAScene([[%s]]):install()\n"):format(filepath))
flua:close()
if rundebug then
-- prepare tmp file
DebuggerAttachDefault({runstart = true})
-- update arg to point to the proper file
rundebug = ('if arg then arg[0] = [[%s]] end '):format(tmpluafile)..rundebug
-- local tmpfile = wx.wxFileName()
-- tmpfile:AssignTempFileName(".")
-- tmpluafile = tmpfile:GetFullPath()
local f = io.open(tmpluafile, "a")
if not f then
DisplayOutput("Can't open temporary file '"..tmpluafile.."' for writing\n")
return
end
f:write(rundebug)
f:close()
else
-- if running on Windows and can't open the file, this may mean that
-- the file path includes unicode characters that need special handling
local fh = io.open(tmpluafile, "r")
if fh then fh:close() end
if ide.osname == 'Windows' and pcall(require, "winapi")
and wfilename:FileExists() and not fh then
winapi.set_encoding(winapi.CP_UTF8)
tmpluafile = winapi.short_path(tmpluafile)
end
end
local params = ide.config.arg.any or ide.config.arg.lua
local code = ([[-e "io.stdout:setvbuf('no')" "%s"]]):format(tmpluafile)
local cmd = '"'..exe..'" '..code..(params and " "..params or "")
-- modify CPATH to work with other Lua versions
local clibs = ('/clibs%s/'):format(version and tostring(version):gsub('%.','') or '')
local _, cpath = wx.wxGetEnv("LUA_CPATH")
if version and cpath and not cpath:find(clibs, 1, true) then
wx.wxSetEnv("LUA_CPATH", cpath:gsub('/clibs/', clibs)) end
-- CommandLineRun(cmd,wdir,tooutput,nohide,stringcallback,uid,endcallback)
local pid = CommandLineRun(cmd,self:fworkdir(wfilename),true,false,nil,nil,
function()
if rundebug then wx.wxRemoveFile(tmpluafile) end
end)
if version and cpath then wx.wxSetEnv("LUA_CPATH", cpath) end
return pid
end,
fprojdir = function(self,wfilename)
return wfilename:GetPath(wx.wxPATH_GET_VOLUME)
end,
fworkdir = function (self,wfilename)
return ide.config.path.projectdir or wfilename:GetPath(wx.wxPATH_GET_VOLUME)
end,
hasdebugger = true,
fattachdebug = function(self) DebuggerAttachDefault() end,
}
local name = "fibaro"
return {
name = "HC3 SDK support",
description = "Support for HC3 SDK emulator and templates.",
author = "Jan Gabrielsson",
version = 0.1,
dependencies = "1.0",
onRegister = function()
--local res,stat = getWebContent("https://raw.githubusercontent.com/jangabrielsson/EventRunner/master/EventRunner4.lua")
--ide:Print(stat)
ide:AddInterpreter("HC3", interpreter)
local menu = ide:FindTopMenu("&View")
menu:Append(idem, "HC3 Emulator\tCtrl-Alt-E"..KSC(idem))
ide:GetMainFrame():Connect(idem, wx.wxEVT_COMMAND_MENU_SELECTED, launchEmulator)
menu = ide:FindTopMenu("&Help")
menu:AppendSeparator()
local function addLinkHelp(name,url)
local id = ID("HC3."..name)
menu = ide:FindTopMenu("&Help")
menu:Append(id, name..KSC(id))
ide:GetMainFrame():Connect(id, wx.wxEVT_COMMAND_MENU_SELECTED, function() openURL(url) end)
end
addLinkHelp("HC3 SDK (fibaroapiHC3.lua) help",urlHC3Help)
addLinkHelp("Fibaro QuickApp manual",urlHC3QAHelp)
addLinkHelp("Fibaro QuickAppChild manual",urlHC3ChildHelp)
addLinkHelp("Fibaro Lua Scene manual",urlHC3ScHelp)
addLinkHelp("Fibaro MQTT Client manual",urlHC3MQTTHelp)
addLinkHelp("Fibaro WebSocket Client manual",urlHC3WSHelp)
menu = ide:FindTopMenu("&File")
menu:AppendSeparator()
menu:Append(idech3, "Download HC3 local resource file"..KSC(idech3))
ide:GetMainFrame():Connect(idech3, wx.wxEVT_COMMAND_MENU_SELECTED, launchHC3Copy)
menu = ide:FindTopMenu("&File")
templSubMenu2 = ide:MakeMenu()
templ2 = menu:AppendSubMenu(templSubMenu2, TR("Download files..."))
if downLoads and downLoads.files then
local i,tab = 1,{}
for name,code in pairs((downLoads or {}).files or {}) do tab[#tab+1]=name end
table.sort(tab)
for _,name in ipairs(tab) do
local id = ID("HC3."..name)
templSubMenu2:Append(id, name..KSC(id))
ide:GetMainFrame():Connect(id, wx.wxEVT_COMMAND_MENU_SELECTED,
function() downloadContent(downLoads.files[name]) end)
end
end
menu = ide:FindTopMenu("&Project")
menu:AppendSeparator()
menu:Append(ide_deploy, "Deploy QuickApp/Scene"..KSC(ide_deploy))
ide:GetMainFrame():Connect(ide_deploy, wx.wxEVT_COMMAND_MENU_SELECTED, deployQA)
menu = ide:FindTopMenu("&Edit")
templSubMenu = ide:MakeMenu()
templ = menu:AppendSubMenu(templSubMenu, TR("HC3 SDK templates..."))
if codeTemplates and codeTemplates.templates then
local i,tab = 1,{}
for name,code in pairs((codeTemplates or {}).templates or {}) do tab[#tab+1]=name end
table.sort(tab)
for _,name in ipairs(tab) do
local id = ID("HC3.temp_ERT"..i); i=i+1
templSubMenu:Append(id, name..KSC(id))
ide:GetMainFrame():Connect(id, wx.wxEVT_COMMAND_MENU_SELECTED,
function() ide:GetEditor():InsertText(-1, codeTemplates.templates[name]) end)
end
end
-- add API with name "sample" and group "lua"
table.insert(ide:GetConfig().api, name)
ide:AddAPI("lua", name, api)
end,
onUnRegister = function()
ide:RemoveMenuItem(idech3)
ide:RemoveMenuItem(idech31)
ide:RemoveMenuItem(idech33)
ide:RemoveMenuItem(idem)
ide:RemoveMenuItem(ideh)
ide:RemoveMenuItem(idet)
-- remove API with name "sample" from group "lua"
ide:RemoveAPI("lua", name)
end,
-- onMenuEditor = function(self, menu, editor, event)
-- menu:AppendSeparator()
-- menu:Append(id, "..."..KSC(id))
-- end
}