33
44package director
55
6- import "os"
7- import "io"
8- import "bufio"
9-
10- import "fmt"
11- import "time"
12- import "strconv"
13- import "strings"
14-
15- import "errors"
16-
17- import "encoding/csv"
18-
19- import "github.com/proactivity-lab/go-loggers"
20- import "github.com/proactivity-lab/go-moteconnection"
21-
22- import dp "github.com/thinnect/go-devparam"
6+ import (
7+ "bufio"
8+ "encoding/csv"
9+ "errors"
10+ "fmt"
11+ "io"
12+ "os"
13+ "strconv"
14+ "strings"
15+ "time"
16+
17+ "github.com/proactivity-lab/go-loggers"
18+ "github.com/raidoz/go-moteconnection"
19+
20+ dp "github.com/thinnect/go-devparam"
21+ )
2322
2423type DeviceParameterTask struct {
25- Address moteconnection.AMAddr
24+ Address moteconnection.EUI64
2625 Parameter string
2726 Type dp.DeviceParameterType
2827 Desired []byte
@@ -36,9 +35,10 @@ type DeviceParameterTask struct {
3635type DeviceParameterDirector struct {
3736 loggers.DIWEloggers
3837
39- conn moteconnection.MoteConnection
40- group moteconnection.AMGroup
41- address moteconnection.AMAddr
38+ conn moteconnection.MoteConnection
39+ group moteconnection.AMGroup
40+ address16 moteconnection.AMAddr
41+ address64 moteconnection.EUI64
4242 //dsp moteconnection.Dispatcher
4343
4444 timeout time.Duration
@@ -66,14 +66,38 @@ func (dpd *DeviceParameterDirector) Option(opts ...option) (option, error) {
6666 return prev , nil
6767}
6868
69+ func NewMistDeviceParameterDirector (conn moteconnection.MoteConnection ,
70+ group moteconnection.AMGroup , address moteconnection.EUI64 ,
71+ opts ... option ) (* DeviceParameterDirector , error ) {
72+
73+ dpd := new (DeviceParameterDirector )
74+ dpd .conn = conn
75+ dpd .group = group
76+ dpd .address64 = address
77+
78+ dpd .timeout = 30 * time .Second
79+ dpd .retries = 2
80+
81+ dpd .interrupt = make (chan bool )
82+
83+ for _ , opt := range opts {
84+ _ , err := opt (dpd )
85+ if err != nil {
86+ return nil , err
87+ }
88+ }
89+
90+ return dpd , nil
91+ }
92+
6993func NewDeviceParameterDirector (conn moteconnection.MoteConnection ,
7094 group moteconnection.AMGroup , address moteconnection.AMAddr ,
7195 opts ... option ) (* DeviceParameterDirector , error ) {
7296
7397 dpd := new (DeviceParameterDirector )
7498 dpd .conn = conn
7599 dpd .group = group
76- dpd .address = address
100+ dpd .address16 = address
77101
78102 dpd .timeout = 30 * time .Second
79103 dpd .retries = 2
@@ -165,7 +189,7 @@ func (dpd *DeviceParameterDirector) run() {
165189 interrupted := false
166190 for interrupted == false {
167191 // organize a queue of nodes
168- ns := make (map [moteconnection.AMAddr ]bool )
192+ ns := make (map [moteconnection.EUI64 ]bool )
169193 for _ , task := range dpd .tasks {
170194 if task .Disabled == false && task .Blocked == false && task .Actual == nil {
171195 ns [task .Address ] = true
@@ -174,15 +198,20 @@ func (dpd *DeviceParameterDirector) run() {
174198 if len (ns ) == 0 {
175199 break
176200 }
177- q := make ([]moteconnection.AMAddr , 0 , len (ns ))
201+ q := make ([]moteconnection.EUI64 , 0 , len (ns ))
178202 for k := range ns {
179203 q = append (q , k )
180204 }
181205
182206 dpd .Debug .Printf ("%d nodes in queue\n " , len (q ))
183207 // start processing the queue
184208 for _ , node := range q {
185- dpm := dp .NewDeviceParameterActiveMessageManager (dpd .conn , dpd .group , dpd .address , node )
209+ var dpm * dp.DeviceParameterManager
210+ if dpd .address64 != 0 {
211+ dpm = dp .NewDeviceParameterMistCommManager (dpd .conn , dpd .group , dpd .address64 , node )
212+ } else {
213+ dpm = dp .NewDeviceParameterActiveMessageManager (dpd .conn , dpd .group , dpd .address16 , moteconnection .AMAddr (node ))
214+ }
186215 dpm .SetTimeout (dpd .timeout )
187216 dpm .SetRetries (int (dpd .retries ))
188217
@@ -312,14 +341,15 @@ func (dpd *DeviceParameterDirector) readTaskFile(filepath string) ([]DeviceParam
312341 }
313342
314343 // validate node address
315- addr64 , err := strconv .ParseUint (line [0 ], 16 , 16 )
344+ addr64 , err := strconv .ParseUint (line [0 ], 16 , 64 )
316345 if err != nil {
317346 return nil , err
318347 }
319- addr := moteconnection .AMAddr (addr64 )
320348
321- if 0 < addr && addr < 0xFFFF {
322- task .Address = addr
349+ if dpd .address64 != 0 && 0 < addr64 && addr64 < 0xFFFFFFFFFFFFFFFF {
350+ task .Address = moteconnection .EUI64 (addr64 )
351+ } else if dpd .address16 != 0 && 0 < addr64 && addr64 < 0xFFFF {
352+ task .Address = moteconnection .EUI64 (addr64 )
323353 } else {
324354 return nil , errors .New (fmt .Sprintf ("'%s' is not a valid address!" , line [0 ]))
325355 }
@@ -363,7 +393,7 @@ func (dpd *DeviceParameterDirector) readTaskFile(filepath string) ([]DeviceParam
363393 return tasks , nil
364394}
365395
366- func (dpd * DeviceParameterDirector ) readNodeFile (filepath string ) ([]moteconnection.AMAddr , error ) {
396+ func (dpd * DeviceParameterDirector ) readNodeFile (filepath string ) ([]moteconnection.EUI64 , error ) {
367397 nf , err := os .Open (filepath )
368398 if err != nil {
369399 return nil , err
@@ -372,12 +402,12 @@ func (dpd *DeviceParameterDirector) readNodeFile(filepath string) ([]moteconnect
372402
373403 scanner := bufio .NewScanner (bufio .NewReader (nf ))
374404
375- nodes := make ([]moteconnection.AMAddr , 0 )
405+ nodes := make ([]moteconnection.EUI64 , 0 )
376406 for scanner .Scan () {
377407 t := strings .TrimSpace (scanner .Text ())
378408 if len (t ) > 0 && strings .HasPrefix (t , "#" ) == false {
379- if addr , err := strconv .ParseUint (t , 16 , 16 ); err == nil {
380- nodes = append (nodes , moteconnection .AMAddr (addr ))
409+ if addr , err := strconv .ParseUint (t , 16 , 64 ); err == nil {
410+ nodes = append (nodes , moteconnection .EUI64 (addr ))
381411 } else {
382412 return nil , err
383413 }
0 commit comments