2017-06-29 12:54:02 -06:00
|
|
|
import { Farmbot } from "farmbot";
|
2017-10-10 11:59:08 -06:00
|
|
|
import { bail } from "./util";
|
|
|
|
import { set } from "lodash";
|
2017-10-10 15:07:23 -06:00
|
|
|
import { AuthState } from "./auth/interfaces";
|
2017-06-29 12:54:02 -06:00
|
|
|
|
2018-01-19 10:49:07 -07:00
|
|
|
let device: Farmbot | undefined;
|
2017-06-29 12:54:02 -06:00
|
|
|
|
2017-10-10 15:07:23 -06:00
|
|
|
const secure = location.protocol === "https:"; // :(
|
|
|
|
|
2018-01-14 08:22:02 -07:00
|
|
|
export const maybeGetDevice = () => device;
|
|
|
|
export const getDevice =
|
|
|
|
(): Farmbot => (maybeGetDevice() || bail("NO DEVICE SET"));
|
2017-06-29 12:54:02 -06:00
|
|
|
|
2017-10-12 14:28:19 -06:00
|
|
|
export function fetchNewDevice(auth: AuthState): Promise<Farmbot> {
|
|
|
|
device = new Farmbot({ token: auth.token.encoded, secure });
|
|
|
|
set(window, "current_bot", device);
|
|
|
|
|
|
|
|
return device
|
|
|
|
.connect()
|
2018-01-19 10:49:07 -07:00
|
|
|
.then(() => device || bail("No."), () => bail("NO CONNECT"));
|
2017-10-10 11:59:08 -06:00
|
|
|
}
|