result.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // SPDX-FileCopyrightText: Adam Evyčędo
  2. //
  3. // SPDX-License-Identifier: AGPL-3.0-or-later
  4. package traffic
  5. import (
  6. "os"
  7. "path/filepath"
  8. "time"
  9. "git.sr.ht/~sircmpwn/go-bare"
  10. )
  11. type TrafficResult interface {
  12. getTraffic() *Traffic
  13. getContext() Context
  14. setTimezone(*time.Location)
  15. setStopsFile(*os.File)
  16. getTimetableHome() string
  17. getStopsFile() *os.File
  18. getStopOffset() uint
  19. setStop(Stop)
  20. setTripsFile(*os.File)
  21. getTripsFile() *os.File
  22. }
  23. func loadTimezone(r TrafficResult) (TrafficResult, error) {
  24. l, err := GetTimezone(Stop{}, r.getTraffic(), r.getContext().FeedID)
  25. r.setTimezone(l)
  26. return r, err
  27. }
  28. func openStopsFile(r TrafficResult) (TrafficResult, error) {
  29. file, err := os.Open(filepath.Join(r.getTimetableHome(), "stops.bare"))
  30. r.setStopsFile(file)
  31. return r, err
  32. }
  33. func seekStopsFile(r TrafficResult) (TrafficResult, error) {
  34. _, err := r.getStopsFile().Seek(int64(r.getStopOffset()), 0)
  35. return r, err
  36. }
  37. func unmarshallStop(r TrafficResult) (TrafficResult, error) {
  38. stop := Stop{}
  39. err := bare.UnmarshalReader(r.getStopsFile(), &stop)
  40. r.getStopsFile().Close()
  41. r.setStop(stop)
  42. return r, err
  43. }
  44. func openTripsFile(r TrafficResult) (TrafficResult, error) {
  45. file, err := os.Open(filepath.Join(r.getTimetableHome(), "trips.bare"))
  46. r.setTripsFile(file)
  47. return r, err
  48. }
  49. func closeFiles(r TrafficResult, err error) (TrafficResult, error) {
  50. if r.getStopsFile() != nil {
  51. r.getStopsFile().Close()
  52. }
  53. if r.getTripsFile() != nil {
  54. r.getTripsFile().Close()
  55. }
  56. return r, err
  57. }