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.

/media/uploads/Leigh_LbR/nre_powered_logo.jpg

Committer:
Leigh_LbR
Date:
Thu Apr 21 12:02:57 2016 +0000
Revision:
0:bf04f62339a4
Child:
1:1de042ff6324
Created the Huxley Integration API. This links up to an implementation of the Huxley National Rail REST proxy to create an easy way to generate real-time departures/arrivals info easily on an mbed device.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Leigh_LbR 0:bf04f62339a4 1 #include "nr_network.h"
Leigh_LbR 0:bf04f62339a4 2 #include "departure_board.h"
Leigh_LbR 0:bf04f62339a4 3 #include "arrival_board.h"
Leigh_LbR 0:bf04f62339a4 4 #include <string>
Leigh_LbR 0:bf04f62339a4 5
Leigh_LbR 0:bf04f62339a4 6 #ifndef nullptr
Leigh_LbR 0:bf04f62339a4 7 #define nullptr 0 // nullptr is undefined by default...
Leigh_LbR 0:bf04f62339a4 8 #endif
Leigh_LbR 0:bf04f62339a4 9
Leigh_LbR 0:bf04f62339a4 10 using std::string;
Leigh_LbR 0:bf04f62339a4 11
Leigh_LbR 0:bf04f62339a4 12 /*
Leigh_LbR 0:bf04f62339a4 13 * Initialise the network connection with the required attributes.
Leigh_LbR 0:bf04f62339a4 14 */
Leigh_LbR 0:bf04f62339a4 15 void NR_Network_Conn::init(const char* address, const char* mask, const char* gateway)
Leigh_LbR 0:bf04f62339a4 16 {
Leigh_LbR 0:bf04f62339a4 17 conn = EthernetInterface();
Leigh_LbR 0:bf04f62339a4 18
Leigh_LbR 0:bf04f62339a4 19 if(address == nullptr) _address[0] = '\0'; else strcpy(_address, address);
Leigh_LbR 0:bf04f62339a4 20 if(mask == nullptr) _sub_mask[0] = '\0'; else strcpy(_sub_mask, mask);
Leigh_LbR 0:bf04f62339a4 21 if(gateway == nullptr) _gateway[0] = '\0'; else strcpy(_gateway, gateway);
Leigh_LbR 0:bf04f62339a4 22 }
Leigh_LbR 0:bf04f62339a4 23
Leigh_LbR 0:bf04f62339a4 24 NR_Network_Conn::NR_Network_Conn()
Leigh_LbR 0:bf04f62339a4 25 {
Leigh_LbR 0:bf04f62339a4 26 init(nullptr, nullptr, nullptr);
Leigh_LbR 0:bf04f62339a4 27 }
Leigh_LbR 0:bf04f62339a4 28
Leigh_LbR 0:bf04f62339a4 29 TCPSocketConnection NR_Network_Conn::GetSocket()
Leigh_LbR 0:bf04f62339a4 30 {
Leigh_LbR 0:bf04f62339a4 31 return socket;
Leigh_LbR 0:bf04f62339a4 32 }
Leigh_LbR 0:bf04f62339a4 33
Leigh_LbR 0:bf04f62339a4 34 Departure_Board NR_Network_Conn::GetDepartures(const string& code_stn, const string& number)
Leigh_LbR 0:bf04f62339a4 35 {
Leigh_LbR 0:bf04f62339a4 36 const string command = "GET /departures/" + code_stn + "/" + number + "/ HTTP/1.1";
Leigh_LbR 0:bf04f62339a4 37
Leigh_LbR 0:bf04f62339a4 38 vector<char> command_cstr (command.begin(), command.end());
Leigh_LbR 0:bf04f62339a4 39 command_cstr.push_back('\0');
Leigh_LbR 0:bf04f62339a4 40
Leigh_LbR 0:bf04f62339a4 41 socket.send_all(&command_cstr[0], sizeof(&command_cstr[0]) - 1); // Ignore the null-terminating character
Leigh_LbR 0:bf04f62339a4 42
Leigh_LbR 0:bf04f62339a4 43 char buf[1000];
Leigh_LbR 0:bf04f62339a4 44 string json;
Leigh_LbR 0:bf04f62339a4 45
Leigh_LbR 0:bf04f62339a4 46 int ret;
Leigh_LbR 0:bf04f62339a4 47
Leigh_LbR 0:bf04f62339a4 48 while(true) {
Leigh_LbR 0:bf04f62339a4 49 ret = conn.GetSocket().receive(buf, sizeof(buf) - 1);
Leigh_LbR 0:bf04f62339a4 50 if(ret <= 0){
Leigh_LbR 0:bf04f62339a4 51 json.append(buf);
Leigh_LbR 0:bf04f62339a4 52 break;
Leigh_LbR 0:bf04f62339a4 53 }
Leigh_LbR 0:bf04f62339a4 54 buf[ret] = '\0';
Leigh_LbR 0:bf04f62339a4 55 json.append(buf);
Leigh_LbR 0:bf04f62339a4 56 memset(buf, '\0', 1000);
Leigh_LbR 0:bf04f62339a4 57 }
Leigh_LbR 0:bf04f62339a4 58
Leigh_LbR 0:bf04f62339a4 59 Departure_Board dep = boardFromJson(json);
Leigh_LbR 0:bf04f62339a4 60
Leigh_LbR 0:bf04f62339a4 61 return dep;
Leigh_LbR 0:bf04f62339a4 62 }
Leigh_LbR 0:bf04f62339a4 63
Leigh_LbR 0:bf04f62339a4 64 Arrival_Board NR_Network_Conn::GetArrivals(const string& code_stn, const string& number)
Leigh_LbR 0:bf04f62339a4 65 {
Leigh_LbR 0:bf04f62339a4 66 const string command = "GET /arrivals/" + code_stn + "/" + number + "/ HTTP/1.1";
Leigh_LbR 0:bf04f62339a4 67
Leigh_LbR 0:bf04f62339a4 68 vector<char> command_cstr (command.begin(), command.end());
Leigh_LbR 0:bf04f62339a4 69 command_cstr.push_back('\0');
Leigh_LbR 0:bf04f62339a4 70
Leigh_LbR 0:bf04f62339a4 71 socket.send_all(&command_cstr[0], sizeof(&command_cstr[0]) - 1); // Ignore the null-terminating character
Leigh_LbR 0:bf04f62339a4 72
Leigh_LbR 0:bf04f62339a4 73 char buf[1000];
Leigh_LbR 0:bf04f62339a4 74 string json;
Leigh_LbR 0:bf04f62339a4 75
Leigh_LbR 0:bf04f62339a4 76 int ret;
Leigh_LbR 0:bf04f62339a4 77
Leigh_LbR 0:bf04f62339a4 78 while(true) {
Leigh_LbR 0:bf04f62339a4 79 ret = socket.receive(buf, sizeof(buf) - 1);
Leigh_LbR 0:bf04f62339a4 80 if(ret <= 0){
Leigh_LbR 0:bf04f62339a4 81 json.append(buf);
Leigh_LbR 0:bf04f62339a4 82 break;
Leigh_LbR 0:bf04f62339a4 83 }
Leigh_LbR 0:bf04f62339a4 84 buf[ret] = '\0';
Leigh_LbR 0:bf04f62339a4 85 json.append(buf);
Leigh_LbR 0:bf04f62339a4 86 memset(buf, '\0', 1000);
Leigh_LbR 0:bf04f62339a4 87 }
Leigh_LbR 0:bf04f62339a4 88
Leigh_LbR 0:bf04f62339a4 89 Arrival_Board arr = arrBoardFromJson(json);
Leigh_LbR 0:bf04f62339a4 90
Leigh_LbR 0:bf04f62339a4 91 return arr;
Leigh_LbR 0:bf04f62339a4 92 }
Leigh_LbR 0:bf04f62339a4 93
Leigh_LbR 0:bf04f62339a4 94 char* NR_Network_Conn::GetIP()
Leigh_LbR 0:bf04f62339a4 95 {
Leigh_LbR 0:bf04f62339a4 96 return conn.getIPAddress();
Leigh_LbR 0:bf04f62339a4 97 }
Leigh_LbR 0:bf04f62339a4 98
Leigh_LbR 0:bf04f62339a4 99 NR_Network_Conn::NR_Network_Conn(const char* address, const char* mask, const char* gateway)
Leigh_LbR 0:bf04f62339a4 100 {
Leigh_LbR 0:bf04f62339a4 101 init(address, mask, gateway);
Leigh_LbR 0:bf04f62339a4 102 }
Leigh_LbR 0:bf04f62339a4 103
Leigh_LbR 0:bf04f62339a4 104 /*
Leigh_LbR 0:bf04f62339a4 105 * Connect to the NR API.
Leigh_LbR 0:bf04f62339a4 106 */
Leigh_LbR 0:bf04f62339a4 107 int NR_Network_Conn::Connect()
Leigh_LbR 0:bf04f62339a4 108 {
Leigh_LbR 0:bf04f62339a4 109 if(_address[0] == '\0' && _sub_mask[0] == '\0' && _gateway == '\0') {
Leigh_LbR 0:bf04f62339a4 110 if(conn.init() < 0) return -1;
Leigh_LbR 0:bf04f62339a4 111 } else {
Leigh_LbR 0:bf04f62339a4 112 if(conn.init(_address, _sub_mask, _gateway) < 0) return -1;
Leigh_LbR 0:bf04f62339a4 113 }
Leigh_LbR 0:bf04f62339a4 114
Leigh_LbR 0:bf04f62339a4 115 conn.connect();
Leigh_LbR 0:bf04f62339a4 116
Leigh_LbR 0:bf04f62339a4 117 socket = TCPSocketConnection();
Leigh_LbR 0:bf04f62339a4 118
Leigh_LbR 0:bf04f62339a4 119 socket.connect("lbhuxley.azurewebsites.net", 80);
Leigh_LbR 0:bf04f62339a4 120
Leigh_LbR 0:bf04f62339a4 121 return 0;
Leigh_LbR 0:bf04f62339a4 122 }
Leigh_LbR 0:bf04f62339a4 123
Leigh_LbR 0:bf04f62339a4 124 void NR_Network_Conn::Disconnect()
Leigh_LbR 0:bf04f62339a4 125 {
Leigh_LbR 0:bf04f62339a4 126 socket.close();
Leigh_LbR 0:bf04f62339a4 127 conn.disconnect();
Leigh_LbR 0:bf04f62339a4 128 }