mirror of
https://github.com/cjdenio/train-map.git
synced 2026-12-01 05:26:19 +00:00
everything
This commit is contained in:
6 files changed
+235
-37
No files matched your search
@@ -0,0 +1,43 @@
|
||||
const std = @import("std");
|
||||
const net = std.Io.net;
|
||||
const msgpack = @import("msgpack");
|
||||
|
||||
pub const ArduinoRouter = struct {
|
||||
stream: net.Stream,
|
||||
io: std.Io,
|
||||
|
||||
pub fn connect(io: std.Io) !@This() {
|
||||
const address = try net.UnixAddress.init("/var/run/arduino-router.sock");
|
||||
const stream = try address.connect(io);
|
||||
|
||||
return .{ .stream = stream, .io = io };
|
||||
}
|
||||
|
||||
pub fn notify(self: @This(), allocator: std.mem.Allocator, method: []const u8, args: []const msgpack.Payload) !void {
|
||||
var request = try msgpack.Payload.arrPayload(3, allocator);
|
||||
|
||||
try request.setArrElement(0, msgpack.Payload.intToPayload(2));
|
||||
try request.setArrElement(1, try msgpack.Payload.strToPayload(method, allocator));
|
||||
|
||||
var argsPayload = try msgpack.Payload.arrPayload(args.len, allocator);
|
||||
|
||||
for (args, 0..) |arg, i| {
|
||||
try argsPayload.setArrElement(i, arg);
|
||||
}
|
||||
|
||||
try request.setArrElement(2, argsPayload);
|
||||
|
||||
var buffer: [1024]u8 = undefined;
|
||||
var writer = self.stream.writer(self.io, &buffer);
|
||||
|
||||
const encoder: msgpack.Encoder = .{ .writer = &writer.interface };
|
||||
|
||||
try encoder.write(request);
|
||||
|
||||
try writer.interface.flush();
|
||||
}
|
||||
|
||||
pub fn deinit(self: @This()) void {
|
||||
self.stream.close(self.io);
|
||||
}
|
||||
};
|
||||
Reference in new issue
Block a user