手持式服务器端 桌面端两个分支
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
handheld/yiwinframe.py

411 lines
14 KiB

3 years ago
import wx
import os
import time
import threading
from pathlib import PurePath,Path
from pubsub import pub
3 years ago
from listctrl import Listctrl
from mypanel import MyPanel,Plot,LineColor
3 years ago
from uiconfig.uisensor import UISensor
# from uiconfig.uilogging import UILogging
from uiconfig.uiabout import About
from uiconfig.uihelp import Help
from uiconfig.ui_plot_setting import UI_Plot_Setting
3 years ago
from myconfig import TOKEN,DeviceType,YAML_FILE_NAME,RETRIEVE_CFG_FILE,DATA_DIR
from myconfig import MyConfig,RamsesFunc
from mythread import Mythead
3 years ago
from configuration import Configuration
# from handheld import HandHeld,HandHeldBuf
from gatherdata import GatherData
3 years ago
from tools.myexception import MyException
from tools.mylogger import log
from awrams import AwramsHandle,HandheldHandle
3 years ago
# -定义菜单ID,关联Event-------------------------
ID_MEASURE = 1
ID_SENSOR_SETTING = 11
ID_LOGGING_SETTING = 12
ID_CAL_INFO = 13
ID_DISPLAY_PARA = 14
ID_PLOT_LAST = 15
ID_PLOT_LAST_7 = 16
ID_PLOT_7 = 17
ID_PLOT_SETTING = 18
ID_GATHER_DATA = 19
3 years ago
ID_HELP = 21
ID_ABOUT = 22
class YiwinFrame(wx.Frame):
'''将buf类传进来'''
def __init__(self, title, parent, size=(900, 750)):
self.device_id = 2
self.device_type = DeviceType.SURFACE.name
self.syscfg = {}
self.sensor_cfg = {}
self.retrieve = {}
self.calinfo_is_ok = False
self.title = title
self.path = PurePath()
# self.save_fname:Path = Path(self.path.joinpath(DATA_FNAME))
self.result = ''
self.displayData:list =[]
self.interval = 0
super(YiwinFrame, self).__init__( parent, title=self.title, size = size )
self.BoxSizer = wx.BoxSizer(wx.HORIZONTAL) # 建立一个Boxsizer
self.SetSizer(self.BoxSizer)
self.Centre(wx.BOTH)
# 建立 listctrl panel 放到BoxSizer
self.listctrl_panel = Listctrl( self ) # 调用自己建立的 Listctrl panel 类
self.BoxSizer.Add( self.listctrl_panel, proportion =-10, border = 2, flag = wx.ALL | wx.EXPAND)
self.list_ctrl = self.listctrl_panel.list_ctrl
# 隐藏 等待show
self.listctrl_panel.Hide()
self.mypanel = MyPanel( self ) # 调用自己建立的 Listctrl panel 类
self.BoxSizer.Add( self.mypanel, proportion =-10, border = 0, flag = wx.ALL | wx.EXPAND)
self.static_text = self.mypanel.staticText1
# 隐藏 等待show
self.mypanel.Hide()
self.plotpanel = Plot( self ) # 调用自己建立的 Listctrl panel 类
self.BoxSizer.Add( self.plotpanel, proportion =-10, border = 0, flag = wx.ALL | wx.EXPAND)
self.plotpanel.set_title_x_y(*self.plotpanel.purewater_legend)
self.plotpanel.Show()
3 years ago
self.statusBar = self.CreateStatusBar() # 创建状态栏
self.statusBar.SetFieldsCount(2) # 状态栏分成3个区域
self.statusBar.SetStatusWidths([-1, -1]) # 区域宽度比列,用负数
self.statusBar.SetStatusText(u" 等待接收消息......", 0)
self.__set_menu() # 添加菜单
self.__attach_events() # 菜单事件
self.__set_properties()
# self.__read_config()
self.__setTimer()
log.info(f"system init....",__name__, "__init__")
self.mycfg = MyConfig()
# self.hh = HandHeld()
self.mythread = Mythead()
pub.subscribe( self.updateDisplay, "update")
3 years ago
pass
def __set_menu(self):
'''
# 设置菜单
'''
self.menubar = wx.MenuBar()
settingMenu = wx.Menu()
settingMenu.Append(ID_SENSOR_SETTING, u'&设置', '...')
settingMenu.AppendSeparator()
settingMenu.Append(ID_CAL_INFO, u'&获取标定信息', ' ')
settingMenu.AppendSeparator()
settingMenu.Append(ID_MEASURE, u'&处理数据', ' ')
3 years ago
# settingMenu.AppendSeparator()
# settingMenu.Append(ID_LOGGING_SETTING, u'&采集设置', ' ')
3 years ago
self.menubar.Append(settingMenu, u'&系统 ')
plotMenu = wx.Menu()
plotMenu.Append(ID_PLOT_LAST, u'&最后一条', '...')
plotMenu.AppendSeparator()
plotMenu.Append(ID_PLOT_LAST_7, u'&最后七条', ' ')
plotMenu.AppendSeparator()
plotMenu.Append(ID_PLOT_7, u'&指定七条', ' ')
plotMenu.AppendSeparator()
plotMenu.Append(ID_PLOT_SETTING, u'&指定设置', ' ')
plotMenu.AppendSeparator()
plotMenu.Append(ID_GATHER_DATA, u'&汇总数据', ' ')
self.menubar.Append(plotMenu, u'&绘图 ')
3 years ago
aboutMenu = wx.Menu()
aboutMenu.Append(ID_HELP, u'&帮助', 'help...')
aboutMenu.AppendSeparator()
aboutMenu.Append(ID_ABOUT, u'&关于我们', '关于我们...')
self.menubar.Append(aboutMenu, u'&帮助 ')
self.SetMenuBar(self.menubar)
pass
def __set_properties(self):
self.SetSize((800, 600))
# self.SetTitle(u'传感器数据采集--上海奕枫仪器设备有限公司')
self.Centre()
def __attach_events(self):
'''
# 绑定菜单事件
'''
self.Bind(wx.EVT_MENU, self.OnSensorSetting, id=ID_SENSOR_SETTING )
self.Bind(wx.EVT_MENU, self.OnCalInfo, id=ID_CAL_INFO )
# self.Bind(wx.EVT_MENU, self.OnStart, id=ID_MEASURE)
self.Bind(wx.EVT_MENU, self.OnDealData, id=ID_MEASURE)
# self.Bind(wx.EVT_MENU, self.OnLoggingSetting, id=ID_LOGGING_SETTING)
self.Bind(wx.EVT_MENU, self.OnPlotLast, id=ID_PLOT_LAST )
self.Bind(wx.EVT_MENU, self.OnPlotLast7, id=ID_PLOT_LAST_7 )
self.Bind(wx.EVT_MENU, self.OnPlot7, id=ID_PLOT_7)
self.Bind(wx.EVT_MENU, self.OnPlotSetting, id=ID_PLOT_SETTING)
self.Bind(wx.EVT_MENU, self.OnGatherData, id=ID_GATHER_DATA)
3 years ago
self.Bind(wx.EVT_MENU, self.OnHelpConfig, id=ID_HELP)
self.Bind(wx.EVT_MENU, self.OnAboutConfig, id=ID_ABOUT)
pass
def __setTimer(self):
self.timer = wx.Timer(self) #创建定时器,菜单以后
self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer) #绑定一个定时器事件
pass
def updateDisplay(self, msg):
log.warning(f" updateDisplay 。。 {msg['data']}")
if msg['flag'] == "notice":
self.__update_notice(msg['data'])
pass
if msg['flag'] == "data":
# log.info(f" ... update .{msg['data']}........... " )
self.__update_data(msg['data'])
pass
pass
def __update_notice(self,msg):
log.info(f" .......{msg}... ",__name__,'__update_notice')
self.alterStatus_0( msg)
# self.popDialog( msg )
pass
def __update_data(self,data ):
log.info(f" .......... ", __name__, '__update_data')
self.plotpanel.set_axes_title( data[0])
self.plotpanel.clear_past()
self.plotpanel.plot_one( "Rs", data[1], data[2], color= LineColor(1).name )
3 years ago
def OnStart( self, event ):
log.info(f"OnStart....interval: {self.interval} min, port: {self.port}")
# self.m = SerialThread()
interval_ms = int(self.interval) * 10 * 1000
self.timer.Start( interval_ms )
def OnStop( self ,event ):
# self.kh.disconnect()
# self.timer.Stop()
pass
def OnTimer(self, event):
'''
定时器实现
'''
# log.info( f"OnTimer...." )
# self.OnDisplaySave()
pass
def onNotify(self, msg:str):
self.mypanel.setStaticText(msg)
pass
def __read_config(self,):
# 读取配置文件
self.mycfg.setDeviceType(DeviceType.SURFACE)
self.sensor_cfg = self.mycfg.read_yaml()
3 years ago
def OnDealData(self, e)-> None:
''' SD卡的文件结构 '''
3 years ago
log.info(f"OnDealData: 处理测量数据", __name__, "", "")
if not self.calinfo_is_ok:
self.onNotify("Pls get the cal info, and try again" )
log.warning(f"Pls get the cal info, and try again", __name__, "OnDealData")
self.statusBar.SetStatusText("Pls get the cal info, and try again",0)
pass
## 调用 AwramsHandle 的函数
self.myhandle:HandheldHandle = self.handheld_handle[self.device_id]
# 处理数据
self.alterStatus_0(" 正在处理数据...." )
try:
self.mythread.set_task( self.read_folders_from_sd )
self.mythread.start()
except Exception as e:
log.error(e)
pass
def read_folders_from_sd(self,):
# myhandle:AwramsHandle = self.awrams_handle[self.device_id]
self.myhandle.read_folders_from_SD( DATA_DIR )
pass
# def OnDealData2(self, e)-> None:
# log.info(f"OnDealData: 处理测量数据", __name__, "", "")
# if not self.calinfo_is_ok:
# self.onNotify("Pls get the cal info, and try again" )
# log.warning(f"Pls get the cal info, and try again", __name__, "OnDealData")
# self.statusBar.SetStatusText("Pls get the cal info, and try again",0)
# pass
# self.hh.getDataFileList()
# self.hh.setDeviceID(self.device_id)
# self.hh.setRetrieve(self.retrieve)
# if self.syscfg != {}:
# self.hh.setSyscfg(self.syscfg)
# else:
# log.warning(f"Pls get the cal info, and try again", __name__, "OnDealData","setCfg")
# MyException( f"System Configuration is empty." )
# pass
# # 处理数据
# self.alterStatus_0(" 正在处理数据...." )
# try:
# self.mythread.set_task( self.hh.dealAllMeasurements )
# self.mythread.start()
# except Exception as e:
# log.error(e)
# pass
3 years ago
def OnCalInfo(self,e):
'''依据传感器获取标定文件信息'''
#######################################
self.onNotify("正在获取标定信息")
self.device_type = DeviceType.SURFACE.name
self.mycfg = MyConfig() # 传入 cfg retrieve 的yml文件
# 配置反演需要的参数 波长 间隔 , rowFactor
self.retrieve = self.mycfg.get_retrieve() # 读retrieve.yml
log.info(f"Retrieve: {self.retrieve}", __name__, "", "")
# log.info(f"syscfg: {device}", __name__, "", "")
self.mycfg.setDeviceType( DeviceType.SURFACE )
self.sensor_cfg = self.mycfg.read_yaml() # 读config.yml, 多个device_id 配置
log.info(f"Current Device: {self.device_type} ", __name__, "", "")
log.info(f"Sensor cfg: {self.sensor_cfg}", __name__, "", "")
self.cal_cfg = {}
3 years ago
cfgr = Configuration( )
cfgr.setDeviceType(self.device_type)
cfgr.setSystemCfgDict(self.sensor_cfg)
3 years ago
for k,v in self.sensor_cfg.items():
3 years ago
cfgr.setSystemCfgDict(v)
try:
cfgr.getCalConfiguration()
except Exception as e:
log.error(f"读取配置文件失败. \n {e}",__name__, "", "" )
raise
log.info(f"v: {cfgr.configuration}", __name__, "", "")
self.cal_cfg.update({k:cfgr.cal_configuration})
3 years ago
log.debug(f"cal_cfg: {self.cal_cfg}", __name__, "", "")
log.warning(f"cal_cfg 2: {self.cal_cfg[2].keys()}", __name__, "", "")
# log.warning(f"cal_cfg 3: {self.cal_cfg[3].keys()}", __name__, "", "")
self.init_data_process()
3 years ago
log.info(f"传感器配置文件读取成功", __name__, "", "")
3 years ago
pass
def init_data_process(self, mode = 0):
''' 多个handle '''
self.handheld_handle = { }
for k,v in self.cal_cfg.items():
ah = HandheldHandle( deviceid=k, cfg=self.sensor_cfg[k], calcfg=v, rtv=self.retrieve)
self.handheld_handle.update( {k:ah } )
3 years ago
def alterStatus_0(self,msg):
self.statusBar.SetStatusText( msg, 0 )
def popDialog(self, msg, msg_type=u"错误提示"):
with wx.MessageDialog( self, msg, msg_type, wx.OK )as dlg:
dlg.ShowModal()
def OnPlotLast(self,e):
3 years ago
# 如何取数据 需要先合并数据?
self.popDialog(" 功能开发中...")
pass
def OnPlotLast7(self,e):
3 years ago
self.popDialog(" 功能开发中...")
pass
def OnPlot7(self,e):
3 years ago
self.popDialog(" 功能开发中...")
pass
def OnGatherData(self,e):
gd = GatherData()
gd.gather_handheld()
self.alterStatus_0(" 数据汇总完成")
pass
def OnPlotSetting(self,e):
with UI_Plot_Setting(
self,
-1 ) as Dialog_Sensor_Setting:
Dialog_Sensor_Setting.CenterOnParent()
resultLog = Dialog_Sensor_Setting.ShowModal()
if resultLog == wx.ID_OK:
log.info( " Plot config dialog confirm, call back " )
self.__read_config()
pass
3 years ago
def OnSensorSetting(self,e):
with UISensor(
self,
-1 ,
did = self.device_id ) as Dialog_Sensor_Setting:
Dialog_Sensor_Setting.CenterOnParent()
resultLog = Dialog_Sensor_Setting.ShowModal()
if resultLog == wx.ID_OK:
print( " Sensor config dialog confirm, call back " )
self.__read_config()
pass
def OnHelpConfig(self, e):
with Help(
self,
-1,
"") as Dialog_Help:
resultLog = Dialog_Help.ShowModal()
if resultLog == wx.ID_OK:
print("Help info")
pass
def OnAboutConfig(self, e):
with About(
self,
-1,
"") as Dialog_About:
resultLog = Dialog_About.ShowModal()
if resultLog == wx.ID_OK:
print("Aboutus")
pass
def OnQuit(self, e):
self.Close()