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