smartthings-phone-presence-sensor
Version:
Presence detection by reading leases on DHCP server
186 lines (150 loc) • 4.88 kB
JavaScript
import { action, observable } from 'mobx';
import { fetchData, sendData } from '../utils/restCalls';
const serverUrl = process.env.SERVER_URL;
export class ComponentStateStore {
users = false;
macs = false;
networks = false;
asus = false;
tpLink = false;
mikrotik = false;
devices = false;
serverConfig = false;
smartapp = false;
error = undefined;
isLoading = false;
isSettingLoading = false;
serverSetting = null;
smartappSetting = null;
asusSetting = null;
routerMessage = null;
smartThingMessage = null;
routerError = false;
smartThingError = false;
components = [];
status = null;
smartappUrl = null;
parseState(data) {
const res = JSON.parse(data);
this.users = res.data.includes('users');
this.macs = res.data.includes('macs');
this.networks = res.data.includes('networks');
this.asus = res.data.includes('asus');
this.error = res.data.includes('message');
this.devices = res.data.includes('devices');
this.serverConfig = res.data.includes('serverConfig');
this.smartapp = res.data.includes('smartapp');
this.routerError = res.data.includes('routerError');
this.smartThingError = res.data.includes('smartThingError');
this.tpLink = res.data.includes('tpLink');
this.mikrotik = res.data.includes('mikrotik');
this.components = res.data;
this.status = res.status;
this.routerMessage = res.routerMessage;
this.smartThingMessage = res.smartThingMessage;
}
parseSetting(data) {
const res = JSON.parse(data);
this.serverSetting = res.data.server;
this.smartappSetting = res.data.smartapp;
this.asusSetting = res.data.router;
}
load() {
this.isLoading = true;
fetchData(`${serverUrl}ui/components`).then(action(({ data }) => {
this.parseState(data);
})).catch(
action(({ data }) => {
this.error = data;
}),
).finally(action(() => {
this.isLoading = false;
}));
}
settingLoad() {
this.isSettingLoading = true;
fetchData(`${serverUrl}ui/settings`).then(action(({ data }) => {
this.parseSetting(data);
})).catch(
action(({ data }) => {
this.error = data;
}),
).finally(action(() => {
this.isSettingLoading = false;
}));
}
serverSettingSave() {
this.isSettingLoading = true;
sendData(`${serverUrl}ui/settings`,
'POST',
JSON.stringify({
server: {
port: this.serverSetting.port,
debug: this.serverSetting.debug,
mobilePresenceJob: this.serverSetting.mobilePresenceJob,
},
}), {
'Content-Type': 'application/json',
}).then(action(({ data }) => {
this.parseSetting(data);
})).catch(
action(({ data }) => {
this.error = data;
}),
).finally(action(() => {
this.isSettingLoading = false;
}));
}
smartAppSave() {
this.isSettingLoading = true;
sendData(`${serverUrl}ui/settings`,
'POST',
JSON.stringify({
smartapp: this.smartappSetting,
}), {
'Content-Type': 'application/json',
}).then(action(({ data }) => {
this.parseSetting(data);
})).catch(
action(({ data }) => {
this.error = data;
}),
).finally(action(() => {
this.isSettingLoading = false;
}));
}
addShard() {
if (this.smartappUrl) {
this.smartappSetting.push(this.smartappUrl);
action(this.smartAppSave());
}
}
asusSave() {
this.isSettingLoading = true;
sendData(`${serverUrl}ui/settings`,
'POST',
JSON.stringify({
router: this.asusSetting,
}), {
'Content-Type': 'application/json',
}).then(action(({ data }) => {
this.parseSetting(data);
})).catch(
action(({ data }) => {
this.error = data;
}),
).finally(action(() => {
this.isSettingLoading = false;
}));
}
setSettingData(elementName, elementValue) {
this.serverSetting[elementName] = elementValue;
}
modifyShard(elementValue) {
this.smartappUrl = elementValue;
}
setAsusSetting(elementName, elementValue) {
this.asusSetting[elementName] = elementValue;
}
}
export default new ComponentStateStore();