Handle Alarms

This commit is contained in:
nemunaire 2022-10-05 22:33:31 +02:00
parent 6e6521c814
commit 9fcb4a1867
24 changed files with 1159 additions and 147 deletions

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
actions
alarms.db
gongs
reveil
routines

View File

@ -1,86 +1,266 @@
package api
import (
"fmt"
"net/http"
"time"
"github.com/gin-gonic/gin"
"git.nemunai.re/nemunaire/reveil/config"
"git.nemunai.re/nemunaire/reveil/model"
)
func declareAlarmsRoutes(cfg *config.Config, router *gin.RouterGroup) {
func declareAlarmsRoutes(cfg *config.Config, db *reveil.LevelDBStorage, router *gin.RouterGroup) {
router.GET("/alarms/next", func(c *gin.Context) {
})
router.POST("/alarms", func(c *gin.Context) {
router.GET("/alarms/single", func(c *gin.Context) {
alarms, err := reveil.GetAlarmsSingle(db)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
return
}
c.JSON(http.StatusOK, alarms)
})
router.POST("/alarms/single", func(c *gin.Context) {
var alarm reveil.AlarmSingle
if err := c.ShouldBindJSON(&alarm); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
return
}
router.GET("/alarms/manuals", func(c *gin.Context) {
if time.Now().After(alarm.Time) {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "This date is already passed."})
return
}
})
router.GET("/alarms/usuals", func(c *gin.Context) {
})
router.GET("/alarms/excepts", func(c *gin.Context) {
alarm.Id = nil
if err := reveil.PutAlarmSingle(db, &alarm); err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
return
}
c.JSON(http.StatusOK, alarm)
})
manualAlarmsRoutes := router.Group("/alarms/manuals/:aid")
manualAlarmsRoutes.Use(manualAlarmHandler)
router.GET("/alarms/repeated", func(c *gin.Context) {
alarms, err := reveil.GetAlarmsRepeated(db)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
return
}
manualAlarmsRoutes.GET("", func(c *gin.Context) {
c.JSON(http.StatusOK, alarms)
})
router.POST("/alarms/repeated", func(c *gin.Context) {
var alarm reveil.AlarmRepeated
if err := c.ShouldBindJSON(&alarm); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
return
}
alarm.Id = nil
if err := reveil.PutAlarmRepeated(db, &alarm); err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
return
}
c.JSON(http.StatusOK, alarm)
})
router.GET("/alarms/exceptions", func(c *gin.Context) {
exceptions, err := reveil.GetAlarmExceptions(db)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
return
}
c.JSON(http.StatusOK, exceptions)
})
router.POST("/alarms/exceptions", func(c *gin.Context) {
var alarm reveil.AlarmException
if err := c.ShouldBindJSON(&alarm); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
return
}
if alarm.Start == nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "Start not defined"})
return
}
if alarm.End == nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "End not defined"})
return
}
if time.Now().After(time.Time(*alarm.End)) {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "End date is already passed."})
return
}
if !time.Time(*alarm.End).After(time.Time(*alarm.Start)) {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "Start is defined after End. Please verify your inputs."})
return
}
alarm.Id = nil
if err := reveil.PutAlarmException(db, &alarm); err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
return
}
c.JSON(http.StatusOK, alarm)
})
singleAlarmsRoutes := router.Group("/alarms/single/:aid")
singleAlarmsRoutes.Use(func(c *gin.Context) {
id, err := reveil.NewIdentifierFromString(c.Param("aid"))
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": fmt.Sprintf("Invalid alarm idenfifier: %s", err.Error())})
return
}
alarm, err := reveil.GetAlarmSingle(db, id)
if err != nil {
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": err.Error()})
return
}
c.Set("alarm", alarm)
c.Next()
})
singleAlarmsRoutes.GET("", func(c *gin.Context) {
c.JSON(http.StatusOK, c.MustGet("alarm"))
})
manualAlarmsRoutes.PUT("", func(c *gin.Context) {
c.JSON(http.StatusOK, c.MustGet("alarm"))
singleAlarmsRoutes.PUT("", func(c *gin.Context) {
oldalarm := c.MustGet("alarm").(*reveil.AlarmSingle)
var alarm reveil.AlarmSingle
if err := c.ShouldBindJSON(&alarm); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
return
}
alarm.Id = oldalarm.Id
if err := reveil.PutAlarmSingle(db, &alarm); err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
return
}
c.JSON(http.StatusOK, alarm)
})
manualAlarmsRoutes.DELETE("", func(c *gin.Context) {
c.JSON(http.StatusOK, c.MustGet("alarm"))
singleAlarmsRoutes.DELETE("", func(c *gin.Context) {
alarm := c.MustGet("alarm").(*reveil.AlarmSingle)
if err := reveil.DeleteAlarmSingle(db, alarm); err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
return
}
c.JSON(http.StatusOK, nil)
})
usualAlarmsRoutes := router.Group("/alarms/usuals/:aid")
usualAlarmsRoutes.Use(usualAlarmHandler)
repeatedAlarmsRoutes := router.Group("/alarms/repeated/:aid")
repeatedAlarmsRoutes.Use(func(c *gin.Context) {
id, err := reveil.NewIdentifierFromString(c.Param("aid"))
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": fmt.Sprintf("Invalid alarm idenfifier: %s", err.Error())})
return
}
usualAlarmsRoutes.GET("", func(c *gin.Context) {
c.JSON(http.StatusOK, c.MustGet("alarm"))
})
usualAlarmsRoutes.PUT("", func(c *gin.Context) {
c.JSON(http.StatusOK, c.MustGet("alarm"))
})
usualAlarmsRoutes.DELETE("", func(c *gin.Context) {
c.JSON(http.StatusOK, c.MustGet("alarm"))
alarm, err := reveil.GetAlarmRepeated(db, id)
if err != nil {
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": err.Error()})
return
}
c.Set("alarm", alarm)
c.Next()
})
exceptAlarmsRoutes := router.Group("/alarms/excepts/:aid")
exceptAlarmsRoutes.Use(exceptAlarmHandler)
repeatedAlarmsRoutes.GET("", func(c *gin.Context) {
c.JSON(http.StatusOK, c.MustGet("alarm"))
})
repeatedAlarmsRoutes.PUT("", func(c *gin.Context) {
oldalarm := c.MustGet("alarm").(*reveil.AlarmRepeated)
exceptAlarmsRoutes.GET("", func(c *gin.Context) {
var alarm reveil.AlarmRepeated
if err := c.ShouldBindJSON(&alarm); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
return
}
alarm.Id = oldalarm.Id
if err := reveil.PutAlarmRepeated(db, &alarm); err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
return
}
c.JSON(http.StatusOK, alarm)
})
repeatedAlarmsRoutes.DELETE("", func(c *gin.Context) {
alarm := c.MustGet("alarm").(*reveil.AlarmRepeated)
if err := reveil.DeleteAlarmRepeated(db, alarm); err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
return
}
c.JSON(http.StatusOK, nil)
})
exceptionAlarmsRoutes := router.Group("/alarms/exceptions/:aid")
exceptionAlarmsRoutes.Use(func(c *gin.Context) {
id, err := reveil.NewIdentifierFromString(c.Param("aid"))
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": fmt.Sprintf("Invalid alarm idenfifier: %s", err.Error())})
return
}
alarm, err := reveil.GetAlarmException(db, id)
if err != nil {
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": err.Error()})
return
}
c.Set("alarm", alarm)
c.Next()
})
exceptionAlarmsRoutes.GET("", func(c *gin.Context) {
c.JSON(http.StatusOK, c.MustGet("alarm"))
})
exceptAlarmsRoutes.PUT("", func(c *gin.Context) {
c.JSON(http.StatusOK, c.MustGet("alarm"))
exceptionAlarmsRoutes.PUT("", func(c *gin.Context) {
oldalarm := c.MustGet("alarm").(*reveil.AlarmException)
var alarm reveil.AlarmException
if err := c.ShouldBindJSON(&alarm); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
return
}
alarm.Id = oldalarm.Id
if err := reveil.PutAlarmException(db, &alarm); err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
return
}
c.JSON(http.StatusOK, alarm)
})
exceptAlarmsRoutes.DELETE("", func(c *gin.Context) {
c.JSON(http.StatusOK, c.MustGet("alarm"))
exceptionAlarmsRoutes.DELETE("", func(c *gin.Context) {
alarm := c.MustGet("alarm").(*reveil.AlarmException)
if err := reveil.DeleteAlarmException(db, alarm); err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
return
}
c.JSON(http.StatusOK, nil)
})
}
func manualAlarmHandler(c *gin.Context) {
c.Set("alarm", nil)
c.Next()
}
func usualAlarmHandler(c *gin.Context) {
c.Set("alarm", nil)
c.Next()
}
func exceptAlarmHandler(c *gin.Context) {
c.Set("alarm", nil)
c.Next()
}

View File

@ -11,7 +11,7 @@ func DeclareRoutes(router *gin.Engine, cfg *config.Config, db *reveil.LevelDBSto
apiRoutes := router.Group("/api")
declareActionsRoutes(cfg, apiRoutes)
declareAlarmsRoutes(cfg, apiRoutes)
declareAlarmsRoutes(cfg, db, apiRoutes)
declareGongsRoutes(cfg, apiRoutes)
declareHistoryRoutes(cfg, apiRoutes)
declareQuotesRoutes(cfg, apiRoutes)

193
model/alarm.go Normal file
View File

@ -0,0 +1,193 @@
package reveil
import (
"fmt"
"time"
)
type Date time.Time
func (d *Date) MarshalJSON() (dst []byte, err error) {
return []byte(fmt.Sprintf("\"%04d-%02d-%02d\"", time.Time(*d).Year(), time.Time(*d).Month(), time.Time(*d).Day())), nil
}
func (d *Date) UnmarshalJSON(src []byte) error {
tmp, err := time.Parse("\"2006-01-02\"", string(src))
if err != nil {
return err
}
*d = Date(tmp)
return nil
}
type Hour time.Time
func (h *Hour) MarshalJSON() (dst []byte, err error) {
return []byte(fmt.Sprintf("\"%02d:%02d\"", time.Time(*h).Hour(), time.Time(*h).Minute())), nil
}
func (h *Hour) UnmarshalJSON(src []byte) error {
tmp, err := time.Parse("\"15:04\"", string(src))
if err != nil {
return err
}
*h = Hour(tmp)
return nil
}
type AlarmRepeated struct {
Id Identifier `json:"id"`
Weekday uint8 `json:"weekday"`
StartTime *Hour `json:"time"`
FollowingRoutines []Identifier `json:"routines"`
IgnoreExceptions bool `json:"ignore_exceptions"`
Comment string `json:"comment"`
}
func GetAlarmRepeated(db *LevelDBStorage, id Identifier) (alarm *AlarmRepeated, err error) {
alarm = &AlarmRepeated{}
err = db.get(fmt.Sprintf("alarm-repeated-%s", id.ToString()), alarm)
return
}
func GetAlarmsRepeated(db *LevelDBStorage) (alarms []*AlarmRepeated, err error) {
iter := db.search("alarm-repeated-")
defer iter.Release()
for iter.Next() {
var a AlarmRepeated
err = decodeData(iter.Value(), &a)
if err != nil {
return
}
alarms = append(alarms, &a)
}
return
}
func PutAlarmRepeated(db *LevelDBStorage, alarm *AlarmRepeated) (err error) {
var key string
var id Identifier
if alarm.Id.IsEmpty() {
key, id, err = db.findBytesKey("alarm-repeated-", IDENTIFIER_LEN)
if err != nil {
return err
}
}
alarm.Id = id
return db.put(key, alarm)
}
func DeleteAlarmRepeated(db *LevelDBStorage, alarm *AlarmRepeated) (err error) {
return db.delete(fmt.Sprintf("alarm-repeated-%s", alarm.Id.ToString()))
}
type AlarmSingle struct {
Id Identifier `json:"id"`
Time time.Time `json:"time"`
FollowingRoutines []Identifier `json:"routines"`
Comment string `json:"comment"`
}
func GetAlarmSingle(db *LevelDBStorage, id Identifier) (alarm *AlarmSingle, err error) {
alarm = &AlarmSingle{}
err = db.get(fmt.Sprintf("alarm-single-%s", id.ToString()), alarm)
return
}
func GetAlarmsSingle(db *LevelDBStorage) (alarms []*AlarmSingle, err error) {
iter := db.search("alarm-single-")
defer iter.Release()
for iter.Next() {
var a AlarmSingle
err = decodeData(iter.Value(), &a)
if err != nil {
return
}
alarms = append(alarms, &a)
}
return
}
func PutAlarmSingle(db *LevelDBStorage, alarm *AlarmSingle) (err error) {
var key string
var id Identifier
if alarm.Id.IsEmpty() {
key, id, err = db.findBytesKey("alarm-single-", IDENTIFIER_LEN)
if err != nil {
return err
}
}
alarm.Id = id
return db.put(key, alarm)
}
func DeleteAlarmSingle(db *LevelDBStorage, alarm *AlarmSingle) (err error) {
return db.delete(fmt.Sprintf("alarm-single-%s", alarm.Id.ToString()))
}
type AlarmException struct {
Id Identifier `json:"id"`
Start *Date `json:"start"`
End *Date `json:"end"`
Comment string `json:"comment"`
}
func GetAlarmException(db *LevelDBStorage, id Identifier) (alarm *AlarmException, err error) {
alarm = &AlarmException{}
err = db.get(fmt.Sprintf("alarm-exception-%s", id.ToString()), alarm)
return
}
func GetAlarmExceptions(db *LevelDBStorage) (alarms []*AlarmException, err error) {
iter := db.search("alarm-exception-")
defer iter.Release()
for iter.Next() {
var a AlarmException
err = decodeData(iter.Value(), &a)
if err != nil {
return
}
alarms = append(alarms, &a)
}
return
}
func PutAlarmException(db *LevelDBStorage, alarm *AlarmException) (err error) {
var key string
var id Identifier
if alarm.Id.IsEmpty() {
key, id, err = db.findBytesKey("alarm-exception-", IDENTIFIER_LEN)
if err != nil {
return err
}
}
alarm.Id = id
return db.put(key, alarm)
}
func DeleteAlarmException(db *LevelDBStorage, alarm *AlarmException) (err error) {
return db.delete(fmt.Sprintf("alarm-exception-%s", alarm.Id.ToString()))
}

View File

@ -33,6 +33,7 @@
},
"type": "module",
"dependencies": {
"dayjs": "^1.11.5",
"sass": "^1.49.7",
"sass-loader": "^13.0.0",
"sveltestrap": "^5.8.3",

View File

@ -1,5 +1,4 @@
<script>
import { goto } from '$app/navigation';
import { page } from '$app/stores';
import {

View File

@ -4,8 +4,12 @@
import {
Button,
Icon,
Spinner,
} from 'sveltestrap';
import DateRangeFormat from '../components/DateRangeFormat.svelte';
import { alarmsExceptions } from '../stores/alarmexceptions';
export let flush = false;
</script>
@ -14,22 +18,36 @@
Exceptions
</h2>
<Button
href="alarms/excepts/new"
href="alarms/exceptions/new"
color="outline-primary"
size="sm"
class="float-end {($page.params.kind === 'excepts' && $page.url.pathname.endsWith('/new'))?'active':''}"
class="float-end {($page.params.kind === 'exceptions' && $page.url.pathname.endsWith('/new'))?'active':''}"
>
<Icon name="plus-lg" />
</Button>
</div>
<div class="list-group" class:list-group-flush={flush}>
<a
href="alarms/excepts/1"
class="list-group-item list-group-item-action"
class:active={$page.params.kind === "excepts" && $page.params.aid === "1"}
>
<Icon name="x-octagon-fill" />
Du <abbr title="test">samedi 14</abbr>
au <abbr title="test">dimanche 26 février</abbr>
</a>
<div class="text-center">
{#if $alarmsExceptions.list !== null}
{#if $alarmsExceptions.list.length}
<div class="list-group" class:list-group-flush={flush}>
{#each $alarmsExceptions.list as alarm (alarm.id)}
<a
href="alarms/exceptions/{alarm.id}"
class="list-group-item list-group-item-action"
class:active={$page.params.kind === "exceptions" && $page.params.aid === alarm.id}
>
Du <DateRangeFormat startDate={alarm._start()} endDate={alarm._end()} dateStyle="long" />
</a>
{/each}
</div>
{:else}
<p class="fst-italic">Pas d'exception programmée</p>
{/if}
{:else}
{#await alarmsExceptions.refresh()}
<div class="d-flex justify-content-center align-items-center gap-2">
<Spinner color="primary" /> Chargement en cours&hellip;
</div>
{/await}
{/if}
</div>

View File

@ -4,8 +4,12 @@
import {
Button,
Icon,
Spinner,
} from 'sveltestrap';
import { weekdayStr } from '../lib/alarmrepeated';
import { alarmsRepeated } from '../stores/alarmrepeated';
export let flush = false;
</script>
@ -14,27 +18,36 @@
Réveils habituels
</h2>
<Button
href="alarms/usuals/new"
href="alarms/repeated/new"
color="outline-primary"
size="sm"
class="float-end {($page.params.kind === 'usuals' && $page.url.pathname.endsWith('/new'))?'active':''}"
class="float-end {($page.params.kind === 'repeated' && $page.url.pathname.endsWith('/new'))?'active':''}"
>
<Icon name="plus-lg" />
</Button>
</div>
<div class="list-group" class:list-group-flush={flush}>
<a
href="alarms/usuals/1"
class="list-group-item list-group-item-action"
class:active={$page.params.kind === "usuals" && $page.params.aid === "1"}
>
Les lundis à 6h50
</a>
<a
href="alarms/usuals/2"
class="list-group-item list-group-item-action"
class:active={$page.params.kind === "usuals" && $page.params.aid === "2"}
>
Les mardis à 6h50
</a>
<div class="text-center">
{#if $alarmsRepeated.list !== null}
{#if $alarmsRepeated.list.length}
<div class="list-group" class:list-group-flush={flush}>
{#each $alarmsRepeated.list as alarm (alarm.id)}
<a
href="alarms/repeated/{alarm.id}"
class="list-group-item list-group-item-action"
class:active={$page.params.kind === "repeated" && $page.params.aid === alarm.id}
>
Les {weekdayStr(alarm.weekday)}s à {alarm.time}
</a>
{/each}
</div>
{:else}
<p class="fst-italic">Pas de réveil habituel programmé</p>
{/if}
{:else}
{#await alarmsRepeated.refresh()}
<div class="d-flex justify-content-center align-items-center gap-2">
<Spinner color="primary" /> Chargement en cours&hellip;
</div>
{/await}
{/if}
</div>

View File

@ -4,8 +4,12 @@
import {
Button,
Icon,
Spinner,
} from 'sveltestrap';
import DateFormat from '../components/DateFormat.svelte';
import { alarmsSingle } from '../stores/alarmsingle';
export let flush = false;
</script>
@ -14,14 +18,36 @@
Réveils manuels
</h2>
<Button
href="alarms/manuals/new"
href="alarms/single/new"
color="outline-primary"
size="sm"
class="float-end {($page.params.kind === 'manuals' && $page.url.pathname.endsWith('/new'))?'active':''}"
class="float-end {($page.params.kind === 'single' && $page.url.pathname.endsWith('/new'))?'active':''}"
>
<Icon name="plus-lg" />
</Button>
</div>
<div class="text-center">
<p class="fst-italic">Pas de prochain réveil manuel programmé</p>
{#if $alarmsSingle.list !== null}
{#if $alarmsSingle.list.length}
<div class="list-group" class:list-group-flush={flush}>
{#each $alarmsSingle.list as alarm (alarm.id)}
<a
href="alarms/single/{alarm.id}"
class="list-group-item list-group-item-action"
class:active={$page.params.kind === "single" && $page.params.aid === alarm.id}
>
Le <DateFormat date={alarm.time} dateStyle="long" timeStyle="long" />
</a>
{/each}
</div>
{:else}
<p class="fst-italic">Pas de prochain réveil manuel programmé</p>
{/if}
{:else}
{#await alarmsSingle.refresh()}
<div class="d-flex justify-content-center align-items-center gap-2">
<Spinner color="primary" /> Chargement en cours&hellip;
</div>
{/await}
{/if}
</div>

View File

@ -0,0 +1,17 @@
<script>
export let date;
export let dateStyle;
export let timeStyle;
function formatDate(input, dateStyle, timeStyle) {
if (typeof input === 'string') {
input = new Date(input);
}
return new Intl.DateTimeFormat(undefined, {
dateStyle,
timeStyle,
}).format(input);
}
</script>
{formatDate(date, dateStyle, timeStyle)}

View File

@ -0,0 +1,18 @@
<script>
export let startDate;
export let endDate;
export let dateStyle;
export let timeStyle;
function formatRange(startDate, endDate, dateStyle, timeStyle) {
if (typeof input === 'string') {
input = new Date(input);
}
return new Intl.DateTimeFormat(undefined, {
dateStyle,
timeStyle,
}).formatRange(startDate, endDate);
}
</script>
{formatRange(startDate, endDate, dateStyle, timeStyle)}

View File

@ -0,0 +1,31 @@
<script>
import dayjs from 'dayjs';
import {
Input,
} from 'sveltestrap';
export let format = 'YYYY-MM-DD HH:mm';
export let date = new Date();
let className = '';
export { className as class };
export let id = null;
export let required = false;
let internal;
const input = (x) => (internal = dayjs(x).format(format));
const output = (x) => {
const d = dayjs(x, format).toDate();
if (d) {
date = d.toISOString();
}
};
$: input(date)
$: output(internal)
</script>
<Input type="datetime-local" class={className} id={id} {required} bind:value={internal} />

View File

@ -91,7 +91,6 @@
<div class="d-flex justify-content-center align-items-center gap-2">
<Spinner color="primary" /> Chargement en cours&hellip;
</div>
{:then gongs}
{/await}
{/if}
</div>

View File

@ -0,0 +1,71 @@
export class AlarmException {
constructor(res) {
if (res) {
this.update(res);
}
}
update({ id, start, end, comment }) {
this.id = id;
this.start = start;
this.end = end;
this.comment = comment;
}
_start() {
return new Date(this.start);
}
_end() {
return new Date(this.end);
}
async delete() {
const res = await fetch(`api/alarms/exceptions/${this.id}`, {
method: 'DELETE',
headers: {'Accept': 'application/json'}
});
if (res.status == 200) {
return true;
} else {
throw new Error((await res.json()).errmsg);
}
}
async save() {
const res = await fetch(this.id?`api/alarms/exceptions/${this.id}`:'api/alarms/exceptions', {
method: this.id?'PUT':'POST',
headers: {'Accept': 'application/json'},
body: JSON.stringify(this),
});
if (res.status == 200) {
const data = await res.json();
this.update(data);
return data;
} else {
throw new Error((await res.json()).errmsg);
}
}
}
export async function getAlarmsException() {
const res = await fetch(`api/alarms/exceptions`, {headers: {'Accept': 'application/json'}})
if (res.status == 200) {
const data = await res.json();
if (data === null)
return [];
else
return data.map((t) => new AlarmException(t));
} else {
throw new Error((await res.json()).errmsg);
}
}
export async function getAlarmException(aid) {
const res = await fetch(`api/alarms/exceptions/${aid}`, {headers: {'Accept': 'application/json'}})
if (res.status == 200) {
return new AlarmException(await res.json());
} else {
throw new Error((await res.json()).errmsg);
}
}

View File

@ -0,0 +1,91 @@
export class AlarmRepeated {
constructor(res) {
if (res) {
this.update(res);
}
}
update({ id, weekday, time, routines, ignore_exceptions, comment }) {
this.id = id;
this.weekday = weekday;
this.time = time;
this.routines = routines;
this.ignore_exceptions = ignore_exceptions;
this.comment = comment;
}
async delete() {
const res = await fetch(`api/alarms/repeated/${this.id}`, {
method: 'DELETE',
headers: {'Accept': 'application/json'}
});
if (res.status == 200) {
return true;
} else {
throw new Error((await res.json()).errmsg);
}
}
async save() {
const res = await fetch(this.id?`api/alarms/repeated/${this.id}`:'api/alarms/repeated', {
method: this.id?'PUT':'POST',
headers: {'Accept': 'application/json'},
body: JSON.stringify(this),
});
if (res.status == 200) {
const data = await res.json();
this.update(data);
return data;
} else {
throw new Error((await res.json()).errmsg);
}
}
}
export async function getAlarmsRepeated() {
const res = await fetch(`api/alarms/repeated`, {headers: {'Accept': 'application/json'}})
if (res.status == 200) {
const data = await res.json();
if (data === null)
return [];
else
return data.map((t) => new AlarmRepeated(t));
} else {
throw new Error((await res.json()).errmsg);
}
}
export async function getAlarmRepeated(aid) {
const res = await fetch(`api/alarms/repeated/${aid}`, {headers: {'Accept': 'application/json'}})
if (res.status == 200) {
return new AlarmRepeated(await res.json());
} else {
throw new Error((await res.json()).errmsg);
}
}
export function weekdayStr(weekday) {
switch (weekday) {
case 0:
case "0":
return "dimanche";
case 1:
case "1":
return "lundi";
case 2:
case "2":
return "mardi";
case 3:
case "3":
return "mercredi";
case 4:
case "4":
return "jeudi";
case 5:
case "5":
return "vendredi";
case 6:
case "6":
return "samedi";
}
}

63
ui/src/lib/alarmsingle.js Normal file
View File

@ -0,0 +1,63 @@
export class AlarmSingle {
constructor(res) {
if (res) {
this.update(res);
}
}
update({ id, time, routines, comment }) {
this.id = id;
this.time = new Date(time);
this.routines = routines;
this.comment = comment;
}
async delete() {
const res = await fetch(`api/alarms/single/${this.id}`, {
method: 'DELETE',
headers: {'Accept': 'application/json'}
});
if (res.status == 200) {
return true;
} else {
throw new Error((await res.json()).errmsg);
}
}
async save() {
const res = await fetch(this.id?`api/alarms/single/${this.id}`:'api/alarms/single', {
method: this.id?'PUT':'POST',
headers: {'Accept': 'application/json'},
body: JSON.stringify(this),
});
if (res.status == 200) {
const data = await res.json();
this.update(data);
return data;
} else {
throw new Error((await res.json()).errmsg);
}
}
}
export async function getAlarmsSingle() {
const res = await fetch(`api/alarms/single`, {headers: {'Accept': 'application/json'}})
if (res.status == 200) {
const data = await res.json();
if (data === null)
return [];
else
return data.map((t) => new AlarmSingle(t));
} else {
throw new Error((await res.json()).errmsg);
}
}
export async function getAlarmSingle(aid) {
const res = await fetch(`api/alarms/single/${aid}`, {headers: {'Accept': 'application/json'}})
if (res.status == 200) {
return new AlarmSingle(await res.json());
} else {
throw new Error((await res.json()).errmsg);
}
}

View File

@ -15,11 +15,11 @@
function slugToComponent(slug) {
switch(slug) {
case "singles":
case "single":
return AlarmSingleList;
case "repeateds":
case "repeated":
return AlarmRepeatedList;
case "excepts":
case "exceptions":
return AlarmExceptionList;
}
}

View File

@ -11,20 +11,20 @@
function slugToComponent(slug) {
switch(slug) {
case "singles":
case "single":
return AlarmSingleList;
case "repeateds":
case "repeated":
return AlarmRepeatedList;
case "exceptions":
return AlarmExceptList;
return AlarmExceptionList;
}
}
function slugToText(slug) {
switch(slug) {
case "singles":
case "single":
return "un réveil manuel";
case "repeateds":
case "repeated":
return "un réveil habituel";
case "exceptions":
return "une exception";

View File

@ -3,33 +3,150 @@
Button,
Col,
Container,
Row,
Icon,
ListGroup,
ListGroupItem,
Row,
Spinner,
} from 'sveltestrap';
import { goto } from '$app/navigation';
import { page } from '$app/stores';
import DateFormat from '../../../../components/DateFormat.svelte';
import DateRangeFormat from '../../../../components/DateRangeFormat.svelte';
import { getAlarmSingle } from '../../../../lib/alarmsingle';
import { getAlarmRepeated, weekdayStr } from '../../../../lib/alarmrepeated';
import { getAlarmException } from '../../../../lib/alarmexception';
import { alarmsRepeated } from '../../../../stores/alarmrepeated';
import { alarmsSingle } from '../../../../stores/alarmsingle';
import { alarmsExceptions } from '../../../../stores/alarmexceptions';
function slugToTitle(slug) {
switch(slug) {
case "manuals":
case "single":
return "Réveil manuel";
case "usuals":
case "repeated":
return "Réveil habituel";
case "excepts":
case "exceptions":
return "Exception";
}
}
let objP;
let obj;
$: {
switch ($page.params["kind"]) {
case "single":
objP = getAlarmSingle($page.params["aid"]);
break;
case "repeated":
objP = getAlarmRepeated($page.params["aid"]);
break;
case "exceptions":
objP = getAlarmException($page.params["aid"]);
break;
}
objP.then((o) => obj = o);
}
function editThis() {
goto('alarms/' + $page.params["kind"]);
}
function deleteThis() {
obj.delete().then(() => {
switch($page.params["kind"]) {
case "single":
alarmsSingle.clear();
break;
case "repeated":
alarmsRepeated.clear();
break;
case "exceptions":
alarmsExceptions.clear();
break;
}
goto('alarms/' + $page.params["kind"]);
})
}
</script>
<Container fluid class="flex-fill">
<h2 class="mb-0">
{slugToTitle($page.params["kind"])} du ...
</h2>
{#if $page.params["kind"] == "manuals"}
manuals
{:else if $page.params["kind"] == "usuals"}
ususlas
{:else if $page.params["kind"] == "excepts"}
excepts
{#if $page.params["kind"] == "single"}
{#await objP}
<div class="d-flex justify-content-center align-items-center gap-2">
<Spinner color="primary" /> Chargement en cours&hellip;
</div>
{:then alarm}
<h2 class="mb-0">
{slugToTitle($page.params["kind"])} du <DateFormat date={alarm.time} dateStyle="long" />
</h2>
<ListGroup class="my-2">
<ListGroupItem>
<strong>Date du réveil</strong> <DateFormat date={alarm.time} dateStyle="long" />
</ListGroupItem>
<ListGroupItem>
<strong>Heure du réveil</strong> <DateFormat date={alarm.time} timeStyle="long" />
</ListGroupItem>
</ListGroup>
{/await}
{:else if $page.params["kind"] == "repeated"}
{#await objP}
<div class="d-flex justify-content-center align-items-center gap-2">
<Spinner color="primary" /> Chargement en cours&hellip;
</div>
{:then alarm}
<h2 class="mb-0">
{slugToTitle($page.params["kind"])} des {weekdayStr(alarm.weekday)} à {alarm.time}
</h2>
<ListGroup class="my-2">
<ListGroupItem>
<strong>Jour de la semaine</strong> {weekdayStr(alarm.weekday)}
</ListGroupItem>
<ListGroupItem>
<strong>Heure du réveil</strong> {alarm.time}
</ListGroupItem>
<ListGroupItem>
<strong>Ignorer les exceptions&nbsp;?</strong> {alarm.ignore_exceptions?"oui":"non"}
</ListGroupItem>
</ListGroup>
{/await}
{:else if $page.params["kind"] == "exceptions"}
{#await objP}
<div class="d-flex justify-content-center align-items-center gap-2">
<Spinner color="primary" /> Chargement en cours&hellip;
</div>
{:then exception}
<h2 class="mb-0">
{slugToTitle($page.params["kind"])} du <DateRangeFormat startDate={exception._start()} endDate={exception._end()} dateStyle="long" />
</h2>
Entre le <DateRangeFormat startDate={exception._start()} endDate={exception._end()} dateStyle="long" />
{/await}
{/if}
{#await objP then alarm}
<ListGroup class="my-2 text-center">
<ListGroupItem
action
tag="button"
class="text-info fw-bold"
on:click={editThis}
>
<Icon name="pencil" />
Éditer ce {slugToTitle($page.params["kind"]).toLowerCase()}
</ListGroupItem>
<ListGroupItem
action
tag="button"
class="text-danger fw-bold"
on:click={deleteThis}
>
<Icon name="trash" />
Supprimer ce {slugToTitle($page.params["kind"]).toLowerCase()}
</ListGroupItem>
</ListGroup>
{/await}
</Container>

View File

@ -1,100 +1,155 @@
<script>
import { goto } from '$app/navigation';
import { page } from '$app/stores';
import {
Button,
Col,
Container,
Form,
FormGroup,
Icon,
Input,
Label,
Row,
Icon,
Spinner,
} from 'sveltestrap';
import { page } from '$app/stores';
import DateTimeInput from '../../../../components/DateTimeInput.svelte';
import { AlarmSingle } from '../../../../lib/alarmsingle';
import { AlarmRepeated } from '../../../../lib/alarmrepeated';
import { AlarmException } from '../../../../lib/alarmexception';
import { alarmsRepeated } from '../../../../stores/alarmrepeated';
import { alarmsSingle } from '../../../../stores/alarmsingle';
import { alarmsExceptions } from '../../../../stores/alarmexceptions';
import { routines } from '../../../../stores/routines';
function slugToTitle(slug) {
switch(slug) {
case "manuals":
case "single":
return "Nouveau réveil manuel";
case "usuals":
case "repeated":
return "Nouveau réveil habituel";
case "excepts":
case "exceptions":
return "Nouvelle exception";
}
}
let obj;
switch($page.params["kind"]) {
case "single":
obj = new AlarmSingle();
break;
case "repeated":
obj = new AlarmRepeated();
break;
case "exceptions":
obj = new AlarmException();
break;
}
function submit() {
obj.save().then((res) => {
switch($page.params["kind"]) {
case "single":
alarmsSingle.clear();
break;
case "repeated":
alarmsRepeated.clear();
break;
case "exceptions":
alarmsExceptions.clear();
break;
}
goto('alarms/' + $page.params["kind"] + '/' + res.id);
});
}
</script>
<Container fluid class="flex-fill">
<Form>
<form on:submit|preventDefault={submit}>
<Button type="submit" color="link" class="d-block d-md-none float-end">
Ajouter
</Button>
<h2 class="mb-0">
{slugToTitle($page.params["kind"])}
</h2>
{#if $page.params["kind"] == "manuals"}
{#if $page.params["kind"] == "single"}
<FormGroup>
<Label for="exampleHour">Heure</Label>
<Input id="exampleHour" type="datetime-local" required />
<DateTimeInput id="exampleHour" required bind:date={obj.time} />
</FormGroup>
<FormGroup>
<Label for="routineSelect">Routines</Label>
<Input type="select" name="select" id="routineSelect">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</Input>
{#if $routines.list}
<Input type="select" id="routineSelect" placeholder="Choissez une nouvelle routine">
{#each $routines.list as routine (routine.id)}
<option value="{routine.id}">{routine.name}</option>
{/each}
</Input>
{:else}
{#await routines.refresh()}
<div class="d-flex justify-content-center align-items-center gap-2">
<Spinner color="primary" /> Chargement en cours&hellip;
</div>
{/await}
{/if}
</FormGroup>
{:else if $page.params["kind"] == "usuals"}
{:else if $page.params["kind"] == "repeated"}
<FormGroup>
<Label for="daySelect">Jour de la semaine</Label>
<Input type="select" id="daySelect" required>
<option value="1">Lundi</option>
<option value="2">Mardi</option>
<option value="3">Mercredi</option>
<option value="4">Jeudi</option>
<option value="5">Vendredi</option>
<option value="6">Samedi</option>
<option value="0">Dimanche</option>
<Input type="select" id="daySelect" required bind:value={obj.weekday}>
<option value={1}>Lundi</option>
<option value={2}>Mardi</option>
<option value={3}>Mercredi</option>
<option value={4}>Jeudi</option>
<option value={5}>Vendredi</option>
<option value={6}>Samedi</option>
<option value={0}>Dimanche</option>
</Input>
</FormGroup>
<FormGroup>
<Label for="exampleHour">Heure</Label>
<Input id="exampleHour" type="time" required />
<Input id="exampleHour" type="time" required bind:value={obj.time} />
</FormGroup>
<FormGroup>
<Label for="routineSelect">Routines</Label>
<Input type="select" id="routineSelect">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</Input>
{#if $routines.list}
<Input type="select" id="routineSelect" placeholder="Choissez une nouvelle routine">
{#each $routines.list as routine (routine.id)}
<option value="{routine.id}">{routine.name}</option>
{/each}
</Input>
{:else}
{#await routines.refresh()}
<div class="d-flex justify-content-center align-items-center gap-2">
<Spinner color="primary" /> Chargement en cours&hellip;
</div>
{/await}
{/if}
</FormGroup>
<FormGroup>
<Input id="ignoreExceptions" type="checkbox" label="Ignorer les exceptions" />
<Input id="ignoreExceptions" type="checkbox" label="Ignorer les exceptions" bind:checked={obj.ignore_exceptions} />
</FormGroup>
{:else if $page.params["kind"] == "excepts"}
{:else if $page.params["kind"] == "exceptions"}
<FormGroup>
<Label for="exceptionStart">Date de début</Label>
<Input id="exceptionStart" type="date" required />
<Input id="exceptionStart" type="date" required bind:value={obj.start} />
</FormGroup>
<FormGroup>
<Label for="exceptionEnd">Date de fin</Label>
<Input id="exceptionEnd" type="date" required />
<Input id="exceptionEnd" type="date" required bind:value={obj.end} />
</FormGroup>
{/if}
<Button type="submit" color="primary" class="d-none d-md-block">
Ajouter
</Button>
</Form>
</form>
</Container>

View File

@ -23,10 +23,6 @@ function createActionsStore() {
return list;
},
getActionByFilename: (fname) => {
},
update: (res_actions, cb=null) => {
if (res_actions.status === 200) {
res_actions.json().then((list) => {

View File

@ -0,0 +1,41 @@
import { derived, writable } from 'svelte/store';
import { getAlarmsException } from '../lib/alarmexception'
function createAlarmsExceptionStore() {
const { subscribe, set, update } = writable({list: null});
return {
subscribe,
set: (v) => {
update((m) => Object.assign(m, v));
},
clear: () => {
update((m) => m = {list: null});
},
refresh: async () => {
const list = await getAlarmsException();
update((m) => (Object.assign(m, {list})));
return list;
},
update: (res_AlarmsException, cb=null) => {
if (res_AlarmsException.status === 200) {
res_AlarmsException.json().then((list) => {
update((m) => (Object.assign(m, {list})));
if (cb) {
cb(list);
}
});
}
},
};
}
export const alarmsExceptions = createAlarmsExceptionStore();

View File

@ -0,0 +1,41 @@
import { derived, writable } from 'svelte/store';
import { getAlarmsRepeated } from '../lib/alarmrepeated'
function createAlarmsRepeatedStore() {
const { subscribe, set, update } = writable({list: null});
return {
subscribe,
set: (v) => {
update((m) => Object.assign(m, v));
},
clear: () => {
update((m) => m = {list: null});
},
refresh: async () => {
const list = await getAlarmsRepeated();
update((m) => (Object.assign(m, {list})));
return list;
},
update: (res_AlarmsRepeated, cb=null) => {
if (res_AlarmsRepeated.status === 200) {
res_AlarmsRepeated.json().then((list) => {
update((m) => (Object.assign(m, {list})));
if (cb) {
cb(list);
}
});
}
},
};
}
export const alarmsRepeated = createAlarmsRepeatedStore();

View File

@ -0,0 +1,41 @@
import { derived, writable } from 'svelte/store';
import { getAlarmsSingle } from '../lib/alarmsingle'
function createAlarmsSingleStore() {
const { subscribe, set, update } = writable({list: null});
return {
subscribe,
set: (v) => {
update((m) => Object.assign(m, v));
},
clear: () => {
update((m) => m = {list: null});
},
refresh: async () => {
const list = await getAlarmsSingle();
update((m) => (Object.assign(m, {list})));
return list;
},
update: (res_AlarmsSingle, cb=null) => {
if (res_AlarmsSingle.status === 200) {
res_AlarmsSingle.json().then((list) => {
update((m) => (Object.assign(m, {list})));
if (cb) {
cb(list);
}
});
}
},
};
}
export const alarmsSingle = createAlarmsSingleStore();