...
Dependencies: C12832 EthernetInterface MbedJSONValue WebSocketClient mbed-rtos mbed
Fork of microServiceBus_node by
main.cpp@1:b182b5dc15e7, 2017-04-23 (annotated)
- Committer:
- wmmihaa
- Date:
- Sun Apr 23 12:15:25 2017 +0000
- Revision:
- 1:b182b5dc15e7
- Parent:
- 0:e542df4c4901
- Child:
- 2:fe293fbcf3e3
...
Who changed what in which revision?
User | Revision | Line number | New 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 | |
wmmihaa | 0:e542df4c4901 | 8 | C12832 lcd(D11, D13, D12, D7, D10); |
wmmihaa | 0:e542df4c4901 | 9 | Serial pc1(USBTX, USBRX); // tx, rx |
wmmihaa | 0:e542df4c4901 | 10 | |
wmmihaa | 1:b182b5dc15e7 | 11 | string _organizationId = "5f5967d9-4020-40d9-86e7-e813664d0397"; // AXIANS |
wmmihaa | 0:e542df4c4901 | 12 | |
wmmihaa | 0:e542df4c4901 | 13 | int main() { |
wmmihaa | 0:e542df4c4901 | 14 | |
wmmihaa | 0:e542df4c4901 | 15 | lcd.cls(); |
wmmihaa | 0:e542df4c4901 | 16 | pc1.printf("\r\nStarting up\r\n"); |
wmmihaa | 0:e542df4c4901 | 17 | EthernetInterface eth; |
wmmihaa | 0:e542df4c4901 | 18 | eth.init(); //Use DHCP |
wmmihaa | 0:e542df4c4901 | 19 | eth.connect(); |
wmmihaa | 0:e542df4c4901 | 20 | pc1.printf("IP Address is %s\r\n", eth.getIPAddress()); |
wmmihaa | 1:b182b5dc15e7 | 21 | pc1.printf("MAC Address is %s\r\n", eth.getMACAddress()); |
wmmihaa | 0:e542df4c4901 | 22 | |
wmmihaa | 1:b182b5dc15e7 | 23 | Websocket ws("ws://microservicebus-northeurope-stage.azurewebsites.net/Services/WsHandler.ashx?id=42"); |
wmmihaa | 0:e542df4c4901 | 24 | bool connected; |
wmmihaa | 0:e542df4c4901 | 25 | connected = ws.connect(); |
wmmihaa | 0:e542df4c4901 | 26 | |
wmmihaa | 0:e542df4c4901 | 27 | if(connected) |
wmmihaa | 0:e542df4c4901 | 28 | { |
wmmihaa | 0:e542df4c4901 | 29 | pc1.printf("Connected successfully\r\n"); |
wmmihaa | 0:e542df4c4901 | 30 | } |
wmmihaa | 0:e542df4c4901 | 31 | else |
wmmihaa | 0:e542df4c4901 | 32 | { |
wmmihaa | 0:e542df4c4901 | 33 | pc1.printf("Unable to connect\r\n"); |
wmmihaa | 0:e542df4c4901 | 34 | ws.close(); |
wmmihaa | 0:e542df4c4901 | 35 | exit(0); |
wmmihaa | 0:e542df4c4901 | 36 | } |
wmmihaa | 0:e542df4c4901 | 37 | |
wmmihaa | 0:e542df4c4901 | 38 | char str[10000]; |
wmmihaa | 0:e542df4c4901 | 39 | |
wmmihaa | 1:b182b5dc15e7 | 40 | // Create signIn message |
wmmihaa | 0:e542df4c4901 | 41 | MbedJSONValue signInJson; |
wmmihaa | 1:b182b5dc15e7 | 42 | signInJson["macAddress"] = eth.getMACAddress(); |
wmmihaa | 1:b182b5dc15e7 | 43 | signInJson["ip"] = eth.getIPAddress(); |
wmmihaa | 1:b182b5dc15e7 | 44 | /* |
wmmihaa | 0:e542df4c4901 | 45 | signInJson["organizationId"] = _organizationId; |
wmmihaa | 0:e542df4c4901 | 46 | signInJson["machineName"] = eth.getMACAddress(); |
wmmihaa | 1:b182b5dc15e7 | 47 | */ |
wmmihaa | 0:e542df4c4901 | 48 | |
wmmihaa | 1:b182b5dc15e7 | 49 | // Submit signIn message |
wmmihaa | 0:e542df4c4901 | 50 | char buf[256]; |
wmmihaa | 0:e542df4c4901 | 51 | snprintf(buf, sizeof buf, "signIn::%s", signInJson.serialize()); |
wmmihaa | 0:e542df4c4901 | 52 | ws.send(buf); |
wmmihaa | 0:e542df4c4901 | 53 | |
wmmihaa | 0:e542df4c4901 | 54 | while(1){ |
wmmihaa | 0:e542df4c4901 | 55 | memset(str, 0, 10000); |
wmmihaa | 0:e542df4c4901 | 56 | wait(1.0f); |
wmmihaa | 0:e542df4c4901 | 57 | ///pc1.printf("."); |
wmmihaa | 0:e542df4c4901 | 58 | |
wmmihaa | 0:e542df4c4901 | 59 | if (ws.read(str)) { |
wmmihaa | 0:e542df4c4901 | 60 | |
wmmihaa | 0:e542df4c4901 | 61 | string msg(str); |
wmmihaa | 0:e542df4c4901 | 62 | |
wmmihaa | 1:b182b5dc15e7 | 63 | if (msg.find("signInMessage::") == 0) |
wmmihaa | 0:e542df4c4901 | 64 | { |
wmmihaa | 0:e542df4c4901 | 65 | pc1.printf("Sign In successfully\r\n"); |
wmmihaa | 1:b182b5dc15e7 | 66 | //pc1.printf("response: %s\r\n", str); |
wmmihaa | 1:b182b5dc15e7 | 67 | |
wmmihaa | 1:b182b5dc15e7 | 68 | msg.replace(0,15,""); |
wmmihaa | 1:b182b5dc15e7 | 69 | MbedJSONValue pingRequestJson; |
wmmihaa | 1:b182b5dc15e7 | 70 | const char * json = msg.c_str(); |
wmmihaa | 1:b182b5dc15e7 | 71 | parse(pingRequestJson, json); |
wmmihaa | 1:b182b5dc15e7 | 72 | std::string organizationId; |
wmmihaa | 1:b182b5dc15e7 | 73 | std::string hubProvider; |
wmmihaa | 1:b182b5dc15e7 | 74 | std::string messagingToken; |
wmmihaa | 1:b182b5dc15e7 | 75 | std::string state; |
wmmihaa | 1:b182b5dc15e7 | 76 | std::string protocol; |
wmmihaa | 1:b182b5dc15e7 | 77 | std::string binaryUri; |
wmmihaa | 1:b182b5dc15e7 | 78 | std::string binaryVersion; |
wmmihaa | 1:b182b5dc15e7 | 79 | bool debug; |
wmmihaa | 1:b182b5dc15e7 | 80 | |
wmmihaa | 1:b182b5dc15e7 | 81 | organizationId = pingRequestJson["organizationId"].get<std::string>(); |
wmmihaa | 1:b182b5dc15e7 | 82 | hubProvider = pingRequestJson["hubProvider"].get<std::string>(); |
wmmihaa | 1:b182b5dc15e7 | 83 | messagingToken = pingRequestJson["messagingToken"].get<std::string>(); |
wmmihaa | 1:b182b5dc15e7 | 84 | state = pingRequestJson["state"].get<std::string>(); |
wmmihaa | 1:b182b5dc15e7 | 85 | protocol = pingRequestJson["protocol"].get<std::string>(); |
wmmihaa | 1:b182b5dc15e7 | 86 | binaryUri = pingRequestJson["binaryUri"].get<std::string>(); |
wmmihaa | 1:b182b5dc15e7 | 87 | binaryVersion = pingRequestJson["binaryVersion"].get<std::string>(); |
wmmihaa | 1:b182b5dc15e7 | 88 | debug = pingRequestJson["debug"].get<bool>(); |
wmmihaa | 1:b182b5dc15e7 | 89 | |
wmmihaa | 1:b182b5dc15e7 | 90 | pc1.printf("organizationId: %s\r\n", organizationId); |
wmmihaa | 1:b182b5dc15e7 | 91 | pc1.printf("hubProvider: %s\r\n", hubProvider); |
wmmihaa | 1:b182b5dc15e7 | 92 | pc1.printf("messagingToken: %s\r\n", messagingToken); |
wmmihaa | 1:b182b5dc15e7 | 93 | pc1.printf("state: %s\r\n", state); |
wmmihaa | 1:b182b5dc15e7 | 94 | pc1.printf("protocol: %s\r\n", protocol); |
wmmihaa | 1:b182b5dc15e7 | 95 | pc1.printf("binaryUri: %s\r\n", binaryUri); |
wmmihaa | 1:b182b5dc15e7 | 96 | pc1.printf("binaryVersion: %s\r\n", binaryVersion); |
wmmihaa | 1:b182b5dc15e7 | 97 | pc1.printf("debug: %s\r\n", debug); |
wmmihaa | 1:b182b5dc15e7 | 98 | |
wmmihaa | 0:e542df4c4901 | 99 | } |
wmmihaa | 0:e542df4c4901 | 100 | else if (msg.find("errorMessage::") == 0) |
wmmihaa | 0:e542df4c4901 | 101 | { |
wmmihaa | 0:e542df4c4901 | 102 | pc1.printf("ERROR...\r\n"); |
wmmihaa | 0:e542df4c4901 | 103 | pc1.printf(str); |
wmmihaa | 0:e542df4c4901 | 104 | pc1.printf("\r\n"); |
wmmihaa | 0:e542df4c4901 | 105 | } |
wmmihaa | 0:e542df4c4901 | 106 | } |
wmmihaa | 0:e542df4c4901 | 107 | |
wmmihaa | 0:e542df4c4901 | 108 | } |
wmmihaa | 0:e542df4c4901 | 109 | ws.close(); |
wmmihaa | 0:e542df4c4901 | 110 | |
wmmihaa | 0:e542df4c4901 | 111 | // lcd.printf("Signing out..."); |
wmmihaa | 0:e542df4c4901 | 112 | } |
wmmihaa | 0:e542df4c4901 | 113 | char* appendCharToCharArray(char* array, char a) |
wmmihaa | 0:e542df4c4901 | 114 | { |
wmmihaa | 0:e542df4c4901 | 115 | size_t len = strlen(array); |
wmmihaa | 0:e542df4c4901 | 116 | |
wmmihaa | 0:e542df4c4901 | 117 | char* ret = new char[len+2]; |
wmmihaa | 0:e542df4c4901 | 118 | |
wmmihaa | 0:e542df4c4901 | 119 | strcpy(ret, array); |
wmmihaa | 0:e542df4c4901 | 120 | ret[len] = a; |
wmmihaa | 0:e542df4c4901 | 121 | ret[len+1] = '\0'; |
wmmihaa | 0:e542df4c4901 | 122 | |
wmmihaa | 0:e542df4c4901 | 123 | return ret; |
wmmihaa | 0:e542df4c4901 | 124 | } |