Raphaël Drouin
/
WebsocketClient
s
Fork of Websocket_Ethernet_HelloWorld by
main.cpp@7:f182bf9a8582, 2017-03-29 (annotated)
- Committer:
- ShaolinPoutine
- Date:
- Wed Mar 29 04:28:39 2017 +0000
- Revision:
- 7:f182bf9a8582
- Parent:
- 6:2fae6e37c5ca
First complete sequence terminated, character handling is needed because string and ifstream cost a lot.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 6:2fae6e37c5ca | 1 | /* Copyright C2014 ARM, MIT License |
mbed_official | 6:2fae6e37c5ca | 2 | * |
mbed_official | 6:2fae6e37c5ca | 3 | * Author: Doug Anson (doug.anson@arm.com) |
mbed_official | 6:2fae6e37c5ca | 4 | * |
mbed_official | 6:2fae6e37c5ca | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
mbed_official | 6:2fae6e37c5ca | 6 | * and associated documentation files the "Software", to deal in the Software without restriction, |
mbed_official | 6:2fae6e37c5ca | 7 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
mbed_official | 6:2fae6e37c5ca | 8 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
mbed_official | 6:2fae6e37c5ca | 9 | * furnished to do so, subject to the following conditions: |
mbed_official | 6:2fae6e37c5ca | 10 | * |
mbed_official | 6:2fae6e37c5ca | 11 | * The above copyright notice and this permission notice shall be included in all copies or |
mbed_official | 6:2fae6e37c5ca | 12 | * substantial portions of the Software. |
mbed_official | 6:2fae6e37c5ca | 13 | * |
mbed_official | 6:2fae6e37c5ca | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
mbed_official | 6:2fae6e37c5ca | 15 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
mbed_official | 6:2fae6e37c5ca | 16 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
mbed_official | 6:2fae6e37c5ca | 17 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
mbed_official | 6:2fae6e37c5ca | 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
mbed_official | 6:2fae6e37c5ca | 19 | */ |
mbed_official | 6:2fae6e37c5ca | 20 | |
samux | 1:1c1802ec42a2 | 21 | #include "mbed.h" |
samux | 1:1c1802ec42a2 | 22 | #include "EthernetInterface.h" |
samux | 1:1c1802ec42a2 | 23 | #include "Websocket.h" |
ShaolinPoutine | 7:f182bf9a8582 | 24 | //#include <string> |
ShaolinPoutine | 7:f182bf9a8582 | 25 | #include <fstream> |
sam_grove | 3:9bd22e5386cd | 26 | |
mbed_official | 6:2fae6e37c5ca | 27 | // Blinky |
sam_grove | 3:9bd22e5386cd | 28 | DigitalOut led(LED1); |
mbed_official | 6:2fae6e37c5ca | 29 | |
ShaolinPoutine | 7:f182bf9a8582 | 30 | EthernetInterface* eth; |
ShaolinPoutine | 7:f182bf9a8582 | 31 | Websocket* webSock; |
ShaolinPoutine | 7:f182bf9a8582 | 32 | |
ShaolinPoutine | 7:f182bf9a8582 | 33 | string type; |
ShaolinPoutine | 7:f182bf9a8582 | 34 | string id; |
ShaolinPoutine | 7:f182bf9a8582 | 35 | string zone; |
ShaolinPoutine | 7:f182bf9a8582 | 36 | string alert_level; |
ShaolinPoutine | 7:f182bf9a8582 | 37 | |
ShaolinPoutine | 7:f182bf9a8582 | 38 | int init_identity() |
ShaolinPoutine | 7:f182bf9a8582 | 39 | { |
ShaolinPoutine | 7:f182bf9a8582 | 40 | char a; |
ShaolinPoutine | 7:f182bf9a8582 | 41 | // find |
ShaolinPoutine | 7:f182bf9a8582 | 42 | while (infile >> a) |
ShaolinPoutine | 7:f182bf9a8582 | 43 | { |
ShaolinPoutine | 7:f182bf9a8582 | 44 | |
ShaolinPoutine | 7:f182bf9a8582 | 45 | } |
ShaolinPoutine | 7:f182bf9a8582 | 46 | /*std::ifstream infile("config"); |
ShaolinPoutine | 7:f182bf9a8582 | 47 | |
ShaolinPoutine | 7:f182bf9a8582 | 48 | std::getline(infile, type); |
ShaolinPoutine | 7:f182bf9a8582 | 49 | std::getline(infile, id);*/ |
ShaolinPoutine | 7:f182bf9a8582 | 50 | /*std::getline(infile, zone); |
ShaolinPoutine | 7:f182bf9a8582 | 51 | std::getline(infile, alert_level);*/ |
ShaolinPoutine | 7:f182bf9a8582 | 52 | } |
ShaolinPoutine | 7:f182bf9a8582 | 53 | |
ShaolinPoutine | 7:f182bf9a8582 | 54 | int init_web_socket() |
ShaolinPoutine | 7:f182bf9a8582 | 55 | { |
ShaolinPoutine | 7:f182bf9a8582 | 56 | eth = new EthernetInterface(); |
ShaolinPoutine | 7:f182bf9a8582 | 57 | while (eth->get_ip_address()[0] == 0) |
ShaolinPoutine | 7:f182bf9a8582 | 58 | { |
ShaolinPoutine | 7:f182bf9a8582 | 59 | eth->connect(); |
ShaolinPoutine | 7:f182bf9a8582 | 60 | } |
ShaolinPoutine | 7:f182bf9a8582 | 61 | printf("IP Address is %s\n\r", eth->get_ip_address()); |
ShaolinPoutine | 7:f182bf9a8582 | 62 | |
ShaolinPoutine | 7:f182bf9a8582 | 63 | webSock = new Websocket("ws://10.163.0.100:8000/", eth); |
ShaolinPoutine | 7:f182bf9a8582 | 64 | int connect_error = webSock->connect(); |
ShaolinPoutine | 7:f182bf9a8582 | 65 | while (connect_error != 0) |
ShaolinPoutine | 7:f182bf9a8582 | 66 | { |
ShaolinPoutine | 7:f182bf9a8582 | 67 | connect_error = webSock->connect(); |
ShaolinPoutine | 7:f182bf9a8582 | 68 | } |
ShaolinPoutine | 7:f182bf9a8582 | 69 | |
ShaolinPoutine | 7:f182bf9a8582 | 70 | wait(0.5); |
ShaolinPoutine | 7:f182bf9a8582 | 71 | return 0; |
ShaolinPoutine | 7:f182bf9a8582 | 72 | } |
ShaolinPoutine | 7:f182bf9a8582 | 73 | |
ShaolinPoutine | 7:f182bf9a8582 | 74 | int send_connection_message() |
ShaolinPoutine | 7:f182bf9a8582 | 75 | { |
ShaolinPoutine | 7:f182bf9a8582 | 76 | // TODO read config file to get real entity type and id |
ShaolinPoutine | 7:f182bf9a8582 | 77 | string entity_type = "sensor"; |
ShaolinPoutine | 7:f182bf9a8582 | 78 | string entity_id = "C48"; |
ShaolinPoutine | 7:f182bf9a8582 | 79 | string msg = "{\"message_id\":\"connection\", \"entity_type\":\"" + entity_type + "\", \"entity_id\":\"" + entity_id +"\"}"; |
ShaolinPoutine | 7:f182bf9a8582 | 80 | |
ShaolinPoutine | 7:f182bf9a8582 | 81 | if (webSock) |
ShaolinPoutine | 7:f182bf9a8582 | 82 | { |
ShaolinPoutine | 7:f182bf9a8582 | 83 | char *cstr = new char[msg.length() + 1]; |
ShaolinPoutine | 7:f182bf9a8582 | 84 | strcpy(cstr, msg.c_str()); |
ShaolinPoutine | 7:f182bf9a8582 | 85 | int err = webSock->send(cstr); |
ShaolinPoutine | 7:f182bf9a8582 | 86 | delete [] cstr; |
ShaolinPoutine | 7:f182bf9a8582 | 87 | return err; |
ShaolinPoutine | 7:f182bf9a8582 | 88 | } |
ShaolinPoutine | 7:f182bf9a8582 | 89 | else |
ShaolinPoutine | 7:f182bf9a8582 | 90 | return -1; |
ShaolinPoutine | 7:f182bf9a8582 | 91 | } |
ShaolinPoutine | 7:f182bf9a8582 | 92 | |
ShaolinPoutine | 7:f182bf9a8582 | 93 | int send_low_battery_message() |
ShaolinPoutine | 7:f182bf9a8582 | 94 | { |
ShaolinPoutine | 7:f182bf9a8582 | 95 | char* msg = "{\"message_id\":\"lowBattery\"}"; |
ShaolinPoutine | 7:f182bf9a8582 | 96 | if (webSock) |
ShaolinPoutine | 7:f182bf9a8582 | 97 | return webSock->send(msg); |
ShaolinPoutine | 7:f182bf9a8582 | 98 | else |
ShaolinPoutine | 7:f182bf9a8582 | 99 | return -1; |
ShaolinPoutine | 7:f182bf9a8582 | 100 | } |
ShaolinPoutine | 7:f182bf9a8582 | 101 | |
ShaolinPoutine | 7:f182bf9a8582 | 102 | int send_alert_message() |
ShaolinPoutine | 7:f182bf9a8582 | 103 | { |
ShaolinPoutine | 7:f182bf9a8582 | 104 | string trespasser = "A58"; |
ShaolinPoutine | 7:f182bf9a8582 | 105 | string alert_type = "Urgent"; |
ShaolinPoutine | 7:f182bf9a8582 | 106 | string zone = "<Salle a manger>"; |
ShaolinPoutine | 7:f182bf9a8582 | 107 | |
ShaolinPoutine | 7:f182bf9a8582 | 108 | char msg[200] = "{\"message_id\":\"alert\", \"alert_type\":\"" + alert_type + "\", \"target_id\":\"" + trespasser + "\", \"zone\":\"" + zone + "\"}"; |
ShaolinPoutine | 7:f182bf9a8582 | 109 | |
ShaolinPoutine | 7:f182bf9a8582 | 110 | if (webSock) |
ShaolinPoutine | 7:f182bf9a8582 | 111 | { |
ShaolinPoutine | 7:f182bf9a8582 | 112 | char *cstr = new char[msg.length() + 1]; |
ShaolinPoutine | 7:f182bf9a8582 | 113 | strcpy(cstr, msg.c_str()); |
ShaolinPoutine | 7:f182bf9a8582 | 114 | int err = webSock->send(cstr); |
ShaolinPoutine | 7:f182bf9a8582 | 115 | delete [] cstr; |
ShaolinPoutine | 7:f182bf9a8582 | 116 | return err; |
ShaolinPoutine | 7:f182bf9a8582 | 117 | } |
ShaolinPoutine | 7:f182bf9a8582 | 118 | else |
ShaolinPoutine | 7:f182bf9a8582 | 119 | return -1; |
ShaolinPoutine | 7:f182bf9a8582 | 120 | } |
ShaolinPoutine | 7:f182bf9a8582 | 121 | |
mbed_official | 6:2fae6e37c5ca | 122 | int main() { |
ShaolinPoutine | 7:f182bf9a8582 | 123 | |
mbed_official | 6:2fae6e37c5ca | 124 | // announce |
mbed_official | 6:2fae6e37c5ca | 125 | printf("Websocket Example v1.0.0\r\n"); |
ShaolinPoutine | 7:f182bf9a8582 | 126 | init_identity(); |
ShaolinPoutine | 7:f182bf9a8582 | 127 | printf("I am %s of type %s", id, type); |
ShaolinPoutine | 7:f182bf9a8582 | 128 | init_web_socket(); |
ShaolinPoutine | 7:f182bf9a8582 | 129 | send_connection_message(); |
ShaolinPoutine | 7:f182bf9a8582 | 130 | send_low_battery_message(); |
ShaolinPoutine | 7:f182bf9a8582 | 131 | send_alert_message(); |
sam_grove | 3:9bd22e5386cd | 132 | |
mbed_official | 6:2fae6e37c5ca | 133 | while (true) { |
mbed_official | 6:2fae6e37c5ca | 134 | |
mbed_official | 6:2fae6e37c5ca | 135 | // blink... |
mbed_official | 6:2fae6e37c5ca | 136 | led = !led; |
mbed_official | 6:2fae6e37c5ca | 137 | wait(0.5); |
mbed_official | 6:2fae6e37c5ca | 138 | |
samux | 1:1c1802ec42a2 | 139 | } |
samux | 1:1c1802ec42a2 | 140 | } |