Browse Source

Add support for all transport means

master
Kenneth Bruen 8 months ago
parent
commit
cbe86869e0
Signed by: kbruen
GPG Key ID: C1980A470C3EE5B1
  1. 28
      src/departure.zig
  2. 60
      src/state.zig

28
src/departure.zig

@ -23,7 +23,7 @@ fn fetchThread(state: *AppState) !void {
defer state.departure_screen_state.fetch_thread = null; defer state.departure_screen_state.fetch_thread = null;
const allocator = state.allocator; const allocator = state.allocator;
var station_id_buf = std.BoundedArray(u8, 10){}; var station_id_buf = std.BoundedArray(u8, 10){};
var include_tram = false; var transport_means = state.departure_screen_state.transport_means;
var curl = Curl.init() orelse return; var curl = Curl.init() orelse return;
defer curl.deinit(); defer curl.deinit();
@ -32,14 +32,16 @@ fn fetchThread(state: *AppState) !void {
if (state.departure_screen_state.last_refresh_time + 30000 < std.time.milliTimestamp()) { if (state.departure_screen_state.last_refresh_time + 30000 < std.time.milliTimestamp()) {
fetch_anyway = true; fetch_anyway = true;
} }
if (!fetch_anyway and std.mem.eql(u8, station_id_buf.slice(), state.departure_screen_state.station_id.items) and include_tram == state.departure_screen_state.include_tram) { const same_station = std.mem.eql(u8, station_id_buf.slice(), state.departure_screen_state.station_id.items);
const same_transport_means = std.meta.eql(transport_means, state.departure_screen_state.transport_means);
if (!fetch_anyway and same_station and same_transport_means) {
std.time.sleep(100 * 1000); std.time.sleep(100 * 1000);
continue; continue;
} }
station_id_buf.resize(state.departure_screen_state.station_id.items.len) catch continue; station_id_buf.resize(state.departure_screen_state.station_id.items.len) catch continue;
std.mem.copyForwards(u8, station_id_buf.slice(), state.departure_screen_state.station_id.items); std.mem.copyForwards(u8, station_id_buf.slice(), state.departure_screen_state.station_id.items);
include_tram = state.departure_screen_state.include_tram; transport_means = state.departure_screen_state.transport_means;
std.debug.print("[departure/fetchThread] Detected update: {s}\n", .{station_id_buf.slice()}); std.debug.print("[departure/fetchThread] Detected update: {s}\n", .{station_id_buf.slice()});
curl.reset(); curl.reset();
@ -51,9 +53,20 @@ fn fetchThread(state: *AppState) !void {
) catch continue; ) catch continue;
defer allocator.free(departures_base); defer allocator.free(departures_base);
var departures_uri = std.Uri.parse(departures_base) catch unreachable; var departures_uri = std.Uri.parse(departures_base) catch unreachable;
const query = std.fmt.allocPrint(allocator, "duration=300&bus=false&ferry=false&taxi=false&pretty=false{s}", .{if (include_tram) "" else "&tram=false&subway=false"}) catch continue; var queryBuilder = std.ArrayList(u8).init(allocator);
defer allocator.free(query); defer queryBuilder.deinit();
departures_uri.query = query; queryBuilder.appendSlice("duration=300&pretty=false") catch continue;
if (!transport_means.ice) queryBuilder.appendSlice("&nationalExpress=false") catch continue;
if (!transport_means.ic) queryBuilder.appendSlice("&national=false") catch continue;
if (!transport_means.ir) queryBuilder.appendSlice("&regionalExpress=false") catch continue;
if (!transport_means.r) queryBuilder.appendSlice("&regional=false") catch continue;
if (!transport_means.s) queryBuilder.appendSlice("&suburban=false") catch continue;
if (!transport_means.bus) queryBuilder.appendSlice("&bus=false") catch continue;
if (!transport_means.ferry) queryBuilder.appendSlice("&ferry=false") catch continue;
if (!transport_means.u) queryBuilder.appendSlice("&subway=false") catch continue;
if (!transport_means.tram) queryBuilder.appendSlice("&tram=false") catch continue;
if (!transport_means.taxi) queryBuilder.appendSlice("&taxi=false") catch continue;
departures_uri.query = queryBuilder.items;
defer departures_uri.query = null; defer departures_uri.query = null;
std.debug.print("[departure/fetchThread] Making request to: {}\n", .{departures_uri}); std.debug.print("[departure/fetchThread] Making request to: {}\n", .{departures_uri});
@ -635,7 +648,8 @@ pub fn render(state: *AppState) !void {
ds.max_next_trains = @min(ds.max_next_trains + 1, if (ds.fetch_result) |fr| @as(c_int, @intCast(fr.value.object.get("departures").?.array.items.len)) else 5); ds.max_next_trains = @min(ds.max_next_trains + 1, if (ds.fetch_result) |fr| @as(c_int, @intCast(fr.value.object.get("departures").?.array.items.len)) else 5);
}, },
rl.KEY_T => { rl.KEY_T => {
ds.include_tram = !ds.include_tram; ds.transport_means.u = !ds.transport_means.u;
ds.transport_means.tram = !ds.transport_means.tram;
}, },
rl.KEY_ONE => { rl.KEY_ONE => {
ds.render_style = .db1; ds.render_style = .db1;

60
src/state.zig

@ -26,6 +26,64 @@ pub const RenderStyle = enum(u8) {
ns = 3, ns = 3,
}; };
pub const TransportationMethodFlags = packed struct {
ice: bool = false,
ic: bool = false,
ir: bool = false,
r: bool = false,
s: bool = false,
bus: bool = false,
tram: bool = false,
u: bool = false,
taxi: bool = false,
ferry: bool = false,
fn all() @This() {
return .{
.ice = true,
.ic = true,
.ir = true,
.r = true,
.s = true,
.bus = true,
.tram = true,
.u = true,
.taxi = true,
.ferry = true,
};
}
fn only_trains() @This() {
return .{
.ice = true,
.ic = true,
.ir = true,
.r = true,
.s = true,
};
}
fn only_long_distance() @This() {
return .{
.ice = true,
.ic = true,
.ir = true,
};
}
fn only_regional() @This() {
return .{
.r = true,
.s = true,
.bus = true,
.tram = true,
.u = true,
.taxi = true,
.ferry = true,
};
}
};
pub const DepartureScreenState = struct { pub const DepartureScreenState = struct {
mutex: std.Thread.Mutex = .{}, mutex: std.Thread.Mutex = .{},
station_id: std.ArrayListUnmanaged(u8), station_id: std.ArrayListUnmanaged(u8),
@ -36,7 +94,7 @@ pub const DepartureScreenState = struct {
fetch_result: ?std.json.Parsed(std.json.Value) = null, fetch_result: ?std.json.Parsed(std.json.Value) = null,
should_refresh: bool = false, should_refresh: bool = false,
max_next_trains: c_int = 5, max_next_trains: c_int = 5,
include_tram: bool = false, transport_means: TransportationMethodFlags = TransportationMethodFlags.only_trains(),
render_style: RenderStyle = .db1, render_style: RenderStyle = .db1,
}; };

Loading…
Cancel
Save