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.
Diff: nr_network.cpp
- Revision:
- 0:bf04f62339a4
- Child:
- 1:1de042ff6324
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nr_network.cpp Thu Apr 21 12:02:57 2016 +0000 @@ -0,0 +1,128 @@ +#include "nr_network.h" +#include "departure_board.h" +#include "arrival_board.h" +#include <string> + +#ifndef nullptr +#define nullptr 0 // nullptr is undefined by default... +#endif + +using std::string; + +/* + * Initialise the network connection with the required attributes. + */ +void NR_Network_Conn::init(const char* address, const char* mask, const char* gateway) +{ + conn = EthernetInterface(); + + if(address == nullptr) _address[0] = '\0'; else strcpy(_address, address); + if(mask == nullptr) _sub_mask[0] = '\0'; else strcpy(_sub_mask, mask); + if(gateway == nullptr) _gateway[0] = '\0'; else strcpy(_gateway, gateway); +} + +NR_Network_Conn::NR_Network_Conn() +{ + init(nullptr, nullptr, nullptr); +} + +TCPSocketConnection NR_Network_Conn::GetSocket() +{ + return socket; +} + +Departure_Board NR_Network_Conn::GetDepartures(const string& code_stn, const string& number) +{ + const string command = "GET /departures/" + code_stn + "/" + number + "/ HTTP/1.1"; + + vector<char> command_cstr (command.begin(), command.end()); + command_cstr.push_back('\0'); + + socket.send_all(&command_cstr[0], sizeof(&command_cstr[0]) - 1); // Ignore the null-terminating character + + char buf[1000]; + string json; + + int ret; + + while(true) { + ret = conn.GetSocket().receive(buf, sizeof(buf) - 1); + if(ret <= 0){ + json.append(buf); + break; + } + buf[ret] = '\0'; + json.append(buf); + memset(buf, '\0', 1000); + } + + Departure_Board dep = boardFromJson(json); + + return dep; +} + +Arrival_Board NR_Network_Conn::GetArrivals(const string& code_stn, const string& number) +{ + const string command = "GET /arrivals/" + code_stn + "/" + number + "/ HTTP/1.1"; + + vector<char> command_cstr (command.begin(), command.end()); + command_cstr.push_back('\0'); + + socket.send_all(&command_cstr[0], sizeof(&command_cstr[0]) - 1); // Ignore the null-terminating character + + char buf[1000]; + string json; + + int ret; + + while(true) { + ret = socket.receive(buf, sizeof(buf) - 1); + if(ret <= 0){ + json.append(buf); + break; + } + buf[ret] = '\0'; + json.append(buf); + memset(buf, '\0', 1000); + } + + Arrival_Board arr = arrBoardFromJson(json); + + return arr; +} + +char* NR_Network_Conn::GetIP() +{ + return conn.getIPAddress(); +} + +NR_Network_Conn::NR_Network_Conn(const char* address, const char* mask, const char* gateway) +{ + init(address, mask, gateway); +} + +/* + * Connect to the NR API. + */ +int NR_Network_Conn::Connect() +{ + if(_address[0] == '\0' && _sub_mask[0] == '\0' && _gateway == '\0') { + if(conn.init() < 0) return -1; + } else { + if(conn.init(_address, _sub_mask, _gateway) < 0) return -1; + } + + conn.connect(); + + socket = TCPSocketConnection(); + + socket.connect("lbhuxley.azurewebsites.net", 80); + + return 0; +} + +void NR_Network_Conn::Disconnect() +{ + socket.close(); + conn.disconnect(); +} \ No newline at end of file