main.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package main
  2. import (
  3. "bufio"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "io/ioutil"
  8. "math/rand"
  9. "net/http"
  10. "os"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. var lamp0IsOn bool = false
  16. var lamp1IsOn bool = false
  17. var emulatedServiceStated []int = []int{0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 2, 0, 1, 0, 0, 2, 0, 0, 0, 1, 0, 0, 1, 0, 1}
  18. type GlobalStatus struct {
  19. Lamp0 bool
  20. Lamp1 bool
  21. }
  22. func main() {
  23. //Create a local webserver
  24. go func() {
  25. fs := http.FileServer(http.Dir("./web"))
  26. http.Handle("/", fs)
  27. http.HandleFunc("/~/mn-cse/mn-name/sm_sensor_1", oprHandler)
  28. http.HandleFunc("/getStatus", statusHandler)
  29. http.ListenAndServe(":8282", nil)
  30. }()
  31. input := ""
  32. for input != "exit" {
  33. input = StringPrompt("osgi> ")
  34. EmulateInput(input)
  35. }
  36. }
  37. func EmulateInput(input string) {
  38. if input == "ss" {
  39. fakeSSOutput, _ := ioutil.ReadFile("ssOutput.txt")
  40. output := string(fakeSSOutput)
  41. lines := strings.Split(output, "\n")
  42. for i := 31; i < 57; i++ {
  43. status := "RESOLVED"
  44. if emulatedServiceStated[i-31] == 0 {
  45. } else if emulatedServiceStated[i-31] == 1 {
  46. status = "ACTIVE "
  47. } else if emulatedServiceStated[i-31] == 2 {
  48. status = "STARTING"
  49. }
  50. fmt.Println(strconv.Itoa(i) + " " + status + " " + lines[i-31])
  51. }
  52. } else if input == "start 41" {
  53. fakeStartOutput, _ := ioutil.ReadFile("startOutput.txt")
  54. output := string(fakeStartOutput)
  55. lines := strings.Split(output, "\n")
  56. for i := 0; i < len(lines); i++ {
  57. if strings.TrimSpace(lines[i]) == "" {
  58. //Give it a break to emulate loading
  59. delayTime := rand.Intn(500-200) + 200
  60. time.Sleep(time.Duration(delayTime) * time.Millisecond)
  61. fmt.Println("")
  62. } else {
  63. fmt.Println(lines[i])
  64. }
  65. }
  66. //Start the UI thread (Blocking)
  67. /*
  68. debug := true
  69. w := webview.New(debug)
  70. defer w.Destroy()
  71. w.SetTitle("Sample Simulated IPE")
  72. w.SetSize(497, 570, webview.HintFixed)
  73. w.Navigate("http://127.0.0.1:8282")
  74. w.Run()
  75. */
  76. select {}
  77. }
  78. }
  79. func StringPrompt(label string) string {
  80. var s string
  81. r := bufio.NewReader(os.Stdin)
  82. for {
  83. fmt.Fprint(os.Stderr, label+" ")
  84. s, _ = r.ReadString('\n')
  85. if s != "" {
  86. break
  87. }
  88. }
  89. return strings.TrimSpace(s)
  90. }
  91. func statusHandler(w http.ResponseWriter, r *http.Request) {
  92. currentStatus := GlobalStatus{
  93. lamp0IsOn,
  94. lamp1IsOn,
  95. }
  96. js, _ := json.Marshal(currentStatus)
  97. w.Write(js)
  98. }
  99. func oprHandler(w http.ResponseWriter, r *http.Request) {
  100. }
  101. func mv(r *http.Request, getParamter string, postMode bool) (string, error) {
  102. if postMode == false {
  103. //Access the paramter via GET
  104. keys, ok := r.URL.Query()[getParamter]
  105. if !ok || len(keys[0]) < 1 {
  106. //log.Println("Url Param " + getParamter +" is missing")
  107. return "", errors.New("GET paramter " + getParamter + " not found or it is empty")
  108. }
  109. // Query()["key"] will return an array of items,
  110. // we only want the single item.
  111. key := keys[0]
  112. return string(key), nil
  113. } else {
  114. //Access the parameter via POST
  115. r.ParseForm()
  116. x := r.Form.Get(getParamter)
  117. if len(x) == 0 || x == "" {
  118. return "", errors.New("POST paramter " + getParamter + " not found or it is empty")
  119. }
  120. return string(x), nil
  121. }
  122. }