Make simplest web-interface for serialsockCANmanage

This commit is contained in:
Edward Emelianov 2022-02-18 12:34:07 +03:00
parent 215bd88698
commit d3b4a8b4bc
5 changed files with 129 additions and 22 deletions

View File

@ -75,6 +75,49 @@ static int CANsend(int sock, char *cmd){
return TRUE;
}
typedef union{
uint8_t b[4];
int32_t i32;
} _4bytes;
typedef union{
uint8_t b[2];
uint16_t u16;
} _2bytes;
// get motor speed & current
// @parameters addr - device address (6 bit), sock - socket fd, cmdidx - command index
static int getpars(int addr, int sock, int cmdidx){
static char buf[BUFLEN];
uint16_t id = 512 + (addr << 3) + 3, ansid = id + 1;
_4bytes udata;
_2bytes idx;
uint32_t tmillis, ID;
uint8_t cmd, subidx;
//uint8_t CANbuf[8] = {0};
sprintf(buf, "s %u 0x31 0 %d %d 0 0 0 0\n", id, (cmdidx>>8), (cmdidx&0xff));
if(CANsend(sock, buf)){
double T0 = dtime();
while(dtime() - T0 < TIMEOUT){
if(1 != canberead(sock)) continue;
int n = read(sock, buf, BUFLEN-1);
DBG("Got %d (%s)", n, buf);
if(n == 0){
WARNX("Server disconnected");
signals(1);
}
buf[n] = 0;
if(buf[n-1] == '\n') buf[n-1] = 0;
if(10 != sscanf(buf, "%d #%x %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx",
&tmillis, &ID, &cmd, &subidx, &idx.b[1], &idx.b[0], &udata.b[3], &udata.b[2], &udata.b[1], &udata.b[0])) continue;
DBG("tmillis=%u, ID=%u, cmd=%u, idx=%u, data=%d", tmillis, ID, cmd, idx.u16, udata.i32);
if(ID != ansid) continue;
LOGMSG("RECEIVE: %s", buf);
break;
}
}else return INT_MIN;
return udata.i32;
}
int start_socket(char *path, CANcmd mesg, int par){
FNAME();
if(!path) return 1;
@ -98,8 +141,6 @@ int start_socket(char *path, CANcmd mesg, int par){
LOGERR("connect()");
ERR("connect()");
}
int Bufsiz = BUFLEN;
char *recvBuff = MALLOC(char, Bufsiz);
char cmd[BUFLEN];
int setcmd = FALSE;
int16_t spd;
@ -117,27 +158,15 @@ int start_socket(char *path, CANcmd mesg, int par){
break;
}
if(setcmd && !CANsend(sock, cmd)) return 1;
for(int i = 0; i < NMOTORS; ++i){
uint16_t id = 512 + ((i+1)<<3) + 3, cmdidx = PARIDX_GETSPEED;
//uint8_t CANbuf[8] = {0};
sprintf(cmd, "s %u 0x31 0 %d %d 0 0 0 0\n", id, (cmdidx>>8), (cmdidx&0xff));
if(!CANsend(sock, cmd)) continue;
double T0 = dtime();
while(dtime() - T0 < TIMEOUT){
if(1 != canberead(sock)) continue;
int n = read(sock, recvBuff, Bufsiz-1);
DBG("Got %d", n);
if(n == 0){
WARNX("Server disconnected");
signals(1);
}
recvBuff[n] = 0;
printf("%s", recvBuff);
if(recvBuff[n-1] == '\n') recvBuff[n-1] = 0;
LOGMSG("RECEIVE: %s", recvBuff);
break;
int gpar = getpars(1, sock, PARIDX_GETSPEED);
if(gpar != INT_MIN){
printf("SPEED=%.1f&", gpar/1000.);
}
gpar = getpars(1, sock, PARIDX_GETCURRENT);
if(gpar != INT_MIN){
printf("CURRENT=%.1f&", gpar/1000.);
}
printf("\n");
close(sock);
signals(0);
return 0;

View File

@ -0,0 +1,4 @@
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Content-type: text/html

View File

@ -0,0 +1,11 @@
#!/bin/bash
while true; do
NewVal=$((cat /home/eddy/bin/acc; /home/eddy/bin/SEWcontrol) | nc -w0 -lp 8080 | grep GET)
E=$(echo $NewVal | sed 's|.*setspeed=\([0-9\.]\+\).*|\1|' | grep "^[0-9]*[.]*[0-9]*$")
#'
if [ "x$E" != "x" ]; then
echo "SPEED=$E"
(( E > 299 )) && (( E < 1301 )) && /home/eddy/bin/SEWcontrol -s $E || /home/eddy/bin/SEWcontrol -0
fi
done

View File

@ -0,0 +1,63 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="ASCII">
<title>BTA primary mirror fans control</title>
<script>
var refrtime = 2000;
var T=setTimeout(refresh, refrtime);
function $(x){
return document.getElementById(x);
}
function callback(resp){
//console.log("Response: " + resp);
const urlParams = new URLSearchParams(resp);
//console.log("Speed=" + urlParams.get('SPEED') + ", Current=" + urlParams.get('CURRENT'));
$("curval").innerText = urlParams.get('CURRENT');
$("spdval").innerText = urlParams.get('SPEED');
delete urlParams;
}
function refresh(){
let xhr = new XMLHttpRequest();
xhr.onload = function(){callback(xhr.response);};
xhr.open("POST", "http://mirtemp.sao.ru:8080/");
xhr.send();
T = setTimeout(refresh, refrtime);
}
function setspeed(val){
let xhr = new XMLHttpRequest();
xhr.open("GET", "http://mirtemp.sao.ru:8080/setspeed=" + val);
xhr.send();
}
function setspeeda(){
setspeed($("setspeed").value);
}
function chkrad(){
var rad = document.getElementsByName('rs');
for(var i=0; i < rad.length; ++i){
if(rad[i].checked){
//console.log("checked: " + rad[i].value);
setspeed(rad[i].value);
}
}
}
</script>
</head>
<body>
<p>
New speed: <input id="setspeed" type="number" step="1" min="300" max="1300" value="500">
<button onclick="setspeeda();">Set</button>&nbsp;&nbsp;&nbsp;
<button onclick="setspeed(0);">Stop</button>
</p><p>
Set speed: <input type="radio" id="lowspd" name="rs" value="300"><label for="lowspd">Low</label>
<input type="radio" id="midspd" name="rs" value="800"><label for="midspd">Mid</label>
<input type="radio" id="highspd" name="rs" value="1300"><label for="highspd">High</label>
<input type="radio" id="stop" name="rs" checked value="0"><label for="stop">Stop</label>
<button onclick="chkrad();">Set</button><br>
</p>
<p>
Current: <span id="curval"></span> A. Speed: <span id="spdval"></span> rpm.
</p>
</body>
</html>

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB