#!/usr/bin/env python # -*- coding: utf-8 -*- import wx import wx.lib.plot as plot import urllib import json import threading import os import sys import time import subprocess import csv import datetime import psutil class MainFrame(wx.Frame): # Constants data_index=0 # Constructor def __init__(self): wx.Frame.__init__(self, None, title="DEMO", size=(800, 480)) self.Center() self.__setup_panels() self.__setup_layouts() self.delegate = None self.plot_data = [] self.plot_data2 = [] self.cpu_visible = True; self.memory_visible = True; self.imagevisible = False; self.logo = wx.StaticBitmap() def add_plot_data(self): temp = [] light = [] #csvファイルを読み込んでデータを配列へ格納する CSV_DIR_PATH = os.path.dirname(os.path.abspath(__file__)) + "/log" filename = ('gatt_data_output_%s.csv' % (datetime.date.today())) csvPath = CSV_DIR_PATH + "/" + filename with open(csvPath) as f: reader = csv.reader(f) for row in reader: temp.append(str(row[1])) light.append(str(row[3])) #配列に格納したデータをグラフデータとしてプロットする self.plot_data.append((self.data_index / 60.0, temp[len(temp)-1])) self.plot_data2.append((self.data_index / 60.0, light[len(light)-1])) self.data_index = self.data_index +1 def refresh(self): self.__update_plot_canvas() def image_window_create(self): if not self.imagevisible: self.plotter.Clear() image = wx.Image('logo.png') bitmap=image.ConvertToBitmap() self.logo = wx.StaticBitmap(self.logo_panel, -1, bitmap, pos=(0, 0), size=(700,480)) self.imagevisible=True else: self.imagevisible=False self.refresh() def __update_plot_canvas(self): index = self.data_index px=index/60 line = plot.PolyLine(self.plot_data, colour='blue', width=3) line2 = plot.PolyLine(self.plot_data2, colour='red', width=3) if(self.cpu_visible & self.memory_visible): gc = plot.PlotGraphics([line,line2], 'Temperature/Light', 'Time(min)', '') elif(self.cpu_visible): gc = plot.PlotGraphics([line2], 'Temperature/Light', 'Time(min)', '') elif(self.memory_visible): gc = plot.PlotGraphics([line], 'Temperature/Light', 'Time(min)', '') else: gc = plot.PlotGraphics([], 'Temperature/Light', 'Time(min)', '') self.plotter.Draw(gc, xAxis=(max(0,px-1), max(1 ,px)), yAxis=(-10, 40)) # Callbacks def window_created(self, event): if not self.plot_panel_created: self.plot_panel_created = True self.__setup_plot_canvas() self.__update_plot_canvas() def window_created2(self, event): if not self.logo_panel_created: self.logo_panel_created = True def cpu_button_pressed(self, event): if self.delegate is not None: self.delegate.cpu_button_pressed() def memory_button_pressed(self, event): if self.delegate is not None: self.delegate.memory_button_pressed() def stress_button_pressed(self, event): if self.delegate is not None: self.delegate.stress_button_pressed() def change_button_pressed(self, event): if self.delegate is not None: self.delegate.change_button_pressed() def exit_button_pressed(self, event): if self.delegate is not None: self.delegate.exit_button_pressed() # Private def __setup_panels(self): self.panel = wx.Panel(self, wx.ID_ANY) self.plot_panel = wx.Panel(self.panel, wx.ID_ANY, pos=(100, 0), size=(700, 480)) self.plot_panel.Bind(wx.EVT_WINDOW_CREATE, self.window_created) self.plot_panel_created = False self.exit_button = wx.Button(self.panel,wx.ID_ANY, label="exit",pos=(0,200)) self.exit_button.Bind(wx.EVT_BUTTON, self.exit_button_pressed) def __setup_layouts(self): self.panel.Layout() def __setup_plot_canvas(self): self.plotter = plot.PlotCanvas(self.plot_panel) self.plotter.SetInitialSize(size=(700, 480)) self.plotter.SetEnableGrid(True) self.__update_plot_canvas() class MainController: # Constants def __init__(self, app): args = sys.argv self.view = MainFrame() self.view.delegate = self self.__start_timer(app) self.view.Show() # MainFrame delegate method def exit_button_pressed(self): cmd="rm /mnt/dummy.iso" res=subprocess.Popen(cmd.strip().split(" ")) wx.Exit() def change_button_pressed(self): self.view.image_window_create() def __start_timer(self, app): self.refresh_timer = wx.Timer(app, id=100) app.Bind(wx.EVT_TIMER, self.timer_fired) self.refresh_timer.Start(60000 *1) def timer_fired(self, event): self.__set_memory_log() if not self.view.imagevisible: self.view.refresh() def __set_memory_log(self): self.view.add_plot_data() if __name__ == "__main__": app = wx.App() controller = MainController(app) app.MainLoop()