Files
sics/tecs/tecs_cli.c
2001-09-03 14:30:38 +00:00

403 lines
8.6 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "myc_mem.h"
#include "myc_err.h"
#include "myc_str.h"
#include "myc_time.h"
#include "sys_util.h"
#include "coc_util.h"
#include "tecs_cli.h"
#include "tecs_data.h"
static char response[COC_RES_LEN];
static char *rwCode="rwacs";
static char *rdCode="rdacs";
pTecsClient TeccInit(char *startcmd, int port) {
CocConn *conn;
NEW(conn, CocConn);
if (startcmd[0]=='#') {
ERR_I(CocInitClient(conn, startcmd+1, port, rdCode, 0, ""));
} else {
ERR_I(CocInitClient(conn, "", port, rwCode, 0, startcmd));
}
return((pTecsClient)conn);
OnError: return(NULL);
}
int TeccGet3(pTecsClient conn, float *tC, float *tX, float *tP) {
int iret;
CocReset(conn);
ERR_I(CocGetFloat(conn, "set", tC));
ERR_I(CocGetFloat(conn, "tempX", tX));
ERR_I(CocGetFloat(conn, "tempP", tP));
ERR_I(CocPutInt(conn, "readTemp", 1));
ERR_I(iret=CocDoIt(conn, response, sizeof(response)));
if (iret) ERR_MSG(response);
return(0);
OnError: return(-1);
}
int TeccGet(pTecsClient conn, float *temp) {
int iret;
CocReset(conn);
ERR_I(CocGetFloat(conn, "tempP", temp));
ERR_I(CocPutInt(conn, "readTemp", 1));
ERR_I(iret=CocDoIt(conn, response, sizeof(response)));
if (iret) ERR_MSG(response);
return(0);
OnError: return(-1);
}
int TeccSet(pTecsClient conn, float temp) {
int iret;
CocReset(conn);
ERR_I(CocPutFloat(conn, "set", temp));
ERR_I(iret=CocDoIt(conn, response, sizeof(response)));
if (iret) ERR_MSG(response);
return(0);
OnError: return(-1);
}
int TeccQuitServer(pTecsClient conn, int kill) {
int iret, cnt;
ERR_I(iret=CocCheck(conn));
if (iret>0) return iret;
CocReset(conn);
ERR_I(CocPutInt(conn, "quit", 1+kill));
ERR_I(iret=CocDoIt(conn, response, sizeof(response)));
if (iret) ERR_MSG(response);
cnt=50;
while (iret==0 && cnt>0) {
CocDelay(100);
ERR_I(iret=CocCheck(conn));
cnt--;
}
if (iret==0) ERR_MSG("Does not quit within 5 seconds");
return 0;
OnError:
return(-1);
}
int TeccSend(pTecsClient conn, char *cmd, char *reply, int replyLen) {
return(CocSetGetN(conn, "send", cmd, reply, replyLen));
}
void TeccClose(pTecsClient conn) {
if (conn!=NULL) {
CocCloseClient(conn);
FREE(conn);
}
}
/* fortran wrappers --------------------------------------------------
reduced functionality:
connection is static, so only one connection at a time may be opened
*/
#ifdef F_CHAR
/* compile only when fortran c interface stuff is defined */
#ifdef __VMS
#define tecs_get_par_ tecs_get_par
#define tecs_get_mult_ tecs_get_mult
#define tecs_set_par_ tecs_set_par
#define tecs_init_ tecs_init
#define tecs_get3_ tecs_get3
#define tecs_get_ tecs_get
#define tecs_set_ tecs_set
#define tecs_is_open_ tecs_is_open
#define tecs_close_ tecs_close
#define tecs_quit_server_ tecs_quit_server
#define tecs_watch_log_ tecs_watch_log
#define tecs_get_data_ tecs_get_data
#define tecs_date_ tecs_date
#define tecs_time_ tecs_time
#define tecs_rights_ tecs_rights
#endif
static pTecsClient conn=NULL;
int tecs_set_par_(F_CHAR(name), F_CHAR(par), int *show, int name_len, int par_len) {
char nbuf[64], pbuf[COC_CMD_LEN];
int iret=-1;
STR_TO_C(nbuf, name);
STR_TO_C(pbuf, par);
CocReset(conn);
ERR_I(CocPutStr(conn, nbuf, pbuf));
ERR_I(CocDoIt(conn, response, sizeof(response)));
if (*show) {
printf("%s", response);
}
return(0);
OnError: return(-1);
}
int tecs_get_par_(F_CHAR(name), F_CHAR(par), int *show, int name_len, int par_len) {
char *b, nbuf[64], pbuf[COC_RES_LEN];
int iret=-1;
STR_TO_C(nbuf, name);
CocReset(conn);
ERR_I(CocGetStr(conn, nbuf, pbuf, sizeof(pbuf)));
ERR_I(CocDoIt(conn, response, sizeof(response)));
if (*show==2) {
b=response;
} else {
b=strchr(response,'=');
if (b==NULL) {
b=response;
} else {
b++;
}
}
if (*show) {
printf("%s", b);
}
return(STR_TO_F(par, pbuf));
OnError: return(-1);
}
int tecs_get_mult_(F_CHAR(names), int *time, int *nvalues, float values[], int names_len) {
char *b, nbuf[64], pbuf[COC_RES_LEN];
char *nams, nam[32];
int i;
STR_TO_C(nbuf, names);
CocReset(conn);
ERR_I(CocGetInt(conn, "rdTim", time));
i=0;
nams=nbuf;
while (nams!=NULL && i < *nvalues) {
nams=str_split(nam, nams, ' ');
if (nam[0]!='\0') {
ERR_I(CocGetFloat(conn, nam, values+i));
i++;
}
}
ERR_I(CocDoIt(conn, response, sizeof(response)));
return(0);
OnError: return(-1);
}
int tecs_init_(F_CHAR(startcmd), int *port, int startcmd_len) {
char sbuf[132];
STR_TO_C(sbuf, startcmd);
ERR_P(conn=TeccInit(sbuf, *port));
return(0);
OnError: return(-1);
}
int tecs_rights_(int write) {
if (write) {
ERR_I(CocSendMagic(conn, rwCode));
} else {
ERR_I(CocSendMagic(conn, rdCode));
}
return(0);
OnError: return(-1);
}
int tecs_get_(float *temp) {
ERR_I(TeccGet(conn, temp));
return(0);
OnError: return(-1);
}
int tecs_get3_(float *t1, float *t2, float *t3) {
ERR_I(TeccGet3(conn, t1, t2, t3));
return(0);
OnError: return(-1);
}
int tecs_set_(float *temp) {
ERR_I(TeccSet(conn, *temp));
return(0);
OnError: return(-1);
}
int tecs_is_open_() {
return(conn!=NULL);
}
void tecs_close_(void) {
TeccClose(conn);
conn=NULL;
}
int tecs_quit_server_(int *kill) {
int iret;
ERR_I(iret=TeccQuitServer(conn, *kill));
return(iret);
OnError: return(-1);
}
int tecs_watch_log_(F_CHAR(list), int list_len) {
char buf[16];
STR_TO_C(buf, list);
ERR_I(CocWatchLog(conn, buf));
return(0);
OnError: return(-1);
}
static char *encode=DATA_CODE;
int DataDecode(float *data, int dataSize, char *coded, int *retSize) {
int i, p, gap, dig1, dig2;
float minD, range;
char ch, *q;
static int decode[256];
static int init=1;
if (coded[0] == '\0') {
*retSize=0;
return 0;
}
if (init) {
init=0;
for (i=0; i<256; i++) {
decode[i]=-1;
}
i=0;
while (encode[i]!=0) {
decode[encode[i]]=i;
i++;
}
}
p=0;
ERR_SI(sscanf(coded, "%d %e %e %n", retSize, &minD, &range, &p)-3);
if (*retSize < dataSize) dataSize = *retSize;
i=0;
while (i < dataSize) {
ch=coded[p++];
dig1=decode[ch];
if (dig1 < 0) { /* code is no 64-digit */
if (ch=='\0' || ch==',') break;
if (ch=='/') {
data[i++] = DATA_UNDEF;
} else if (ch=='.') {
data[i++] = DATA_GAP;
}
} else {
ch=coded[p++];
dig2=decode[ch];
while (dig2<0) {
if (ch=='\0' || ch==',') break;
ch=coded[p++];
dig2=decode[ch];
}
if (ch=='\0' || ch==',') break;
data[i++] = (dig1 + dig2 * 64) / 4095.0 * range + minD;
}
}
while (i < dataSize) {
data[i++] = DATA_UNDEF;
}
if (ch=='\0') {
return p-1;
} else if (ch!=',') {
q=strchr(coded+p, ',');
if (q==NULL) return strlen(coded);
return q-coded+1;
}
return p;
OnError:
*retSize=0;
return -1;
}
int tecs_get_data_(F_CHAR(names), int *startTime, int *endTime, int *step, int *tbase
, float xdata[], float ydata[], int *maxLen, int *width
, int retLen[], int names_len) {
char nam[64];
char str[128];
char res[COC_RES_LEN];
char *cod;
float offset, fact, *py;
int i, j, k, l, iret, retSize;
if (*endTime - *startTime > *step * (*maxLen-1)) {
printf("maxLen too small\n");
}
STR_TO_C(nam, names);
if (*step <= 0) {
*step = 60;
}
sprintf(str, "%d %d %d %s", *startTime, *endTime, *step, nam);
CocReset(conn);
ERR_I(CocPutStr(conn, "pltdata", str));
ERR_I(CocGetStr(conn, "pltdata", res, sizeof(res)));
ERR_I(iret=CocDoIt(conn, response, sizeof(response)));
if (iret) ERR_MSG(response);
cod=res;
for (i=0; i < *width; i++) {
py = ydata + i * *maxLen;
ERR_I(l=DataDecode(py, *maxLen, cod, retLen+i));
if (retLen[i]>0) {
fact = (float)(*endTime - *startTime) / retLen[i];
}
offset = *startTime - *tbase;
k = i * *maxLen;
for (j=0; j<retLen[i]; j++) {
if (py[j] != DATA_GAP) {
ydata[k] = py[j];
xdata[k] = offset + j * fact;
k++;
}
}
retLen[i] = k - i * *maxLen;
cod+=l;
}
return 0;
OnError:
return -1;
}
/*
float tecs_date_(time_t *time) {
struct tm tim;
tim=*localtime(time);
return(tim.tm_mday+(tim.tm_mon+1)*0.01);
}
time_t tecs_time_(float *date) {
struct tm tim;
time_t t, now;
int m;
time(&now);
tim=*localtime(&now);
tim.tm_hour=0;
tim.tm_min=0;
tim.tm_sec=0;
tim.tm_mday=(int)*date;
m=(int)(*date*100+0.1) % 100;
if (m > 0) tim.tm_mon=m-1;
t=mktime(&tim);
if (t > now) {
tim.tm_year--;
t=mktime(&tim);
}
return t;
}
*/
#endif