github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/plugins/hdd/actor.go (about)

     1  // This file is part of the Smart Home
     2  // Program complex distribution https://github.com/e154/smart-home
     3  // Copyright (C) 2016-2023, Filippov Alex
     4  //
     5  // This library is free software: you can redistribute it and/or
     6  // modify it under the terms of the GNU Lesser General Public
     7  // License as published by the Free Software Foundation; either
     8  // version 3 of the License, or (at your option) any later version.
     9  //
    10  // This library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13  // Library General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public
    16  // License along with this library.  If not, see
    17  // <https://www.gnu.org/licenses/>.
    18  
    19  package hdd
    20  
    21  import (
    22  	"sync"
    23  
    24  	"github.com/rcrowley/go-metrics"
    25  	"github.com/shirou/gopsutil/v3/disk"
    26  
    27  	"github.com/e154/smart-home/common/events"
    28  	m "github.com/e154/smart-home/models"
    29  	"github.com/e154/smart-home/system/supervisor"
    30  )
    31  
    32  // Actor ...
    33  type Actor struct {
    34  	*supervisor.BaseActor
    35  	cores           int64
    36  	model           string
    37  	total           metrics.Gauge
    38  	free            metrics.Gauge
    39  	usedPercent     metrics.GaugeFloat64
    40  	allCpuPrevTotal float64
    41  	allCpuPrevIdle  float64
    42  	updateLock      *sync.Mutex
    43  	MountPoint      string
    44  }
    45  
    46  // NewActor ...
    47  func NewActor(entity *m.Entity,
    48  	service supervisor.Service) *Actor {
    49  
    50  	var mountPoint string
    51  	if _mountPoint, ok := entity.Settings[AttrMountPoint]; ok {
    52  		mountPoint = _mountPoint.String()
    53  	}
    54  	if mountPoint == "" {
    55  		mountPoint = "/"
    56  	}
    57  
    58  	actor := &Actor{
    59  		BaseActor:  supervisor.NewBaseActor(entity, service),
    60  		updateLock: &sync.Mutex{},
    61  		MountPoint: mountPoint,
    62  	}
    63  
    64  	actor.Attrs = NewAttr()
    65  	actor.Setts = entity.Settings
    66  
    67  	if actor.Setts == nil {
    68  		actor.Setts = NewSettings()
    69  	}
    70  
    71  	if actor.Actions == nil {
    72  		actor.Actions = NewActions()
    73  	}
    74  
    75  	return actor
    76  }
    77  
    78  func (e *Actor) Destroy() {
    79  
    80  }
    81  
    82  func (e *Actor) runAction(_ events.EventCallEntityAction) {
    83  	go e.selfUpdate()
    84  }
    85  
    86  func (e *Actor) selfUpdate() {
    87  
    88  	e.updateLock.Lock()
    89  	defer e.updateLock.Unlock()
    90  
    91  	var mountPoint = "/"
    92  	if e.MountPoint != "" {
    93  		mountPoint = e.MountPoint
    94  	}
    95  
    96  	if r, err := disk.Usage(mountPoint); err == nil {
    97  		e.AttrMu.Lock()
    98  		e.Attrs[AttrFstype].Value = r.Fstype
    99  		e.Attrs[AttrTotal].Value = r.Total
   100  		e.Attrs[AttrFree].Value = r.Free
   101  		e.Attrs[AttrUsed].Value = r.Used
   102  		e.Attrs[AttrUsedPercent].Value = r.UsedPercent
   103  		e.Attrs[AttrInodesTotal].Value = r.InodesTotal
   104  		e.Attrs[AttrInodesUsed].Value = r.InodesUsed
   105  		e.Attrs[AttrInodesFree].Value = r.InodesFree
   106  		e.Attrs[AttrInodesUsedPercent].Value = r.InodesUsedPercent
   107  		e.AttrMu.Unlock()
   108  	}
   109  	e.SaveState(false, false)
   110  }