Skip to content

Commit

Permalink
SMC768 module, chart, view
Browse files Browse the repository at this point in the history
  • Loading branch information
parMaster committed Nov 5, 2023
1 parent 379d0c8 commit c00b6ba
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 7 deletions.
2 changes: 2 additions & 0 deletions config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ modules:
addr: 0x40
system:
enabled: true
# smc768: # for Macmini 2014 with SMC768
# enabled: true
19 changes: 12 additions & 7 deletions mod-smc768.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ var Sensors = []string{
"ThrottleTime", // Core throttle time in milliseconds
}

type Smc768Response map[string]string
type Smc768Data map[string]string

type Smc768Response map[string][]string

type Smc768Reporter struct {
data Smc768Response
Expand All @@ -53,7 +55,7 @@ func LoadSmc768Reporter(cfg config.Smc768, store storage.Storer, dbg bool) (*Smc
return &Smc768Reporter{
dbg: dbg,
store: store,
data: make(map[string]string),
data: make(Smc768Response),
}, nil
}

Expand All @@ -65,14 +67,14 @@ func (r *Smc768Reporter) Collect(ctx context.Context) (err error) {
r.mx.Lock()
defer r.mx.Unlock()

r.data = r.ReadSMC768()
data := r.ReadSMC768()
if err != nil {
return errors.Join(err, fmt.Errorf("failed to get load avg: %v", err))
}

if r.store != nil {
for _, label := range Sensors {
err := r.store.Write(ctx, model.Data{Module: r.Name(), Topic: label, Value: r.data[label]})
err := r.store.Write(ctx, model.Data{Module: r.Name(), Topic: label, Value: data[label]})
if err != nil {
return errors.Join(err, fmt.Errorf("failed to write to storage: %v", err))
}
Expand All @@ -87,21 +89,24 @@ func (r *Smc768Reporter) Report() (interface{}, error) {
return r.data, nil
}

func (r *Smc768Reporter) ReadSMC768() Smc768Response {
func (r *Smc768Reporter) ReadSMC768() Smc768Data {

data := make(Smc768Response)
data := make(Smc768Data)

// Read the data from the SMC768 and store it in the data map
for i := 1; i <= 60; i++ {
value := ReadInput(fmt.Sprintf("/sys/devices/platform/applesmc.768/temp%d_input", i))
label := ReadInput(fmt.Sprintf("/sys/devices/platform/applesmc.768/temp%d_label", i))
if slices.Contains(Sensors, label) {
data[label] = value
r.data[label] = append(r.data[label], value)
}
}

data["Exhaust"] = ReadInput("/sys/devices/platform/applesmc.768/fan1_input")
r.data["Exhaust"] = append(r.data["Exhaust"], data["Exhaust"])
data["ThrottleTime"] = ReadInput("/sys/devices/system/cpu/cpu0/thermal_throttle/core_throttle_total_time_ms")
r.data["ThrottleTime"] = append(r.data["ThrottleTime"], data["ThrottleTime"])

if r.dbg {
log.Printf("[DEBUG] Smc768Reporter: data:")
Expand All @@ -120,7 +125,7 @@ func ReadInput(file string) (v string) {
fmt.Println(err)
}

v = strings.TrimSpace(string(value))
v = strings.Trim(strings.TrimSpace(string(value)), "\n")

return
}
Expand Down
39 changes: 39 additions & 0 deletions web/chart.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<div id='TimeInState'><!-- Plotly chart will be drawn inside this DIV --></div>
<div id='AmbTempChart'><!-- Plotly chart will be drawn inside this DIV --></div>
<div id='PressureChart'><!-- Plotly chart will be drawn inside this DIV --></div>
<div id='Smc768Chart'><!-- Plotly chart will be drawn inside this DIV --></div>
<!-- theme switch -->
<div id="theme-switch" style="position: absolute; top: 0; left: 0; padding: 4px; font-size: small; background-color: rgba(0,0,0,0.5); color: white; cursor: pointer;">
<span id="theme-switch-text">Dark/Light</span>
Expand Down Expand Up @@ -207,6 +208,44 @@
Plotly.newPlot('TimeInState', [TimeInState], TISlayout);
}

// check if there is object with name "Modules" and if it has "bmp280" and "htu21" keys
if (data["Modules"] && data["Modules"]["smc768"]
&& data["Modules"]["smc768"]["TC0C"]
&& data["Modules"]["smc768"]["Exhaust"] ) {

var cpu_temp = {
x: data["Dates"],
y: data["Modules"]["smc768"]["TC0C"],
type: 'scatter',
name: 'CPU core Temp, m˚C'
};
var fan_rpm = {
x: data["Dates"],
y: data["Modules"]["smc768"]["Exhaust"],
type: 'scatter',
name: 'Fan RPM',
yaxis: 'y2',
};

var Layout = {
yaxis: {
title: 'CPU core Temp, m˚C',
gridcolor: 'rgba(99, 110, 250, 0.2)'
},
yaxis2: {
title: 'Fan RPM',
overlaying: 'y',
side: 'right',
gridcolor: 'rgba(239, 85, 59, 0.2)'
},
margin: {"t": 32, "b": 0, "l": 0, "r": 0},
height: 400,
template: template
}

Plotly.newPlot('Smc768Chart', [cpu_temp, fan_rpm], Layout);
}

tempDiv.on('plotly_relayout', function(eventdata){
Plotly.relayout(loadDiv, eventdata);
Plotly.relayout('AmbTempChart', eventdata);
Expand Down
58 changes: 58 additions & 0 deletions web/view.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<div id='main'><!-- CPU temp and fan RPM chart --></div>
<div id='la5m'><!-- 5 minute load average chart --></div>
<div id='bmp280'><!-- atmospheric pressure chart --></div>
<div id='smc768'><!-- atmospheric pressure chart --></div>
<!-- theme switch -->
<div id="theme-switch" style="position: absolute; top: 0; left: 0; padding: 4px; font-size: small; background-color: rgba(0,0,0,0.5); color: white; cursor: pointer;">
<span id="theme-switch-text">Dark/Light</span>
Expand Down Expand Up @@ -103,6 +104,62 @@
Plotly.newPlot('main', plots, MainLayout);
}

async function loadSmc768() {
let data = await getData("smc768");

// check if there temp data
if (data == null || data["TC0C"] == null) {
return;
}

for (var key in data["TC0C"]) {
data["TC0C"][key] = parseInt(data["TC0C"][key]);
}

var temp = {
x: Object.keys(data["TC0C"]),
y: Object.values(data["TC0C"]),
type: 'scatter',
name: 'CPU, m˚C'
};

var Layout = {
yaxis: {
title: 'CPU, m˚C',
gridcolor: 'rgba(99, 110, 250, 0.2)'
},
margin: {"t": 32, "b": 0, "l": 0, "r": 0},
height: 400,
template: template
}
plots = [temp];

// check if there rpm data
if (data["Exhaust"] != null) {

for (var key in data["Exhaust"]) {
data["Exhaust"][key] = parseInt(data["Exhaust"][key]);
}

var rpm = {
x: Object.keys(data["Exhaust"]),
y: Object.values(data["Exhaust"]),
type: 'scatter',
name: 'Fan RPM',
yaxis: 'y2',
};
Layout.yaxis2 = {
title: 'Fan RPM',
overlaying: 'y',
side: 'right',
gridcolor: 'rgba(239, 85, 59, 0.2)',
showgrid: false,
}
plots.push(rpm);
}
Plotly.newPlot('smc768', plots, Layout);
}

async function loadLa5m() {
let data = await getData("system");

Expand Down Expand Up @@ -180,6 +237,7 @@
await loadMain();
await loadLa5m();
await loadBMP280();
await loadSmc768();

var mainDiv = document.getElementById('main');
var la5mDiv = document.getElementById('la5m');
Expand Down

0 comments on commit c00b6ba

Please sign in to comment.