Skip to content
Permalink
Browse files

combine server and simulator into one app

  • Loading branch information...
cbrake committed Dec 11, 2018
1 parent 1037949 commit bf4dfb460d372cf954d3934ef53289e4b0424a91
@@ -1,4 +1,4 @@
frontend/public
frontend/elm-stuff
siot-portal
/siot
data.db
@@ -1,4 +1,4 @@
package main
package api

import (
"bytes"
@@ -8,7 +8,6 @@ import (
"net/http"
"os"

"github.com/simpleiot/simpleiot/api"
"github.com/simpleiot/simpleiot/assets/frontend"
"github.com/simpleiot/simpleiot/db"
)
@@ -48,7 +47,7 @@ func (h *App) ServeHTTP(res http.ResponseWriter, req *http.Request) {
if req.URL.Path == "/" {
h.IndexHandler.ServeHTTP(res, req)
} else {
head, req.URL.Path = api.ShiftPath(req.URL.Path)
head, req.URL.Path = ShiftPath(req.URL.Path)
switch head {
case "public":
h.PublicHandler.ServeHTTP(res, req)
@@ -65,17 +64,14 @@ func NewAppHandler(db *db.Db) http.Handler {
return &App{
PublicHandler: http.FileServer(frontend.FileSystem()),
IndexHandler: NewIndexHandler(),
V1ApiHandler: api.NewV1Handler(db),
V1ApiHandler: NewV1Handler(db),
}
}

func httpServer(port string, db *db.Db) error {
address := fmt.Sprintf(":%s", port)
// Server starts a API server instance
func Server() error {
log.Println("Starting http server")
return http.ListenAndServe(address, NewAppHandler(db))
}

func main() {
port := os.Getenv("SIOT_PORT")
if port == "" {
port = "8080"
@@ -93,8 +89,6 @@ func main() {
}

log.Println("Starting portal on port: ", port)
err = httpServer(port, db)
if err != nil {
log.Println("Error starting server: ", err)
}
address := fmt.Sprintf(":%s", port)
return http.ListenAndServe(address, NewAppHandler(db))
}
File renamed without changes.
File renamed without changes.
@@ -0,0 +1,27 @@
package main

import (
"flag"
"log"

"github.com/simpleiot/simpleiot/api"
"github.com/simpleiot/simpleiot/sim"
)

func main() {
flagSim := flag.Bool("sim", false, "Start device simulator")
flagSimPortal := flag.String("simPortal", "http://localhost:8080", "Portal URL")
flagSimDeviceID := flag.String("simDeviceId", "1234", "Simulation Device ID")

flag.Parse()

if *flagSim {
sim.DeviceSim(*flagSimPortal, *flagSimDeviceID)
}

// default action is to start server
err := api.Server()
if err != nil {
log.Println("Error starting server: ", err)
}
}
@@ -47,7 +47,7 @@ app_build_dependencies() {

app_build() {
app_build_dependencies || return 1
go build -o siot cmd/portal/main.go || return 1
go build -o siot cmd/siot/main.go || return 1
return 0
}

@@ -59,7 +59,7 @@ app_deploy() {

app_run() {
app_build_dependencies || return 1
go run cmd/portal/main.go || return 1
go run cmd/siot/main.go || return 1
return 0
}

@@ -0,0 +1,68 @@
package sim

import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"log"
"net/http"
"time"

"github.com/simpleiot/simpleiot/data"
)

func packetDelay() {
time.Sleep(5 * time.Second)
}

func newSendSample(portalURL string) func(string, data.Sample) error {
return func(id string, sample data.Sample) error {
sampleURL := portalURL + "/v1/devices/" + id + "/sample"

tempJSON, err := json.Marshal(sample)
if err != nil {
log.Println("Error encoding temp: ", err)
}

resp, err := http.Post(sampleURL, "application/json", bytes.NewBuffer(tempJSON))

if err != nil {
return err
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
errstring := "Server error: " + resp.Status + " " + sampleURL
body, _ := ioutil.ReadAll(resp.Body)
errstring += " " + string(body)
return errors.New(errstring)
}

return nil
}
}

// DeviceSim simulates a simple device
func DeviceSim(portal, deviceID string) {
log.Printf("starting simulator: ID: %v, portal: %v\n", deviceID, portal)

sendSample := newSendSample(portal)
tempSim := NewSim(72, 0.2, 70, 75)
voltSim := NewSim(2, 0.1, 1, 5)

for {
tempSample := data.NewSample("T0", tempSim.Sim())
err := sendSample(deviceID, tempSample)
if err != nil {
log.Println("Error sending sample: ", err)
}
voltSample := data.NewSample("V0", voltSim.Sim())
err = sendSample(deviceID, voltSample)
if err != nil {
log.Println("Error sending sample: ", err)
}
packetDelay()
}
}

0 comments on commit bf4dfb4

Please sign in to comment.
You can’t perform that action at this time.