10-08-2017, 10:16 PM
Function dialog_HTTP_server_for_Tasker
;This is a simple HTTP server that can receive and return data.
;Can be used for example to launch QM macros from other computers and smartphones.
;Below is instructions how to send commands from an Android mobile device with Tasker.
;On your Android device install Tasker app. It is for sending HTTP requests. Costs $2.99. Or find a free app that can send HTTP requests.
;In Tasker create new task and add action HTTP Post.
;;;Set field Server:Port, like 192.168.8.123:5033. As Server, use one of IP addresses that you can see in QM Options -> Network. The 5033 is server's port that is used in this macro, see below.
;;;Set field Data / File. Can be any text or variable or file. At first just for testing.
;;;Set field Content Type, like text/plain.
;;;Optionally set field Output File, like Documents/test1.txt. Only if you want to receive some data from QM.
;Run this function and in the dialog click button Start.
;Run the Tasker task.
;;;In QM output you should see Tasker's HTTP POST request that includes data at the end.
;;;If was set Output File, the task creates the file. It contains text DATA.
if(getopt(nthreads)>1) ret ;;allow single instance
#compile "__TcpSocket"
str dd=
;BEGIN DIALOG
;0 "" 0x90C80AC8 0x0 0 0 230 116 "TCP server"
;4 Static 0x54000000 0x0 170 8 22 12 "Port"
;5 Edit 0x54032000 0x200 196 6 32 14 "port"
;6 Button 0x54032000 0x0 6 6 48 14 "Start"
;7 Button 0x5C032000 0x0 58 6 48 14 "Stop"
;14 Static 0x54000000 0x0 6 30 36 12 "Response"
;15 Edit 0x54231044 0x200 46 28 182 85 "res"
;END DIALOG
;DIALOG EDITOR: "" 0x2030300 "*" "" ""
str controls = "5 15"
str e5por e15res
e5por=5033
;minimal HTTP response with no data.
;e15res="HTTP/1.1 200 OK[][]"
;HTTP response with some data.
e15res="HTTP/1.1 200 OK[]Content-Type: text/plain[]Content-Length: 4[][]DATA"
if(!ShowDialog(dd &sub.DlgProc &controls 0 0 0 0 0 0 100)) ret
#sub DlgProc
function# hDlg message wParam lParam
TcpSocket- t_server
sel message
,case WM_INITDIALOG
,case WM_DESTROY
,t_server.Close
,case WM_COMMAND goto messages2
ret
;messages2
sel wParam
,case 6 ;;Start
,t_server.ServerStart(GetDlgItemInt(hDlg 5 0 0) &sub.OnClientConnected hDlg 1)
,EnableWindow lParam 0
,EnableWindow id(7 hDlg) 1
,
,case 7 ;;Stop
,t_server.Close
,EnableWindow lParam 0
,EnableWindow id(6 hDlg) 1
ret 1
err+ out _error.description
#sub OnClientConnected
function TcpSocket&client $clientIp hDlg !*reserved
;This function is called in server side, when a client connects.
;This function runs in separate thread for each client connection.
out F"<><z 0xc0ffff>SERVER: client connected:</z> {clientIp}"
str s
client.Receive(s 1000)
out F"<><Z 0xc0ffff>SERVER: client request:</Z>"
out s
s.getwintext(id(15 hDlg))
client.Send(s)
err+ out _error.description