main.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. "os/exec"
  12. "strconv"
  13. "strings"
  14. "time"
  15. "github.com/webview/webview"
  16. )
  17. var lamp0IsOn bool = false
  18. var lamp1IsOn bool = false
  19. 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}
  20. type GlobalStatus struct {
  21. Lamp0 bool
  22. Lamp1 bool
  23. }
  24. func main() {
  25. //Clear terminal screen
  26. cmd := exec.Command("clear") //Linux example, its tested
  27. cmd.Stdout = os.Stdout
  28. cmd.Run()
  29. //Create a local webserver
  30. go func() {
  31. fs := http.FileServer(http.Dir("./web"))
  32. http.Handle("/", fs)
  33. http.HandleFunc("/~/mn-cse/mn-name/sm_sensor_1/sm_DATA", cinHandler)
  34. http.HandleFunc("/~/mn-cse/", createAEHandler)
  35. http.HandleFunc("/~/mn-cse/mn-name/sm_sensor_1", oprHandler)
  36. http.HandleFunc("/getStatus", statusHandler)
  37. http.ListenAndServe(":8282", nil)
  38. }()
  39. input := ""
  40. for input != "exit" {
  41. input = StringPrompt("osgi> ")
  42. EmulateInput(input)
  43. }
  44. }
  45. func EmulateInput(input string) {
  46. if input == "ss" {
  47. fakeSSOutput, _ := ioutil.ReadFile("ssOutput.txt")
  48. output := string(fakeSSOutput)
  49. lines := strings.Split(output, "\n")
  50. for i := 31; i < 57; i++ {
  51. status := "RESOLVED"
  52. if emulatedServiceStated[i-31] == 0 {
  53. } else if emulatedServiceStated[i-31] == 1 {
  54. status = "ACTIVE "
  55. } else if emulatedServiceStated[i-31] == 2 {
  56. status = "STARTING"
  57. }
  58. fmt.Println(strconv.Itoa(i) + " " + status + " " + lines[i-31])
  59. }
  60. } else if input == "start 41" {
  61. fakeStartOutput, _ := ioutil.ReadFile("startOutput.txt")
  62. output := string(fakeStartOutput)
  63. lines := strings.Split(output, "\n")
  64. for i := 0; i < len(lines); i++ {
  65. if strings.TrimSpace(lines[i]) == "" {
  66. //Give it a break to emulate loading
  67. delayTime := rand.Intn(500-200) + 200
  68. time.Sleep(time.Duration(delayTime) * time.Millisecond)
  69. fmt.Println("")
  70. } else {
  71. fmt.Println(lines[i])
  72. }
  73. }
  74. //Start the UI thread (Blocking)
  75. debug := true
  76. w := webview.New(debug)
  77. defer w.Destroy()
  78. w.SetTitle("Sample Simulated IPE")
  79. w.SetSize(582, 580, webview.HintFixed)
  80. w.Navigate("http://127.0.0.1:8282")
  81. w.Run()
  82. }
  83. }
  84. func StringPrompt(label string) string {
  85. var s string
  86. r := bufio.NewReader(os.Stdin)
  87. for {
  88. fmt.Fprint(os.Stderr, label+" ")
  89. s, _ = r.ReadString('\n')
  90. if s != "" {
  91. break
  92. }
  93. }
  94. return strings.TrimSpace(s)
  95. }
  96. func statusHandler(w http.ResponseWriter, r *http.Request) {
  97. currentStatus := GlobalStatus{
  98. lamp0IsOn,
  99. lamp1IsOn,
  100. }
  101. js, _ := json.Marshal(currentStatus)
  102. w.Write(js)
  103. }
  104. func oprHandler(w http.ResponseWriter, r *http.Request) {
  105. time.Sleep(300 * time.Millisecond)
  106. if r.Method == http.MethodDelete {
  107. w.WriteHeader(http.StatusNotFound)
  108. return
  109. } else if r.Method == http.MethodPost {
  110. /*
  111. content, err := ioutil.ReadAll(r.Body)
  112. if err != nil {
  113. panic(err)
  114. }
  115. log.Println("Create CNT:", string(content))
  116. */
  117. w.WriteHeader(http.StatusCreated)
  118. return
  119. }
  120. }
  121. func createAEHandler(w http.ResponseWriter, r *http.Request) {
  122. w.WriteHeader(http.StatusCreated)
  123. }
  124. func cinHandler(w http.ResponseWriter, r *http.Request) {
  125. time.Sleep(300 * time.Millisecond)
  126. content, err := ioutil.ReadAll(r.Body)
  127. if err != nil {
  128. panic(err)
  129. }
  130. if strings.Contains(string(content), "ClBSRUZJWCBzb3NhOiA8aHR0cDovL3d3dy53My5vcmcvbnMvc29zYS8+ClBSRUZJWCBleHE6IDxodHRwOi8vZXhhbXBsZS5vcmcvbnMjPgpTRUxFQ1QgP3Jlc3VsdApXSEVSRSB7CiAgICBleHE6b2JzMDA2IGEgc29zYTpPYnNlcnZhdGlvbjsKICAgICAgICBzb3NhOmhhc1Jlc3VsdCA/dmFsCiAgICBCSU5EICgKICAgICAgQ09BTEVTQ0UoCiAgICAgICAgSUYoP3ZhbCA+PSAzMCwgImh0dHA6Ly8xMjcuMC4wLjE6ODA4MC9+L21uLWNzZS9tbi1uYW1lL0xBTVBfMT9vcD1zZXRPbiZsYW1waWQ9TEFNUF8xIiwgMS8wKSwKICAgICAgICBJRig/dmFsIDw9IDI1LCAiaHR0cDovLzEyNy4wLjAuMTo4MDgwL34vbW4tY3NlL21uLW5hbWUvTEFNUF8xP29wPXNldE9mZiZsYW1waWQ9TEFNUF8xIiwgMS8wKSwKICAgICAgICAiRVJST1IiCiAgICAgICkgQVMgP3Jlc3VsdAogICAgKQp9Cg==") {
  131. //Set lightbulb 0 to on
  132. lamp0IsOn = !lamp0IsOn
  133. }
  134. //log.Println("CREATE CIN", string(content))
  135. w.WriteHeader(http.StatusCreated)
  136. }
  137. func mv(r *http.Request, getParamter string, postMode bool) (string, error) {
  138. if postMode == false {
  139. //Access the paramter via GET
  140. keys, ok := r.URL.Query()[getParamter]
  141. if !ok || len(keys[0]) < 1 {
  142. //log.Println("Url Param " + getParamter +" is missing")
  143. return "", errors.New("GET paramter " + getParamter + " not found or it is empty")
  144. }
  145. // Query()["key"] will return an array of items,
  146. // we only want the single item.
  147. key := keys[0]
  148. return string(key), nil
  149. } else {
  150. //Access the parameter via POST
  151. r.ParseForm()
  152. x := r.Form.Get(getParamter)
  153. if len(x) == 0 || x == "" {
  154. return "", errors.New("POST paramter " + getParamter + " not found or it is empty")
  155. }
  156. return string(x), nil
  157. }
  158. }