This commit is contained in:
parent
2aa12c1c16
commit
a2cd67af7c
3 changed files with 31 additions and 25 deletions
|
|
@ -2,7 +2,7 @@
|
|||
.name = .controlr,
|
||||
.version = "0.1.0",
|
||||
.fingerprint = 0x8deabca9d06691ba,
|
||||
.minimum_zig_version = "0.15.0",
|
||||
.minimum_zig_version = "0.16.0",
|
||||
.dependencies = .{},
|
||||
.paths = .{
|
||||
"build.zig",
|
||||
|
|
|
|||
38
src/main.zig
38
src/main.zig
|
|
@ -17,11 +17,9 @@ const CognitoCredentials = struct {
|
|||
};
|
||||
|
||||
/// Reads username and password from .credentials file (one per line)
|
||||
fn readCredentials(allocator: std.mem.Allocator) !CognitoCredentials {
|
||||
fn readCredentials(io: std.Io, allocator: std.mem.Allocator) !CognitoCredentials {
|
||||
// TODO: something different
|
||||
const file = try std.fs.cwd().openFile(".credentials", .{});
|
||||
defer file.close();
|
||||
const content = try file.readToEndAlloc(allocator, 1024);
|
||||
const content = try std.Io.Dir.cwd().readFileAlloc(io, ".credentials", allocator, .unlimited);
|
||||
var it = std.mem.splitScalar(u8, std.mem.trim(
|
||||
u8,
|
||||
content,
|
||||
|
|
@ -37,29 +35,28 @@ fn readCredentials(allocator: std.mem.Allocator) !CognitoCredentials {
|
|||
};
|
||||
}
|
||||
|
||||
pub fn main() !void {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
defer _ = gpa.deinit();
|
||||
const allocator = gpa.allocator();
|
||||
pub fn main(init: std.process.Init) !void {
|
||||
const allocator = init.gpa;
|
||||
const io = init.io;
|
||||
|
||||
const args = try std.process.argsAlloc(allocator);
|
||||
defer std.process.argsFree(allocator, args);
|
||||
const args = try init.minimal.args.toSlice(init.arena.allocator());
|
||||
const debug_mode = args.len > 1 and std.mem.eql(u8, args[1], "--debug");
|
||||
|
||||
var stdout_buffer: [1024]u8 = undefined;
|
||||
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
|
||||
var stdout_writer = std.Io.File.stdout().writer(io, &stdout_buffer);
|
||||
const stdout = &stdout_writer.interface;
|
||||
|
||||
var stderr_buffer: [1024]u8 = undefined;
|
||||
var stderr_writer = std.fs.File.stderr().writer(&stderr_buffer);
|
||||
var stderr_writer = std.Io.File.stderr().writer(io, &stderr_buffer);
|
||||
const stderr = &stderr_writer.interface;
|
||||
|
||||
var creds = try readCredentials(allocator);
|
||||
var creds = try readCredentials(io, allocator);
|
||||
defer creds.deinit();
|
||||
|
||||
try stdout.print("🔐 Authenticating...\n", .{});
|
||||
try stdout.flush();
|
||||
var auth = try rinnai.authenticate(
|
||||
io,
|
||||
allocator,
|
||||
creds.username,
|
||||
creds.password,
|
||||
|
|
@ -75,6 +72,7 @@ pub fn main() !void {
|
|||
try stdout.print("📱 Fetching devices...\n", .{});
|
||||
try stdout.flush();
|
||||
var result = try rinnai.getDevices(
|
||||
io,
|
||||
allocator,
|
||||
auth.id_token,
|
||||
creds.username,
|
||||
|
|
@ -115,7 +113,12 @@ pub fn main() !void {
|
|||
try stdout.print("🔍 Checking recirculation status for {?s}...\n", .{device.device_name});
|
||||
try stdout.flush();
|
||||
|
||||
var status = try rinnai.getRecirculationStatus(allocator, auth.id_token, sid);
|
||||
var status = try rinnai.getRecirculationStatus(
|
||||
io,
|
||||
allocator,
|
||||
auth.id_token,
|
||||
sid,
|
||||
);
|
||||
defer status.deinit();
|
||||
|
||||
try stdout.print("\n{f}", .{status});
|
||||
|
|
@ -154,6 +157,7 @@ pub fn main() !void {
|
|||
// application doesn't wait, it just yolo's here, so
|
||||
// we'll do the same
|
||||
try rinnai.setRecirculation(
|
||||
io,
|
||||
allocator,
|
||||
auth.id_token,
|
||||
sid,
|
||||
|
|
@ -162,10 +166,11 @@ pub fn main() !void {
|
|||
|
||||
try stdout.print("⏳ Waiting 3 seconds...\n", .{});
|
||||
try stdout.flush();
|
||||
std.Thread.sleep(3 * std.time.ns_per_s);
|
||||
try std.Io.sleep(io, .fromSeconds(3), .real);
|
||||
}
|
||||
}
|
||||
rinnai.setRecirculation(
|
||||
io,
|
||||
allocator,
|
||||
auth.id_token,
|
||||
tn,
|
||||
|
|
@ -182,9 +187,10 @@ pub fn main() !void {
|
|||
|
||||
const max = 10;
|
||||
for (0..10) |i| {
|
||||
std.Thread.sleep((20 / max) * std.time.ns_per_s);
|
||||
try std.Io.sleep(io, .fromSeconds(20 / max), .real);
|
||||
|
||||
var post_command_state = try rinnai.getRecirculationStatus(
|
||||
io,
|
||||
allocator,
|
||||
auth.id_token,
|
||||
sid,
|
||||
|
|
|
|||
|
|
@ -80,8 +80,8 @@ pub const DeviceShadow = struct {
|
|||
|
||||
/// Authenticates with AWS Cognito and returns ID token and user UUID.
|
||||
/// Caller must call `deinit()` on the returned AuthResult when done.
|
||||
pub fn authenticate(allocator: std.mem.Allocator, username: []const u8, password: []const u8) !AuthResult {
|
||||
var client = http.Client{ .allocator = allocator };
|
||||
pub fn authenticate(io: std.Io, allocator: std.mem.Allocator, username: []const u8, password: []const u8) !AuthResult {
|
||||
var client = http.Client{ .io = io, .allocator = allocator };
|
||||
defer client.deinit();
|
||||
|
||||
const body = try std.fmt.allocPrint(allocator,
|
||||
|
|
@ -147,8 +147,8 @@ pub fn authenticate(allocator: std.mem.Allocator, username: []const u8, password
|
|||
|
||||
/// Fetches device list from AppSync GraphQL API for the given user.
|
||||
/// Caller must call `deinit()` on the returned DeviceList when done.
|
||||
pub fn getDevices(allocator: std.mem.Allocator, id_token: []const u8, username: []const u8) !DeviceList {
|
||||
var client = http.Client{ .allocator = allocator };
|
||||
pub fn getDevices(io: std.Io, allocator: std.mem.Allocator, id_token: []const u8, username: []const u8) !DeviceList {
|
||||
var client = http.Client{ .io = io, .allocator = allocator };
|
||||
defer client.deinit();
|
||||
|
||||
const query =
|
||||
|
|
@ -206,8 +206,8 @@ pub fn getDevices(allocator: std.mem.Allocator, id_token: []const u8, username:
|
|||
|
||||
/// Starts or stops recirculation for the specified device.
|
||||
/// Pass duration_minutes to start, or null to stop.
|
||||
pub fn setRecirculation(allocator: std.mem.Allocator, id_token: []const u8, thing_name: []const u8, duration_minutes: ?u32) !void {
|
||||
var client = http.Client{ .allocator = allocator };
|
||||
pub fn setRecirculation(io: std.Io, allocator: std.mem.Allocator, id_token: []const u8, thing_name: []const u8, duration_minutes: ?u32) !void {
|
||||
var client = http.Client{ .io = io, .allocator = allocator };
|
||||
defer client.deinit();
|
||||
|
||||
const url = try std.fmt.allocPrint(allocator, "{s}/{s}/shadow", .{ shadow_api_url, thing_name });
|
||||
|
|
@ -249,8 +249,8 @@ pub fn setRecirculation(allocator: std.mem.Allocator, id_token: []const u8, thin
|
|||
|
||||
/// Queries the device shadow to get current recirculation status.
|
||||
/// Caller must call `deinit()` on the returned DeviceShadow when done.
|
||||
pub fn getRecirculationStatus(allocator: std.mem.Allocator, id_token: []const u8, serial_number: []const u8) !DeviceShadow {
|
||||
var client = http.Client{ .allocator = allocator };
|
||||
pub fn getRecirculationStatus(io: std.Io, allocator: std.mem.Allocator, id_token: []const u8, serial_number: []const u8) !DeviceShadow {
|
||||
var client = http.Client{ .io = io, .allocator = allocator };
|
||||
defer client.deinit();
|
||||
|
||||
const query =
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue