Kaynağa Gözat

finalized working in Ubuntu

TC 3 yıl önce
ebeveyn
işleme
17eecb6b41
2 değiştirilmiş dosya ile 53 ekleme ve 10 silme
  1. BIN
      lab6
  2. 53 10
      main.go

+ 53 - 10
main.go

@@ -9,9 +9,12 @@ import (
 	"math/rand"
 	"net/http"
 	"os"
+	"os/exec"
 	"strconv"
 	"strings"
 	"time"
+
+	"github.com/webview/webview"
 )
 
 var lamp0IsOn bool = false
@@ -25,10 +28,17 @@ type GlobalStatus struct {
 }
 
 func main() {
+	//Clear terminal screen
+	cmd := exec.Command("clear") //Linux example, its tested
+	cmd.Stdout = os.Stdout
+	cmd.Run()
+
 	//Create a local webserver
 	go func() {
 		fs := http.FileServer(http.Dir("./web"))
 		http.Handle("/", fs)
+		http.HandleFunc("/~/mn-cse/mn-name/sm_sensor_1/sm_DATA", cinHandler)
+		http.HandleFunc("/~/mn-cse/", createAEHandler)
 		http.HandleFunc("/~/mn-cse/mn-name/sm_sensor_1", oprHandler)
 		http.HandleFunc("/getStatus", statusHandler)
 
@@ -75,16 +85,14 @@ func EmulateInput(input string) {
 		}
 
 		//Start the UI thread (Blocking)
-		/*
-			debug := true
-			w := webview.New(debug)
-			defer w.Destroy()
-			w.SetTitle("Sample Simulated IPE")
-			w.SetSize(497, 570, webview.HintFixed)
-			w.Navigate("http://127.0.0.1:8282")
-			w.Run()
-		*/
-		select {}
+		debug := true
+		w := webview.New(debug)
+		defer w.Destroy()
+		w.SetTitle("Sample Simulated IPE")
+		w.SetSize(582, 580, webview.HintFixed)
+		w.Navigate("http://127.0.0.1:8282")
+		w.Run()
+
 	}
 }
 
@@ -112,7 +120,42 @@ func statusHandler(w http.ResponseWriter, r *http.Request) {
 }
 
 func oprHandler(w http.ResponseWriter, r *http.Request) {
+	time.Sleep(300 * time.Millisecond)
+	if r.Method == http.MethodDelete {
+		w.WriteHeader(http.StatusNotFound)
+		return
+	} else if r.Method == http.MethodPost {
+		/*
+			content, err := ioutil.ReadAll(r.Body)
+			if err != nil {
+				panic(err)
+			}
+
+			log.Println("Create CNT:", string(content))
+		*/
+		w.WriteHeader(http.StatusCreated)
+		return
+	}
+}
+
+func createAEHandler(w http.ResponseWriter, r *http.Request) {
+	w.WriteHeader(http.StatusCreated)
+}
+
+func cinHandler(w http.ResponseWriter, r *http.Request) {
+	time.Sleep(300 * time.Millisecond)
+	content, err := ioutil.ReadAll(r.Body)
+	if err != nil {
+		panic(err)
+	}
+
+	if strings.Contains(string(content), "ClBSRUZJWCBzb3NhOiA8aHR0cDovL3d3dy53My5vcmcvbnMvc29zYS8+ClBSRUZJWCBleHE6IDxodHRwOi8vZXhhbXBsZS5vcmcvbnMjPgpTRUxFQ1QgP3Jlc3VsdApXSEVSRSB7CiAgICBleHE6b2JzMDA2IGEgc29zYTpPYnNlcnZhdGlvbjsKICAgICAgICBzb3NhOmhhc1Jlc3VsdCA/dmFsCiAgICBCSU5EICgKICAgICAgQ09BTEVTQ0UoCiAgICAgICAgSUYoP3ZhbCA+PSAzMCwgImh0dHA6Ly8xMjcuMC4wLjE6ODA4MC9+L21uLWNzZS9tbi1uYW1lL0xBTVBfMT9vcD1zZXRPbiZsYW1waWQ9TEFNUF8xIiwgMS8wKSwKICAgICAgICBJRig/dmFsIDw9IDI1LCAiaHR0cDovLzEyNy4wLjAuMTo4MDgwL34vbW4tY3NlL21uLW5hbWUvTEFNUF8xP29wPXNldE9mZiZsYW1waWQ9TEFNUF8xIiwgMS8wKSwKICAgICAgICAiRVJST1IiCiAgICAgICkgQVMgP3Jlc3VsdAogICAgKQp9Cg==") {
+		//Set lightbulb 0 to on
+		lamp0IsOn = !lamp0IsOn
+	}
 
+	//log.Println("CREATE CIN", string(content))
+	w.WriteHeader(http.StatusCreated)
 }
 
 func mv(r *http.Request, getParamter string, postMode bool) (string, error) {