新增测点配置
This commit is contained in:
@@ -1,33 +1,96 @@
|
||||
package cron
|
||||
|
||||
import (
|
||||
"energy-management-system/utils"
|
||||
"energy-management-system/global"
|
||||
"energy-management-system/utils/exception"
|
||||
"energy-management-system/utils/recovery"
|
||||
"fmt"
|
||||
"github.com/robfig/cron/v3"
|
||||
"os"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"time"
|
||||
)
|
||||
|
||||
var servers server
|
||||
var funs customFunc
|
||||
var jobs customJob
|
||||
|
||||
type server struct {
|
||||
Cron []string
|
||||
Func []func()
|
||||
type customFunc struct {
|
||||
Specs []string
|
||||
Func []func()
|
||||
}
|
||||
|
||||
func (c *server) register(cron string, f func()) {
|
||||
c.Cron = append(c.Cron, cron)
|
||||
func (c *customFunc) register(spec string, f func()) {
|
||||
c.Specs = append(c.Specs, spec)
|
||||
c.Func = append(c.Func, f)
|
||||
}
|
||||
|
||||
func CronRun() {
|
||||
c := cron.New()
|
||||
for k, v := range servers.Cron {
|
||||
eid, err := c.AddFunc(v, wrap(servers.Func[k]))
|
||||
fmt.Println("定时任务:", eid, "添加成功")
|
||||
utils.Exit(err, "添加定时器错误:")
|
||||
// RegisterCronFunc 参数:规则(s m h d m w),函数
|
||||
func RegisterCronFunc(spec string, f func()) {
|
||||
funs.register(spec, f)
|
||||
}
|
||||
|
||||
func RegisterRuntimeCronFunc(spec string, f func()) {
|
||||
_, err := global.Cron.AddFunc(spec, wrap(f))
|
||||
if err != nil {
|
||||
exception.PEM(err, "添加定时func失败")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
type customJob struct {
|
||||
Specs []string
|
||||
Jobs []cron.Job
|
||||
}
|
||||
|
||||
func (c *customJob) register(spec string, f cron.Job) {
|
||||
c.Specs = append(c.Specs, spec)
|
||||
c.Jobs = append(c.Jobs, f)
|
||||
}
|
||||
|
||||
// RegisterCronJob 参数:规则(s m h d m w),函数
|
||||
func RegisterCronJob(spec string, job cron.Job) {
|
||||
jobs.register(spec, job)
|
||||
}
|
||||
func RegisterRuntimeCronJob(spec string, job cron.Job) {
|
||||
_, err := global.Cron.AddJob(spec, job)
|
||||
if err != nil {
|
||||
exception.PEM(err, "添加定时job失败")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func InitCron() {
|
||||
// 设置时区
|
||||
loc, _ := time.LoadLocation("Asia/Shanghai")
|
||||
|
||||
location := cron.WithLocation(loc)
|
||||
// 开启秒
|
||||
withSeconds := cron.WithSeconds()
|
||||
// 自定义规则
|
||||
withParser := cron.WithParser(cron.NewParser(cron.SecondOptional | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor))
|
||||
|
||||
// cron初始化
|
||||
c := cron.New(location, withSeconds, withParser)
|
||||
|
||||
// 初始化预定Job
|
||||
for idx, spec := range jobs.Specs {
|
||||
_, err := c.AddJob(spec, jobs.Jobs[idx])
|
||||
//_, err := c.AddFunc(v, wrap(servers.Func[k]))
|
||||
if err != nil {
|
||||
fmt.Printf("[-]注册初始定时job失败: %s \n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
// 初始化预定func
|
||||
for idx, spec := range funs.Specs {
|
||||
_, err := c.AddFunc(spec, wrap(funs.Func[idx]))
|
||||
if err != nil {
|
||||
fmt.Printf("[-]注册初始定时func失败: %s \n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
global.Cron = c
|
||||
c.Start()
|
||||
}
|
||||
|
||||
@@ -37,8 +100,3 @@ func wrap(f func()) func() {
|
||||
f()
|
||||
}
|
||||
}
|
||||
|
||||
// Register 参数:规则(s m h d m w),函数
|
||||
func Register(rule string, f func()) {
|
||||
servers.register(rule, f)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package gorm
|
||||
|
||||
import (
|
||||
"energy-management-system/global"
|
||||
"energy-management-system/model"
|
||||
"energy-management-system/model/init-db-data"
|
||||
peak_valley "energy-management-system/model/peak-valley"
|
||||
"fmt"
|
||||
@@ -66,6 +67,9 @@ func AutoMigrate(db *gorm.DB) {
|
||||
|
||||
new(peak_valley.PeakValleyQuarter),
|
||||
new(peak_valley.PeakValleyRule),
|
||||
|
||||
new(model.DevicePoint),
|
||||
new(model.PointName),
|
||||
//new(model.Role),
|
||||
//new(model.UserRole),
|
||||
//new(model.Api),
|
||||
|
||||
22
core/influxdb/influxdb.go
Normal file
22
core/influxdb/influxdb.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package influxdb
|
||||
|
||||
import (
|
||||
"energy-management-system/global"
|
||||
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
|
||||
)
|
||||
|
||||
func InitInFluxDb() {
|
||||
|
||||
// Create a new client using an InfluxDB server base URL and an authentication token
|
||||
client := influxdb2.NewClient(global.AppConf.InFluxDb.Host, global.AppConf.InFluxDb.Token)
|
||||
// Use blocking write client for writes to desired bucket
|
||||
|
||||
//获取非阻塞式写入对象
|
||||
//writeAPI = client.WriteAPI("my-org", "my-bucket")
|
||||
//writeAPI := client.WriteAPIBlocking("my-org", "my-bucket")
|
||||
|
||||
//获取阻塞式写入对象
|
||||
//writeBlockingAPI := client.WriteAPIBlocking("iuu", "iuu-bucket")
|
||||
|
||||
global.InFluxDb = client
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/gookit/goutil/dump"
|
||||
"github.com/spf13/viper"
|
||||
"os"
|
||||
)
|
||||
@@ -54,6 +55,6 @@ func InitViper(path ...string) {
|
||||
}
|
||||
fmt.Println("[+]加载配置文件完成")
|
||||
|
||||
//dump.P(global.AppConf)
|
||||
dump.P(global.AppConf)
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user