Skip to content
Snippets Groups Projects
Commit fc2b7668 authored by KERDREUX Jerome's avatar KERDREUX Jerome
Browse files

Rewrite the code to avoid wrapping and globals

parent 1c07b20e
No related branches found
No related tags found
No related merge requests found
......@@ -17,8 +17,13 @@ type Counter struct {
State bool
}
type CounterDevice struct {
devices map[uuid.UUID]*Counter
engine *xaal.Engine
device *xaal.Device
}
var (
devices map[uuid.UUID]*Counter
deviceIcons = map[string]string{
"lamp": " ",
"motion": "󰶑 ",
......@@ -26,44 +31,39 @@ var (
}
)
func getCounter(msg *xaal.Message) *Counter {
cnt, found := devices[msg.Source]
// ----------------------------------------------------------------------------
// CounterDevice
// -----------------------------------------------------------------------------
func NewCounterDevice(eng *xaal.Engine) *CounterDevice {
dev := schemas.NewHmi(uuid.Random())
return &CounterDevice{
devices: make(map[uuid.UUID]*Counter),
engine: eng,
device: dev,
}
}
func (d *CounterDevice) getCounter(msg *xaal.Message) *Counter {
cnt, found := d.devices[msg.Source]
if found {
return cnt
}
return nil
}
func addCounter(msg *xaal.Message, prefix string) *Counter {
func (d *CounterDevice) addCounter(msg *xaal.Message, prefix string) *Counter {
cnt := &Counter{
Prefix: prefix,
State: false,
}
devices[msg.Source] = cnt
d.devices[msg.Source] = cnt
return cnt
}
func filterCounter(msg *xaal.Message) string {
// let's split the msg.DevType on points and keep the first part
tmp := strings.Split(msg.DevType, ".")
prefix := tmp[0]
_, found := deviceIcons[prefix]
if !found {
return ""
}
return prefix
}
func getAttribute(msg *xaal.Message, attr string) bool {
if value, ok := msg.Body[attr].(bool); ok {
return value
}
return false
}
func updateCounter(msg *xaal.Message, prefix string) {
func (d *CounterDevice) updateCounter(msg *xaal.Message, prefix string) {
// we can extract from msg, but already done so use it
cnt := getCounter(msg)
cnt := d.getCounter(msg)
old := cnt.State
// Lamps
......@@ -80,58 +80,43 @@ func updateCounter(msg *xaal.Message, prefix string) {
}
if cnt.State != old {
display()
d.display()
}
}
func updateAlive(msg *xaal.Message) {
cnt := getCounter(msg)
func (d *CounterDevice) updateAlive(msg *xaal.Message) {
cnt := d.getCounter(msg)
if timeout, ok := msg.Body["timeout"].(uint64); ok {
cnt.Until = time.Now().Add(time.Duration(timeout) * time.Second)
}
}
// display outputs the result on stdout and flush
func display() {
// slog.Info("Devices", "count", len(devices))
displayPrefix("lamp")
fmt.Print(" ")
displayPrefix("motion")
fmt.Print(" ")
displayPrefix("contact")
fmt.Print("\n")
}
func displayPrefix(prefix string) string {
return fmt.Sprintf("%s%d", deviceIcons[prefix], countPrefix(prefix))
}
func handleMessage(eng *xaal.Engine, msg *xaal.Message) {
func (d *CounterDevice) handleMessage(msg *xaal.Message) {
// let's filter the messages we are interested in
prefix := filterCounter(msg)
prefix := getPrefix(msg)
if prefix == "" {
return
}
cnt := getCounter(msg)
cnt := d.getCounter(msg)
if cnt == nil {
cnt = addCounter(msg, prefix)
sendGetAttritube(eng, msg.Source)
cnt = d.addCounter(msg, prefix)
d.sendGetAttritube(msg.Source)
}
if msg.IsAlive() {
updateAlive(msg)
d.updateAlive(msg)
}
if msg.IsAttributesChange() || msg.IsGetAttributes() {
updateCounter(msg, prefix)
d.updateCounter(msg, prefix)
}
}
// countPrefix counts the number of devices with a given prefix
func countPrefix(prefix string) int {
func (d *CounterDevice) countPrefix(prefix string) int {
total := 0
for _, cnt := range devices {
for _, cnt := range d.devices {
if cnt.Prefix == prefix && cnt.State {
total++
}
......@@ -139,21 +124,56 @@ func countPrefix(prefix string) int {
return total
}
func sendGetAttritube(eng *xaal.Engine, addr uuid.UUID) {
dev := schemas.NewHmi(uuid.Random())
// isAlive sends a message to all devices asking if they are alive
func (d *CounterDevice) sendIsAlive() {
body := make(xaal.MessageBody)
msg := eng.MessagesFactory.BuildMessage(dev, []uuid.UUID{addr}, xaal.MSGTypeRequest, string(xaal.MSGActionGetAttributes), body)
eng.Send(msg)
body["dev_types"] = []string{"any.any"}
msg := d.engine.MessagesFactory.BuildMessage(d.device, []uuid.UUID{uuid.Nil}, xaal.MSGTypeRequest, string(xaal.MSGActionIsAlive), body)
d.engine.Send(msg)
}
// isAlive sends a message to all devices asking if they are alive
func isAlive(eng *xaal.Engine) {
addr := uuid.Random()
dev := schemas.NewHmi(addr)
// sendGetAttritube sends a message to all devices asking for their attributes
func (d *CounterDevice) sendGetAttritube(addr uuid.UUID) {
body := make(xaal.MessageBody)
body["dev_types"] = []string{"any.any"}
msg := eng.MessagesFactory.BuildMessage(dev, []uuid.UUID{uuid.Nil}, xaal.MSGTypeRequest, string(xaal.MSGActionIsAlive), body)
eng.Send(msg)
msg := d.engine.MessagesFactory.BuildMessage(d.device, []uuid.UUID{addr}, xaal.MSGTypeRequest, string(xaal.MSGActionGetAttributes), body)
d.engine.Send(msg)
}
// display outputs the result on stdout and flush
func (d *CounterDevice) display() {
// slog.Info("Devices", "count", len(devices))
prefixs := []string{"lamp", "motion", "contact"}
for _, prefix := range prefixs {
fmt.Print(d.displayPrefix(prefix))
fmt.Print(" ")
}
fmt.Print("\n")
}
func (d *CounterDevice) displayPrefix(prefix string) string {
return fmt.Sprintf("%s%d", deviceIcons[prefix], d.countPrefix(prefix))
}
// ----------------------------------------------------------------------------
// Message handling functions
// -----------------------------------------------------------------------------
func getPrefix(msg *xaal.Message) string {
// let's split the msg.DevType on points and keep the first part
tmp := strings.Split(msg.DevType, ".")
prefix := tmp[0]
_, found := deviceIcons[prefix]
if !found {
return ""
}
return prefix
}
func getAttribute(msg *xaal.Message, attr string) bool {
if value, ok := msg.Body[attr].(bool); ok {
return value
}
return false
}
func init() {
......@@ -163,15 +183,9 @@ func init() {
func main() {
eng := xaal.NewEngine()
eng.DisableMessageFilter()
dev := NewCounterDevice(eng)
devices = make(map[uuid.UUID]*Counter)
handleMessageWrapper := func(msg *xaal.Message) {
handleMessage(eng, msg)
}
eng.Subscribe(handleMessageWrapper)
isAlive(eng)
eng.Subscribe(dev.handleMessage)
dev.sendIsAlive()
eng.Run()
// Let's count the devices
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment