-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathRelays.svelte
184 lines (169 loc) · 5.81 KB
/
Relays.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<script>
import { PageHeader } from '@keenmate/svelte-adminlte';
import { _ } from 'svelte-i18n';
import { onMount, onDestroy, getContext, setContext } from 'svelte';
import { setCustomPageTitle, customPageTitleUsed } from '../stores/page-title';
import { fetchRelays, deleteRelay, toggleRelay, dimRelay, manualRelay, updateSystemSettings } from '../providers/api';
import { successNotification, errorNotification } from '../providers/notification-provider';
import { isAuthenticated } from '../stores/authentication';
import RelayCard from '../user-controls/RelayCard.svelte';
import EventModel from '../modals/EventFormModal.svelte';
let relays = [];
let showModal;
const { confirmModal } = getContext('confirm');
const loadData = () => {
fetchRelays(false, (data) => (relays = data));
};
const loadCalendar = () => {
location.href = '/#/calendar';
};
const deleteRelayAction = (relay) => {
confirmModal(
$_('relays.delete.confirm.message', {
default: "Are you sure you want to delete the relay ''{name}''?",
values: { name: relay.name },
}),
async () => {
try {
await deleteRelay(relay.id);
successNotification(
$_('relays.delete.ok.message', {
default: "The relay ''{name}'' is deleted.",
values: { name: relay.name },
}),
$_('notification.delete.ok.title', { default: 'OK' }),
);
loadData();
} catch (e) {
errorNotification(
$_('relays.delete.error.message', {
default: "The relay ''{name}'' could not be deleted!\nError: {error}",
values: { name: relay.name, error: e.message },
}),
$_('notification.delete.error.title', { default: 'ERROR' }),
);
}
},
);
};
const ignoreRelayAction = (relay) => {
confirmModal(
$_('relays.confirm.ignore.message', {
default: "Are you sure you want to ignore the relay ''{name}''?",
values: { name: relay.name },
}),
async () => {
try {
await updateSystemSettings({ exclude_ids: relay.id });
successNotification(
$_('relays.exclude.ok.message', {
default: "The relay ''{name}'' is ignored.",
values: { name: relay.name },
}),
$_('notification.exclude.ok.title', { default: 'OK' }),
);
loadData();
} catch (e) {
errorNotification(
$_('relays.exclude.error.message', {
default: "The relay ''{name}'' could not be ignored!\nError: {error}",
values: { name: relay.name, error: e.message },
}),
$_('notification.exclude.error.title', { default: 'ERROR' }),
);
}
},
);
};
const replaceHardware = (relay) => {
let eventData = { mode: 'reminder', summary: 'Replace hardware relay ' + relay.name };
showModal(eventData);
};
const relayManualMode = (relay) => {
confirmModal(
$_('relays.confirm.manual_mode.message', {
default: "Toggle manual mode for relay ''{name}''?",
values: { name: relay.name },
}),
async () => {
try {
await manualRelay(relay.id);
successNotification(
$_('relays.manual_mode.ok.message', {
default: "The manual mode for relay ''{name}'' is changed.",
values: { name: relay.name },
}),
$_('notification.manual_mode.ok.title', { default: 'OK' }),
);
loadData();
} catch (e) {
errorNotification(
$_('relays.manual_mode.error.message', {
default: "Error changing manual mode for relay ''{name}''.\nError: {error}",
values: { name: relay.name, error: e.message },
}),
$_('notification.manual_mode.error.title', { default: 'ERROR' }),
);
}
},
);
};
const toggleRelayAction = async (relay, state) => {
try {
let result = '';
if (relay.dimmer) {
await dimRelay(relay.id, state, (data) => (result = data));
} else {
await toggleRelay(relay.id, (data) => (result = data));
}
result = relay.dimmer ? result.value + '%' : result.value > 0 ? 'on' : 'off';
successNotification(
$_('relays.toggle.ok.message', {
default: "Toggled relay ''{name}'' to state {value}",
values: { name: relay.name, value: result },
}),
$_('notification.toggle.ok.title', { default: 'OK' }),
);
} catch (e) {
errorNotification(
$_('relays.toggle.error.message', {
default: "Error toggling relay ''{name}''.\nError: {error}",
values: { name: relay.name, error: e.message },
}),
$_('notification.toggle.error.title', { default: 'ERROR' }),
);
}
};
setContext('relayActions', {
toggleAction: (relay, state) => toggleRelayAction(relay, state),
deleteAction: (relay) => deleteRelayAction(relay),
ignoreAction: (relay) => ignoreRelayAction(relay),
replaceAction: (relay) => replaceHardware(relay),
manualAction: (relay) => relayManualMode(relay),
});
onMount(() => {
setCustomPageTitle($_('relays.title', { default: 'Relays' }));
loadData();
});
onDestroy(() => {
customPageTitleUsed.set(false);
});
</script>
<PageHeader>
{$_(`relays.title`)}
</PageHeader>
<div class="container-fluid">
<div class="row">
{#if relays.length > 0}
<!-- Sort based on translated names -->
{#each relays.sort((a, b) => a.name.localeCompare(b.name)) as relay}
<div class="col-12">
<RelayCard {relay} />
</div>
{/each}
{/if}
</div>
</div>
{#if $isAuthenticated}
<EventModel bind:show={showModal} on:save={loadCalendar} />
{/if}