smartthings-phone-presence-sensor
Version:
Presence detection by reading leases on DHCP server
75 lines (63 loc) • 1.94 kB
JavaScript
import { observable, action } from 'mobx';
import { fetchData, sendData } from '../utils/restCalls';
const serverUrl = process.env.SERVER_URL;
export class DevicesStore {
devices = [];
maclist = [];
blockedMacs = [];
error = undefined;
isDevicesLoading = false;
parseState(data) {
const res = JSON.parse(data);
if (res.data && res.data.availableMacs) {
this.devices = res.data.availableMacs || [];
this.maclist = res.data.maclist || [];
this.blockedMacs = res.data.blockedMacs || [];
}
}
load() {
this.isDevicesLoading = true;
fetchData(`${serverUrl}ui/presenceMobiles`).then(action(({ data }) => {
this.parseState(data);
})).catch(
action(({ data }) => {
this.error = data;
}),
).finally(action(() => {
this.isDevicesLoading = false;
}));
}
blockAccess(mac) {
this.isDevicesLoading = true;
sendData(`${serverUrl}ui/blockMac`,
'POST',
JSON.stringify({ macs: [mac] }), {
'Content-Type': 'application/json',
}).then(action(() => {
action(UsersStore.this.load());
})).catch(
action(({ data }) => {
this.error = data;
}),
).finally(action(() => {
this.isDevicesLoading = false;
}));
}
unBlockAccess(mac) {
this.isDevicesLoading = true;
sendData(`${serverUrl}ui/unBlockMac`,
'POST',
JSON.stringify({ macs: [mac] }), {
'Content-Type': 'application/json',
}).then(action(() => {
action(DevicesStore.this.load());
})).catch(
action(({ data }) => {
this.error = data;
}),
).finally(action(() => {
this.isDevicesLoading = false;
}));
}
}
export default new DevicesStore();