Skip to content
Permalink
Browse files

add timeout to network client

  • Loading branch information...
cbrake committed Sep 6, 2019
1 parent b67ac18 commit 56e0eb8e04fecfbd6775f91d2bcd9666a41183e8
Showing with 11 additions and 6 deletions.
  1. +9 −4 api/client.go
  2. +2 −2 sim/device.go
@@ -7,15 +7,20 @@ import (
"io/ioutil"
"log"
"net/http"
"time"

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

// NewSendSamples returns a function that can be used to send samples
// to a SimpleIoT portal instance
func NewSendSamples(portalURL string, debug bool) func(string, []data.Sample) error {
return func(id string, samples []data.Sample) error {
sampleURL := portalURL + "/v1/devices/" + id + "/samples"
func NewSendSamples(portalURL, deviceID string, timeout time.Duration, debug bool) func([]data.Sample) error {
var netClient = &http.Client{
Timeout: timeout,
}

return func(samples []data.Sample) error {
sampleURL := portalURL + "/v1/devices/" + deviceID + "/samples"

tempJSON, err := json.Marshal(samples)
if err != nil {
@@ -26,7 +31,7 @@ func NewSendSamples(portalURL string, debug bool) func(string, []data.Sample) er
log.Println("Sending samples: ", string(tempJSON))
}

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

if err != nil {
return err
@@ -16,7 +16,7 @@ func packetDelay() {
func DeviceSim(portal, deviceID string) {
log.Printf("starting simulator: ID: %v, portal: %v\n", deviceID, portal)

sendSamples := api.NewSendSamples(portal, false)
sendSamples := api.NewSendSamples(portal, deviceID, time.Second*10, false)
tempSim := NewSim(72, 0.2, 70, 75)
voltSim := NewSim(2, 0.1, 1, 5)
voltSim2 := NewSim(5, 0.5, 1, 10)
@@ -40,7 +40,7 @@ func DeviceSim(portal, deviceID string) {
Value: voltSim2.Sim(),
}

err := sendSamples(deviceID, samples)
err := sendSamples(samples)
if err != nil {
log.Println("Error sending samples: ", err)
}

0 comments on commit 56e0eb8

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