diff --git a/plugin_manager/manager.go b/plugin_manager/manager.go index d9f6d31f..bfb4dbb4 100644 --- a/plugin_manager/manager.go +++ b/plugin_manager/manager.go @@ -11,6 +11,7 @@ import ( "strings" "time" + "github.com/sirupsen/logrus" zero "github.com/wdvxdr1123/ZeroBot" "github.com/wdvxdr1123/ZeroBot/extension/rate" "github.com/wdvxdr1123/ZeroBot/message" @@ -256,11 +257,11 @@ func init() { // 插件主体 Handle(func(ctx *zero.Ctx) { dateStrs := ctx.State["regex_matched"].([]string) ts := timer.GetFilledTimer(dateStrs, ctx.Event.SelfID, false) - if ts.Enable { + if ts.En() { go clock.RegisterTimer(ts, ctx.Event.GroupID, true) ctx.SendChain(message.Text("记住了~")) } else { - ctx.SendChain(message.Text("参数非法!")) + ctx.SendChain(message.Text("参数非法:" + ts.Alert)) } }) // 定时 cron 提醒 @@ -268,17 +269,22 @@ func init() { // 插件主体 Handle(func(ctx *zero.Ctx) { dateStrs := ctx.State["regex_matched"].([]string) var url, alert string - if len(dateStrs) == 3 { - url = dateStrs[1] + switch len(dateStrs) { + case 4: + url = dateStrs[2] + alert = dateStrs[3] + case 3: alert = dateStrs[2] - } else { - alert = dateStrs[1] + default: + ctx.SendChain(message.Text("参数非法!")) + return } - ts := timer.GetFilledCronTimer(dateStrs[0], alert, url, ctx.Event.SelfID) + logrus.Debugln("[manager] cron:", dateStrs[1]) + ts := timer.GetFilledCronTimer(dateStrs[1], alert, url, ctx.Event.SelfID) if clock.RegisterTimer(ts, ctx.Event.GroupID, true) { ctx.SendChain(message.Text("记住了~")) } else { - ctx.SendChain(message.Text("参数非法!")) + ctx.SendChain(message.Text("参数非法:" + ts.Alert)) } }) // 取消定时 @@ -298,7 +304,7 @@ func init() { // 插件主体 zero.OnRegex(`^取消在"(.*)"的提醒`, zero.AdminPermission, zero.OnlyGroup).SetBlock(true).SetPriority(40). Handle(func(ctx *zero.Ctx) { dateStrs := ctx.State["regex_matched"].([]string) - ts := timer.Timer{Cron: dateStrs[0]} + ts := timer.Timer{Cron: dateStrs[1]} ti := ts.GetTimerInfo(ctx.Event.GroupID) ok := clock.CancelTimer(ti) if ok { diff --git a/plugin_manager/timer/migrate/main.go b/plugin_manager/timer/migrate/main.go index 8b7abf05..e657edda 100644 --- a/plugin_manager/timer/migrate/main.go +++ b/plugin_manager/timer/migrate/main.go @@ -4,6 +4,7 @@ import ( "fmt" io "io" "os" + "time" "github.com/FloatTech/ZeroBot-Plugin/utils/file" ) @@ -50,15 +51,17 @@ func main() { if loadTimers(os.Args[1]) { timersmapnew.Timers = make(map[string]*Timer) for s, t := range timersmap.Timers { - timersmapnew.Timers[s] = &Timer{ - Enable: t.Enable, - Alert: t.Alert, - Url: t.Url, - Month: t.Month, - Day: t.Day, - Hour: t.Hour, - Minute: t.Minute, + tm := &Timer{ + Alert: t.Alert, + Url: t.Url, } + tm.SetMonth(time.Month(t.Month)) + tm.SetDay(int(t.Day)) + tm.SetHour(int(t.Hour)) + tm.SetMinute(int(t.Minute)) + tm.SetWeek(time.Weekday(t.Week)) + tm.SetEn(t.Enable) + timersmapnew.Timers[s] = tm } saveTimers(os.Args[2]) } diff --git a/plugin_manager/timer/migrate/timer.pb.go b/plugin_manager/timer/migrate/timer.pb.go index 39d2055a..fc0ea1fd 100644 --- a/plugin_manager/timer/migrate/timer.pb.go +++ b/plugin_manager/timer/migrate/timer.pb.go @@ -21,19 +21,14 @@ var _ = math.Inf const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package type Timer struct { - Enable bool `protobuf:"varint,1,opt,name=enable,proto3" json:"enable,omitempty"` - Alert string `protobuf:"bytes,2,opt,name=alert,proto3" json:"alert,omitempty"` - Url string `protobuf:"bytes,4,opt,name=url,proto3" json:"url,omitempty"` - Month int32 `protobuf:"varint,8,opt,name=month,proto3" json:"month,omitempty"` - Day int32 `protobuf:"varint,16,opt,name=day,proto3" json:"day,omitempty"` - Week int32 `protobuf:"varint,32,opt,name=week,proto3" json:"week,omitempty"` - Hour int32 `protobuf:"varint,64,opt,name=hour,proto3" json:"hour,omitempty"` - Minute int32 `protobuf:"varint,128,opt,name=minute,proto3" json:"minute,omitempty"` - Selfid int64 `protobuf:"varint,129,opt,name=selfid,proto3" json:"selfid,omitempty"` - Cron string `protobuf:"bytes,130,opt,name=cron,proto3" json:"cron,omitempty"` - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` + Alert string `protobuf:"bytes,1,opt,name=alert,proto3" json:"alert,omitempty"` + Cron string `protobuf:"bytes,2,opt,name=cron,proto3" json:"cron,omitempty"` + En1Month4Day5Week3Hour5Min6 int32 `protobuf:"varint,4,opt,name=en1month4day5week3hour5min6,proto3" json:"en1month4day5week3hour5min6,omitempty"` + Selfid int64 `protobuf:"varint,8,opt,name=selfid,proto3" json:"selfid,omitempty"` + Url string `protobuf:"bytes,16,opt,name=url,proto3" json:"url,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` } func (m *Timer) Reset() { *m = Timer{} } @@ -69,13 +64,6 @@ func (m *Timer) XXX_DiscardUnknown() { var xxx_messageInfo_Timer proto.InternalMessageInfo -func (m *Timer) GetEnable() bool { - if m != nil { - return m.Enable - } - return false -} - func (m *Timer) GetAlert() string { if m != nil { return m.Alert @@ -83,44 +71,16 @@ func (m *Timer) GetAlert() string { return "" } -func (m *Timer) GetUrl() string { +func (m *Timer) GetCron() string { if m != nil { - return m.Url + return m.Cron } return "" } -func (m *Timer) GetMonth() int32 { +func (m *Timer) GetEn1Month4Day5Week3Hour5Min6() int32 { if m != nil { - return m.Month - } - return 0 -} - -func (m *Timer) GetDay() int32 { - if m != nil { - return m.Day - } - return 0 -} - -func (m *Timer) GetWeek() int32 { - if m != nil { - return m.Week - } - return 0 -} - -func (m *Timer) GetHour() int32 { - if m != nil { - return m.Hour - } - return 0 -} - -func (m *Timer) GetMinute() int32 { - if m != nil { - return m.Minute + return m.En1Month4Day5Week3Hour5Min6 } return 0 } @@ -132,9 +92,9 @@ func (m *Timer) GetSelfid() int64 { return 0 } -func (m *Timer) GetCron() string { +func (m *Timer) GetUrl() string { if m != nil { - return m.Cron + return m.Url } return "" } @@ -195,26 +155,23 @@ func init() { func init() { proto.RegisterFile("timer.proto", fileDescriptor_ad0307ee16b652d2) } var fileDescriptor_ad0307ee16b652d2 = []byte{ - // 293 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x4c, 0x91, 0xcf, 0x4a, 0xc3, 0x40, - 0x10, 0xc6, 0x9d, 0xb6, 0x09, 0xed, 0xc4, 0x43, 0x58, 0x45, 0x07, 0x91, 0x10, 0x72, 0xca, 0xa9, - 0x87, 0xea, 0x41, 0x3c, 0x89, 0x20, 0x9e, 0xbc, 0x2c, 0xbe, 0x40, 0x6a, 0x57, 0x1a, 0x9a, 0x3f, - 0x65, 0xb3, 0x51, 0x72, 0x53, 0x8f, 0x3e, 0x81, 0x8f, 0xe4, 0xd1, 0x27, 0x10, 0x89, 0x2f, 0x22, - 0x3b, 0x1b, 0xa4, 0xb7, 0xef, 0xf7, 0x9b, 0x8f, 0x24, 0x33, 0xc1, 0xc0, 0xe4, 0xa5, 0xd2, 0xf3, - 0xad, 0xae, 0x4d, 0x2d, 0x3c, 0x86, 0xe4, 0x1b, 0xd0, 0xbb, 0xb7, 0x49, 0x1c, 0xa1, 0xaf, 0xaa, - 0x6c, 0x59, 0x28, 0x82, 0x18, 0xd2, 0xa9, 0x1c, 0x48, 0x1c, 0xa2, 0x97, 0x15, 0x4a, 0x1b, 0x1a, - 0xc5, 0x90, 0xce, 0xa4, 0x03, 0x11, 0xe2, 0xb8, 0xd5, 0x05, 0x4d, 0xd8, 0xd9, 0x68, 0x7b, 0x65, - 0x5d, 0x99, 0x35, 0x4d, 0x63, 0x48, 0x3d, 0xe9, 0xc0, 0xf6, 0x56, 0x59, 0x47, 0x21, 0x3b, 0x1b, - 0x85, 0xc0, 0xc9, 0xb3, 0x52, 0x1b, 0x8a, 0x59, 0x71, 0xb6, 0x6e, 0x5d, 0xb7, 0x9a, 0xae, 0x9c, - 0xb3, 0x59, 0x1c, 0xa3, 0x5f, 0xe6, 0x55, 0x6b, 0x14, 0xbd, 0x00, 0xeb, 0x01, 0xed, 0xa0, 0x51, - 0xc5, 0x63, 0xbe, 0xa2, 0x57, 0x3b, 0x18, 0xcb, 0x01, 0xc5, 0x01, 0x4e, 0x1e, 0x74, 0x5d, 0xd1, - 0x1b, 0xf0, 0x57, 0x31, 0x24, 0xef, 0x80, 0x33, 0x5e, 0xb0, 0xb9, 0xcb, 0xb6, 0xe2, 0x1c, 0x7d, - 0xde, 0xbb, 0x21, 0x88, 0xc7, 0x69, 0xb0, 0x38, 0x9d, 0xbb, 0x9b, 0xfc, 0x37, 0x86, 0x74, 0x53, - 0x19, 0xdd, 0xc9, 0xa1, 0x7b, 0x72, 0x8b, 0xc1, 0x8e, 0xb6, 0x3b, 0x6d, 0x54, 0x47, 0xee, 0x2d, - 0x36, 0x8a, 0x04, 0xbd, 0xa7, 0xac, 0x68, 0x15, 0xdf, 0x28, 0x58, 0xec, 0xef, 0x3e, 0x55, 0xba, - 0xd1, 0xe5, 0xe8, 0x02, 0xae, 0xc3, 0xcf, 0x3e, 0x82, 0xaf, 0x3e, 0x82, 0x9f, 0x3e, 0x82, 0x8f, - 0xdf, 0x68, 0x6f, 0xe9, 0xf3, 0xdf, 0x38, 0xfb, 0x0b, 0x00, 0x00, 0xff, 0xff, 0x04, 0x03, 0x99, - 0x28, 0x9c, 0x01, 0x00, 0x00, + // 256 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x2e, 0xc9, 0xcc, 0x4d, + 0x2d, 0xd2, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x05, 0x73, 0x94, 0xe6, 0x32, 0x72, 0xb1, + 0x86, 0x80, 0x58, 0x42, 0x22, 0x5c, 0xac, 0x89, 0x39, 0xa9, 0x45, 0x25, 0x12, 0x8c, 0x0a, 0x8c, + 0x1a, 0x9c, 0x41, 0x10, 0x8e, 0x90, 0x10, 0x17, 0x4b, 0x72, 0x51, 0x7e, 0x9e, 0x04, 0x13, 0x58, + 0x10, 0xcc, 0x16, 0x72, 0xe0, 0x92, 0x4e, 0xcd, 0x33, 0xcc, 0xcd, 0xcf, 0x2b, 0xc9, 0x30, 0x49, + 0x49, 0xac, 0x34, 0x2d, 0x4f, 0x4d, 0xcd, 0x36, 0xce, 0xc8, 0x2f, 0x2d, 0x32, 0xcd, 0xcd, 0xcc, + 0x33, 0x93, 0x60, 0x51, 0x60, 0xd4, 0x60, 0x0d, 0xc2, 0xa7, 0x44, 0x48, 0x8c, 0x8b, 0xad, 0x38, + 0x35, 0x27, 0x2d, 0x33, 0x45, 0x82, 0x43, 0x81, 0x51, 0x83, 0x39, 0x08, 0xca, 0x13, 0x12, 0xe0, + 0x62, 0x2e, 0x2d, 0xca, 0x91, 0x10, 0x00, 0x5b, 0x06, 0x62, 0x2a, 0x75, 0x31, 0x72, 0x71, 0x82, + 0xdd, 0x57, 0xec, 0x9b, 0x58, 0x20, 0x64, 0xc2, 0xc5, 0x06, 0x76, 0x76, 0xb1, 0x04, 0xa3, 0x02, + 0xb3, 0x06, 0xb7, 0x91, 0x8c, 0x1e, 0xc4, 0x4b, 0x70, 0x15, 0x50, 0x96, 0x6b, 0x5e, 0x49, 0x51, + 0x65, 0x10, 0x54, 0xad, 0x94, 0x3b, 0x17, 0x37, 0x92, 0x30, 0xc8, 0x92, 0xec, 0xd4, 0x4a, 0xa8, + 0x37, 0x41, 0x4c, 0x21, 0x25, 0x2e, 0xd6, 0xb2, 0xc4, 0x9c, 0xd2, 0x54, 0xb0, 0x2f, 0xb9, 0x8d, + 0x78, 0x90, 0x4d, 0x0d, 0x82, 0x48, 0x59, 0x31, 0x59, 0x30, 0x3a, 0x09, 0x9c, 0x78, 0x24, 0xc7, + 0x78, 0xe1, 0x91, 0x1c, 0xe3, 0x83, 0x47, 0x72, 0x8c, 0x33, 0x1e, 0xcb, 0x31, 0x24, 0xb1, 0x81, + 0x03, 0xd3, 0x18, 0x10, 0x00, 0x00, 0xff, 0xff, 0xf5, 0xc1, 0x9e, 0x44, 0x5b, 0x01, 0x00, 0x00, } func (m *Timer) Marshal() (dAtA []byte, err error) { @@ -241,78 +198,38 @@ func (m *Timer) MarshalToSizedBuffer(dAtA []byte) (int, error) { i -= len(m.XXX_unrecognized) copy(dAtA[i:], m.XXX_unrecognized) } - if len(m.Cron) > 0 { - i -= len(m.Cron) - copy(dAtA[i:], m.Cron) - i = encodeVarintTimer(dAtA, i, uint64(len(m.Cron))) - i-- - dAtA[i] = 0x8 - i-- - dAtA[i] = 0x92 - } - if m.Selfid != 0 { - i = encodeVarintTimer(dAtA, i, uint64(m.Selfid)) - i-- - dAtA[i] = 0x8 - i-- - dAtA[i] = 0x88 - } - if m.Minute != 0 { - i = encodeVarintTimer(dAtA, i, uint64(m.Minute)) - i-- - dAtA[i] = 0x8 - i-- - dAtA[i] = 0x80 - } - if m.Hour != 0 { - i = encodeVarintTimer(dAtA, i, uint64(m.Hour)) - i-- - dAtA[i] = 0x4 - i-- - dAtA[i] = 0x80 - } - if m.Week != 0 { - i = encodeVarintTimer(dAtA, i, uint64(m.Week)) - i-- - dAtA[i] = 0x2 - i-- - dAtA[i] = 0x80 - } - if m.Day != 0 { - i = encodeVarintTimer(dAtA, i, uint64(m.Day)) - i-- - dAtA[i] = 0x1 - i-- - dAtA[i] = 0x80 - } - if m.Month != 0 { - i = encodeVarintTimer(dAtA, i, uint64(m.Month)) - i-- - dAtA[i] = 0x40 - } if len(m.Url) > 0 { i -= len(m.Url) copy(dAtA[i:], m.Url) i = encodeVarintTimer(dAtA, i, uint64(len(m.Url))) i-- - dAtA[i] = 0x22 + dAtA[i] = 0x1 + i-- + dAtA[i] = 0x82 + } + if m.Selfid != 0 { + i = encodeVarintTimer(dAtA, i, uint64(m.Selfid)) + i-- + dAtA[i] = 0x40 + } + if m.En1Month4Day5Week3Hour5Min6 != 0 { + i = encodeVarintTimer(dAtA, i, uint64(m.En1Month4Day5Week3Hour5Min6)) + i-- + dAtA[i] = 0x20 + } + if len(m.Cron) > 0 { + i -= len(m.Cron) + copy(dAtA[i:], m.Cron) + i = encodeVarintTimer(dAtA, i, uint64(len(m.Cron))) + i-- + dAtA[i] = 0x12 } if len(m.Alert) > 0 { i -= len(m.Alert) copy(dAtA[i:], m.Alert) i = encodeVarintTimer(dAtA, i, uint64(len(m.Alert))) i-- - dAtA[i] = 0x12 - } - if m.Enable { - i-- - if m.Enable { - dAtA[i] = 1 - } else { - dAtA[i] = 0 - } - i-- - dAtA[i] = 0x8 + dAtA[i] = 0xa } return len(dAtA) - i, nil } @@ -387,36 +304,21 @@ func (m *Timer) Size() (n int) { } var l int _ = l - if m.Enable { - n += 2 - } l = len(m.Alert) if l > 0 { n += 1 + l + sovTimer(uint64(l)) } - l = len(m.Url) + l = len(m.Cron) if l > 0 { n += 1 + l + sovTimer(uint64(l)) } - if m.Month != 0 { - n += 1 + sovTimer(uint64(m.Month)) - } - if m.Day != 0 { - n += 2 + sovTimer(uint64(m.Day)) - } - if m.Week != 0 { - n += 2 + sovTimer(uint64(m.Week)) - } - if m.Hour != 0 { - n += 2 + sovTimer(uint64(m.Hour)) - } - if m.Minute != 0 { - n += 2 + sovTimer(uint64(m.Minute)) + if m.En1Month4Day5Week3Hour5Min6 != 0 { + n += 1 + sovTimer(uint64(m.En1Month4Day5Week3Hour5Min6)) } if m.Selfid != 0 { - n += 2 + sovTimer(uint64(m.Selfid)) + n += 1 + sovTimer(uint64(m.Selfid)) } - l = len(m.Cron) + l = len(m.Url) if l > 0 { n += 2 + l + sovTimer(uint64(l)) } @@ -487,26 +389,6 @@ func (m *Timer) Unmarshal(dAtA []byte) error { } switch fieldNum { case 1: - if wireType != 0 { - return fmt.Errorf("proto: wrong wireType = %d for field Enable", wireType) - } - var v int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTimer - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - v |= int(b&0x7F) << shift - if b < 0x80 { - break - } - } - m.Enable = bool(v != 0) - case 2: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field Alert", wireType) } @@ -538,153 +420,7 @@ func (m *Timer) Unmarshal(dAtA []byte) error { } m.Alert = string(dAtA[iNdEx:postIndex]) iNdEx = postIndex - case 4: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Url", wireType) - } - var stringLen uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTimer - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - stringLen |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - intStringLen := int(stringLen) - if intStringLen < 0 { - return ErrInvalidLengthTimer - } - postIndex := iNdEx + intStringLen - if postIndex < 0 { - return ErrInvalidLengthTimer - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.Url = string(dAtA[iNdEx:postIndex]) - iNdEx = postIndex - case 8: - if wireType != 0 { - return fmt.Errorf("proto: wrong wireType = %d for field Month", wireType) - } - m.Month = 0 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTimer - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - m.Month |= int32(b&0x7F) << shift - if b < 0x80 { - break - } - } - case 16: - if wireType != 0 { - return fmt.Errorf("proto: wrong wireType = %d for field Day", wireType) - } - m.Day = 0 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTimer - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - m.Day |= int32(b&0x7F) << shift - if b < 0x80 { - break - } - } - case 32: - if wireType != 0 { - return fmt.Errorf("proto: wrong wireType = %d for field Week", wireType) - } - m.Week = 0 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTimer - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - m.Week |= int32(b&0x7F) << shift - if b < 0x80 { - break - } - } - case 64: - if wireType != 0 { - return fmt.Errorf("proto: wrong wireType = %d for field Hour", wireType) - } - m.Hour = 0 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTimer - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - m.Hour |= int32(b&0x7F) << shift - if b < 0x80 { - break - } - } - case 128: - if wireType != 0 { - return fmt.Errorf("proto: wrong wireType = %d for field Minute", wireType) - } - m.Minute = 0 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTimer - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - m.Minute |= int32(b&0x7F) << shift - if b < 0x80 { - break - } - } - case 129: - if wireType != 0 { - return fmt.Errorf("proto: wrong wireType = %d for field Selfid", wireType) - } - m.Selfid = 0 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTimer - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - m.Selfid |= int64(b&0x7F) << shift - if b < 0x80 { - break - } - } - case 130: + case 2: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field Cron", wireType) } @@ -716,6 +452,76 @@ func (m *Timer) Unmarshal(dAtA []byte) error { } m.Cron = string(dAtA[iNdEx:postIndex]) iNdEx = postIndex + case 4: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field En1Month4Day5Week3Hour5Min6", wireType) + } + m.En1Month4Day5Week3Hour5Min6 = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTimer + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.En1Month4Day5Week3Hour5Min6 |= int32(b&0x7F) << shift + if b < 0x80 { + break + } + } + case 8: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field Selfid", wireType) + } + m.Selfid = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTimer + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.Selfid |= int64(b&0x7F) << shift + if b < 0x80 { + break + } + } + case 16: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Url", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTimer + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthTimer + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthTimer + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Url = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipTimer(dAtA[iNdEx:]) diff --git a/plugin_manager/timer/migrate/wrap.go b/plugin_manager/timer/migrate/wrap.go new file mode 100644 index 00000000..441e7e20 --- /dev/null +++ b/plugin_manager/timer/migrate/wrap.go @@ -0,0 +1,75 @@ +package main + +import "time" + +func (m *Timer) En() (en bool) { + return m.En1Month4Day5Week3Hour5Min6&0x800000 != 0 +} + +func (m *Timer) Month() (mon time.Month) { + mon = time.Month((m.En1Month4Day5Week3Hour5Min6 & 0x780000) >> 19) + if mon == 0b1111 { + mon = -1 + } + return +} + +func (m *Timer) Day() (d int) { + d = int((m.En1Month4Day5Week3Hour5Min6 & 0x07c000) >> 14) + if d == 0b11111 { + d = -1 + } + return +} + +func (m *Timer) Week() (w time.Weekday) { + w = time.Weekday((m.En1Month4Day5Week3Hour5Min6 & 0x003800) >> 11) + if w == 0b111 { + w = -1 + } + return +} + +func (m *Timer) Hour() (h int) { + h = int((m.En1Month4Day5Week3Hour5Min6 & 0x0007c0) >> 6) + if h == 0b11111 { + h = -1 + } + return +} + +func (m *Timer) Minute() (min int) { + min = int(m.En1Month4Day5Week3Hour5Min6 & 0x00003f) + if min == 0b111111 { + min = -1 + } + return +} + +func (m *Timer) SetEn(en bool) { + if en { + m.En1Month4Day5Week3Hour5Min6 |= 0x800000 + } else { + m.En1Month4Day5Week3Hour5Min6 &= 0x7fffff + } +} + +func (m *Timer) SetMonth(mon time.Month) { + m.En1Month4Day5Week3Hour5Min6 = ((int32(mon) << 19) & 0x780000) | (m.En1Month4Day5Week3Hour5Min6 & 0x87ffff) +} + +func (m *Timer) SetDay(d int) { + m.En1Month4Day5Week3Hour5Min6 = ((int32(d) << 14) & 0x07c000) | (m.En1Month4Day5Week3Hour5Min6 & 0xf83fff) +} + +func (m *Timer) SetWeek(w time.Weekday) { + m.En1Month4Day5Week3Hour5Min6 = ((int32(w) << 11) & 0x003800) | (m.En1Month4Day5Week3Hour5Min6 & 0xffc7ff) +} + +func (m *Timer) SetHour(h int) { + m.En1Month4Day5Week3Hour5Min6 = ((int32(h) << 6) & 0x0007c0) | (m.En1Month4Day5Week3Hour5Min6 & 0xfff83f) +} + +func (m *Timer) SetMinute(min int) { + m.En1Month4Day5Week3Hour5Min6 = (int32(min) & 0x00003f) | (m.En1Month4Day5Week3Hour5Min6 & 0xffffc0) +} diff --git a/plugin_manager/timer/msg.go b/plugin_manager/timer/msg.go index 8058a98a..cd4d19ac 100644 --- a/plugin_manager/timer/msg.go +++ b/plugin_manager/timer/msg.go @@ -13,5 +13,4 @@ func (ts *Timer) sendmsg(grp int64, ctx *zero.Ctx) { } else { ctx.SendChain(atall, message.Text(ts.Alert), message.Image(ts.Url).Add("cache", "0")) } - return } diff --git a/plugin_manager/timer/parse.go b/plugin_manager/timer/parse.go index 81975f1d..83986817 100644 --- a/plugin_manager/timer/parse.go +++ b/plugin_manager/timer/parse.go @@ -4,6 +4,7 @@ import ( "fmt" "strconv" "strings" + "time" "unicode" "github.com/sirupsen/logrus" @@ -14,7 +15,7 @@ func (ts *Timer) GetTimerInfo(grp int64) string { if ts.Cron != "" { return fmt.Sprintf("[%d]%s", grp, ts.Cron) } - return fmt.Sprintf("[%d]%d月%d日%d周%d:%d", grp, ts.Month, ts.Day, ts.Week, ts.Hour, ts.Minute) + return fmt.Sprintf("[%d]%d月%d日%d周%d:%d", grp, ts.Month(), ts.Day(), ts.Week(), ts.Hour(), ts.Minute()) } // GetFilledCronTimer 获得以cron填充好的ts @@ -35,55 +36,60 @@ func GetFilledTimer(dateStrs []string, botqq int64, matchDateOnly bool) *Timer { minuteStr := []rune(dateStrs[4]) var ts Timer - ts.Month = chineseNum2Int(monthStr) - if (ts.Month != -1 && ts.Month <= 0) || ts.Month > 12 { // 月份非法 - logrus.Println("[群管]月份非法!") + mon := time.Month(chineseNum2Int(monthStr)) + if (mon != -1 && mon <= 0) || mon > 12 { // 月份非法 + ts.Alert = "月份非法!" return &ts } + ts.SetMonth(mon) lenOfDW := len(dayWeekStr) if lenOfDW == 4 { // 包括末尾的"日" dayWeekStr = []rune{dayWeekStr[0], dayWeekStr[2]} // 去除中间的十 - ts.Day = chineseNum2Int(dayWeekStr) - if (ts.Day != -1 && ts.Day <= 0) || ts.Day > 31 { // 日期非法 - logrus.Println("[群管]日期非法1!") + d := chineseNum2Int(dayWeekStr) + if (d != -1 && d <= 0) || d > 31 { // 日期非法 + ts.Alert = "日期非法1!" return &ts } + ts.SetDay(d) } else if dayWeekStr[lenOfDW-1] == rune('日') { // xx日 dayWeekStr = dayWeekStr[:lenOfDW-1] - ts.Day = chineseNum2Int(dayWeekStr) - if (ts.Day != -1 && ts.Day <= 0) || ts.Day > 31 { // 日期非法 - logrus.Println("[群管]日期非法2!") + d := chineseNum2Int(dayWeekStr) + if (d != -1 && d <= 0) || d > 31 { // 日期非法 + ts.Alert = "日期非法2!" return &ts } + ts.SetDay(d) } else if dayWeekStr[0] == rune('每') { // 每周 - ts.Week = -1 + ts.SetWeek(-1) } else { // 周x - ts.Week = chineseNum2Int(dayWeekStr[1:]) - if ts.Week == 7 { // 周天是0 - ts.Week = 0 + w := chineseNum2Int(dayWeekStr[1:]) + if w == 7 { // 周天是0 + w = 0 } - if ts.Week < 0 || ts.Week > 6 { // 星期非法 - ts.Week = -11 - logrus.Println("[群管]星期非法!") + if w < 0 || w > 6 { // 星期非法 + ts.Alert = "星期非法!" return &ts } + ts.SetWeek(time.Weekday(w)) } if len(hourStr) == 3 { hourStr = []rune{hourStr[0], hourStr[2]} // 去除中间的十 } - ts.Hour = chineseNum2Int(hourStr) - if ts.Hour < -1 || ts.Hour > 23 { // 小时非法 - logrus.Println("[群管]小时非法!") + h := chineseNum2Int(hourStr) + if h < -1 || h > 23 { // 小时非法 + ts.Alert = "小时非法!" return &ts } + ts.SetHour(h) if len(minuteStr) == 3 { minuteStr = []rune{minuteStr[0], minuteStr[2]} // 去除中间的十 } - ts.Minute = chineseNum2Int(minuteStr) - if ts.Minute < -1 || ts.Minute > 59 { // 分钟非法 - logrus.Println("[群管]分钟非法!") + min := chineseNum2Int(minuteStr) + if min < -1 || min > 59 { // 分钟非法 + ts.Alert = "分钟非法!" return &ts } + ts.SetMinute(min) if !matchDateOnly { urlStr := dateStrs[5] if urlStr != "" { // 是图片url @@ -96,14 +102,14 @@ func GetFilledTimer(dateStrs []string, botqq int64, matchDateOnly bool) *Timer { } } ts.Alert = dateStrs[6] - ts.Enable = true + ts.SetEn(true) } ts.Selfid = botqq return &ts } // chineseNum2Int 汉字数字转int,仅支持-10~99,最多两位数,其中"每"解释为-1,"每二"为-2,以此类推 -func chineseNum2Int(rs []rune) int32 { +func chineseNum2Int(rs []rune) int { r := -1 l := len(rs) mai := rune('每') @@ -128,7 +134,7 @@ func chineseNum2Int(rs []rune) int32 { r = ten + ge } } - return int32(r) + return r } // chineseChar2Int 处理单个字符的映射0~10 diff --git a/plugin_manager/timer/sleep.go b/plugin_manager/timer/sleep.go index d6d2ac08..961cb362 100644 --- a/plugin_manager/timer/sleep.go +++ b/plugin_manager/timer/sleep.go @@ -17,12 +17,12 @@ func firstWeek(date *time.Time, week time.Weekday) (d time.Time) { func (ts *Timer) nextWakeTime() (date time.Time) { date = time.Now() - m := ts.Month - d := ts.Day - h := ts.Hour - mn := ts.Minute - w := ts.Week - unit := time.Duration(int(ts.Minute)-date.Minute()) * time.Minute + m := ts.Month() + d := ts.Day() + h := ts.Hour() + mn := ts.Minute() + w := ts.Week() + unit := time.Duration(ts.Minute()-date.Minute()) * time.Minute logrus.Debugln("[timer] unit init:", unit) if mn >= 0 { switch { @@ -49,39 +49,39 @@ func (ts *Timer) nextWakeTime() (date time.Time) { logrus.Debugln("[timer] unit:", unit) stable := 0 if mn < 0 { - mn = int32(date.Minute()) + mn = date.Minute() } if h < 0 { - h = int32(date.Hour()) + h = date.Hour() } else { stable |= 0x8 } if d < 0 { - d = int32(date.Day()) + d = date.Day() } else if d > 0 { stable |= 0x4 } else { - d = int32(date.Day()) + d = date.Day() if w >= 0 { stable |= 0x2 } } if m < 0 { - m = int32(date.Month()) + m = date.Month() } else { stable |= 0x1 } switch stable { case 0b0101: - if ts.Day != int32(time.Now().Day()) || ts.Month != int32(time.Now().Month()) { + if ts.Day() != time.Now().Day() || ts.Month() != time.Now().Month() { h = 0 } case 0b1001: - if ts.Month != int32(time.Now().Month()) { + if ts.Month() != time.Now().Month() { d = 0 } case 0b0001: - if ts.Month != int32(time.Now().Month()) { + if ts.Month() != time.Now().Month() { d = 0 h = 0 } @@ -95,13 +95,13 @@ func (ts *Timer) nextWakeTime() (date time.Time) { } logrus.Debugln("[timer] date after add:", date) if time.Until(date) <= 0 { - if ts.Month < 0 { - if ts.Day > 0 || (ts.Day == 0 && ts.Week >= 0) { + if ts.Month() < 0 { + if ts.Day() > 0 || (ts.Day() == 0 && ts.Week() >= 0) { date = date.AddDate(0, 1, 0) - } else if ts.Day < 0 || ts.Week < 0 { - if ts.Hour > 0 { + } else if ts.Day() < 0 || ts.Week() < 0 { + if ts.Hour() > 0 { date = date.AddDate(0, 0, 1) - } else if ts.Minute > 0 { + } else if ts.Minute() > 0 { date = date.Add(time.Hour) } } @@ -132,7 +132,7 @@ func (ts *Timer) nextWakeTime() (date time.Time) { } } logrus.Debugln("[timer] date after s4:", date) - if stable&0x2 != 0 && int32(date.Weekday()) != w { + if stable&0x2 != 0 && date.Weekday() != w { switch { case stable*0x1 == 0: date = date.AddDate(0, 1, 0) @@ -149,8 +149,8 @@ func (ts *Timer) nextWakeTime() (date time.Time) { } func (ts *Timer) judgeHM(grp int64) { - if ts.Hour < 0 || ts.Hour == int32(time.Now().Hour()) { - if ts.Minute < 0 || ts.Minute == int32(time.Now().Minute()) { + if ts.Hour() < 0 || ts.Hour() == time.Now().Hour() { + if ts.Minute() < 0 || ts.Minute() == time.Now().Minute() { if ts.Selfid != 0 { ts.sendmsg(grp, zero.GetBot(ts.Selfid)) } else { diff --git a/plugin_manager/timer/timer.go b/plugin_manager/timer/timer.go index f0b54cb6..fbf58c4b 100644 --- a/plugin_manager/timer/timer.go +++ b/plugin_manager/timer/timer.go @@ -48,6 +48,7 @@ func NewClock(pbfile string) (c Clock) { c.pbfile = &pbfile c.cron = cron.New() c.entries = make(map[string]cron.EntryID) + c.cron.Start() return } @@ -56,15 +57,12 @@ func (c *Clock) RegisterTimer(ts *Timer, grp int64, save bool) bool { key := ts.GetTimerInfo(grp) t, ok := c.GetTimer(key) if t != ts && ok { // 避免重复注册定时器 - t.Enable = false + t.SetEn(false) } c.timersmu.Lock() (*c.timers)[key] = ts c.timersmu.Unlock() - if save { - c.SaveTimers() - } - logrus.Printf("[群管]注册计时器[%t]%s", ts.Enable, key) + logrus.Println("[群管]注册计时器", key) if ts.Cron != "" { var ctx *zero.Ctx if ts.Selfid != 0 { @@ -81,20 +79,27 @@ func (c *Clock) RegisterTimer(ts *Timer, grp int64, save bool) bool { c.entmu.Lock() c.entries[key] = eid c.entmu.Unlock() + if save { + c.SaveTimers() + } return true } + ts.Alert = err.Error() } else { - for ts.Enable { + if save { + c.SaveTimers() + } + for ts.En() { nextdate := ts.nextWakeTime() sleepsec := time.Until(nextdate) logrus.Printf("[群管]计时器%s将睡眠%ds", key, sleepsec/time.Second) time.Sleep(sleepsec) - if ts.Enable { - if ts.Month < 0 || ts.Month == int32(time.Now().Month()) { - if ts.Day < 0 || ts.Day == int32(time.Now().Day()) { + if ts.En() { + if ts.Month() < 0 || ts.Month() == time.Now().Month() { + if ts.Day() < 0 || ts.Day() == time.Now().Day() { ts.judgeHM(grp) - } else if ts.Day == 0 { - if ts.Week < 0 || ts.Week == int32(time.Now().Weekday()) { + } else if ts.Day() == 0 { + if ts.Week() < 0 || ts.Week() == time.Now().Weekday() { ts.judgeHM(grp) } } @@ -116,7 +121,7 @@ func (c *Clock) CancelTimer(key string) bool { delete(c.entries, key) c.entmu.Unlock() } else { - t.Enable = false + t.SetEn(false) } c.timersmu.Lock() delete(*c.timers, key) // 避免重复取消 diff --git a/plugin_manager/timer/timer.pb.go b/plugin_manager/timer/timer.pb.go index ac92beff..ab06211c 100644 --- a/plugin_manager/timer/timer.pb.go +++ b/plugin_manager/timer/timer.pb.go @@ -23,19 +23,14 @@ var _ = math.Inf const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package type Timer struct { - Enable bool `protobuf:"varint,1,opt,name=enable,proto3" json:"enable,omitempty"` - Alert string `protobuf:"bytes,2,opt,name=alert,proto3" json:"alert,omitempty"` - Url string `protobuf:"bytes,4,opt,name=url,proto3" json:"url,omitempty"` - Month int32 `protobuf:"varint,8,opt,name=month,proto3" json:"month,omitempty"` - Day int32 `protobuf:"varint,16,opt,name=day,proto3" json:"day,omitempty"` - Week int32 `protobuf:"varint,32,opt,name=week,proto3" json:"week,omitempty"` - Hour int32 `protobuf:"varint,64,opt,name=hour,proto3" json:"hour,omitempty"` - Minute int32 `protobuf:"varint,128,opt,name=minute,proto3" json:"minute,omitempty"` - Selfid int64 `protobuf:"varint,129,opt,name=selfid,proto3" json:"selfid,omitempty"` - Cron string `protobuf:"bytes,130,opt,name=cron,proto3" json:"cron,omitempty"` - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` + Alert string `protobuf:"bytes,1,opt,name=alert,proto3" json:"alert,omitempty"` + Cron string `protobuf:"bytes,2,opt,name=cron,proto3" json:"cron,omitempty"` + En1Month4Day5Week3Hour5Min6 int32 `protobuf:"varint,4,opt,name=en1month4day5week3hour5min6,proto3" json:"en1month4day5week3hour5min6,omitempty"` + Selfid int64 `protobuf:"varint,8,opt,name=selfid,proto3" json:"selfid,omitempty"` + Url string `protobuf:"bytes,16,opt,name=url,proto3" json:"url,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` } func (m *Timer) Reset() { *m = Timer{} } @@ -71,13 +66,6 @@ func (m *Timer) XXX_DiscardUnknown() { var xxx_messageInfo_Timer proto.InternalMessageInfo -func (m *Timer) GetEnable() bool { - if m != nil { - return m.Enable - } - return false -} - func (m *Timer) GetAlert() string { if m != nil { return m.Alert @@ -85,44 +73,16 @@ func (m *Timer) GetAlert() string { return "" } -func (m *Timer) GetUrl() string { +func (m *Timer) GetCron() string { if m != nil { - return m.Url + return m.Cron } return "" } -func (m *Timer) GetMonth() int32 { +func (m *Timer) GetEn1Month4Day5Week3Hour5Min6() int32 { if m != nil { - return m.Month - } - return 0 -} - -func (m *Timer) GetDay() int32 { - if m != nil { - return m.Day - } - return 0 -} - -func (m *Timer) GetWeek() int32 { - if m != nil { - return m.Week - } - return 0 -} - -func (m *Timer) GetHour() int32 { - if m != nil { - return m.Hour - } - return 0 -} - -func (m *Timer) GetMinute() int32 { - if m != nil { - return m.Minute + return m.En1Month4Day5Week3Hour5Min6 } return 0 } @@ -134,9 +94,9 @@ func (m *Timer) GetSelfid() int64 { return 0 } -func (m *Timer) GetCron() string { +func (m *Timer) GetUrl() string { if m != nil { - return m.Cron + return m.Url } return "" } @@ -197,26 +157,23 @@ func init() { func init() { proto.RegisterFile("timer.proto", fileDescriptor_ad0307ee16b652d2) } var fileDescriptor_ad0307ee16b652d2 = []byte{ - // 293 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x4c, 0x91, 0xcf, 0x4a, 0xc3, 0x40, - 0x10, 0xc6, 0x9d, 0xb6, 0x09, 0xed, 0xc4, 0x43, 0x58, 0x45, 0x07, 0x91, 0x10, 0x72, 0xca, 0xa9, - 0x87, 0xea, 0x41, 0x3c, 0x89, 0x20, 0x9e, 0xbc, 0x2c, 0xbe, 0x40, 0x6a, 0x57, 0x1a, 0x9a, 0x3f, - 0x65, 0xb3, 0x51, 0x72, 0x53, 0x8f, 0x3e, 0x81, 0x8f, 0xe4, 0xd1, 0x27, 0x10, 0x89, 0x2f, 0x22, - 0x3b, 0x1b, 0xa4, 0xb7, 0xef, 0xf7, 0x9b, 0x8f, 0x24, 0x33, 0xc1, 0xc0, 0xe4, 0xa5, 0xd2, 0xf3, - 0xad, 0xae, 0x4d, 0x2d, 0x3c, 0x86, 0xe4, 0x1b, 0xd0, 0xbb, 0xb7, 0x49, 0x1c, 0xa1, 0xaf, 0xaa, - 0x6c, 0x59, 0x28, 0x82, 0x18, 0xd2, 0xa9, 0x1c, 0x48, 0x1c, 0xa2, 0x97, 0x15, 0x4a, 0x1b, 0x1a, - 0xc5, 0x90, 0xce, 0xa4, 0x03, 0x11, 0xe2, 0xb8, 0xd5, 0x05, 0x4d, 0xd8, 0xd9, 0x68, 0x7b, 0x65, - 0x5d, 0x99, 0x35, 0x4d, 0x63, 0x48, 0x3d, 0xe9, 0xc0, 0xf6, 0x56, 0x59, 0x47, 0x21, 0x3b, 0x1b, - 0x85, 0xc0, 0xc9, 0xb3, 0x52, 0x1b, 0x8a, 0x59, 0x71, 0xb6, 0x6e, 0x5d, 0xb7, 0x9a, 0xae, 0x9c, - 0xb3, 0x59, 0x1c, 0xa3, 0x5f, 0xe6, 0x55, 0x6b, 0x14, 0xbd, 0x00, 0xeb, 0x01, 0xed, 0xa0, 0x51, - 0xc5, 0x63, 0xbe, 0xa2, 0x57, 0x3b, 0x18, 0xcb, 0x01, 0xc5, 0x01, 0x4e, 0x1e, 0x74, 0x5d, 0xd1, - 0x1b, 0xf0, 0x57, 0x31, 0x24, 0xef, 0x80, 0x33, 0x5e, 0xb0, 0xb9, 0xcb, 0xb6, 0xe2, 0x1c, 0x7d, - 0xde, 0xbb, 0x21, 0x88, 0xc7, 0x69, 0xb0, 0x38, 0x9d, 0xbb, 0x9b, 0xfc, 0x37, 0x86, 0x74, 0x53, - 0x19, 0xdd, 0xc9, 0xa1, 0x7b, 0x72, 0x8b, 0xc1, 0x8e, 0xb6, 0x3b, 0x6d, 0x54, 0x47, 0xee, 0x2d, - 0x36, 0x8a, 0x04, 0xbd, 0xa7, 0xac, 0x68, 0x15, 0xdf, 0x28, 0x58, 0xec, 0xef, 0x3e, 0x55, 0xba, - 0xd1, 0xe5, 0xe8, 0x02, 0xae, 0xc3, 0xcf, 0x3e, 0x82, 0xaf, 0x3e, 0x82, 0x9f, 0x3e, 0x82, 0x8f, - 0xdf, 0x68, 0x6f, 0xe9, 0xf3, 0xdf, 0x38, 0xfb, 0x0b, 0x00, 0x00, 0xff, 0xff, 0x04, 0x03, 0x99, - 0x28, 0x9c, 0x01, 0x00, 0x00, + // 256 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x2e, 0xc9, 0xcc, 0x4d, + 0x2d, 0xd2, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x05, 0x73, 0x94, 0xe6, 0x32, 0x72, 0xb1, + 0x86, 0x80, 0x58, 0x42, 0x22, 0x5c, 0xac, 0x89, 0x39, 0xa9, 0x45, 0x25, 0x12, 0x8c, 0x0a, 0x8c, + 0x1a, 0x9c, 0x41, 0x10, 0x8e, 0x90, 0x10, 0x17, 0x4b, 0x72, 0x51, 0x7e, 0x9e, 0x04, 0x13, 0x58, + 0x10, 0xcc, 0x16, 0x72, 0xe0, 0x92, 0x4e, 0xcd, 0x33, 0xcc, 0xcd, 0xcf, 0x2b, 0xc9, 0x30, 0x49, + 0x49, 0xac, 0x34, 0x2d, 0x4f, 0x4d, 0xcd, 0x36, 0xce, 0xc8, 0x2f, 0x2d, 0x32, 0xcd, 0xcd, 0xcc, + 0x33, 0x93, 0x60, 0x51, 0x60, 0xd4, 0x60, 0x0d, 0xc2, 0xa7, 0x44, 0x48, 0x8c, 0x8b, 0xad, 0x38, + 0x35, 0x27, 0x2d, 0x33, 0x45, 0x82, 0x43, 0x81, 0x51, 0x83, 0x39, 0x08, 0xca, 0x13, 0x12, 0xe0, + 0x62, 0x2e, 0x2d, 0xca, 0x91, 0x10, 0x00, 0x5b, 0x06, 0x62, 0x2a, 0x75, 0x31, 0x72, 0x71, 0x82, + 0xdd, 0x57, 0xec, 0x9b, 0x58, 0x20, 0x64, 0xc2, 0xc5, 0x06, 0x76, 0x76, 0xb1, 0x04, 0xa3, 0x02, + 0xb3, 0x06, 0xb7, 0x91, 0x8c, 0x1e, 0xc4, 0x4b, 0x70, 0x15, 0x50, 0x96, 0x6b, 0x5e, 0x49, 0x51, + 0x65, 0x10, 0x54, 0xad, 0x94, 0x3b, 0x17, 0x37, 0x92, 0x30, 0xc8, 0x92, 0xec, 0xd4, 0x4a, 0xa8, + 0x37, 0x41, 0x4c, 0x21, 0x25, 0x2e, 0xd6, 0xb2, 0xc4, 0x9c, 0xd2, 0x54, 0xb0, 0x2f, 0xb9, 0x8d, + 0x78, 0x90, 0x4d, 0x0d, 0x82, 0x48, 0x59, 0x31, 0x59, 0x30, 0x3a, 0x09, 0x9c, 0x78, 0x24, 0xc7, + 0x78, 0xe1, 0x91, 0x1c, 0xe3, 0x83, 0x47, 0x72, 0x8c, 0x33, 0x1e, 0xcb, 0x31, 0x24, 0xb1, 0x81, + 0x03, 0xd3, 0x18, 0x10, 0x00, 0x00, 0xff, 0xff, 0xf5, 0xc1, 0x9e, 0x44, 0x5b, 0x01, 0x00, 0x00, } func (m *Timer) Marshal() (dAtA []byte, err error) { @@ -243,78 +200,38 @@ func (m *Timer) MarshalToSizedBuffer(dAtA []byte) (int, error) { i -= len(m.XXX_unrecognized) copy(dAtA[i:], m.XXX_unrecognized) } - if len(m.Cron) > 0 { - i -= len(m.Cron) - copy(dAtA[i:], m.Cron) - i = encodeVarintTimer(dAtA, i, uint64(len(m.Cron))) - i-- - dAtA[i] = 0x8 - i-- - dAtA[i] = 0x92 - } - if m.Selfid != 0 { - i = encodeVarintTimer(dAtA, i, uint64(m.Selfid)) - i-- - dAtA[i] = 0x8 - i-- - dAtA[i] = 0x88 - } - if m.Minute != 0 { - i = encodeVarintTimer(dAtA, i, uint64(m.Minute)) - i-- - dAtA[i] = 0x8 - i-- - dAtA[i] = 0x80 - } - if m.Hour != 0 { - i = encodeVarintTimer(dAtA, i, uint64(m.Hour)) - i-- - dAtA[i] = 0x4 - i-- - dAtA[i] = 0x80 - } - if m.Week != 0 { - i = encodeVarintTimer(dAtA, i, uint64(m.Week)) - i-- - dAtA[i] = 0x2 - i-- - dAtA[i] = 0x80 - } - if m.Day != 0 { - i = encodeVarintTimer(dAtA, i, uint64(m.Day)) - i-- - dAtA[i] = 0x1 - i-- - dAtA[i] = 0x80 - } - if m.Month != 0 { - i = encodeVarintTimer(dAtA, i, uint64(m.Month)) - i-- - dAtA[i] = 0x40 - } if len(m.Url) > 0 { i -= len(m.Url) copy(dAtA[i:], m.Url) i = encodeVarintTimer(dAtA, i, uint64(len(m.Url))) i-- - dAtA[i] = 0x22 + dAtA[i] = 0x1 + i-- + dAtA[i] = 0x82 + } + if m.Selfid != 0 { + i = encodeVarintTimer(dAtA, i, uint64(m.Selfid)) + i-- + dAtA[i] = 0x40 + } + if m.En1Month4Day5Week3Hour5Min6 != 0 { + i = encodeVarintTimer(dAtA, i, uint64(m.En1Month4Day5Week3Hour5Min6)) + i-- + dAtA[i] = 0x20 + } + if len(m.Cron) > 0 { + i -= len(m.Cron) + copy(dAtA[i:], m.Cron) + i = encodeVarintTimer(dAtA, i, uint64(len(m.Cron))) + i-- + dAtA[i] = 0x12 } if len(m.Alert) > 0 { i -= len(m.Alert) copy(dAtA[i:], m.Alert) i = encodeVarintTimer(dAtA, i, uint64(len(m.Alert))) i-- - dAtA[i] = 0x12 - } - if m.Enable { - i-- - if m.Enable { - dAtA[i] = 1 - } else { - dAtA[i] = 0 - } - i-- - dAtA[i] = 0x8 + dAtA[i] = 0xa } return len(dAtA) - i, nil } @@ -389,36 +306,21 @@ func (m *Timer) Size() (n int) { } var l int _ = l - if m.Enable { - n += 2 - } l = len(m.Alert) if l > 0 { n += 1 + l + sovTimer(uint64(l)) } - l = len(m.Url) + l = len(m.Cron) if l > 0 { n += 1 + l + sovTimer(uint64(l)) } - if m.Month != 0 { - n += 1 + sovTimer(uint64(m.Month)) - } - if m.Day != 0 { - n += 2 + sovTimer(uint64(m.Day)) - } - if m.Week != 0 { - n += 2 + sovTimer(uint64(m.Week)) - } - if m.Hour != 0 { - n += 2 + sovTimer(uint64(m.Hour)) - } - if m.Minute != 0 { - n += 2 + sovTimer(uint64(m.Minute)) + if m.En1Month4Day5Week3Hour5Min6 != 0 { + n += 1 + sovTimer(uint64(m.En1Month4Day5Week3Hour5Min6)) } if m.Selfid != 0 { - n += 2 + sovTimer(uint64(m.Selfid)) + n += 1 + sovTimer(uint64(m.Selfid)) } - l = len(m.Cron) + l = len(m.Url) if l > 0 { n += 2 + l + sovTimer(uint64(l)) } @@ -489,26 +391,6 @@ func (m *Timer) Unmarshal(dAtA []byte) error { } switch fieldNum { case 1: - if wireType != 0 { - return fmt.Errorf("proto: wrong wireType = %d for field Enable", wireType) - } - var v int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTimer - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - v |= int(b&0x7F) << shift - if b < 0x80 { - break - } - } - m.Enable = bool(v != 0) - case 2: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field Alert", wireType) } @@ -540,153 +422,7 @@ func (m *Timer) Unmarshal(dAtA []byte) error { } m.Alert = string(dAtA[iNdEx:postIndex]) iNdEx = postIndex - case 4: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Url", wireType) - } - var stringLen uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTimer - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - stringLen |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - intStringLen := int(stringLen) - if intStringLen < 0 { - return ErrInvalidLengthTimer - } - postIndex := iNdEx + intStringLen - if postIndex < 0 { - return ErrInvalidLengthTimer - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.Url = string(dAtA[iNdEx:postIndex]) - iNdEx = postIndex - case 8: - if wireType != 0 { - return fmt.Errorf("proto: wrong wireType = %d for field Month", wireType) - } - m.Month = 0 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTimer - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - m.Month |= int32(b&0x7F) << shift - if b < 0x80 { - break - } - } - case 16: - if wireType != 0 { - return fmt.Errorf("proto: wrong wireType = %d for field Day", wireType) - } - m.Day = 0 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTimer - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - m.Day |= int32(b&0x7F) << shift - if b < 0x80 { - break - } - } - case 32: - if wireType != 0 { - return fmt.Errorf("proto: wrong wireType = %d for field Week", wireType) - } - m.Week = 0 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTimer - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - m.Week |= int32(b&0x7F) << shift - if b < 0x80 { - break - } - } - case 64: - if wireType != 0 { - return fmt.Errorf("proto: wrong wireType = %d for field Hour", wireType) - } - m.Hour = 0 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTimer - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - m.Hour |= int32(b&0x7F) << shift - if b < 0x80 { - break - } - } - case 128: - if wireType != 0 { - return fmt.Errorf("proto: wrong wireType = %d for field Minute", wireType) - } - m.Minute = 0 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTimer - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - m.Minute |= int32(b&0x7F) << shift - if b < 0x80 { - break - } - } - case 129: - if wireType != 0 { - return fmt.Errorf("proto: wrong wireType = %d for field Selfid", wireType) - } - m.Selfid = 0 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTimer - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - m.Selfid |= int64(b&0x7F) << shift - if b < 0x80 { - break - } - } - case 130: + case 2: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field Cron", wireType) } @@ -718,6 +454,76 @@ func (m *Timer) Unmarshal(dAtA []byte) error { } m.Cron = string(dAtA[iNdEx:postIndex]) iNdEx = postIndex + case 4: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field En1Month4Day5Week3Hour5Min6", wireType) + } + m.En1Month4Day5Week3Hour5Min6 = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTimer + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.En1Month4Day5Week3Hour5Min6 |= int32(b&0x7F) << shift + if b < 0x80 { + break + } + } + case 8: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field Selfid", wireType) + } + m.Selfid = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTimer + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.Selfid |= int64(b&0x7F) << shift + if b < 0x80 { + break + } + } + case 16: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Url", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTimer + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthTimer + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthTimer + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Url = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipTimer(dAtA[iNdEx:]) diff --git a/plugin_manager/timer/timer.proto b/plugin_manager/timer/timer.proto index 28358341..b4a78568 100644 --- a/plugin_manager/timer/timer.proto +++ b/plugin_manager/timer/timer.proto @@ -2,16 +2,11 @@ syntax = "proto3"; package timer; message Timer { - bool enable = 1; - string alert = 2; - string url = 4; - int32 month = 8; - int32 day = 16; - int32 week = 32; - int32 hour = 64; - int32 minute = 128; - int64 selfid = 129; - string cron = 130; + string alert = 1; + string cron = 2; + int32 en1month4day5week3hour5min6 = 4; + int64 selfid = 8; + string url = 16; } message TimersMap { diff --git a/plugin_manager/timer/timer_test.go b/plugin_manager/timer/timer_test.go index ef96663b..94b8b572 100644 --- a/plugin_manager/timer/timer_test.go +++ b/plugin_manager/timer/timer_test.go @@ -10,11 +10,7 @@ import ( func TestNextWakeTime(t *testing.T) { logrus.SetLevel(logrus.DebugLevel) ts := &Timer{ - Month: 10, - Day: -1, - Week: 0, - Hour: -1, - Minute: 6, + En1Month4Day5Week3Hour5Min6: 0xffffff, } t1 := time.Until(ts.nextWakeTime()) if t1 < 0 { diff --git a/plugin_manager/timer/wrap.go b/plugin_manager/timer/wrap.go new file mode 100644 index 00000000..7675b689 --- /dev/null +++ b/plugin_manager/timer/wrap.go @@ -0,0 +1,75 @@ +package timer + +import "time" + +func (m *Timer) En() (en bool) { + return m.En1Month4Day5Week3Hour5Min6&0x800000 != 0 +} + +func (m *Timer) Month() (mon time.Month) { + mon = time.Month((m.En1Month4Day5Week3Hour5Min6 & 0x780000) >> 19) + if mon == 0b1111 { + mon = -1 + } + return +} + +func (m *Timer) Day() (d int) { + d = int((m.En1Month4Day5Week3Hour5Min6 & 0x07c000) >> 14) + if d == 0b11111 { + d = -1 + } + return +} + +func (m *Timer) Week() (w time.Weekday) { + w = time.Weekday((m.En1Month4Day5Week3Hour5Min6 & 0x003800) >> 11) + if w == 0b111 { + w = -1 + } + return +} + +func (m *Timer) Hour() (h int) { + h = int((m.En1Month4Day5Week3Hour5Min6 & 0x0007c0) >> 6) + if h == 0b11111 { + h = -1 + } + return +} + +func (m *Timer) Minute() (min int) { + min = int(m.En1Month4Day5Week3Hour5Min6 & 0x00003f) + if min == 0b111111 { + min = -1 + } + return +} + +func (m *Timer) SetEn(en bool) { + if en { + m.En1Month4Day5Week3Hour5Min6 |= 0x800000 + } else { + m.En1Month4Day5Week3Hour5Min6 &= 0x7fffff + } +} + +func (m *Timer) SetMonth(mon time.Month) { + m.En1Month4Day5Week3Hour5Min6 = ((int32(mon) << 19) & 0x780000) | (m.En1Month4Day5Week3Hour5Min6 & 0x87ffff) +} + +func (m *Timer) SetDay(d int) { + m.En1Month4Day5Week3Hour5Min6 = ((int32(d) << 14) & 0x07c000) | (m.En1Month4Day5Week3Hour5Min6 & 0xf83fff) +} + +func (m *Timer) SetWeek(w time.Weekday) { + m.En1Month4Day5Week3Hour5Min6 = ((int32(w) << 11) & 0x003800) | (m.En1Month4Day5Week3Hour5Min6 & 0xffc7ff) +} + +func (m *Timer) SetHour(h int) { + m.En1Month4Day5Week3Hour5Min6 = ((int32(h) << 6) & 0x0007c0) | (m.En1Month4Day5Week3Hour5Min6 & 0xfff83f) +} + +func (m *Timer) SetMinute(min int) { + m.En1Month4Day5Week3Hour5Min6 = (int32(min) & 0x00003f) | (m.En1Month4Day5Week3Hour5Min6 & 0xffffc0) +}