B4J Question User interface with non-ui server

derez

Expert
Licensed User
Longtime User
It sounds a contradiction, but -
in the CCTV server I want to use the sendimage response to provide commands to the camera device.
The server is non-ui, so there is no user interface (!)
Is it possible to put some button on the html and then pass its action/status to the sendimage for response ? I don't see a feedback from the getimages to anything but the html page.

Edit: I learned how to make a button but what should I put as its action ?
B4X:
 resp.Write("<form action=???>").Write("<input Type=button value=Something>").Write("</form>")
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
See this new tutorial: http://www.b4x.com/android/forum/threads/server-guess-my-number-example.37458/

It explains how you can send commands and interact with the server.

In the case of the CCTV server you need to somehow send the commands from the server to the devices. I recommend you to use the SendImage handler for that. Store the command and send it to each of the devices as the result of SendImage.
 
Upvote 0

derez

Expert
Licensed User
Longtime User
I can't find how to make the button in the index.html and to make the new handler respond to it.
Can you please show me ? I need one button to activate a new handler in the cctv server.
Or - show me how to connect the button that I know how to create with the handler.
The rest - I know how to do.
 
Upvote 0

derez

Expert
Licensed User
Longtime User
Edit: forget the rest of the post, it works

I put this line
B4X:
<button type="button" id="Something">Something</button><br/>
instead of this in the "Guess my number":
B4X:
Enter number: <input type="text" id="txtNumber"></input>
    <button type="button" id="btnGuess">Guess!</button><br/><br/>
    <button type="button" id="btnReset">Reset secret number</button><br/>
And added this before </body> :
B4X:
<script type="text/javascript">
    $("#Something").click(function(){
        $.post("Something");
    });
</script>
But the button does not appear in the page. I don't see where it is added elsewhere, unless in the style part (? ).
In the server I added this line:
B4X:
srvr.AddHandler("/Something", "Something", False)
and added a handler class named "Something" which has to do the rest (but is not reached yet).

Edit:
In another trial I left the index.html as is and added these lines in getimages before the loop:
B4X:
 resp.Write("<button type=button id=Something>Something</button><br/>")
resp.Write("<script Type=text/javascript>$(#Something).click(function(){$.post(Something);});")
Now I see the button but it is not activating the handler yet.
 
Last edited:
Upvote 0

derez

Expert
Licensed User
Longtime User
I did this and I have a button (before all the pictures) and it activates another handler successfully.
Now I would like to have a button next to each picture. The pictures are created by the loop in the getimages handler so I have to make the button there.
I can create the buttons there but they do not do any action ( or at least the action does not go to the handler).
The buttons are created like this:
B4X:
 resp.Write("<button type=button action=/Rotation id=Rotation>Rotation</button><br/>")
where Rotation is the name of the button and the handler.
 
Upvote 0

derez

Expert
Licensed User
Longtime User
In the html source I see exactly the index.html, I don't see the lines that I add by resp.write.
The button is added by resp.write in the same way it is added at index.html but it does not perform the event like the index button. I tried to add also the script but it does not work.
 
Upvote 0

derez

Expert
Licensed User
Longtime User
Erel
Can you please explain the headers and parameters in the handler's req and resp objects?
The req has get methods and the resp set methods. What are the headers and parameters ?
If I want to send time with the job from the client, how can I do that ? (I want to screen out the old messages).
Thanks
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
http://www.example.com/?key1=value1&key2=value2
This query includes two parameters (key1 and key2).
Parameters can also be sent as the post payload.

Both the request and response can include any number of headers. You can see them with firebug:
SS-2014-02-07_08.37.25.png
 
Upvote 0

derez

Expert
Licensed User
Longtime User
I also see that in the request headers there is no date or time, so it has to be added as a parameter.
 
Upvote 0

derez

Expert
Licensed User
Longtime User
Although I would like to know how to add parameters, I think I got over the server showing "old" pictures:
I put the time in the index.html file to 300 and the time interval in the client to 1000. Now the server has enough spare time to consume all the requests and not add them to the buffer.:)
I think that the time interval should be more then no. of cameras * server interval.
 
Last edited:
Upvote 0

derez

Expert
Licensed User
Longtime User
Someone gave this solution and it is reported successful:
B4X:
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("IDToken1", "username"));
nvps.add(new BasicNameValuePair("IDToken2", "password"));
httPost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
But httpjob does not have setEntity method, and what is UrlEncodedFormEntity ?
Can it be added with reflector ?

Another similar code:
B4X:
List formparams = new ArrayList();
formparams.add(new BasicNameValuePair("param1", "value1"));
formparams.add(new BasicNameValuePair("param2", "value2"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
HttpPost httppost = new HttpPost("http://localhost/handler.do");
httppost.setEntity(entity);
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The above code is a multipart request. You can build such a request. There is an example in the forum. However it is not needed.

You can add any data you like as a header or a query parameter.
B4X:
j.PostBytes(ServerLink & "?key1=value2", jpeg)
You shouldn't add a slash before ? as it will point to the wrong url in this case.

Make sure to call req.InputStream before calling req.GetParameter.
 
Upvote 0

derez

Expert
Licensed User
Longtime User
Great, that works.
I pass the time, see the log of the order:
13:24:09
13:24:07
13:24:10
13:24:08
13:24:11
13:24:12
13:24:13
13:24:14
13:24:15
13:24:16
13:24:18
13:24:19
13:24:20
13:24:21
After the start it gets to order.
 
Upvote 0

derez

Expert
Licensed User
Longtime User
Erel
I want to use the time in GetImages but I can't find the place where it is called. The B4a client sends it to SendImage. Who calls GetImages ?

Edit:
I screen out the old images by this, in SendImage:
B4X:
Public Sub Handle(req As ServletRequest, resp As ServletResponse)
    Dim out As OutputStream
    out.InitializeToBytesArray(0)
    Dim In As InputStream = req.InputStream
    Dim t As Long = req.GetParameter("time")
    If t < Main.last Then
    '    Log(DateTime.Time(t) & " " & DateTime.Time(Main.last) & " -")
        Return
    Else
    '    Log(DateTime.Time(t) & " " & DateTime.Time(Main.last) & " +")
        Main.last = t
    End If
    File.Copy2(In, out)
.....

Remember that the time in the server and the time in the client are different so you can't compare them.

But I still ask for explanation for the question above.
 
Last edited:
Upvote 0
Top