From eefa4a18831d0d23211f237f0758fe5ebdc179e1 Mon Sep 17 00:00:00 2001 From: Jeremy Edwards <1312331+jeremyje@users.noreply.github.com> Date: Mon, 24 Oct 2022 22:53:19 -0700 Subject: [PATCH] Drivers --- internal/drivers/drivers.go | 16 +++++++ internal/drivers/wmi_windows.go | 66 ++++++++++++++++++++++++++++ internal/drivers/wmi_windows_test.go | 27 ++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 internal/drivers/drivers.go create mode 100644 internal/drivers/wmi_windows.go create mode 100644 internal/drivers/wmi_windows_test.go diff --git a/internal/drivers/drivers.go b/internal/drivers/drivers.go new file mode 100644 index 0000000..ddae3b8 --- /dev/null +++ b/internal/drivers/drivers.go @@ -0,0 +1,16 @@ +// Copyright 2022 Jeremy Edwards +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Package drivers contains logic for accessing CPU temperature from various sources. +package drivers diff --git a/internal/drivers/wmi_windows.go b/internal/drivers/wmi_windows.go new file mode 100644 index 0000000..66b517c --- /dev/null +++ b/internal/drivers/wmi_windows.go @@ -0,0 +1,66 @@ +// Copyright 2022 Jeremy Edwards +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//go:build windows +// +build windows + +package drivers + +import ( + "fmt" + "log" + + "github.com/yusufpapurcu/wmi" +) + +type Sensor struct { + Identifier string + Name string + Index int + InstanceId int + Max float32 + Min float32 + Parent string + ProcessId string + SensorType string + Value float32 +} + +/* +Identifier : /lpc/nct6798d/temperature/4 +Index : 4 +InstanceId : 3928 +Max : 42 +Min : 42 +Name : Temperature #4 +Parent : /lpc/nct6798d +ProcessId : 4f21c82c-c97a-4965-8637-d4e61b3a20f4 +SensorType : Temperature +Value : 42 +PSComputerName : QUARTZ +*/ +func Wmi() string { + var dst []Sensor + q := wmi.CreateQuery(&dst, "") + err := wmi.QueryNamespace(q, &dst, "root\\OpenHardwareMonitor") + if err != nil { + log.Fatal(err) + } + + s := "" + for i, v := range dst { + s += fmt.Sprintf("\n%d - %+v", i, v) + } + return s +} diff --git a/internal/drivers/wmi_windows_test.go b/internal/drivers/wmi_windows_test.go new file mode 100644 index 0000000..4a221a6 --- /dev/null +++ b/internal/drivers/wmi_windows_test.go @@ -0,0 +1,27 @@ +// Copyright 2022 Jeremy Edwards +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//go:build windows +// +build windows + +package drivers + +import "testing" + +func TestWMI(t *testing.T) { + s := Wmi() + if s != "" { + t.Error(s) + } +}