2020-03-23 14:48:49 +00:00
|
|
|
#include "cwCommon.h"
|
|
|
|
#include "cwLog.h"
|
|
|
|
#include "cwCommonImpl.h"
|
|
|
|
#include "cwMem.h"
|
2020-04-07 20:24:34 +00:00
|
|
|
#include "cwThread.h"
|
|
|
|
#include "cwWebSock.h"
|
2020-04-21 21:56:38 +00:00
|
|
|
#include "cwObject.h"
|
2020-03-23 14:48:49 +00:00
|
|
|
#include "cwUi.h"
|
|
|
|
#include "cwUiTest.h"
|
|
|
|
|
|
|
|
|
|
|
|
namespace cw
|
|
|
|
{
|
|
|
|
namespace ui
|
|
|
|
{
|
|
|
|
typedef struct ui_test_str
|
|
|
|
{
|
2020-04-24 20:37:15 +00:00
|
|
|
const char* uiCfgFn;
|
2020-04-07 20:24:34 +00:00
|
|
|
srv::handle_t wsUiSrvH;
|
2020-04-24 20:37:15 +00:00
|
|
|
|
2020-04-24 14:20:25 +00:00
|
|
|
bool appCheckFl;
|
|
|
|
unsigned appSelectIndex;
|
|
|
|
int appInteger;
|
|
|
|
float appFloat;
|
|
|
|
int appProgress;
|
|
|
|
char* appString;
|
|
|
|
|
2020-03-23 14:48:49 +00:00
|
|
|
} ui_test_t;
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
kDivId,
|
|
|
|
kBtnId,
|
|
|
|
kCheckId,
|
|
|
|
kSelectId,
|
|
|
|
kOption0Id,
|
|
|
|
kOption1Id,
|
|
|
|
kOption2Id,
|
|
|
|
kOption3Id,
|
|
|
|
kStringId,
|
2020-04-24 14:20:25 +00:00
|
|
|
kIntegerId,
|
|
|
|
kFloatId,
|
2020-04-06 23:17:04 +00:00
|
|
|
kProgressId,
|
|
|
|
|
|
|
|
kPanelDivId,
|
|
|
|
kPanelBtnId,
|
|
|
|
kPanelCheckId
|
2020-03-23 14:48:49 +00:00
|
|
|
};
|
|
|
|
|
2020-03-31 17:10:45 +00:00
|
|
|
rc_t _uiTestCreateUi( ui_test_t* p, unsigned wsSessId )
|
2020-03-23 14:48:49 +00:00
|
|
|
{
|
|
|
|
rc_t rc = kOkRC;
|
|
|
|
unsigned uuid = kInvalidId;
|
|
|
|
unsigned selUuId = kInvalidId;
|
|
|
|
unsigned divUuId = kInvalidId;
|
|
|
|
|
2020-04-06 23:17:04 +00:00
|
|
|
appIdMap_t mapA[] =
|
2020-04-07 20:24:34 +00:00
|
|
|
{
|
|
|
|
{ ui::kRootAppId, kPanelDivId, "panelDivId" },
|
|
|
|
{ ui::kPanelDivId, kPanelBtnId, "myBtn1Id" },
|
|
|
|
{ ui::kPanelDivId, kPanelCheckId, "myCheck1Id" },
|
|
|
|
};
|
|
|
|
|
|
|
|
handle_t uiH = srv::uiHandle(p->wsUiSrvH);
|
2020-04-06 23:17:04 +00:00
|
|
|
|
2020-04-07 20:24:34 +00:00
|
|
|
|
|
|
|
registerAppIds(uiH, mapA, sizeof(mapA)/sizeof(mapA[0]));
|
2020-04-06 23:17:04 +00:00
|
|
|
|
2020-04-07 20:24:34 +00:00
|
|
|
if((rc = createDiv( uiH, divUuId, wsSessId, kInvalidId, "myDivId", kDivId, "divClass", "My Panel" )) != kOkRC )
|
2020-03-23 14:48:49 +00:00
|
|
|
goto errLabel;
|
2020-04-24 20:37:15 +00:00
|
|
|
|
2020-04-07 20:24:34 +00:00
|
|
|
if((rc = createButton( uiH, uuid, wsSessId, divUuId, "myBtnId", kBtnId, "btnClass", "Push Me" )) != kOkRC )
|
2020-03-23 14:48:49 +00:00
|
|
|
goto errLabel;
|
|
|
|
|
2020-04-07 20:24:34 +00:00
|
|
|
if((rc = createCheck( uiH, uuid, wsSessId, divUuId, "myCheckId", kCheckId, "checkClass", "Check Me", true )) != kOkRC )
|
2020-03-23 14:48:49 +00:00
|
|
|
goto errLabel;
|
|
|
|
|
2020-04-07 20:24:34 +00:00
|
|
|
if((rc = createSelect( uiH, selUuId, wsSessId, divUuId, "mySelId", kSelectId, "selClass", "Select" )) != kOkRC )
|
2020-03-23 14:48:49 +00:00
|
|
|
goto errLabel;
|
|
|
|
|
2020-04-07 20:24:34 +00:00
|
|
|
if((rc = createOption( uiH, uuid, wsSessId, selUuId, "myOpt0Id", kOption0Id, "optClass", "Option 0" )) != kOkRC )
|
2020-03-23 14:48:49 +00:00
|
|
|
goto errLabel;
|
|
|
|
|
2020-04-07 20:24:34 +00:00
|
|
|
if((rc = createOption( uiH, uuid, wsSessId, selUuId, "myOpt1Id", kOption1Id, "optClass", "Option 1" )) != kOkRC )
|
2020-03-23 14:48:49 +00:00
|
|
|
goto errLabel;
|
|
|
|
|
2020-04-07 20:24:34 +00:00
|
|
|
if((rc = createOption( uiH, uuid, wsSessId, selUuId, "myOpt2Id", kOption2Id, "optClass", "Option 2" )) != kOkRC )
|
2020-03-23 14:48:49 +00:00
|
|
|
goto errLabel;
|
|
|
|
|
2020-04-07 20:24:34 +00:00
|
|
|
if((rc = createString( uiH, uuid, wsSessId, divUuId, "myStringId", kStringId, "stringClass", "String", "a string value" )) != kOkRC )
|
2020-03-23 14:48:49 +00:00
|
|
|
goto errLabel;
|
|
|
|
|
2020-04-24 14:20:25 +00:00
|
|
|
if((rc = createNumber( uiH, uuid, wsSessId, divUuId, "myIntegerId", kIntegerId, "integerClass", "Integer", 10, 0, 100, 1, 0 )) != kOkRC )
|
|
|
|
goto errLabel;
|
|
|
|
|
|
|
|
if((rc = createNumber( uiH, uuid, wsSessId, divUuId, "myFloatId", kFloatId, "floatClass", "Float", 10.0, 0.53, 100.97, 1.0, 5 )) != kOkRC )
|
2020-03-23 14:48:49 +00:00
|
|
|
goto errLabel;
|
|
|
|
|
2020-04-07 20:24:34 +00:00
|
|
|
if((rc = createProgress( uiH, uuid, wsSessId, divUuId, "myProgressId", kProgressId, "progressClass", "Progress", 5, 0, 10 )) != kOkRC )
|
2020-03-31 17:10:45 +00:00
|
|
|
goto errLabel;
|
|
|
|
|
2020-04-07 20:24:34 +00:00
|
|
|
if((rc = createFromFile( uiH, p->uiCfgFn, wsSessId )) != kOkRC )
|
2020-03-23 14:48:49 +00:00
|
|
|
goto errLabel;
|
|
|
|
|
|
|
|
errLabel:
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2020-03-31 17:10:45 +00:00
|
|
|
rc_t _handleUiValueMsg( ui_test_t* p, unsigned wsSessId, unsigned parentAppId, unsigned uuId, unsigned appId, const value_t* v )
|
2020-03-23 14:48:49 +00:00
|
|
|
{
|
|
|
|
rc_t rc = kOkRC;
|
|
|
|
|
|
|
|
switch( appId )
|
|
|
|
{
|
|
|
|
case kBtnId:
|
|
|
|
printf("Click!\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kCheckId:
|
|
|
|
printf("Check:%i\n", v->u.b);
|
2020-04-24 14:20:25 +00:00
|
|
|
p->appCheckFl = v->u.b;
|
2020-03-23 14:48:49 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case kSelectId:
|
|
|
|
printf("Selected: optionId:%i\n", v->u.i);
|
2020-04-24 14:20:25 +00:00
|
|
|
p->appSelectIndex = v->u.i;
|
2020-03-23 14:48:49 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case kStringId:
|
|
|
|
printf("String: %s\n",v->u.s);
|
2020-04-24 14:20:25 +00:00
|
|
|
mem::release(p->appString);
|
|
|
|
if( v->u.s != nullptr )
|
|
|
|
p->appString = mem::duplStr(v->u.s);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kIntegerId:
|
|
|
|
printf("Integer: %i\n",v->u.i);
|
|
|
|
p->appInteger = v->u.i;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kFloatId:
|
|
|
|
printf("Float: %f\n",v->u.f);
|
|
|
|
p->appFloat = v->u.f;
|
2020-03-23 14:48:49 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
2020-04-24 14:20:25 +00:00
|
|
|
|
|
|
|
rc_t _handleUiEchoMsg( ui_test_t* p, unsigned wsSessId, unsigned parentAppId, unsigned uuId, unsigned appId )
|
|
|
|
{
|
|
|
|
rc_t rc = kOkRC;
|
|
|
|
|
|
|
|
switch( appId )
|
|
|
|
{
|
|
|
|
case kCheckId:
|
|
|
|
sendValueBool( uiHandle( p->wsUiSrvH ), wsSessId, uuId, p->appCheckFl );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kSelectId:
|
|
|
|
sendValueInt( uiHandle( p->wsUiSrvH ), wsSessId, uuId, p->appSelectIndex );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kStringId:
|
|
|
|
sendValueString( uiHandle( p->wsUiSrvH ), wsSessId, uuId, p->appString );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kIntegerId:
|
|
|
|
sendValueInt( uiHandle( p->wsUiSrvH ), wsSessId, uuId, p->appInteger );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kFloatId:
|
|
|
|
sendValueFloat( uiHandle( p->wsUiSrvH ), wsSessId, uuId, p->appFloat );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kProgressId:
|
|
|
|
sendValueInt( uiHandle( p->wsUiSrvH ), wsSessId, uuId, p->appProgress );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
2020-03-23 14:48:49 +00:00
|
|
|
|
2020-03-31 17:10:45 +00:00
|
|
|
|
2020-04-24 14:20:25 +00:00
|
|
|
|
2020-03-31 17:10:45 +00:00
|
|
|
// This function is called by the websocket with messages comring from a remote UI.
|
|
|
|
rc_t _uiTestCallback( void* cbArg, unsigned wsSessId, opId_t opId, unsigned parentAppId, unsigned uuId, unsigned appId, const value_t* v )
|
2020-03-23 14:48:49 +00:00
|
|
|
{
|
|
|
|
ui_test_t* p = (ui_test_t*)cbArg;
|
|
|
|
|
|
|
|
switch( opId )
|
|
|
|
{
|
|
|
|
case kConnectOpId:
|
2020-04-24 20:37:15 +00:00
|
|
|
cwLogInfo("UI Test Connect: wsSessId:%i.",wsSessId);
|
2020-03-23 14:48:49 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case kDisconnectOpId:
|
2020-04-24 20:37:15 +00:00
|
|
|
cwLogInfo("UI Test Disconnect: wsSessId:%i.",wsSessId);
|
2020-03-23 14:48:49 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case kInitOpId:
|
2020-03-31 17:10:45 +00:00
|
|
|
_uiTestCreateUi(p,wsSessId);
|
2020-03-23 14:48:49 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case kValueOpId:
|
2020-03-31 17:10:45 +00:00
|
|
|
_handleUiValueMsg( p, wsSessId, parentAppId, uuId, appId, v );
|
2020-03-23 14:48:49 +00:00
|
|
|
break;
|
|
|
|
|
2020-04-24 14:20:25 +00:00
|
|
|
case kEchoOpId:
|
|
|
|
_handleUiEchoMsg( p, wsSessId, parentAppId, uuId, appId );
|
|
|
|
break;
|
|
|
|
|
2020-03-23 14:48:49 +00:00
|
|
|
case kInvalidOpId:
|
|
|
|
// fall through
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
return kOkRC;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cw::rc_t cw::ui::test( )
|
|
|
|
{
|
|
|
|
rc_t rc = kOkRC;
|
2020-03-31 17:10:45 +00:00
|
|
|
const char* physRootDir = "/home/kevin/src/cwtest/src/libcw/html/uiTest";
|
2020-03-23 14:48:49 +00:00
|
|
|
const char* dfltPageFn = "index.html";
|
|
|
|
int port = 5687;
|
|
|
|
unsigned rcvBufByteN = 2048;
|
|
|
|
unsigned xmtBufByteN = 2048;
|
2020-04-07 20:24:34 +00:00
|
|
|
unsigned fmtBufByteN = 4096;
|
2020-03-23 14:48:49 +00:00
|
|
|
unsigned websockTimeOutMs = 50;
|
|
|
|
const unsigned sbufN = 31;
|
|
|
|
char sbuf[ sbufN+1 ];
|
2020-04-07 20:24:34 +00:00
|
|
|
ui_test_t* app = mem::allocZ<ui_test_t>();
|
2020-03-31 17:10:45 +00:00
|
|
|
|
2020-04-24 14:20:25 +00:00
|
|
|
|
|
|
|
app->appCheckFl = true;
|
|
|
|
app->appSelectIndex = 1;
|
|
|
|
app->appInteger = 5;
|
|
|
|
app->appFloat = 2.56;
|
|
|
|
app->appProgress = 7;
|
|
|
|
app->appString = mem::duplStr("fooz");
|
|
|
|
|
|
|
|
|
2020-04-07 20:24:34 +00:00
|
|
|
app->uiCfgFn = "/home/kevin/src/cwtest/src/libcw/html/uiTest/ui.cfg";
|
2020-03-23 14:48:49 +00:00
|
|
|
|
2020-04-07 20:24:34 +00:00
|
|
|
// create the UI server
|
|
|
|
if((rc = srv::create(app->wsUiSrvH, port, physRootDir, app, _uiTestCallback, nullptr, dfltPageFn, websockTimeOutMs, rcvBufByteN, xmtBufByteN, fmtBufByteN )) != kOkRC )
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
|
|
|
|
// start the UI server
|
|
|
|
if((rc = srv::start(app->wsUiSrvH)) != kOkRC )
|
2020-03-23 14:48:49 +00:00
|
|
|
goto errLabel;
|
|
|
|
|
|
|
|
|
|
|
|
printf("'quit' to exit\n");
|
|
|
|
|
|
|
|
// readline loop
|
|
|
|
while( true )
|
|
|
|
{
|
|
|
|
printf("? ");
|
|
|
|
if( std::fgets(sbuf,sbufN,stdin) == sbuf )
|
|
|
|
{
|
|
|
|
printf("Sending:%s",sbuf);
|
|
|
|
|
|
|
|
if( strcmp(sbuf,"quit\n") == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
errLabel:
|
|
|
|
rc_t rc1 = kOkRC;
|
2020-04-07 20:24:34 +00:00
|
|
|
if( app->wsUiSrvH.isValid() )
|
|
|
|
rc1 = srv::destroy(app->wsUiSrvH);
|
2020-04-24 14:20:25 +00:00
|
|
|
|
|
|
|
mem::release(app->appString);
|
2020-04-07 20:24:34 +00:00
|
|
|
mem::release(app);
|
2020-03-23 14:48:49 +00:00
|
|
|
|
|
|
|
return rcSelect(rc,rc1);
|
|
|
|
}
|