ssssss
Revision 1:e6addce35f93, committed 2016-12-17
- Comitter:
- 3116redtrain
- Date:
- Sat Dec 17 11:03:10 2016 +0000
- Parent:
- 0:01a2e92d2924
- Commit message:
- first comp;
Changed in this revision
CushionSock.cpp | Show annotated file Show diff for this revision Revisions of this file |
CushionSock.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 01a2e92d2924 -r e6addce35f93 CushionSock.cpp --- 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;
diff -r 01a2e92d2924 -r e6addce35f93 CushionSock.h --- a/CushionSock.h Sat Dec 17 09:13:37 2016 +0000 +++ b/CushionSock.h Sat Dec 17 11:03:10 2016 +0000 @@ -4,7 +4,6 @@ #include "mbed.h" #include "Websocket.h" #include "EthernetNetIf.h" -#include "picojson.h" #include "MbedJSONValue.h" @@ -16,13 +15,15 @@ int MESS_SIT; int MESS_FRUST; int MESS_TEST; + int MESS_TWEET; /* Constructor @param hostAndport format : "hostname:port" */ - CushionSock(char * hostAndPort); + CushionSock(); + void init(char * hostAndPort); /* connect to server @return true if the connection is established, false otherwise @@ -55,8 +56,5 @@ private: EthernetNetIf *eth; Websocket *ws; - - std::map<int,string> type_name; - std::map<string,int> name_type; }; #endif \ No newline at end of file