main.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "time"
  6. fho "git.mokkon.com/sainw/fho_forward.git/client"
  7. )
  8. const (
  9. address = "fhogrpc-test.seinmaungengineering.com:443"
  10. )
  11. func main() {
  12. client, err := fho.NewClient(address, "da910506-4bdc-4dc2-9481-58a4e3e3c8bf", "1adf4633-7e9e-4bd0-b512-7b087d9c9719")
  13. if err != nil {
  14. panic(err)
  15. }
  16. // go listenProducts(client)
  17. // go listenPrices(client)
  18. // go query(client)
  19. // update(client)
  20. // updateFromFHO(client)
  21. go listenConnection(client)
  22. select {}
  23. }
  24. func update(client *fho.FhoClient) {
  25. products := make([]*fho.PriceChangeHistoryProduct, 0)
  26. products = append(products, &fho.PriceChangeHistoryProduct{ID: "127", ProductID: "id0001", Name: "92", Color: "2334444", Price: 1222})
  27. products = append(products, &fho.PriceChangeHistoryProduct{ID: "128", ProductID: "id0002", Name: "95", Color: "2989834", Price: 1333})
  28. products = append(products, &fho.PriceChangeHistoryProduct{ID: "129", ProductID: "id0003", Name: "Diesel", Color: "2989834", Price: 1444})
  29. products = append(products, &fho.PriceChangeHistoryProduct{ID: "120", ProductID: "id0004", Name: "Premium", Color: "2989834", Price: 1555})
  30. t := time.Now()
  31. history := fho.PriceChangeHistory{ID: "1111113", PriceChangeTime: t.Format(fho.DATETIME_FROM_ISO_FORMAT),
  32. Status: "Successful", PriceChangeHistoryProducts: products}
  33. err := fho.UpdatePriceHisotry(client, &history)
  34. if err != nil {
  35. log.Println("Error query:", err.Error())
  36. }
  37. }
  38. func updateFromFHO(client *fho.FhoClient) error {
  39. prices, err := fho.QueryPrice(client, time.Now().Add(time.Hour*-114))
  40. if err != nil {
  41. log.Println("Error query:", err.Error())
  42. return err
  43. }
  44. for _, price := range prices {
  45. fmt.Println("Query=====>")
  46. printPrice(price)
  47. }
  48. _price := prices[0]
  49. t := time.Now()
  50. history := fho.PriceChangeHistory{ID: "1111114", PriceChangeTime: t.Format(fho.DATETIME_FROM_ISO_FORMAT),
  51. PriceScheduleID: _price.ID, Status: "Successful"}
  52. err = fho.UpdatePriceHisotry(client, &history)
  53. if err != nil {
  54. log.Println("Error updatePriceHisotry:", err.Error())
  55. }
  56. return nil
  57. }
  58. func query(client *fho.FhoClient) {
  59. products, err := fho.QueryProducts(client, time.Now().Add(time.Hour*-114))
  60. if err != nil {
  61. log.Println("Error query:", err.Error())
  62. }
  63. for _, product := range products {
  64. fmt.Println("Query=====>")
  65. printProduct(product)
  66. }
  67. }
  68. func listenProducts(client *fho.FhoClient) error {
  69. err := fho.ListenOnProducts(client, time.Now().Add(time.Hour*-114), func(products []*fho.Product) error {
  70. for _, p := range products {
  71. fmt.Println("Listening=====>")
  72. printProduct(p)
  73. }
  74. return nil
  75. })
  76. return err
  77. }
  78. func listenPrices(client *fho.FhoClient) error {
  79. err := fho.ListenOnPrice(client, time.Now().Add(time.Hour*-314), func(prices []*fho.PriceSchedule) error {
  80. for _, p := range prices {
  81. fmt.Println("Listening=====>")
  82. printPrice(p)
  83. }
  84. return nil
  85. })
  86. if err != nil {
  87. log.Println("error listenPrices:", err.Error())
  88. }
  89. return err
  90. }
  91. func printProduct(p *fho.Product) {
  92. fmt.Printf("ID : %v\n", p.ID)
  93. fmt.Printf("Name : %v\n", p.Name)
  94. fmt.Printf("Color : %v\n", p.Color)
  95. fmt.Printf("IsCreated : %v\n", p.IsCreated)
  96. fmt.Printf("IsUpdated : %v\n", p.IsUpdated)
  97. fmt.Printf("IsDeleted : %v\n", p.IsDeleted)
  98. fmt.Printf("LatestUpdatedTime : %v\n", p.LatestUpdatedTime)
  99. }
  100. func printPrice(p *fho.PriceSchedule) {
  101. fmt.Printf("ID : %v\n", p.ID)
  102. fmt.Printf("Date : %v\n", p.Date)
  103. fmt.Printf("AfterShiftChange : %v\n", p.AfterShiftChange)
  104. fmt.Printf("IsCreated : %v\n", p.IsCreated)
  105. fmt.Printf("IsUpdated : %v\n", p.IsUpdated)
  106. fmt.Printf("IsDeleted : %v\n", p.IsDeleted)
  107. fmt.Printf("LatestUpdatedTime : %v\n", p.LatestUpdatedTime)
  108. fmt.Printf("Products -------------------\n")
  109. for _, prd := range p.PriceScheduleProducts {
  110. fmt.Printf("prd.ProductID : %v\n", prd.ProductID)
  111. fmt.Printf("prd.Name : %v\n", prd.Name)
  112. fmt.Printf("prd.Color : %v\n", prd.Color)
  113. fmt.Printf("prd.Price : %v\n", prd.Price)
  114. }
  115. }
  116. func listenConnection(client *fho.FhoClient) error {
  117. err := fho.Heartbeat(client, func(isConnected bool) {
  118. if isConnected {
  119. log.Println("FHO connection is online")
  120. } else {
  121. log.Println("FHO connection is offline")
  122. }
  123. })
  124. return err
  125. }