ssssss
Diff: CushionSock.cpp
- Revision:
- 1:e6addce35f93
- Parent:
- 0:01a2e92d2924
--- a/CushionSock.cpp Sat Dec 17 09:13:37 2016 +0000 +++ b/CushionSock.cpp Sat Dec 17 11:03:10 2016 +0000 @@ -1,6 +1,23 @@ #include "CushionSock.h" +#define STOP 0x01 +#define START 0x02 +#define SIT 0x11 +#define FRUST 0x12 +#define TEST 0x21 +#define TWEET 0x13 -CushionSock::CushionSock(char * hostAndPort){ +CushionSock::CushionSock(){ + printf("begin\r\n"); + + MESS_STOP = 0x01; + MESS_START = 0x02; + MESS_SIT = 0x11; + MESS_FRUST = 0x12; + MESS_TEST =0x21; + MESS_TWEET = 0x13; +} + +void CushionSock::init(char * hostAndPort){ eth = new EthernetNetIf(); EthernetErr ethErr = eth->setup(); if (ethErr) { @@ -9,68 +26,64 @@ printf("hostandport %s\n", hostAndPort); ws = new Websocket(hostAndPort, eth); - - printf("begin\r\n"); - - MESS_STOP = 0x01; - MESS_START = 0x02; - MESS_SIT = 0x11; - MESS_FRUST = 0x12; - MESS_TEST =0x21; - type_name[MESS_STOP]="stop"; - type_name[MESS_START]="start"; - type_name[MESS_SIT]="status_sit"; - type_name[MESS_FRUST]="status_frust"; - type_name[MESS_TEST]="test"; - name_type["stop"]=MESS_STOP; - name_type["start"]=MESS_START; - name_type["status_sit"]=MESS_SIT; - name_type["status_frust"]=MESS_FRUST; - name_type["test"]=MESS_TEST; + printf("websock init\n"); +} - printf("websock init\n"); - int failcount=0; - - while(! ws->connect()) { +bool CushionSock::connect(){ + printf("connecting..."); + int failcount=0; + + while(! ws->connect()) { failcount++; if(failcount>10){ printf("cannot connect failed.\n"); - break; + return false; } printf("cannot connect websocket, retrying...\n"); wait(2); } -} - - -bool CushionSock::connect(){ - return true; } int CushionSock::mess_send(int type, char * msg){ - string name=type_name[type]; + string name; + switch(type){ + case TEST: + name="test"; + break; + case STOP: + name="stop"; + break; + case START: + name="start"; + break; + case SIT: + name="status_sit"; + break; + case FRUST: + name="status_frust"; + break; + case TWEET: + name="status_tweet"; + default: + name="null"; + } MbedJSONValue json; json["name"]=name.c_str(); json["value"]=msg; ws->send((char*)json.serialize().c_str()); + printf("data send\n"); return 1; } int CushionSock::mess_recv(char * msg){ - picojson::value v; - char recv[256]; - if (ws->read(recv)) { - int type; - string name; - string sbuf(recv); - const char * crecv=sbuf.c_str(); - string err = picojson::parse(v,crecv,crecv+strlen(crecv)); - if(!err.empty()) - return -1; - name=v.get("name").get<string>(); - strncpy(msg,v.get("value").get<string>().c_str(),256); - type=name_type[name]; + if (ws->read(msg)) { + int type=0; + printf("recieved msg : %s \n",msg); + if(strcmp(msg,"start")==0) + type=MESS_START; + if(strcmp(msg,"stop")==0) + type=MESS_STOP; return type; }else{ return -1;