@@ -15,6 +15,7 @@ import (
1515 "time"
1616
1717 "github.com/buzzpi/agent/internal/config"
18+ "github.com/buzzpi/agent/internal/vchiq"
1819 "github.com/buzzpi/agent/internal/version"
1920)
2021
@@ -209,3 +210,177 @@ func (s *Service) Health() interface{} {
209210 "uptime_ms" : time .Since (s .startTime ).Milliseconds (),
210211 }
211212}
213+
214+ // CPUInfoResponse is the response for device.cpu.
215+ type CPUInfoResponse struct {
216+ Model string `json:"model"`
217+ Cores string `json:"cores"`
218+ MHz string `json:"mhz"`
219+ Temperature string `json:"temperature"`
220+ Frequency float64 `json:"frequency_mhz"`
221+ UsagePercent float64 `json:"usage_percent"`
222+ Serial string `json:"serial,omitempty"`
223+ Revision string `json:"revision,omitempty"`
224+ Info []vchiq.LscpuField `json:"info,omitempty"`
225+ }
226+
227+ func (s * Service ) HandleCPU (ctx context.Context , params json.RawMessage ) (interface {}, error ) {
228+ cpuUsage , _ , _ := s .readCPUStats ()
229+
230+ model , _ := vchiq .GetCPUModel ()
231+ cores , _ := vchiq .GetCPUCores ()
232+ mhz , _ := vchiq .GetCPUMHz ()
233+ temp , _ := vchiq .GetCPUTemperature ()
234+ freq , _ := vchiq .GetCPUFrequency ()
235+ serial , _ := vchiq .GetCPUSerial ()
236+ revision , _ := vchiq .GetCPURevision ()
237+ info , _ := vchiq .GetCPUInfo ()
238+
239+ return & CPUInfoResponse {
240+ Model : model ,
241+ Cores : cores ,
242+ MHz : mhz ,
243+ Temperature : temp ,
244+ Frequency : freq ,
245+ UsagePercent : cpuUsage ,
246+ Serial : serial ,
247+ Revision : revision ,
248+ Info : info ,
249+ }, nil
250+ }
251+
252+ // ThrottlingResponse is the response for device.throttling.
253+ type ThrottlingResponse struct {
254+ Throttled int64 `json:"throttled"`
255+ ThrottledInfo string `json:"throttled_info"`
256+ }
257+
258+ func (s * Service ) HandleThrottling (ctx context.Context , params json.RawMessage ) (interface {}, error ) {
259+ throttled , err := vchiq .GetThrottled ()
260+ if err != nil {
261+ return nil , fmt .Errorf ("get throttled: %w" , err )
262+ }
263+ info , err := vchiq .GetThrottledInfo ()
264+ if err != nil {
265+ return nil , fmt .Errorf ("get throttled info: %w" , err )
266+ }
267+ return & ThrottlingResponse {
268+ Throttled : throttled ,
269+ ThrottledInfo : info ,
270+ }, nil
271+ }
272+
273+ // GPUDetailsResponse is the response for device.gpu.
274+ type GPUDetailsResponse struct {
275+ GPUTemperature string `json:"gpu_temperature"`
276+ ARM string `json:"arm_memory"`
277+ GPU string `json:"gpu_memory"`
278+ }
279+
280+ func (s * Service ) HandleGPU (ctx context.Context , params json.RawMessage ) (interface {}, error ) {
281+ temp , _ := vchiq .GetGPUTemperature ()
282+ arm , gpu , _ := vchiq .GetVCGencmdMemory ()
283+ return & GPUDetailsResponse {
284+ GPUTemperature : temp ,
285+ ARM : arm ,
286+ GPU : gpu ,
287+ }, nil
288+ }
289+
290+ // VoltageResponse is the response for device.voltage.
291+ type VoltageResponse struct {
292+ CoreVoltage string `json:"core_voltage"`
293+ }
294+
295+ func (s * Service ) HandleVoltage (ctx context.Context , params json.RawMessage ) (interface {}, error ) {
296+ volt , err := vchiq .GetCoreVoltage ()
297+ if err != nil {
298+ return nil , fmt .Errorf ("get core voltage: %w" , err )
299+ }
300+ return & VoltageResponse {CoreVoltage : volt }, nil
301+ }
302+
303+ // ProcessResponse is the response for device.processes.
304+ type ProcessResponse struct {
305+ Processes []vchiq.ProcessInfo `json:"processes"`
306+ }
307+
308+ func (s * Service ) HandleProcesses (ctx context.Context , params json.RawMessage ) (interface {}, error ) {
309+ procs , err := vchiq .ListProcesses ()
310+ if err != nil {
311+ return nil , fmt .Errorf ("list processes: %w" , err )
312+ }
313+ return & ProcessResponse {Processes : procs }, nil
314+ }
315+
316+ // USBResponse is the response for device.usb.
317+ type USBResponse struct {
318+ Devices []vchiq.USBDevice `json:"devices"`
319+ }
320+
321+ func (s * Service ) HandleUSB (ctx context.Context , params json.RawMessage ) (interface {}, error ) {
322+ devices , err := vchiq .GetUSBList ()
323+ if err != nil {
324+ return nil , fmt .Errorf ("list USB devices: %w" , err )
325+ }
326+ return & USBResponse {Devices : devices }, nil
327+ }
328+
329+ // OSInfoResponse is the response for device.os.
330+ type OSInfoResponse struct {
331+ Hostname string `json:"hostname"`
332+ OSName string `json:"os_name"`
333+ KernelVersion string `json:"kernel_version"`
334+ Uptime string `json:"uptime"`
335+ LoadAverage string `json:"load_average"`
336+ FQDN string `json:"fqdn"`
337+ IPs []string `json:"ips"`
338+ }
339+
340+ func (s * Service ) HandleOS (ctx context.Context , params json.RawMessage ) (interface {}, error ) {
341+ hostname , _ := vchiq .GetHostname ()
342+ osName , _ := vchiq .GetOSName ()
343+ kernel , _ := vchiq .GetKernelVersion ()
344+ uptime , _ := vchiq .GetUptime ()
345+ loadAvg , _ := vchiq .GetLoadAverage ()
346+ fqdn , _ := vchiq .GetFQDN ()
347+
348+ var ips []string
349+ netIPs , _ := vchiq .GetIPs ()
350+ for _ , ip := range netIPs {
351+ ips = append (ips , ip .String ())
352+ }
353+
354+ return & OSInfoResponse {
355+ Hostname : hostname ,
356+ OSName : osName ,
357+ KernelVersion : kernel ,
358+ Uptime : uptime ,
359+ LoadAverage : loadAvg ,
360+ FQDN : fqdn ,
361+ IPs : ips ,
362+ }, nil
363+ }
364+
365+ // ModelResponse is the response for device.model.
366+ type ModelResponse struct {
367+ DeviceName string `json:"device_name"`
368+ Revision string `json:"revision"`
369+ MinimalPowerSupply float64 `json:"minimal_power_supply_amps"`
370+ VcgencmdInstalled bool `json:"vcgencmd_installed"`
371+ }
372+
373+ func (s * Service ) HandleModel (ctx context.Context , params json.RawMessage ) (interface {}, error ) {
374+ deviceName , err := vchiq .GetDeviceName ()
375+ if err != nil {
376+ return nil , fmt .Errorf ("get device name: %w" , err )
377+ }
378+ revision , _ := vchiq .GetCPURevision ()
379+ power , _ := vchiq .GetMinimalPowerSupply (deviceName )
380+ return & ModelResponse {
381+ DeviceName : deviceName ,
382+ Revision : revision ,
383+ MinimalPowerSupply : power ,
384+ VcgencmdInstalled : vchiq .IsVcgencmdInstalled (),
385+ }, nil
386+ }
0 commit comments