diff --git a/CHANGELOG.md b/CHANGELOG.md index df857598..ecfe7363 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ Starting from v2.2.5, all notable changes to this project will be documented in this file. +## v3.2.2 + +### New Features + +- Enable mainline data protocol support for AnyShake Explorer without GNSS module. +- Added earthquake event source API support of EQZT. + ## v3.2.1 ### Breaking Changes diff --git a/VERSION b/VERSION index 040943e5..b57c3c66 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -v3.2.1 +v3.2.2 diff --git a/api/v1/station/explorer.go b/api/v1/station/explorer.go index 5cfd67a0..6be9e637 100644 --- a/api/v1/station/explorer.go +++ b/api/v1/station/explorer.go @@ -12,10 +12,7 @@ func (e *explorerInfo) get(timeSource *timesource.Source, explorerDeps *explorer e.Received = explorerDeps.Health.GetReceived() e.SampleRate = explorerDeps.Health.GetSampleRate() - currentTime, err := timeSource.Get() - if err != nil { - return err - } + currentTime := timeSource.Get() e.Elapsed = int64(currentTime.Sub(explorerDeps.Health.GetStartTime()).Seconds()) e.Latitude = float64(int(explorerDeps.Config.GetLatitude()*1000)) / 1000 diff --git a/api/v1/station/os.go b/api/v1/station/os.go index 9a97e786..43c4b657 100644 --- a/api/v1/station/os.go +++ b/api/v1/station/os.go @@ -20,16 +20,12 @@ func (o *osInfo) get(timeSource *timesource.Source) error { return err } - timestamp, err := timeSource.Get() - if err != nil { - return err - } - + currentTime := timeSource.Get() o.Uptime = int64(up.Seconds()) o.OS = runtime.GOOS o.Arch = runtime.GOARCH o.Distro = osutil.Name o.Hostname = hostname - o.Timestamp = timestamp.UnixMilli() + o.Timestamp = currentTime.UnixMilli() return nil } diff --git a/api/v1/trace/cea.go b/api/v1/trace/cea.go index 1b39f998..4ef23ace 100644 --- a/api/v1/trace/cea.go +++ b/api/v1/trace/cea.go @@ -9,6 +9,7 @@ import ( "github.com/PuerkitoBio/goquery" "github.com/anyshake/observer/utils/request" + "github.com/corpix/uarand" ) type CEA_DASE struct { @@ -27,6 +28,7 @@ func (c *CEA_DASE) Fetch() ([]byte, error) { res, err := request.GET( "https://www-dase.cea.fr/evenement/derniers_evenements.php", 10*time.Second, time.Second, 3, false, nil, + map[string]string{"User-Agent": uarand.GetRandom()}, ) if err != nil { return nil, err diff --git a/api/v1/trace/ceic.go b/api/v1/trace/ceic.go index 7890177b..b96e38c8 100644 --- a/api/v1/trace/ceic.go +++ b/api/v1/trace/ceic.go @@ -5,6 +5,7 @@ import ( "time" "github.com/anyshake/observer/utils/request" + "github.com/corpix/uarand" ) type CEIC struct { @@ -23,6 +24,7 @@ func (c *CEIC) Fetch() ([]byte, error) { res, err := request.GET( "https://news.ceic.ac.cn/ajax/google", 10*time.Second, time.Second, 3, false, nil, + map[string]string{"User-Agent": uarand.GetRandom()}, ) if err != nil { return nil, err diff --git a/api/v1/trace/cwa.go b/api/v1/trace/cwa.go index bc4df8c0..053bfa1b 100644 --- a/api/v1/trace/cwa.go +++ b/api/v1/trace/cwa.go @@ -12,6 +12,7 @@ import ( "github.com/PuerkitoBio/goquery" "github.com/anyshake/observer/utils/request" + "github.com/corpix/uarand" ) const HOST_IP_TO_BYPASS_GFW = "168.95.246.1:443" @@ -41,6 +42,7 @@ func (c *CWA) Fetch() ([]byte, error) { "https://www.cwa.gov.tw/V8/C/E/MOD/MAP_LIST.html", 10*time.Second, time.Second, 3, false, c.createGFWBypasser(), + map[string]string{"User-Agent": uarand.GetRandom()}, ) if err != nil { return nil, err diff --git a/api/v1/trace/data.go b/api/v1/trace/data.go index bf99eb78..7c8e3380 100644 --- a/api/v1/trace/data.go +++ b/api/v1/trace/data.go @@ -1,6 +1,8 @@ package trace -import "strconv" +import ( + "strconv" +) func string2Float(num string) float64 { r, err := strconv.ParseFloat(num, 64) diff --git a/api/v1/trace/eqzt.go b/api/v1/trace/eqzt.go new file mode 100644 index 00000000..378a91b9 --- /dev/null +++ b/api/v1/trace/eqzt.go @@ -0,0 +1,189 @@ +package trace + +import ( + "bytes" + "fmt" + "io" + "regexp" + "strconv" + "strings" + "time" + + "github.com/PuerkitoBio/goquery" + "github.com/anyshake/observer/utils/request" + "github.com/corpix/uarand" + "golang.org/x/text/encoding/simplifiedchinese" + "golang.org/x/text/transform" +) + +type EQZT struct { + dataSourceCache +} + +func (j *EQZT) Property() string { + return "昭通市地震信息系统" +} + +func (j *EQZT) Fetch() ([]byte, error) { + if time.Since(j.Time) <= EXPIRATION { + return j.Cache, nil + } + + res, err := request.GET( + "http://www.eqzt.com/eqzt/seis/view_sbml.php?dzml=sbml", + 10*time.Second, time.Second, 3, false, nil, + map[string]string{"User-Agent": uarand.GetRandom()}, + ) + if err != nil { + return nil, err + } + + j.Time = time.Now() + j.Cache = make([]byte, len(res)) + copy(j.Cache, res) + + return res, nil +} + +func (j *EQZT) Parse(data []byte) (map[string]any, error) { + result := make(map[string]any) + result["data"] = make([]any, 0) + + reader := transform.NewReader(bytes.NewReader(data), simplifiedchinese.GB18030.NewDecoder()) + data, err := io.ReadAll(reader) + if err != nil { + return nil, err + } + + htmlReader := bytes.NewBuffer(data) + doc, err := goquery.NewDocumentFromReader(htmlReader) + if err != nil { + return nil, err + } + doc.Find("table").Each(func(i int, s *goquery.Selection) { + if val, ok := s.Attr("align"); !ok || val != "center" { + return + } + s.Find("tr").Each(func(i int, s *goquery.Selection) { + if i == 0 { + return + } + item := make(map[string]any) + s.Find("td").Each(func(i int, s *goquery.Selection) { + if val, ok := s.Attr("colspan"); ok && val == "8" { + return + } + value := s.Text() + switch i { + case 0: + item["timestamp"] = j.getTimestamp(value) + case 1: + item["latitude"] = j.getLatitude(value) + case 2: + item["longitude"] = j.getLongitude(value) + case 4: + item["magnitude"] = j.getMagnitude(value) + case 5: + trimVal := strings.TrimFunc(value, func(r rune) bool { return r == ' ' }) + item["event"] = trimVal + item["region"] = fmt.Sprintf("云南及周边区域 - %s", trimVal) + } + }) + result["data"] = append(result["data"].([]any), item) + }) + }) + + return result, nil +} + +func (j *EQZT) Format(latitude, longitude float64, data map[string]any) ([]seismicEvent, error) { + var list []seismicEvent + for _, v := range data["data"].([]any) { + _, ok := v.(map[string]any)["timestamp"] + if !ok { + continue + } + l := seismicEvent{ + Verfied: true, + Depth: -1, + Latitude: v.(map[string]any)["latitude"].(float64), + Longitude: v.(map[string]any)["longitude"].(float64), + Event: v.(map[string]any)["event"].(string), + Region: v.(map[string]any)["region"].(string), + Timestamp: v.(map[string]any)["timestamp"].(int64), + Magnitude: v.(map[string]any)["magnitude"].(float64), + } + l.Distance = getDistance(latitude, l.Latitude, longitude, l.Longitude) + l.Estimation = getSeismicEstimation(l.Depth, l.Distance) + + list = append(list, l) + } + + return list, nil +} + +func (j *EQZT) List(latitude, longitude float64) ([]seismicEvent, error) { + res, err := j.Fetch() + if err != nil { + return nil, err + } + + data, err := j.Parse(res) + if err != nil { + return nil, err + } + + list, err := j.Format(latitude, longitude, data) + if err != nil { + return nil, err + } + + return list, nil +} + +func (j *EQZT) getTimestamp(text string) int64 { + timestamp, _ := time.Parse("2006-01-02 15:04:05", text) + return timestamp.UnixMilli() +} + +func (j *EQZT) getLatitude(text string) float64 { + text = strings.ReplaceAll(text, "°", " ") + text = strings.ReplaceAll(text, "′", "") + + parts := strings.Fields(text) + if len(parts) != 2 { + return 0.0 + } + + degrees, err1 := strconv.ParseFloat(parts[0], 64) + minutes, err2 := strconv.ParseFloat(parts[1], 64) + if err1 != nil || err2 != nil { + return 0.0 + } + + return degrees + minutes/60 +} + +func (j *EQZT) getLongitude(text string) float64 { + text = strings.ReplaceAll(text, "°", " ") + text = strings.ReplaceAll(text, "′", "") + + parts := strings.Fields(text) + if len(parts) != 2 { + return 0.0 + } + + degrees, err1 := strconv.ParseFloat(parts[0], 64) + minutes, err2 := strconv.ParseFloat(parts[1], 64) + if err1 != nil || err2 != nil { + return 0.0 + } + + return degrees + minutes/60 +} + +func (j *EQZT) getMagnitude(text string) float64 { + re := regexp.MustCompile(`\d+(\.\d+)?`) + text = re.FindString(text) + return string2Float(text) +} diff --git a/api/v1/trace/hko.go b/api/v1/trace/hko.go index ef0b6e10..b2f41239 100644 --- a/api/v1/trace/hko.go +++ b/api/v1/trace/hko.go @@ -6,6 +6,7 @@ import ( "time" "github.com/anyshake/observer/utils/request" + "github.com/corpix/uarand" "github.com/sbabiv/xml2map" ) @@ -25,6 +26,7 @@ func (h *HKO) Fetch() ([]byte, error) { res, err := request.GET( "https://www.hko.gov.hk/gts/QEM/eq_app-30d_uc.xml", 10*time.Second, time.Second, 3, false, nil, + map[string]string{"User-Agent": uarand.GetRandom()}, ) if err != nil { return nil, err diff --git a/api/v1/trace/ingv.go b/api/v1/trace/ingv.go index bf10fd74..6574feef 100644 --- a/api/v1/trace/ingv.go +++ b/api/v1/trace/ingv.go @@ -7,6 +7,7 @@ import ( "time" "github.com/anyshake/observer/utils/request" + "github.com/corpix/uarand" ) type INGV struct { @@ -25,6 +26,7 @@ func (c *INGV) Fetch() ([]byte, error) { res, err := request.GET( "https://webservices.ingv.it/fdsnws/event/1/query?minmag=-1&format=text&timezone=UTC", 10*time.Second, time.Second, 3, false, nil, + map[string]string{"User-Agent": uarand.GetRandom()}, ) if err != nil { return nil, err diff --git a/api/v1/trace/jma.go b/api/v1/trace/jma.go index 02bf61b1..fcb62b12 100644 --- a/api/v1/trace/jma.go +++ b/api/v1/trace/jma.go @@ -6,6 +6,7 @@ import ( "time" "github.com/anyshake/observer/utils/request" + "github.com/corpix/uarand" ) type JMA struct { @@ -24,6 +25,7 @@ func (j *JMA) Fetch() ([]byte, error) { res, err := request.GET( "https://www.jma.go.jp/bosai/quake/data/list.json", 10*time.Second, time.Second, 3, false, nil, + map[string]string{"User-Agent": uarand.GetRandom()}, ) if err != nil { return nil, err diff --git a/api/v1/trace/kma.go b/api/v1/trace/kma.go index fbe0dfbb..0a8ea64c 100644 --- a/api/v1/trace/kma.go +++ b/api/v1/trace/kma.go @@ -8,6 +8,7 @@ import ( "github.com/PuerkitoBio/goquery" "github.com/anyshake/observer/utils/request" + "github.com/corpix/uarand" ) type KMA struct { @@ -26,6 +27,7 @@ func (k *KMA) Fetch() ([]byte, error) { res, err := request.GET( "https://www.weather.go.kr/w/eqk-vol/search/korea.do", 10*time.Second, time.Second, 3, false, nil, + map[string]string{"User-Agent": uarand.GetRandom()}, ) if err != nil { return nil, err diff --git a/api/v1/trace/module.go b/api/v1/trace/module.go index d3699030..1bb75399 100644 --- a/api/v1/trace/module.go +++ b/api/v1/trace/module.go @@ -31,6 +31,7 @@ func (t *Trace) Register(rg *gin.RouterGroup, resolver *v1.Resolver) error { "SCEA_E": &SCEA_E{}, "SCEA_B": &SCEA_B{}, "CEA_DASE": &CEA_DASE{}, + "EQZT": &EQZT{}, } var explorerDeps *explorer.ExplorerDependency diff --git a/api/v1/trace/scea-b.go b/api/v1/trace/scea-b.go index 2f1dd79b..77855f0e 100644 --- a/api/v1/trace/scea-b.go +++ b/api/v1/trace/scea-b.go @@ -6,6 +6,7 @@ import ( "time" "github.com/anyshake/observer/utils/request" + "github.com/corpix/uarand" ) type SCEA_B struct { @@ -24,6 +25,7 @@ func (s *SCEA_B) Fetch() ([]byte, error) { res, err := request.GET( "http://118.113.105.29:8002/api/bulletin/jsonPageList?pageSize=100", 10*time.Second, time.Second, 3, false, nil, + map[string]string{"User-Agent": uarand.GetRandom()}, ) if err != nil { return nil, err diff --git a/api/v1/trace/scea-e.go b/api/v1/trace/scea-e.go index 824b394c..cfb1bfd3 100644 --- a/api/v1/trace/scea-e.go +++ b/api/v1/trace/scea-e.go @@ -4,6 +4,7 @@ import ( "time" "github.com/anyshake/observer/utils/request" + "github.com/corpix/uarand" ) type SCEA_E struct { @@ -23,6 +24,7 @@ func (s *SCEA_E) Fetch() ([]byte, error) { res, err := request.GET( "http://118.113.105.29:8002/api/earlywarning/jsonPageList?pageSize=100", 10*time.Second, time.Second, 3, false, nil, + map[string]string{"User-Agent": uarand.GetRandom()}, ) if err != nil { return nil, err diff --git a/api/v1/trace/usgs.go b/api/v1/trace/usgs.go index 885d8b41..4374a860 100644 --- a/api/v1/trace/usgs.go +++ b/api/v1/trace/usgs.go @@ -6,6 +6,7 @@ import ( "time" "github.com/anyshake/observer/utils/request" + "github.com/corpix/uarand" ) type USGS struct { @@ -24,6 +25,7 @@ func (u *USGS) Fetch() ([]byte, error) { res, err := request.GET( "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojson", 10*time.Second, time.Second, 3, false, nil, + map[string]string{"User-Agent": uarand.GetRandom()}, ) if err != nil { return nil, err diff --git a/drivers/explorer/impl.go b/drivers/explorer/impl.go index 14b08d3f..a0c2c41c 100644 --- a/drivers/explorer/impl.go +++ b/drivers/explorer/impl.go @@ -186,11 +186,7 @@ func (e *ExplorerDriverImpl) handleReadLegacyPacket(deps *ExplorerDependency, fi return case currentTick := <-ticker.C: if len(packetBuffer) > 0 { - currentTime, err := deps.FallbackTime.Get() - if err != nil { - continue - } - + currentTime := deps.FallbackTime.Get() deps.Health.SetUpdatedAt(currentTime) deps.Health.SetReceived(deps.Health.GetReceived() + 1) @@ -260,10 +256,19 @@ func (e *ExplorerDriverImpl) handleReadMainlinePacket(deps *ExplorerDependency, } }() + // When device ID was set to 19890604 (dummy value), the device is running without GNSS module. + // In this case, the latitude, longitude, elevation will not be updated. + // The timestamp will be replaced with the NTP time. + noGnssMode := false + if deps.Config.GetDeviceId() == 19890604 { + noGnssMode = true + } + // Read data from the FIFO buffer continuously var ( packetBuffer = []mainlinePacket{} - nextTick = int64(0) + nextTick = int64(0) // Expected timestamp for the next published data on message bus + timeDiff = int64(0) // For non-GNSS mode, we need time difference between the packet and NTP time timer = time.NewTimer(time.Millisecond) ) for { @@ -287,24 +292,34 @@ func (e *ExplorerDriverImpl) handleReadMainlinePacket(deps *ExplorerDependency, // Update the device ID, latitude, longitude, elevation switch e.mainlinePacket.VariableName { case "device_id": - deps.Config.SetDeviceId(binary.LittleEndian.Uint32(e.mainlinePacket.VariableData[:])) + if !noGnssMode { + deps.Config.SetDeviceId(binary.LittleEndian.Uint32(e.mainlinePacket.VariableData[:])) + } case "latitude": latitude := math.Float32frombits(binary.LittleEndian.Uint32(e.mainlinePacket.VariableData[:])) - if latitude >= -90 && latitude <= 90 { + if latitude >= -90 && latitude <= 90 && !noGnssMode { deps.Config.SetLatitude(float64(latitude)) } case "longitude": longitude := math.Float32frombits(binary.LittleEndian.Uint32(e.mainlinePacket.VariableData[:])) - if longitude >= -180 && longitude <= 180 { + if longitude >= -180 && longitude <= 180 && !noGnssMode { deps.Config.SetLongitude(float64(longitude)) } case "elevation": elevation := math.Float32frombits(binary.LittleEndian.Uint32(e.mainlinePacket.VariableData[:])) - if elevation >= 0 { + if elevation >= 0 && !noGnssMode { deps.Config.SetElevation(float64(elevation)) } } + if noGnssMode { + // Update time difference at the first packet or everyday at 00:00:00 UTC + currentTime := deps.FallbackTime.Get() + if nextTick == 0 || currentTime.Unix()%(int64(time.Hour.Seconds())*24) == 0 { + timeDiff = currentTime.UTC().UnixMilli() - e.mainlinePacket.Timestamp + } + } + // Append the packet to the buffer if nextTick == 0 { nextTick = e.mainlinePacket.Timestamp @@ -338,7 +353,7 @@ func (e *ExplorerDriverImpl) handleReadMainlinePacket(deps *ExplorerDependency, Z_Axis: z_axis_count, E_Axis: e_axis_count, N_Axis: n_axis_count, - Timestamp: e.mainlinePacket.Timestamp - time.Second.Milliseconds(), + Timestamp: (e.mainlinePacket.Timestamp - time.Second.Milliseconds()) + timeDiff, } deps.messageBus.Publish("explorer", &finalPacket) @@ -370,11 +385,7 @@ func (e *ExplorerDriverImpl) readerDaemon(deps *ExplorerDependency) { func (e *ExplorerDriverImpl) Init(deps *ExplorerDependency, logger ExplorerLogger) error { e.logger = logger - currentTime, err := deps.FallbackTime.Get() - if err != nil { - return err - } - + currentTime := deps.FallbackTime.Get() deps.Health.SetStartTime(currentTime) deps.subscribers = haxmap.New[string, ExplorerEventHandler]() deps.messageBus = messagebus.New(1024) diff --git a/frontend/src/.env b/frontend/src/.env index 064879d3..61965c07 100644 --- a/frontend/src/.env +++ b/frontend/src/.env @@ -1,2 +1,2 @@ -REACT_APP_VERSION=v3.2.1 -REACT_APP_RELEASE=59b9250b-20240901015842 +REACT_APP_VERSION=v3.2.2 +REACT_APP_RELEASE=540f1487-20240901120534 diff --git a/go.mod b/go.mod index 7c0dc9c2..e3d41d13 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/bclswl0827/sqlite v1.11.1-0.20240613172512-9e6ac9861470 github.com/beevik/ntp v1.4.3 github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be + github.com/corpix/uarand v0.2.0 github.com/gin-contrib/gzip v0.0.6 github.com/gin-gonic/gin v1.10.0 github.com/gorilla/websocket v1.5.0 @@ -108,6 +109,6 @@ require ( golang.org/x/crypto v0.24.0 // indirect golang.org/x/net v0.26.0 // indirect golang.org/x/sys v0.21.0 // indirect - golang.org/x/text v0.16.0 // indirect + golang.org/x/text v0.16.0 gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 7a05d832..f4b26d3a 100644 --- a/go.sum +++ b/go.sum @@ -58,6 +58,8 @@ github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg= github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY= github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be h1:J5BL2kskAlV9ckgEsNQXscjIaLiOYiZ75d4e94E6dcQ= github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be/go.mod h1:mk5IQ+Y0ZeO87b858TlA645sVcEcbiX6YqP98kt+7+w= +github.com/corpix/uarand v0.2.0 h1:U98xXwud/AVuCpkpgfPF7J5TQgr7R5tqT8VZP5KWbzE= +github.com/corpix/uarand v0.2.0/go.mod h1:/3Z1QIqWkDIhf6XWn/08/uMHoQ8JUoTIKc2iPchBOmM= github.com/creack/goselect v0.1.3-0.20221130125424-8eac7f782437 h1:vR0VDJLclKuJceyXLWbj8ZFPvUvOkSSQ6fJ63R4pzRY= github.com/creack/goselect v0.1.3-0.20221130125424-8eac7f782437/go.mod h1:a/NhLweNvqIYMuxcMOuWY516Cimucms3DglDzQP3hKY= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= diff --git a/services/miniseed/start.go b/services/miniseed/start.go index c8a762df..184ebff9 100644 --- a/services/miniseed/start.go +++ b/services/miniseed/start.go @@ -62,7 +62,7 @@ func (m *MiniSeedService) Start(options *services.Options, waitGroup *sync.WaitG m.miniseedBuffer = make([]explorer.ExplorerData, m.writeBufferInterval) // Get sequence number if file exists - currentTime, _ := options.TimeSource.Get() + currentTime := options.TimeSource.Get() for _, channelCode := range []string{ explorer.EXPLORER_CHANNEL_CODE_Z, explorer.EXPLORER_CHANNEL_CODE_E, diff --git a/services/seedlink/provider.go b/services/seedlink/provider.go index f7cf80d0..ed58bed7 100644 --- a/services/seedlink/provider.go +++ b/services/seedlink/provider.go @@ -29,7 +29,7 @@ func (p *provider) GetStartTime() time.Time { } func (p *provider) GetCurrentTime() time.Time { - currentTime, _ := p.timeSource.Get() + currentTime := p.timeSource.Get() return currentTime } diff --git a/services/seedlink/start.go b/services/seedlink/start.go index 29083cd0..5c16de67 100644 --- a/services/seedlink/start.go +++ b/services/seedlink/start.go @@ -35,7 +35,7 @@ func (s *SeedLinkService) Start(options *services.Options, waitGroup *sync.WaitG return } - currentTime, _ := options.TimeSource.Get() + currentTime := options.TimeSource.Get() messageBus := messagebus.New(65535) // Subscribe to Explorer events diff --git a/services/timesync/start.go b/services/timesync/start.go index 7e5d7629..f9aa9e50 100644 --- a/services/timesync/start.go +++ b/services/timesync/start.go @@ -21,15 +21,11 @@ func (s *TimeSyncService) Start(options *services.Options, waitGroup *sync.WaitG logger.GetLogger(s.GetServiceName()).Infoln("service has been stopped") return case <-ticker.C: - currentTime, err := options.TimeSource.Get() - if err != nil { - logger.GetLogger(s.GetServiceName()).Errorln(err) - continue - } - // Update time source at 00:00:00 UTC every day + currentTime := options.TimeSource.Get() if currentTime.Unix()%86400 == 0 { - if err = options.TimeSource.Update(); err != nil { + err := options.TimeSource.Update() + if err != nil { logger.GetLogger(s.GetServiceName()).Errorln(err) } else { logger.GetLogger(s.GetServiceName()).Info("time source has been updated") diff --git a/utils/timesource/get.go b/utils/timesource/get.go index ade31ccd..a5d61186 100644 --- a/utils/timesource/get.go +++ b/utils/timesource/get.go @@ -1,18 +1,13 @@ package timesource import ( - "errors" "time" ) -func (g *Source) Get() (time.Time, error) { +func (g *Source) Get() time.Time { g.rwMutex.RLock() defer g.rwMutex.RUnlock() - if g.LocalBaseTime.IsZero() || g.ReferenceTime.IsZero() { - return time.Now().UTC(), errors.New("empty BaseTime or RefTime is not allowed") - } - elapsed := time.Since(g.LocalBaseTime.UTC()) - return g.ReferenceTime.Add(elapsed).UTC(), nil + return g.ReferenceTime.Add(elapsed).UTC() }