github.com/simpleiot/simpleiot@v0.18.3/node/onewire-manager.go (about)

     1  package node
     2  
     3  import (
     4  	"log"
     5  	"os"
     6  	"path/filepath"
     7  	"regexp"
     8  	"strconv"
     9  
    10  	"github.com/nats-io/nats.go"
    11  	"github.com/simpleiot/simpleiot/client"
    12  	"github.com/simpleiot/simpleiot/data"
    13  )
    14  
    15  // oneWireManager is responsible for finding new busses and sync database state
    16  // with what is here
    17  type oneWireManager struct {
    18  	nc         *nats.Conn
    19  	busses     map[string]*oneWire
    20  	rootNodeID string
    21  }
    22  
    23  func newOneWireManager(nc *nats.Conn, rootNodeID string) *oneWireManager {
    24  	return &oneWireManager{
    25  		nc:         nc,
    26  		busses:     make(map[string]*oneWire),
    27  		rootNodeID: rootNodeID,
    28  	}
    29  }
    30  
    31  var reBusMaster = regexp.MustCompile(`w1_bus_master(\d+)`)
    32  
    33  func (owm *oneWireManager) update() error {
    34  	nodes, err := client.GetNodes(owm.nc, owm.rootNodeID, "all", data.NodeTypeOneWire, false)
    35  	if err != nil {
    36  		return err
    37  	}
    38  
    39  	found := make(map[string]bool)
    40  
    41  	for _, node := range nodes {
    42  		found[node.ID] = true
    43  		_, ok := owm.busses[node.ID]
    44  		if !ok {
    45  			var err error
    46  			bus, err := newOneWire(owm.nc, node)
    47  			if err != nil {
    48  				log.Println("Error creating new modbus:", err)
    49  				continue
    50  			}
    51  			owm.busses[node.ID] = bus
    52  		}
    53  	}
    54  
    55  	// remove busses that have been deleted
    56  	for id, bus := range owm.busses {
    57  		_, ok := found[id]
    58  		if !ok {
    59  			// bus was deleted so close and clear it
    60  			log.Println("removing onewire bus:", bus.owNode.description)
    61  			bus.stop()
    62  			delete(owm.busses, id)
    63  		}
    64  	}
    65  
    66  	// detect one wire busses
    67  	dirs, _ := filepath.Glob("/sys/bus/w1/devices/w1_bus_master*")
    68  
    69  	for _, dir := range dirs {
    70  		f, _ := os.Stat(dir)
    71  		if f.IsDir() {
    72  			ms := reBusMaster.FindStringSubmatch(dir)
    73  			if len(ms) < 2 {
    74  				continue
    75  			}
    76  
    77  			index, err := strconv.Atoi(ms[1])
    78  
    79  			if err != nil {
    80  				log.Println("Error extracting 1-wire bus number:", err)
    81  			}
    82  
    83  			// loop through busses and make sure it exists
    84  			found := false
    85  			for _, b := range owm.busses {
    86  				if b.owNode.index == index {
    87  					found = true
    88  					break
    89  				}
    90  			}
    91  
    92  			if !found {
    93  				log.Printf("Adding 1-wire bus #%v\n", index)
    94  
    95  				n := data.NodeEdge{
    96  					Type:   data.NodeTypeOneWire,
    97  					Parent: owm.rootNodeID,
    98  					Points: data.Points{
    99  						data.Point{
   100  							Type:  data.PointTypeIndex,
   101  							Value: float64(index),
   102  						},
   103  						data.Point{
   104  							Type: data.PointTypeDescription,
   105  							Text: "New bus, please edit",
   106  						},
   107  					},
   108  				}
   109  
   110  				err := client.SendNode(owm.nc, n, "")
   111  				if err != nil {
   112  					log.Println("Error sending new 1-wire node:", err)
   113  				}
   114  			}
   115  		}
   116  	}
   117  
   118  	return nil
   119  
   120  }