...

Dependencies:   C12832 EthernetInterface MbedJSONValue WebSocketClient mbed-rtos mbed

Fork of microServiceBus_node by Mikael Hakansson

Committer:
wmmihaa
Date:
Thu Aug 25 13:53:00 2016 +0000
Revision:
0:e542df4c4901
Child:
1:b182b5dc15e7
Created

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wmmihaa 0:e542df4c4901 1 #include "mbed.h"
wmmihaa 0:e542df4c4901 2 #include "EthernetInterface.h"
wmmihaa 0:e542df4c4901 3 #include "Websocket.h"
wmmihaa 0:e542df4c4901 4 #include "MbedJSONValue.h"
wmmihaa 0:e542df4c4901 5 #include "C12832.h"
wmmihaa 0:e542df4c4901 6 #include <string>
wmmihaa 0:e542df4c4901 7 #include "bootloader.h"
wmmihaa 0:e542df4c4901 8
wmmihaa 0:e542df4c4901 9
wmmihaa 0:e542df4c4901 10 C12832 lcd(D11, D13, D12, D7, D10);
wmmihaa 0:e542df4c4901 11 Serial pc1(USBTX, USBRX); // tx, rx
wmmihaa 0:e542df4c4901 12
wmmihaa 0:e542df4c4901 13 string _organizationId = "74ff8902-2057-499a-bca8-36a699c2458f";
wmmihaa 0:e542df4c4901 14
wmmihaa 0:e542df4c4901 15 int main() {
wmmihaa 0:e542df4c4901 16
wmmihaa 0:e542df4c4901 17 lcd.cls();
wmmihaa 0:e542df4c4901 18 pc1.printf("\r\nStarting up\r\n");
wmmihaa 0:e542df4c4901 19 EthernetInterface eth;
wmmihaa 0:e542df4c4901 20 eth.init(); //Use DHCP
wmmihaa 0:e542df4c4901 21 eth.connect();
wmmihaa 0:e542df4c4901 22 pc1.printf("IP Address is %s\r\n", eth.getIPAddress());
wmmihaa 0:e542df4c4901 23
wmmihaa 0:e542df4c4901 24 //Websocket ws("ws://microservicebus-northeurope-stage.azurewebsites.net/Services/WsHandler.ashx?id=42");
wmmihaa 0:e542df4c4901 25 Websocket ws("ws://192.168.1.64/Services/WsHandler.ashx?id=42");
wmmihaa 0:e542df4c4901 26 bool connected;
wmmihaa 0:e542df4c4901 27 connected = ws.connect();
wmmihaa 0:e542df4c4901 28
wmmihaa 0:e542df4c4901 29 if(connected)
wmmihaa 0:e542df4c4901 30 {
wmmihaa 0:e542df4c4901 31 pc1.printf("Connected successfully\r\n");
wmmihaa 0:e542df4c4901 32 }
wmmihaa 0:e542df4c4901 33 else
wmmihaa 0:e542df4c4901 34 {
wmmihaa 0:e542df4c4901 35 pc1.printf("Unable to connect\r\n");
wmmihaa 0:e542df4c4901 36 ws.close();
wmmihaa 0:e542df4c4901 37 exit(0);
wmmihaa 0:e542df4c4901 38 }
wmmihaa 0:e542df4c4901 39
wmmihaa 0:e542df4c4901 40 //while (!ws.connect());
wmmihaa 0:e542df4c4901 41 char str[10000];
wmmihaa 0:e542df4c4901 42
wmmihaa 0:e542df4c4901 43 MbedJSONValue signInJson;
wmmihaa 0:e542df4c4901 44 signInJson["nodeName"] = eth.getMACAddress();
wmmihaa 0:e542df4c4901 45 signInJson["organizationId"] = _organizationId;
wmmihaa 0:e542df4c4901 46 signInJson["machineName"] = eth.getMACAddress();
wmmihaa 0:e542df4c4901 47 signInJson["ip"] = eth.getIPAddress();
wmmihaa 0:e542df4c4901 48
wmmihaa 0:e542df4c4901 49 char buf[256];
wmmihaa 0:e542df4c4901 50 snprintf(buf, sizeof buf, "signIn::%s", signInJson.serialize());
wmmihaa 0:e542df4c4901 51 ws.send(buf);
wmmihaa 0:e542df4c4901 52
wmmihaa 0:e542df4c4901 53 while(1){
wmmihaa 0:e542df4c4901 54 memset(str, 0, 10000);
wmmihaa 0:e542df4c4901 55 wait(1.0f);
wmmihaa 0:e542df4c4901 56 ///pc1.printf(".");
wmmihaa 0:e542df4c4901 57
wmmihaa 0:e542df4c4901 58 if (ws.read(str)) {
wmmihaa 0:e542df4c4901 59
wmmihaa 0:e542df4c4901 60 string msg(str);
wmmihaa 0:e542df4c4901 61
wmmihaa 0:e542df4c4901 62 if (msg.find("broadcast::") == 0)
wmmihaa 0:e542df4c4901 63 {
wmmihaa 0:e542df4c4901 64 pc1.printf("Broadcast: ");
wmmihaa 0:e542df4c4901 65 pc1.printf(str);
wmmihaa 0:e542df4c4901 66 pc1.printf("\r\n");
wmmihaa 0:e542df4c4901 67 }
wmmihaa 0:e542df4c4901 68 else if (msg.find("signInMessage::") == 0)
wmmihaa 0:e542df4c4901 69 {
wmmihaa 0:e542df4c4901 70 pc1.printf("Sign In successfully\r\n");
wmmihaa 0:e542df4c4901 71 }
wmmihaa 0:e542df4c4901 72 else if (msg.find("reboot::") == 0)
wmmihaa 0:e542df4c4901 73 {
wmmihaa 0:e542df4c4901 74 pc1.printf("Disconnecting...\r\n");
wmmihaa 0:e542df4c4901 75 eth.disconnect();
wmmihaa 0:e542df4c4901 76 pc1.printf("\r\nCalling bootloader...\r\n");
wmmihaa 0:e542df4c4901 77 write_flash();
wmmihaa 0:e542df4c4901 78 }
wmmihaa 0:e542df4c4901 79 else if (msg.find("errorMessage::") == 0)
wmmihaa 0:e542df4c4901 80 {
wmmihaa 0:e542df4c4901 81 pc1.printf("ERROR...\r\n");
wmmihaa 0:e542df4c4901 82 pc1.printf(str);
wmmihaa 0:e542df4c4901 83 pc1.printf("\r\n");
wmmihaa 0:e542df4c4901 84 }
wmmihaa 0:e542df4c4901 85 else if (msg.find("ping::") == 0)
wmmihaa 0:e542df4c4901 86 {
wmmihaa 0:e542df4c4901 87 msg.replace(0,6,"");
wmmihaa 0:e542df4c4901 88 pc1.printf("ping from:");
wmmihaa 0:e542df4c4901 89 pc1.printf(msg.c_str());
wmmihaa 0:e542df4c4901 90 pc1.printf("\r\n");
wmmihaa 0:e542df4c4901 91
wmmihaa 0:e542df4c4901 92 MbedJSONValue pingRequestJson;
wmmihaa 0:e542df4c4901 93 const char * json = msg.c_str();
wmmihaa 0:e542df4c4901 94 parse(pingRequestJson, json);
wmmihaa 0:e542df4c4901 95 std::string connectionid;
wmmihaa 0:e542df4c4901 96 connectionid = pingRequestJson["connectionid"].get<std::string>();
wmmihaa 0:e542df4c4901 97
wmmihaa 0:e542df4c4901 98 MbedJSONValue pingResponseJson;
wmmihaa 0:e542df4c4901 99 pingResponseJson["nodeName"] = eth.getMACAddress();
wmmihaa 0:e542df4c4901 100 pingResponseJson["organizationId"] = _organizationId;
wmmihaa 0:e542df4c4901 101 pingResponseJson["connectionid"] = connectionid;
wmmihaa 0:e542df4c4901 102 pingResponseJson["status"] = "online";
wmmihaa 0:e542df4c4901 103
wmmihaa 0:e542df4c4901 104 snprintf(buf, sizeof buf, "pingResponse::%s", pingResponseJson.serialize());
wmmihaa 0:e542df4c4901 105 ws.send(buf);
wmmihaa 0:e542df4c4901 106 }
wmmihaa 0:e542df4c4901 107 }
wmmihaa 0:e542df4c4901 108
wmmihaa 0:e542df4c4901 109 }
wmmihaa 0:e542df4c4901 110 ws.close();
wmmihaa 0:e542df4c4901 111
wmmihaa 0:e542df4c4901 112 // lcd.printf("Signing out...");
wmmihaa 0:e542df4c4901 113 }
wmmihaa 0:e542df4c4901 114 char* appendCharToCharArray(char* array, char a)
wmmihaa 0:e542df4c4901 115 {
wmmihaa 0:e542df4c4901 116 size_t len = strlen(array);
wmmihaa 0:e542df4c4901 117
wmmihaa 0:e542df4c4901 118 char* ret = new char[len+2];
wmmihaa 0:e542df4c4901 119
wmmihaa 0:e542df4c4901 120 strcpy(ret, array);
wmmihaa 0:e542df4c4901 121 ret[len] = a;
wmmihaa 0:e542df4c4901 122 ret[len+1] = '\0';
wmmihaa 0:e542df4c4901 123
wmmihaa 0:e542df4c4901 124 return ret;
wmmihaa 0:e542df4c4901 125 }