API for linking to the Huxley National Rail REST proxy for the retrieval of live National Rail data. Still needs work (sadly), but works well for the time being!
Dependencies: EthernetInterface
National Rail Huxley Integration Interface
This API provides a clear link between the National Rail Huxley Integration JSON interface (available at https://huxley.unop.uk/), and mbed devices, over an Ethernet connection. This project is still very much a work-in-progress, but hopefully I will be able to provide a comprehensive method of retrieving live National Rail departures data in a simple and effective way.
nr_network.cpp@8:6e063a3827c0, 2016-05-05 (annotated)
- Committer:
- Leigh_LbR
- Date:
- Thu May 05 14:52:33 2016 +0000
- Revision:
- 8:6e063a3827c0
- Parent:
- 1:1de042ff6324
- Child:
- 11:4532ff549fcf
Changed "Connect" to take a URL instead of having my own Huxley implementation as the default
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Leigh_LbR | 0:bf04f62339a4 | 1 | #include "departure_board.h" |
Leigh_LbR | 0:bf04f62339a4 | 2 | #include "arrival_board.h" |
Leigh_LbR | 1:1de042ff6324 | 3 | #include "nr_network.h" |
Leigh_LbR | 0:bf04f62339a4 | 4 | #include <string> |
Leigh_LbR | 1:1de042ff6324 | 5 | #include <vector> |
Leigh_LbR | 0:bf04f62339a4 | 6 | |
Leigh_LbR | 0:bf04f62339a4 | 7 | #ifndef nullptr |
Leigh_LbR | 0:bf04f62339a4 | 8 | #define nullptr 0 // nullptr is undefined by default... |
Leigh_LbR | 0:bf04f62339a4 | 9 | #endif |
Leigh_LbR | 0:bf04f62339a4 | 10 | |
Leigh_LbR | 0:bf04f62339a4 | 11 | using std::string; |
Leigh_LbR | 1:1de042ff6324 | 12 | using std::vector; |
Leigh_LbR | 0:bf04f62339a4 | 13 | |
Leigh_LbR | 0:bf04f62339a4 | 14 | /* |
Leigh_LbR | 0:bf04f62339a4 | 15 | * Initialise the network connection with the required attributes. |
Leigh_LbR | 0:bf04f62339a4 | 16 | */ |
Leigh_LbR | 0:bf04f62339a4 | 17 | void NR_Network_Conn::init(const char* address, const char* mask, const char* gateway) |
Leigh_LbR | 0:bf04f62339a4 | 18 | { |
Leigh_LbR | 0:bf04f62339a4 | 19 | conn = EthernetInterface(); |
Leigh_LbR | 0:bf04f62339a4 | 20 | |
Leigh_LbR | 0:bf04f62339a4 | 21 | if(address == nullptr) _address[0] = '\0'; else strcpy(_address, address); |
Leigh_LbR | 0:bf04f62339a4 | 22 | if(mask == nullptr) _sub_mask[0] = '\0'; else strcpy(_sub_mask, mask); |
Leigh_LbR | 0:bf04f62339a4 | 23 | if(gateway == nullptr) _gateway[0] = '\0'; else strcpy(_gateway, gateway); |
Leigh_LbR | 0:bf04f62339a4 | 24 | } |
Leigh_LbR | 0:bf04f62339a4 | 25 | |
Leigh_LbR | 0:bf04f62339a4 | 26 | NR_Network_Conn::NR_Network_Conn() |
Leigh_LbR | 0:bf04f62339a4 | 27 | { |
Leigh_LbR | 0:bf04f62339a4 | 28 | init(nullptr, nullptr, nullptr); |
Leigh_LbR | 0:bf04f62339a4 | 29 | } |
Leigh_LbR | 0:bf04f62339a4 | 30 | |
Leigh_LbR | 0:bf04f62339a4 | 31 | TCPSocketConnection NR_Network_Conn::GetSocket() |
Leigh_LbR | 0:bf04f62339a4 | 32 | { |
Leigh_LbR | 0:bf04f62339a4 | 33 | return socket; |
Leigh_LbR | 0:bf04f62339a4 | 34 | } |
Leigh_LbR | 0:bf04f62339a4 | 35 | |
Leigh_LbR | 0:bf04f62339a4 | 36 | Departure_Board NR_Network_Conn::GetDepartures(const string& code_stn, const string& number) |
Leigh_LbR | 0:bf04f62339a4 | 37 | { |
Leigh_LbR | 0:bf04f62339a4 | 38 | const string command = "GET /departures/" + code_stn + "/" + number + "/ HTTP/1.1"; |
Leigh_LbR | 0:bf04f62339a4 | 39 | |
Leigh_LbR | 0:bf04f62339a4 | 40 | vector<char> command_cstr (command.begin(), command.end()); |
Leigh_LbR | 0:bf04f62339a4 | 41 | command_cstr.push_back('\0'); |
Leigh_LbR | 0:bf04f62339a4 | 42 | |
Leigh_LbR | 0:bf04f62339a4 | 43 | socket.send_all(&command_cstr[0], sizeof(&command_cstr[0]) - 1); // Ignore the null-terminating character |
Leigh_LbR | 0:bf04f62339a4 | 44 | |
Leigh_LbR | 0:bf04f62339a4 | 45 | char buf[1000]; |
Leigh_LbR | 0:bf04f62339a4 | 46 | string json; |
Leigh_LbR | 0:bf04f62339a4 | 47 | |
Leigh_LbR | 0:bf04f62339a4 | 48 | int ret; |
Leigh_LbR | 0:bf04f62339a4 | 49 | |
Leigh_LbR | 0:bf04f62339a4 | 50 | while(true) { |
Leigh_LbR | 1:1de042ff6324 | 51 | ret = socket.receive(buf, sizeof(buf) - 1); |
Leigh_LbR | 0:bf04f62339a4 | 52 | if(ret <= 0){ |
Leigh_LbR | 0:bf04f62339a4 | 53 | json.append(buf); |
Leigh_LbR | 0:bf04f62339a4 | 54 | break; |
Leigh_LbR | 0:bf04f62339a4 | 55 | } |
Leigh_LbR | 0:bf04f62339a4 | 56 | buf[ret] = '\0'; |
Leigh_LbR | 0:bf04f62339a4 | 57 | json.append(buf); |
Leigh_LbR | 0:bf04f62339a4 | 58 | memset(buf, '\0', 1000); |
Leigh_LbR | 0:bf04f62339a4 | 59 | } |
Leigh_LbR | 0:bf04f62339a4 | 60 | |
Leigh_LbR | 1:1de042ff6324 | 61 | Departure_Board dep = depBoardFromJson(json); |
Leigh_LbR | 0:bf04f62339a4 | 62 | |
Leigh_LbR | 0:bf04f62339a4 | 63 | return dep; |
Leigh_LbR | 0:bf04f62339a4 | 64 | } |
Leigh_LbR | 0:bf04f62339a4 | 65 | |
Leigh_LbR | 0:bf04f62339a4 | 66 | Arrival_Board NR_Network_Conn::GetArrivals(const string& code_stn, const string& number) |
Leigh_LbR | 0:bf04f62339a4 | 67 | { |
Leigh_LbR | 0:bf04f62339a4 | 68 | const string command = "GET /arrivals/" + code_stn + "/" + number + "/ HTTP/1.1"; |
Leigh_LbR | 0:bf04f62339a4 | 69 | |
Leigh_LbR | 0:bf04f62339a4 | 70 | vector<char> command_cstr (command.begin(), command.end()); |
Leigh_LbR | 0:bf04f62339a4 | 71 | command_cstr.push_back('\0'); |
Leigh_LbR | 0:bf04f62339a4 | 72 | |
Leigh_LbR | 0:bf04f62339a4 | 73 | socket.send_all(&command_cstr[0], sizeof(&command_cstr[0]) - 1); // Ignore the null-terminating character |
Leigh_LbR | 0:bf04f62339a4 | 74 | |
Leigh_LbR | 0:bf04f62339a4 | 75 | char buf[1000]; |
Leigh_LbR | 0:bf04f62339a4 | 76 | string json; |
Leigh_LbR | 0:bf04f62339a4 | 77 | |
Leigh_LbR | 0:bf04f62339a4 | 78 | int ret; |
Leigh_LbR | 0:bf04f62339a4 | 79 | |
Leigh_LbR | 0:bf04f62339a4 | 80 | while(true) { |
Leigh_LbR | 0:bf04f62339a4 | 81 | ret = socket.receive(buf, sizeof(buf) - 1); |
Leigh_LbR | 0:bf04f62339a4 | 82 | if(ret <= 0){ |
Leigh_LbR | 0:bf04f62339a4 | 83 | json.append(buf); |
Leigh_LbR | 0:bf04f62339a4 | 84 | break; |
Leigh_LbR | 0:bf04f62339a4 | 85 | } |
Leigh_LbR | 0:bf04f62339a4 | 86 | buf[ret] = '\0'; |
Leigh_LbR | 0:bf04f62339a4 | 87 | json.append(buf); |
Leigh_LbR | 0:bf04f62339a4 | 88 | memset(buf, '\0', 1000); |
Leigh_LbR | 0:bf04f62339a4 | 89 | } |
Leigh_LbR | 0:bf04f62339a4 | 90 | |
Leigh_LbR | 0:bf04f62339a4 | 91 | Arrival_Board arr = arrBoardFromJson(json); |
Leigh_LbR | 0:bf04f62339a4 | 92 | |
Leigh_LbR | 0:bf04f62339a4 | 93 | return arr; |
Leigh_LbR | 0:bf04f62339a4 | 94 | } |
Leigh_LbR | 0:bf04f62339a4 | 95 | |
Leigh_LbR | 0:bf04f62339a4 | 96 | char* NR_Network_Conn::GetIP() |
Leigh_LbR | 0:bf04f62339a4 | 97 | { |
Leigh_LbR | 0:bf04f62339a4 | 98 | return conn.getIPAddress(); |
Leigh_LbR | 0:bf04f62339a4 | 99 | } |
Leigh_LbR | 0:bf04f62339a4 | 100 | |
Leigh_LbR | 0:bf04f62339a4 | 101 | NR_Network_Conn::NR_Network_Conn(const char* address, const char* mask, const char* gateway) |
Leigh_LbR | 0:bf04f62339a4 | 102 | { |
Leigh_LbR | 0:bf04f62339a4 | 103 | init(address, mask, gateway); |
Leigh_LbR | 0:bf04f62339a4 | 104 | } |
Leigh_LbR | 0:bf04f62339a4 | 105 | |
Leigh_LbR | 0:bf04f62339a4 | 106 | /* |
Leigh_LbR | 8:6e063a3827c0 | 107 | * Connect to the Huxley (NR) API. |
Leigh_LbR | 8:6e063a3827c0 | 108 | * @param url URL of the Huxley implementation to be used |
Leigh_LbR | 0:bf04f62339a4 | 109 | */ |
Leigh_LbR | 8:6e063a3827c0 | 110 | int NR_Network_Conn::Connect(const char* url) |
Leigh_LbR | 0:bf04f62339a4 | 111 | { |
Leigh_LbR | 0:bf04f62339a4 | 112 | if(_address[0] == '\0' && _sub_mask[0] == '\0' && _gateway == '\0') { |
Leigh_LbR | 0:bf04f62339a4 | 113 | if(conn.init() < 0) return -1; |
Leigh_LbR | 0:bf04f62339a4 | 114 | } else { |
Leigh_LbR | 0:bf04f62339a4 | 115 | if(conn.init(_address, _sub_mask, _gateway) < 0) return -1; |
Leigh_LbR | 0:bf04f62339a4 | 116 | } |
Leigh_LbR | 0:bf04f62339a4 | 117 | |
Leigh_LbR | 0:bf04f62339a4 | 118 | conn.connect(); |
Leigh_LbR | 0:bf04f62339a4 | 119 | |
Leigh_LbR | 0:bf04f62339a4 | 120 | socket = TCPSocketConnection(); |
Leigh_LbR | 0:bf04f62339a4 | 121 | |
Leigh_LbR | 8:6e063a3827c0 | 122 | socket.connect(url, 80); |
Leigh_LbR | 0:bf04f62339a4 | 123 | |
Leigh_LbR | 0:bf04f62339a4 | 124 | return 0; |
Leigh_LbR | 0:bf04f62339a4 | 125 | } |
Leigh_LbR | 0:bf04f62339a4 | 126 | |
Leigh_LbR | 0:bf04f62339a4 | 127 | void NR_Network_Conn::Disconnect() |
Leigh_LbR | 0:bf04f62339a4 | 128 | { |
Leigh_LbR | 0:bf04f62339a4 | 129 | socket.close(); |
Leigh_LbR | 0:bf04f62339a4 | 130 | conn.disconnect(); |
Leigh_LbR | 0:bf04f62339a4 | 131 | } |