Skip to content

Commit

Permalink
Develop (#279)
Browse files Browse the repository at this point in the history
* technical debt
* added help notice for time trigger
* added help notice for system event trigger
* updated entity storage table
* add custom fields to entity storage table
* sort filter list
  • Loading branch information
e154 authored Jul 13, 2024
1 parent a0740f9 commit 20a7215
Show file tree
Hide file tree
Showing 25 changed files with 970 additions and 190 deletions.
17 changes: 16 additions & 1 deletion adaptors/entity_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type IEntityStorage interface {
List(ctx context.Context, limit, offset int64, orderBy, sort string,
entityIds []common.EntityId,
startDate, endDate *time.Time) (list []*m.EntityStorage, total int64, err error)
GetLastThreeById(ctx context.Context, entityId common.EntityId, id int64) (list []*m.EntityStorage, err error)
DeleteOldest(ctx context.Context, days int) (err error)
fromDb(dbVer *db.EntityStorage) (ver *m.EntityStorage)
toDb(ver *m.EntityStorage) (dbVer *db.EntityStorage)
Expand Down Expand Up @@ -72,7 +73,7 @@ func (n *EntityStorage) GetLastByEntityId(ctx context.Context, entityId common.E
return
}

// ListByEntityId ...
// List ...
func (n *EntityStorage) List(ctx context.Context, limit, offset int64, orderBy, sort string, entityIds []common.EntityId, startDate, endDate *time.Time) (list []*m.EntityStorage, total int64, err error) {
var dbList []*db.EntityStorage
if dbList, total, err = n.table.List(ctx, int(limit), int(offset), orderBy, sort, entityIds, startDate, endDate); err != nil {
Expand All @@ -86,6 +87,20 @@ func (n *EntityStorage) List(ctx context.Context, limit, offset int64, orderBy,
return
}

// GetLastThreeById ...
func (n *EntityStorage) GetLastThreeById(ctx context.Context, entityId common.EntityId, id int64) (list []*m.EntityStorage, err error) {
var dbList []*db.EntityStorage
if dbList, err = n.table.GetLastThreeById(ctx, entityId, id); err != nil {
return
}

list = make([]*m.EntityStorage, len(dbList))
for i, dbVer := range dbList {
list[i] = n.fromDb(dbVer)
}
return
}

// DeleteOldest ...
func (n *EntityStorage) DeleteOldest(ctx context.Context, days int) (err error) {
err = n.table.DeleteOldest(ctx, days)
Expand Down
18 changes: 18 additions & 0 deletions common/events/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ type EventGetLastState struct {
EntityId common.EntityId `json:"entity_id"`
}

// EventGetStateById ...
type EventGetStateById struct {
Common
EntityId common.EntityId `json:"entity_id"`
StorageId int64 `json:"storage_id"`
}

// EventStateById ...
type EventStateById struct {
UserID int64 `json:"user_id"`
SessionID string `json:"session_id"`
EntityId common.EntityId `json:"entity_id"`
StorageId int64 `json:"storage_id"`
PluginName string `json:"plugin_name"`
OldState EventEntityState `json:"old_state"`
NewState EventEntityState `json:"new_state"`
}

// EventCallEntityAction ...
type EventCallEntityAction struct {
PluginName *string `json:"plugin_name"`
Expand Down
40 changes: 35 additions & 5 deletions db/entity_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"context"
"encoding/json"
"fmt"
"strings"
"time"

"github.com/e154/smart-home/common/apperr"
Expand Down Expand Up @@ -79,6 +80,27 @@ func (n *EntityStorages) GetLastByEntityId(ctx context.Context, entityId common.
return
}

// GetLastThreeById ...
func (n *EntityStorages) GetLastThreeById(ctx context.Context, entityId common.EntityId, id int64) (list []*EntityStorage, err error) {
list = make([]*EntityStorage, 2)
err = n.Db.WithContext(ctx).Model(&EntityStorage{}).
Order("id desc").
Where("entity_id = ? and id <= ?", entityId, id).
Limit(3).
Find(&list).
Error

if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
err = errors.Wrap(apperr.ErrEntityNotFound, fmt.Sprintf("id \"%s\"", entityId))
return
}
err = errors.Wrap(apperr.ErrEntityStorageGet, err.Error())
return
}
return
}

// List ...
func (n *EntityStorages) List(ctx context.Context, limit, offset int, orderBy, sort string, entityIds []common.EntityId, startDate, endDate *time.Time) (list []*EntityStorage, total int64, err error) {

Expand All @@ -101,15 +123,23 @@ func (n *EntityStorages) List(ctx context.Context, limit, offset int, orderBy, s
}

list = make([]*EntityStorage, 0)
q = q.
Limit(limit).
Offset(offset)

if sort != "" && orderBy != "" {
q = q.
Order(fmt.Sprintf("%s %s", sort, orderBy))
switch sort {
case "id", "entity_id", "state", "created_at":
q = q.
Order(fmt.Sprintf("%s %s", sort, orderBy))
default:
sort = strings.ReplaceAll(sort, "attributes_", "")
q = q.
Order(fmt.Sprintf("attributes->>'%s' %s", sort, orderBy))
}
}

q = q.
Limit(limit).
Offset(offset)

err = q.
Find(&list).
Error
Expand Down
4 changes: 4 additions & 0 deletions models/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ type AttributeValue map[string]interface{}
// Attributes ...
type Attributes map[string]*Attribute

func NewAttributes() map[string]*Attribute {
return make(map[string]*Attribute)
}

// Serialize ...
func (a Attributes) Serialize() (to AttributeValue) {

Expand Down
3 changes: 3 additions & 0 deletions static_source/admin/src/components/CronFormHelper/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import CronFormHelper from './src/CronFormHelper.vue'

export {CronFormHelper}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<script setup lang="ts">
</script>

<template>
<div class="tip custom-block w-[100%]">
<div>
<div>The cron expression is made of six fields. Each field can have the following
values.
</div>
<table class="dYaBhr">
<thead>
<tr>
<th>*</th>
<th>*</th>
<th>*</th>
<th>*</th>
<th>*</th>
<th>*</th>
</tr>
</thead>
<tbody>
<tr>
<td>second (0-59)</td>
<td>minute (0-59)</td>
<td>hour (0 - 23)</td>
<td>day of the month (1 - 31)</td>
<td>month (1 - 12)</td>
<td>day of the week (0 - 6)</td>
</tr>
</tbody>
</table>
<div>Here are some examples for you.</div>
<table class="dYaBhr">
<thead>
<tr>
<th>Cron expression</th>
<th>Schedule</th>
</tr>
</thead>
<tbody>
<tr>
<td>* * * * * *</td>
<td>Every second</td>
</tr>
<tr>
<td>0 * * * * *</td>
<td>Every minute</td>
</tr>
<tr>
<td>0 0 * * * *</td>
<td>Every hour</td>
</tr>
<tr>
<td>0 0 0 * * *</td>
<td>Every day at 12:00 AM</td>
</tr>
<tr>
<td>0 0 0 * * FRI</td>
<td>At 12:00 AM, only on Friday</td>
</tr>
<tr>
<td>0 0 0 1 * *</td>
<td>At 12:00 AM, on day 1 of the month</td>
</tr>
</tbody>
</table>
</div>
</div>
</template>

<style lang="less" scoped>
.light {
.custom-block.tip {
padding: 8px 16px;
background-color: #409eff1a;
border-radius: 4px;
border-left: 5px solid var(--el-color-primary);
}
}

.dark {
.custom-block.tip {
padding: 8px 16px;
background-color: #409eff1a;
border-radius: 4px;
border-left: 5px solid var(--el-color-primary);
}
}

.dYaBhr thead th {
border: 1px solid ;
padding: 0 10px;
}

.dYaBhr tbody td {
border: 1px solid ;
padding: 0 10px;
}

</style>
4 changes: 4 additions & 0 deletions static_source/admin/src/components/Form/src/componentMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import {ActionSearch} from '@/components/ActionSearch'
import {ActionsSearch} from '@/components/ActionsSearch'
import {ScriptsSearch} from '@/components/ScriptsSearch'
import {ScriptFormHelper} from '@/components/ScriptFormHelper'
import {SystemEventsHelper} from '@/components/SystemEventsHelper'
import {CronFormHelper} from '@/components/CronFormHelper'
import {TagsSearch} from '@/components/TagsSearch'
import {ScriptSearch} from '@/components/ScriptSearch'
import {RoleSearch} from '@/components/RoleSearch'
Expand Down Expand Up @@ -67,6 +69,8 @@ const componentMap: Recordable<Component, ComponentName> = {
Scripts: ScriptsSearch,
Tags: TagsSearch,
ScriptHelper: ScriptFormHelper,
SystemEventsHelper: SystemEventsHelper,
CronHelper: CronFormHelper,
Entity: EntitySearch,
Entities: EntitiesSearch,
Plugin: PluginSearch,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import SystemEventsHelper from './src/SystemEventsHelper.vue'

export {SystemEventsHelper}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<script setup lang="ts">
</script>

<template>
<div class="tip custom-block w-[100%]">
<p><strong>System events list</strong></p>

<div><strong>tasks</strong></div>
event_task_completed
event_created_task_model
event_removed_task_model
event_updated_task_model
event_task_loaded
event_task_unloaded
event_trigger_completed
event_call_trigger
event_created_trigger_model
event_removed_trigger_model
event_updated_trigger_model
event_trigger_loaded
event_trigger_unloaded
event_action_completed
event_call_task_action
event_call_action
event_added_action_model
event_removed_action_model
event_updated_action_model
event_action_loaded
event_action_unloaded
event_added_condition_model
event_removed_condition_model
event_updated_condition_model

<div><strong>backup</strong></div>
event_created_backup
event_removed_backup
event_uploaded_backup
event_started_restore

<div><strong>entity</strong></div>
event_state_changed
event_last_state_changed
event_get_last_state
event_call_entity_action
event_call_scene
event_added_actor
event_created_entity_model
event_updated_entity_model
event_updated_metric
event_entity_unloaded
event_entity_loaded
event_entity_set_state

<div><strong>mqtt</strong></div>
event_mqtt_new_client

<div><strong>plugins</strong></div>
event_plugin_loaded
event_plugin_unloaded

<div><strong>scripts</strong></div>
event_created_script_model
event_updated_script_model
event_removed_script_model

<div><strong>services</strong></div>
event_service_started
event_service_stopped
event_service_restarted

<div><strong>users</strong></div>
event_update_user_location
event_direct_message
event_user_signed_in

<div><strong>variables</strong></div>
event_updated_variable_model
event_removed_variable_model

<div><strong>server version</strong></div>
event_server_version
event_get_server_version

<div><strong>zigbee2mqtt</strong></div>
event_created_zigbee2mqtt_model
event_updated_zigbee2mqtt_model
event_removed_zigbee2mqtt_model


</div>
</template>

<style lang="less" scoped>
.light {
.custom-block.tip {
padding: 8px 16px;
background-color: #409eff1a;
border-radius: 4px;
border-left: 5px solid var(--el-color-primary);
}
}

.dark {
.custom-block.tip {
padding: 8px 16px;
background-color: #409eff1a;
border-radius: 4px;
border-left: 5px solid var(--el-color-primary);
}
}

.dYaBhr thead th {
border: 1px solid ;
padding: 0 10px;
}

.dYaBhr tbody td {
border: 1px solid ;
padding: 0 10px;
}

</style>
Loading

0 comments on commit 20a7215

Please sign in to comment.