bobcat/ts/bobcat.classes.bobcat.ts
2022-02-25 21:45:21 +01:00

66 lines
1.9 KiB
TypeScript

import * as plugins from './bobcat.plugins';
import * as interfaces from './interfaces';
/**
* maps to an individual bobcat miner
*/
export class Bobcat {
// STATIC
public static async createFromNetworkAddress(networkAddressArg: string) {
const newBobcat = new Bobcat(networkAddressArg);
await newBobcat.checkMinerStatus().catch();
await newBobcat.gatherMinerDetails().catch();
return newBobcat;
}
// INSTANCE
public networkAddress: string;
public latestStatus: interfaces.IMinerStatus;
public latestMinerDetails: interfaces.IMinerDetailsResponse
constructor(networkAddressArg: string) {
this.networkAddress = networkAddressArg;
}
/**
* checks the status of the miner
*/
public async checkMinerStatus() {
const response = await plugins.smartrequest.getJson(`http://${this.networkAddress}/status.json`);
const body: interfaces.IMinerStatus = response.body;
this.latestStatus = body;
return this.latestStatus;
}
/**
* gathers the miner details
*/
public async gatherMinerDetails() {
const response = await plugins.smartrequest.getJson(`http://${this.networkAddress}/miner.json`);
const body: interfaces.IMinerDetailsResponse = response.body;
this.latestMinerDetails = body;
return this.latestMinerDetails;
}
/**
* runs maintenance on the bobcat
*/
public async runMaintenance() {
await this.checkMinerStatus();
await this.gatherMinerDetails();
if (this.latestStatus.status !== 'Synced') {
console.log(`Miner ${this.latestMinerDetails.animal} is not synced. Restarting now!`);
await this.restart().catch();
}
}
public async restart() {
const response = await plugins.smartrequest.request(`http://${this.networkAddress}/admin/reboot`, {
method: 'POST',
headers: {
Authorization: 'Basic ' + Buffer.from('bobcat:miner').toString('base64')
}
});
console.log(response.statusCode);
}
}