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
nr_network.cpp
00001 #include "departure_board.h" 00002 #include "arrival_board.h" 00003 #include "nr_network.h" 00004 #include "mbed.h" 00005 #include <string> 00006 #include <vector> 00007 00008 #ifndef nullptr 00009 #define nullptr 0 // nullptr is undefined by default... 00010 #endif 00011 00012 using std::string; 00013 using std::vector; 00014 00015 /* 00016 * Initialise the network connection with the required attributes. 00017 */ 00018 void NR_Network_Conn::init(const char* address, const char* mask, const char* gateway) 00019 { 00020 conn = EthernetInterface(); 00021 00022 if(address == nullptr) _address[0] = '\0'; else strcpy(_address, address); 00023 if(mask == nullptr) _sub_mask[0] = '\0'; else strcpy(_sub_mask, mask); 00024 if(gateway == nullptr) _gateway[0] = '\0'; else strcpy(_gateway, gateway); 00025 } 00026 00027 NR_Network_Conn::NR_Network_Conn() 00028 { 00029 init(nullptr, nullptr, nullptr); 00030 } 00031 00032 TCPSocketConnection NR_Network_Conn::GetSocket() 00033 { 00034 return socket; 00035 } 00036 00037 Departure_Board NR_Network_Conn::GetDepartures(const string& code_stn, const string& number) 00038 { 00039 const string command = "GET /departures/" + code_stn + "/" + number + "/ HTTP/1.1"; 00040 00041 vector<char> command_cstr (command.begin(), command.end()); 00042 command_cstr.push_back('\0'); 00043 00044 socket.send_all(&command_cstr[0], sizeof(&command_cstr[0]) - 1); // Ignore the null-terminating character 00045 00046 char buf[1000]; 00047 string json; 00048 00049 int ret; 00050 00051 while(true) { 00052 ret = socket.receive(buf, sizeof(buf) - 1); 00053 if(ret <= 0){ 00054 json.append(buf); 00055 break; 00056 } 00057 buf[ret] = '\0'; 00058 json.append(buf); 00059 memset(buf, '\0', 1000); 00060 } 00061 00062 Departure_Board dep = depBoardFromJson(json); 00063 00064 return dep; 00065 } 00066 00067 Arrival_Board NR_Network_Conn::GetArrivals(const string& code_stn, const string& number) 00068 { 00069 const string command = "GET /arrivals/" + code_stn + "/" + number + "/ HTTP/1.1"; 00070 00071 vector<char> command_cstr (command.begin(), command.end()); 00072 command_cstr.push_back('\0'); 00073 00074 socket.send_all(&command_cstr[0], sizeof(&command_cstr[0]) - 1); // Ignore the null-terminating character 00075 00076 char buf[1000]; 00077 string json; 00078 00079 int ret; 00080 00081 while(true) { 00082 ret = socket.receive(buf, sizeof(buf) - 1); 00083 if(ret <= 0){ 00084 json.append(buf); 00085 break; 00086 } 00087 buf[ret] = '\0'; 00088 json.append(buf); 00089 memset(buf, '\0', 1000); 00090 } 00091 00092 Arrival_Board arr = arrBoardFromJson(json); 00093 00094 return arr; 00095 } 00096 00097 char* NR_Network_Conn::GetIP() 00098 { 00099 return conn.getIPAddress(); 00100 } 00101 00102 NR_Network_Conn::NR_Network_Conn(const char* address, const char* mask, const char* gateway) 00103 { 00104 init(address, mask, gateway); 00105 } 00106 00107 /* 00108 * Connect to the Huxley (NR) API. 00109 * @param url URL of the Huxley implementation to be used 00110 */ 00111 int NR_Network_Conn::Connect(const char* url) 00112 { 00113 Serial pc(USBTX, USBRX); 00114 pc.baud(115200); 00115 pc.printf("Initiating connection (2A)...\n"); 00116 00117 if((_address[0] == '\0') || (_sub_mask[0] == '\0') || (_gateway == '\0')) { 00118 pc.printf("IP Address is being set by DHCP.\n"); 00119 if(conn.init() < 0) return -1; 00120 } else { 00121 pc.printf("IP Address is being set manually.\n"); 00122 if(conn.init(_address, _sub_mask, _gateway) < 0) return -1; 00123 } 00124 00125 pc.printf("Connecting (2B)...\n"); 00126 00127 if(int err = conn.connect() < 0) return err; 00128 00129 pc.printf("Creating TCPSocketConnection (2C)...\n"); 00130 00131 socket = TCPSocketConnection(); 00132 00133 socket.connect(url, 80); 00134 00135 return 0; 00136 } 00137 00138 void NR_Network_Conn::Disconnect() 00139 { 00140 socket.close(); 00141 conn.disconnect(); 00142 }
Generated on Mon Jul 18 2022 22:01:04 by 1.7.2