Created
June 18, 2020 04:09
-
-
Save Niraj-Fonseka/d6670af1b61d52058915432ef10b9d6a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sensor | |
import ( | |
"log" | |
"math/rand" | |
"sync" | |
"time" | |
) | |
type Sensor struct { | |
Data map[string]int64 | |
M *sync.RWMutex | |
} | |
//NewSensor creates a new sensor object | |
func NewSensor() *Sensor { | |
return &Sensor{ | |
Data: make(map[string]int64), | |
M: &sync.RWMutex{}, | |
} | |
} | |
func (s *Sensor) SetTempSensor() { | |
for { | |
s.M.Lock() | |
s.Data["temp"] = int64(rand.Intn(120)) | |
s.M.Unlock() | |
time.Sleep(5 * time.Second) | |
} | |
} | |
func (s *Sensor) SetHumiditySensor() { | |
for { | |
s.M.Lock() | |
s.Data["humidity"] = int64(rand.Intn(100)) | |
s.M.Unlock() | |
time.Sleep(2 * time.Second) | |
} | |
} | |
//StartMonitoring will start fetch data from fake sensors in Goroutines | |
func (s *Sensor) StartMonitoring() { | |
log.Println("Start monitoring...") | |
go s.SetHumiditySensor() | |
go s.SetTempSensor() | |
} | |
//GetTempSensor returns the latest temperature sensor data | |
func (s *Sensor) GetTempSensor() int64 { | |
s.M.RLock() | |
defer s.M.RUnlock() | |
return s.Data["temp"] | |
} | |
//GetHumiditySensor returns the latest temperature sensor data | |
func (s *Sensor) GetHumiditySensor() int64 { | |
s.M.RLock() | |
defer s.M.RUnlock() | |
return s.Data["humidity"] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment