🎨 优化目录结构

This commit is contained in:
fumiama
2022-02-25 22:15:14 +08:00
parent 5ccf753af3
commit 0cfb2e4e06
101 changed files with 116 additions and 116 deletions

View File

@@ -0,0 +1,16 @@
package timer
import (
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message"
)
func (t *Timer) sendmsg(grp int64, ctx *zero.Ctx) {
ctx.Event = new(zero.Event)
ctx.Event.GroupID = grp
if t.URL == "" {
ctx.SendChain(atall, message.Text(t.Alert))
} else {
ctx.SendChain(atall, message.Text(t.Alert), message.Image(t.URL).Add("cache", "0"))
}
}

View File

@@ -0,0 +1,166 @@
package timer
import (
"crypto/md5"
"encoding/binary"
"fmt"
"strconv"
"strings"
"time"
"unicode"
"github.com/sirupsen/logrus"
"github.com/wdvxdr1123/ZeroBot/utils/helper"
)
// GetTimerInfo 获得标准化定时字符串
func (t *Timer) GetTimerInfo() string {
if t.Cron != "" {
return fmt.Sprintf("[%d]%s", t.GrpID, t.Cron)
}
return fmt.Sprintf("[%d]%d月%d日%d周%d:%d", t.GrpID, t.Month(), t.Day(), t.Week(), t.Hour(), t.Minute())
}
// GetTimerID 获得标准化 ID
func (t *Timer) GetTimerID() uint32 {
key := t.GetTimerInfo()
m := md5.Sum(helper.StringToBytes(key))
return binary.LittleEndian.Uint32(m[:4])
}
// GetFilledCronTimer 获得以cron填充好的ts
func GetFilledCronTimer(croncmd string, alert string, img string, botqq, gid int64) *Timer {
var t Timer
t.Alert = alert
t.Cron = croncmd
t.URL = img
t.SelfID = botqq
t.GrpID = gid
return &t
}
// GetFilledTimer 获得填充好的ts
func GetFilledTimer(dateStrs []string, botqq, grp int64, matchDateOnly bool) *Timer {
monthStr := []rune(dateStrs[1])
dayWeekStr := []rune(dateStrs[2])
hourStr := []rune(dateStrs[3])
minuteStr := []rune(dateStrs[4])
var t Timer
mon := time.Month(chineseNum2Int(monthStr))
if (mon != -1 && mon <= 0) || mon > 12 { // 月份非法
t.Alert = "月份非法!"
return &t
}
t.SetMonth(mon)
lenOfDW := len(dayWeekStr)
switch {
case lenOfDW == 4: // 包括末尾的"日"
dayWeekStr = []rune{dayWeekStr[0], dayWeekStr[2]} // 去除中间的十
d := chineseNum2Int(dayWeekStr)
if (d != -1 && d <= 0) || d > 31 { // 日期非法
t.Alert = "日期非法1"
return &t
}
t.SetDay(d)
case dayWeekStr[lenOfDW-1] == rune('日'): // xx日
dayWeekStr = dayWeekStr[:lenOfDW-1]
d := chineseNum2Int(dayWeekStr)
if (d != -1 && d <= 0) || d > 31 { // 日期非法
t.Alert = "日期非法2"
return &t
}
t.SetDay(d)
case dayWeekStr[0] == rune('每'): // 每周
t.SetWeek(-1)
default: // 周x
w := chineseNum2Int(dayWeekStr[1:])
if w == 7 { // 周天是0
w = 0
}
if w < 0 || w > 6 { // 星期非法
t.Alert = "星期非法!"
return &t
}
t.SetWeek(time.Weekday(w))
}
if len(hourStr) == 3 {
hourStr = []rune{hourStr[0], hourStr[2]} // 去除中间的十
}
h := chineseNum2Int(hourStr)
if h < -1 || h > 23 { // 小时非法
t.Alert = "小时非法!"
return &t
}
t.SetHour(h)
if len(minuteStr) == 3 {
minuteStr = []rune{minuteStr[0], minuteStr[2]} // 去除中间的十
}
min := chineseNum2Int(minuteStr)
if min < -1 || min > 59 { // 分钟非法
t.Alert = "分钟非法!"
return &t
}
t.SetMinute(min)
if !matchDateOnly {
urlStr := dateStrs[5]
if urlStr != "" { // 是图片url
t.URL = urlStr[3:] // utf-8下用为3字节
logrus.Println("[群管]" + t.URL)
if !strings.HasPrefix(t.URL, "http") {
t.URL = "illegal"
logrus.Println("[群管]url非法")
return &t
}
}
t.Alert = dateStrs[6]
t.SetEn(true)
}
t.SelfID = botqq
t.GrpID = grp
return &t
}
// chineseNum2Int 汉字数字转int仅支持-1099最多两位数其中"每"解释为-1"每二"为-2以此类推
func chineseNum2Int(rs []rune) int {
r := -1
l := len(rs)
mai := rune('每')
if unicode.IsDigit(rs[0]) { // 默认可能存在的第二位也为int
r, _ = strconv.Atoi(string(rs))
} else {
switch {
case rs[0] == mai:
if l == 2 {
r = -chineseChar2Int(rs[1])
}
case l == 1:
r = chineseChar2Int(rs[0])
default:
ten := chineseChar2Int(rs[0])
if ten != 10 {
ten *= 10
}
ge := chineseChar2Int(rs[1])
if ge == 10 {
ge = 0
}
r = ten + ge
}
}
return r
}
// chineseChar2Int 处理单个字符的映射0~10
func chineseChar2Int(c rune) int {
if c == rune('日') || c == rune('天') { // 周日/周天
return 7
}
match := []rune("零一二三四五六七八九十")
for i, m := range match {
if c == m {
return i
}
}
return 0
}

View File

@@ -0,0 +1,165 @@
package timer
import (
"time"
"github.com/sirupsen/logrus"
zero "github.com/wdvxdr1123/ZeroBot"
)
func firstWeek(date *time.Time, week time.Weekday) (d time.Time) {
d = date.AddDate(0, 0, 1-date.Day())
for d.Weekday() != week {
d = d.AddDate(0, 0, 1)
}
return
}
func (t *Timer) nextWakeTime() (date time.Time) {
date = time.Now()
m := t.Month()
d := t.Day()
h := t.Hour()
mn := t.Minute()
w := t.Week()
var unit time.Duration
logrus.Debugln("[timer] unit init:", unit)
if mn >= 0 {
switch {
case h < 0:
if unit <= time.Second {
unit = time.Hour
}
case d < 0 || w < 0:
if unit <= time.Second {
unit = time.Hour * 24
}
case d == 0 && w >= 0:
delta := time.Hour * 24 * time.Duration(int(w)-int(date.Weekday()))
if delta < 0 {
delta = time.Hour * 24 * 7
}
unit += delta
case m < 0:
unit = -1
}
} else {
unit = time.Minute
}
logrus.Debugln("[timer] unit:", unit)
stable := 0
if mn < 0 {
mn = date.Minute()
}
if h < 0 {
h = date.Hour()
} else {
stable |= 0x8
}
switch {
case d < 0:
d = date.Day()
case d > 0:
stable |= 0x4
default:
d = date.Day()
if w >= 0 {
stable |= 0x2
}
}
if m < 0 {
m = date.Month()
} else {
stable |= 0x1
}
switch stable {
case 0b0101:
if t.Day() != time.Now().Day() || t.Month() != time.Now().Month() {
h = 0
}
case 0b1001:
if t.Month() != time.Now().Month() {
d = 0
}
case 0b0001:
if t.Month() != time.Now().Month() {
d = 0
h = 0
}
}
logrus.Debugln("[timer] stable:", stable)
logrus.Debugln("[timer] m:", m, "d:", d, "h:", h, "mn:", mn, "w:", w)
date = time.Date(date.Year(), m, d, h, mn, date.Second(), date.Nanosecond(), date.Location())
logrus.Debugln("[timer] date original:", date)
if unit > 0 {
date = date.Add(unit)
}
logrus.Debugln("[timer] date after add:", date)
if time.Until(date) <= 0 {
if t.Month() < 0 {
if t.Day() > 0 || (t.Day() == 0 && t.Week() >= 0) {
date = date.AddDate(0, 1, 0)
} else if t.Day() < 0 || t.Week() < 0 {
if t.Hour() > 0 {
date = date.AddDate(0, 0, 1)
} else if t.Minute() > 0 {
date = date.Add(time.Hour)
}
}
} else {
date = date.AddDate(1, 0, 0)
}
}
logrus.Debugln("[timer] date after fix:", date)
if stable&0x8 != 0 && date.Hour() != h {
switch {
case stable&0x4 == 0:
date = date.AddDate(0, 0, 1).Add(-time.Hour)
case stable&0x2 == 0:
date = date.AddDate(0, 0, 7).Add(-time.Hour)
case stable*0x1 == 0:
date = date.AddDate(0, 1, 0).Add(-time.Hour)
default:
date = date.AddDate(1, 0, 0).Add(-time.Hour)
}
}
logrus.Debugln("[timer] date after s8:", date)
if stable&0x4 != 0 && date.Day() != d {
switch {
case stable*0x1 == 0:
date = date.AddDate(0, 1, -1)
default:
date = date.AddDate(1, 0, -1)
}
}
logrus.Debugln("[timer] date after s4:", date)
if stable&0x2 != 0 && date.Weekday() != w {
switch {
case stable*0x1 == 0:
date = date.AddDate(0, 1, 0)
default:
date = date.AddDate(1, 0, 0)
}
date = firstWeek(&date, w)
}
logrus.Debugln("[timer] date after s2:", date)
if time.Until(date) <= 0 {
date = time.Now().Add(time.Minute)
}
return date
}
func (t *Timer) judgeHM() {
if t.Hour() < 0 || t.Hour() == time.Now().Hour() {
if t.Minute() < 0 || t.Minute() == time.Now().Minute() {
if t.SelfID != 0 {
t.sendmsg(t.GrpID, zero.GetBot(t.SelfID))
} else {
zero.RangeBot(func(id int64, ctx *zero.Ctx) (_ bool) {
t.sendmsg(t.GrpID, ctx)
return
})
}
}
}
}

View File

@@ -0,0 +1,28 @@
package timer
import (
sql "github.com/FloatTech/sqlite"
)
// Timer 计时器
type Timer struct {
ID uint32 `db:"id"`
En1Month4Day5Week3Hour5Min6 int32 `db:"emdwhm"`
SelfID int64 `db:"sid"`
GrpID int64 `db:"gid"`
Alert string `db:"alert"`
Cron string `db:"cron"`
URL string `db:"url"`
}
// InsertInto 插入自身
func (t *Timer) InsertInto(db *sql.Sqlite) error {
return db.Insert("timer", t)
}
/*
func getTimerFrom(db *sql.Sqlite, id uint32) (t Timer, err error) {
err = db.Find("timer", &t, "where id = "+strconv.Itoa(int(id)))
return
}
*/

View File

@@ -0,0 +1,194 @@
// Package timer 群管定时器
package timer
import (
"strconv"
"strings"
"sync"
"time"
sql "github.com/FloatTech/sqlite"
"github.com/fumiama/cron"
"github.com/sirupsen/logrus"
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message"
)
// Clock 时钟
type Clock struct {
db *sql.Sqlite
timers *(map[uint32]*Timer)
timersmu sync.RWMutex
// cron 定时器
cron *cron.Cron
// entries key <-> cron
entries map[uint32]cron.EntryID
entmu sync.Mutex
}
var (
// @全体成员
atall = message.MessageSegment{
Type: "at",
Data: map[string]string{
"qq": "all",
},
}
)
// NewClock 添加一个新时钟
func NewClock(db *sql.Sqlite) (c Clock) {
c.cron = cron.New()
c.entries = make(map[uint32]cron.EntryID)
c.timers = &map[uint32]*Timer{}
c.loadTimers(db)
c.cron.Start()
return
}
// RegisterTimer 注册计时器
func (c *Clock) RegisterTimer(ts *Timer, save bool) bool {
var key uint32
if save {
key = ts.GetTimerID()
ts.ID = key
} else {
key = ts.ID
}
t, ok := c.GetTimer(key)
if t != ts && ok { // 避免重复注册定时器
t.SetEn(false)
}
logrus.Println("[群管]注册计时器", key)
if ts.Cron != "" {
var ctx *zero.Ctx
if ts.SelfID != 0 {
ctx = zero.GetBot(ts.SelfID)
} else {
zero.RangeBot(func(id int64, c *zero.Ctx) bool {
ctx = c
ts.SelfID = id
return false
})
}
eid, err := c.cron.AddFunc(ts.Cron, func() { ts.sendmsg(ts.GrpID, ctx) })
if err == nil {
c.entmu.Lock()
c.entries[key] = eid
c.entmu.Unlock()
if save {
err = c.AddTimerIntoDB(ts)
}
if err == nil {
err = c.AddTimerIntoMap(ts)
}
return err == nil
}
ts.Alert = err.Error()
} else {
if save {
_ = c.AddTimerIntoDB(ts)
}
_ = c.AddTimerIntoMap(ts)
for ts.En() {
nextdate := ts.nextWakeTime()
sleepsec := time.Until(nextdate)
logrus.Printf("[群管]计时器%08x将睡眠%ds", key, sleepsec/time.Second)
time.Sleep(sleepsec)
if ts.En() {
if ts.Month() < 0 || ts.Month() == time.Now().Month() {
if ts.Day() < 0 || ts.Day() == time.Now().Day() {
ts.judgeHM()
} else if ts.Day() == 0 {
if ts.Week() < 0 || ts.Week() == time.Now().Weekday() {
ts.judgeHM()
}
}
}
}
}
}
return false
}
// CancelTimer 取消计时器
func (c *Clock) CancelTimer(key uint32) bool {
t, ok := c.GetTimer(key)
if ok {
if t.Cron != "" {
c.entmu.Lock()
e := c.entries[key]
c.cron.Remove(e)
delete(c.entries, key)
c.entmu.Unlock()
} else {
t.SetEn(false)
}
c.timersmu.Lock()
delete(*c.timers, key) // 避免重复取消
e := c.db.Del("timer", "where id = "+strconv.Itoa(int(key)))
c.timersmu.Unlock()
return e == nil
}
return false
}
// ListTimers 列出本群所有计时器
func (c *Clock) ListTimers(grpID int64) []string {
// 数组默认长度为map长度,后面append时,不需要重新申请内存和拷贝,效率很高
if c.timers != nil {
c.timersmu.RLock()
keys := make([]string, 0, len(*c.timers))
for _, v := range *c.timers {
if v.GrpID == grpID {
k := v.GetTimerInfo()
start := strings.Index(k, "]")
msg := strings.ReplaceAll(k[start+1:]+"\n", "-1", "每")
msg = strings.ReplaceAll(msg, "月0日0周", "月周天")
msg = strings.ReplaceAll(msg, "月0日", "月")
msg = strings.ReplaceAll(msg, "日0周", "日")
keys = append(keys, msg)
}
}
c.timersmu.RUnlock()
return keys
}
return nil
}
// GetTimer 获得定时器
func (c *Clock) GetTimer(key uint32) (t *Timer, ok bool) {
c.timersmu.RLock()
t, ok = (*c.timers)[key]
c.timersmu.RUnlock()
return
}
// AddTimerIntoDB 添加定时器
func (c *Clock) AddTimerIntoDB(t *Timer) (err error) {
c.timersmu.Lock()
err = c.db.Insert("timer", t)
c.timersmu.Unlock()
return
}
// AddTimerIntoMap 添加定时器到缓存
func (c *Clock) AddTimerIntoMap(t *Timer) (err error) {
c.timersmu.Lock()
(*c.timers)[t.ID] = t
c.timersmu.Unlock()
return
}
func (c *Clock) loadTimers(db *sql.Sqlite) {
c.db = db
err := c.db.Create("timer", &Timer{})
if err == nil {
var t Timer
_ = c.db.FindFor("timer", &t, "", func() error {
tescape := t
go c.RegisterTimer(&tescape, false)
return nil
})
}
}

View File

@@ -0,0 +1,33 @@
package timer
import (
"testing"
"time"
sql "github.com/FloatTech/sqlite"
"github.com/sirupsen/logrus"
)
func TestNextWakeTime(t *testing.T) {
logrus.SetLevel(logrus.DebugLevel)
ts := &Timer{}
ts.SetMonth(-1)
ts.SetWeek(6)
ts.SetHour(16)
ts.SetMinute(30)
t1 := time.Until(ts.nextWakeTime())
if t1 < 0 {
t.Log(t1)
t.Fail()
}
t.Log(t1)
t.Fail()
}
func TestClock(t *testing.T) {
db := &sql.Sqlite{DBPath: "test.db"}
c := NewClock(db)
c.AddTimerIntoDB(GetFilledTimer([]string{"", "12", "-1", "12", "0", "", "test"}, 0, 0, false))
t.Log(c.ListTimers(0))
t.Fail()
}

View File

@@ -0,0 +1,87 @@
package timer
import "time"
// En isEnabled 1bit
func (t *Timer) En() (en bool) {
return t.En1Month4Day5Week3Hour5Min6&0x800000 != 0
}
// Month 4bits
func (t *Timer) Month() (mon time.Month) {
mon = time.Month((t.En1Month4Day5Week3Hour5Min6 & 0x780000) >> 19)
if mon == 0b1111 {
mon = -1
}
return
}
// Day 5bits
func (t *Timer) Day() (d int) {
d = int((t.En1Month4Day5Week3Hour5Min6 & 0x07c000) >> 14)
if d == 0b11111 {
d = -1
}
return
}
// Week 3bits
func (t *Timer) Week() (w time.Weekday) {
w = time.Weekday((t.En1Month4Day5Week3Hour5Min6 & 0x003800) >> 11)
if w == 0b111 {
w = -1
}
return
}
// Hour 5bits
func (t *Timer) Hour() (h int) {
h = int((t.En1Month4Day5Week3Hour5Min6 & 0x0007c0) >> 6)
if h == 0b11111 {
h = -1
}
return
}
// Minute 6bits
func (t *Timer) Minute() (min int) {
min = int(t.En1Month4Day5Week3Hour5Min6 & 0x00003f)
if min == 0b111111 {
min = -1
}
return
}
// SetEn ...
func (t *Timer) SetEn(en bool) {
if en {
t.En1Month4Day5Week3Hour5Min6 |= 0x800000
} else {
t.En1Month4Day5Week3Hour5Min6 &= 0x7fffff
}
}
// SetMonth ...
func (t *Timer) SetMonth(mon time.Month) {
t.En1Month4Day5Week3Hour5Min6 = ((int32(mon) << 19) & 0x780000) | (t.En1Month4Day5Week3Hour5Min6 & 0x87ffff)
}
// SetDay ...
func (t *Timer) SetDay(d int) {
t.En1Month4Day5Week3Hour5Min6 = ((int32(d) << 14) & 0x07c000) | (t.En1Month4Day5Week3Hour5Min6 & 0xf83fff)
}
// SetWeek ...
func (t *Timer) SetWeek(w time.Weekday) {
t.En1Month4Day5Week3Hour5Min6 = ((int32(w) << 11) & 0x003800) | (t.En1Month4Day5Week3Hour5Min6 & 0xffc7ff)
}
// SetHour ...
func (t *Timer) SetHour(h int) {
t.En1Month4Day5Week3Hour5Min6 = ((int32(h) << 6) & 0x0007c0) | (t.En1Month4Day5Week3Hour5Min6 & 0xfff83f)
}
// SetMinute ...
func (t *Timer) SetMinute(min int) {
t.En1Month4Day5Week3Hour5Min6 = (int32(min) & 0x00003f) | (t.En1Month4Day5Week3Hour5Min6 & 0xffffc0)
}