ssssss

Committer:
3116redtrain
Date:
Sat Dec 17 11:03:10 2016 +0000
Revision:
1:e6addce35f93
Parent:
0:01a2e92d2924
first comp;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
3116redtrain 0:01a2e92d2924 1 #include "CushionSock.h"
3116redtrain 1:e6addce35f93 2 #define STOP 0x01
3116redtrain 1:e6addce35f93 3 #define START 0x02
3116redtrain 1:e6addce35f93 4 #define SIT 0x11
3116redtrain 1:e6addce35f93 5 #define FRUST 0x12
3116redtrain 1:e6addce35f93 6 #define TEST 0x21
3116redtrain 1:e6addce35f93 7 #define TWEET 0x13
3116redtrain 0:01a2e92d2924 8
3116redtrain 1:e6addce35f93 9 CushionSock::CushionSock(){
3116redtrain 1:e6addce35f93 10 printf("begin\r\n");
3116redtrain 1:e6addce35f93 11
3116redtrain 1:e6addce35f93 12 MESS_STOP = 0x01;
3116redtrain 1:e6addce35f93 13 MESS_START = 0x02;
3116redtrain 1:e6addce35f93 14 MESS_SIT = 0x11;
3116redtrain 1:e6addce35f93 15 MESS_FRUST = 0x12;
3116redtrain 1:e6addce35f93 16 MESS_TEST =0x21;
3116redtrain 1:e6addce35f93 17 MESS_TWEET = 0x13;
3116redtrain 1:e6addce35f93 18 }
3116redtrain 1:e6addce35f93 19
3116redtrain 1:e6addce35f93 20 void CushionSock::init(char * hostAndPort){
3116redtrain 0:01a2e92d2924 21 eth = new EthernetNetIf();
3116redtrain 0:01a2e92d2924 22 EthernetErr ethErr = eth->setup();
3116redtrain 0:01a2e92d2924 23 if (ethErr) {
3116redtrain 0:01a2e92d2924 24 printf("\r\nERROR %d in setup.\r\n", ethErr);
3116redtrain 0:01a2e92d2924 25 }
3116redtrain 0:01a2e92d2924 26 printf("hostandport %s\n", hostAndPort);
3116redtrain 0:01a2e92d2924 27
3116redtrain 0:01a2e92d2924 28 ws = new Websocket(hostAndPort, eth);
3116redtrain 1:e6addce35f93 29 printf("websock init\n");
3116redtrain 1:e6addce35f93 30 }
3116redtrain 0:01a2e92d2924 31
3116redtrain 1:e6addce35f93 32 bool CushionSock::connect(){
3116redtrain 1:e6addce35f93 33 printf("connecting...");
3116redtrain 1:e6addce35f93 34 int failcount=0;
3116redtrain 1:e6addce35f93 35
3116redtrain 1:e6addce35f93 36 while(! ws->connect()) {
3116redtrain 0:01a2e92d2924 37 failcount++;
3116redtrain 0:01a2e92d2924 38 if(failcount>10){
3116redtrain 0:01a2e92d2924 39 printf("cannot connect failed.\n");
3116redtrain 1:e6addce35f93 40 return false;
3116redtrain 0:01a2e92d2924 41 }
3116redtrain 0:01a2e92d2924 42 printf("cannot connect websocket, retrying...\n");
3116redtrain 0:01a2e92d2924 43 wait(2);
3116redtrain 0:01a2e92d2924 44 }
3116redtrain 0:01a2e92d2924 45 return true;
3116redtrain 0:01a2e92d2924 46 }
3116redtrain 0:01a2e92d2924 47
3116redtrain 0:01a2e92d2924 48 int CushionSock::mess_send(int type, char * msg){
3116redtrain 1:e6addce35f93 49 string name;
3116redtrain 1:e6addce35f93 50 switch(type){
3116redtrain 1:e6addce35f93 51 case TEST:
3116redtrain 1:e6addce35f93 52 name="test";
3116redtrain 1:e6addce35f93 53 break;
3116redtrain 1:e6addce35f93 54 case STOP:
3116redtrain 1:e6addce35f93 55 name="stop";
3116redtrain 1:e6addce35f93 56 break;
3116redtrain 1:e6addce35f93 57 case START:
3116redtrain 1:e6addce35f93 58 name="start";
3116redtrain 1:e6addce35f93 59 break;
3116redtrain 1:e6addce35f93 60 case SIT:
3116redtrain 1:e6addce35f93 61 name="status_sit";
3116redtrain 1:e6addce35f93 62 break;
3116redtrain 1:e6addce35f93 63 case FRUST:
3116redtrain 1:e6addce35f93 64 name="status_frust";
3116redtrain 1:e6addce35f93 65 break;
3116redtrain 1:e6addce35f93 66 case TWEET:
3116redtrain 1:e6addce35f93 67 name="status_tweet";
3116redtrain 1:e6addce35f93 68 default:
3116redtrain 1:e6addce35f93 69 name="null";
3116redtrain 1:e6addce35f93 70 }
3116redtrain 0:01a2e92d2924 71 MbedJSONValue json;
3116redtrain 0:01a2e92d2924 72 json["name"]=name.c_str();
3116redtrain 0:01a2e92d2924 73 json["value"]=msg;
3116redtrain 0:01a2e92d2924 74 ws->send((char*)json.serialize().c_str());
3116redtrain 1:e6addce35f93 75 printf("data send\n");
3116redtrain 0:01a2e92d2924 76 return 1;
3116redtrain 0:01a2e92d2924 77 }
3116redtrain 0:01a2e92d2924 78
3116redtrain 0:01a2e92d2924 79 int CushionSock::mess_recv(char * msg){
3116redtrain 1:e6addce35f93 80 if (ws->read(msg)) {
3116redtrain 1:e6addce35f93 81 int type=0;
3116redtrain 1:e6addce35f93 82 printf("recieved msg : %s \n",msg);
3116redtrain 1:e6addce35f93 83 if(strcmp(msg,"start")==0)
3116redtrain 1:e6addce35f93 84 type=MESS_START;
3116redtrain 1:e6addce35f93 85 if(strcmp(msg,"stop")==0)
3116redtrain 1:e6addce35f93 86 type=MESS_STOP;
3116redtrain 0:01a2e92d2924 87 return type;
3116redtrain 0:01a2e92d2924 88 }else{
3116redtrain 0:01a2e92d2924 89 return -1;
3116redtrain 0:01a2e92d2924 90 }
3116redtrain 0:01a2e92d2924 91 }
3116redtrain 0:01a2e92d2924 92
3116redtrain 0:01a2e92d2924 93 bool CushionSock::is_connected(){
3116redtrain 0:01a2e92d2924 94 return ws->connected();
3116redtrain 0:01a2e92d2924 95 }
3116redtrain 0:01a2e92d2924 96
3116redtrain 0:01a2e92d2924 97 bool CushionSock::close(){
3116redtrain 0:01a2e92d2924 98 return ws->close();
3116redtrain 0:01a2e92d2924 99 }