# -*- coding: cp1252 -*-
'''
Created on 13.11.2011
@author: sth2mt
'''
import xml.sax as sax
import XML_Commands as XML_SendCmd
import FertigungsTest.Hardware.WLAN as WLAN
import FertigungsTest.Hardware.Typschild as Typschild
import FertigungsTest.Tstfnc.Software as Software
import os
import FertigungsTest.GUI.Display as Display
# Tstfunc
Index = {"tag":0,"id":1,"name":2,"type":3,"unit":4,"info":5,"wert":6,"function":7,"param":8,"liste":9}
Param = {"None":0,"production":1,"browser":2}
class CommandHandler(sax.handler.ContentHandler):
'''
classdocs
'''
def __init__(self,mode):
'''
Constructor
'''
self._Thread_name = "Parser.CommandHandler"
self.stout = False
self.LogOut = False
self.fOutput = file
self.AppMode = mode
self.ergebnis = { }
self.wert = ""
self.aktiv = None
self.typ = None
self.Command = ""
self.Content = ""
self.AktivePrueffunktion = None
self.Allgemeinfunktion = None
self.threads = []
self.O_XML_Cmd = XML_SendCmd.XML_PruefString(self.AppMode)
self.O_Display = Display.Treiber()
self.O_Display.BuildDefaultScreen()
self.O_Display.paintDisplay()
self.printf( "Constructor %s AppMode=%i" % (self._Thread_name,self.AppMode))
def __del__(self):
'''
Destructor
'''
self.printf( "Destructor %s" % self._Thread_name)
if self.O_XML_Cmd != None:
del self.O_XML_Cmd
self.O_XML_Cmd = None
if self.O_Display != None:
del self.O_Display
self.O_Display = None
pass
def printf(self,string):
if self.stout:
print string
if self.LogOut:
if self.fOutput.closed:
self.fOutput = open ("Parser.log","a+")
print >> self.fOutput,("%s" % string)
#Ausgabe in logDatei umbiegen
if (self.fOutput.closed==False):
self.fOutput.flush()
self.fOutput.close()
pass
def start(self):
#print "XML::Parser::Start"
#print "%s"%self.O_XML_Cmd.BuildCommands()
pass
def startElement(self,name,attrs):
#self.printf( "startElement name=1%s1 attrs=2%s2"%(name,attrs))
#self.printf( "startElement() name=%s"%(name))
self.Content = ""
if name == "Command":
self.wert = ""
self.aktiv = name
self.printf( "startElement() aktiv=%s"%(name))
self.Command = ""
if self.Command == "TypPltWr":
self.printf( "startElement() Command == TypPltWr")
if name == "TypePlate":
self.aktiv = name
self.printf( "startElement() aktiv=%s"%(name))
#else:
#print "Unbekannter Tag !!!!!"
pass
def endElement(self,name):
#self.printf( "endElement() %s %s"%(self.Command,name))
if name == "Command":
self.printf("%s"%self.Command)
self.aktiv = None
# variable Liste xml_PruefCommands nach PruefKommando durchsuchen
if self.AktivePrueffunktion == None:
for Command in self.O_XML_Cmd.xml_PruefCommands[Index["liste"]]:
if Command[Index["wert"]] == self.Command:
#Pruefkommando bekannt, PruefObjekt instanzieren, diesen Parser und CommandSerializer uebergeben
self.O_Prueffunktion=Command[Index["function"]].Prueffunktionen(self,self.O_XML_Cmd,Command[Index["param"]])
self.AktivePrueffunktion = self.Command
self.threads.append(self.O_Prueffunktion)
self.O_Prueffunktion.start()
# Feste bekannt Kommandos Interpretieren
if self.Command == "dir":
print "%s"%self.O_XML_Cmd.BuildCommands()
self.printf ("%s"%self.O_XML_Cmd.BuildCommands())
elif self.Command == "Tstfunc":
if self.AppMode == Param["production"]:
print "%s"%self.O_XML_Cmd.BuildPruefCommands()
self.printf("%s"%self.O_XML_Cmd.BuildPruefCommands())
else :
print "%s"%self.O_XML_Cmd.BuildWebPruefCommands()
self.printf("%s"%self.O_XML_Cmd.BuildWebPruefCommands())
elif self.Command == "GetMAC":
O_WLAN = WLAN.Treiber()
xml_Wert = self.O_XML_Cmd.VerketteXML("Wert","2", "MAC-Addr", "string", None, "Mac-Adresse", O_WLAN.f_GetMACString(),None,None,None)
print "%s"%self.O_XML_Cmd.BuildRueckgabewert(self.Command,"Rueckgabewert von %s"%self.Command,xml_Wert,0,None)
self.printf("%s"%self.O_XML_Cmd.BuildRueckgabewert(self.Command,"Rueckgabewert von %s"%self.Command,xml_Wert,0,None))
del O_WLAN
elif self.Command == "TypPltRd":
O_TypPlate = Typschild.Treiber()
xml_Wert = self.O_XML_Cmd.VerketteXML("TypePlate","1", "Typschild", "string", None, "Typeschild Inhalt", O_TypPlate.ReadBrcTypschild(),None,None,None)
print "%s"%self.O_XML_Cmd.BuildRueckgabewert(self.Command,"Rueckgabewert von %s"%self.Command,xml_Wert,0,None)
self.printf("%s"%self.O_XML_Cmd.BuildRueckgabewert(self.Command,"Rueckgabewert von %s"%self.Command,xml_Wert,0,None))
del O_TypPlate
elif self.Command == "TypPltDel":
O_TypPlate = Typschild.Treiber()
xml_Wert = self.O_XML_Cmd.VerketteXML("TypePlate","1", "Typschild", "string", None, "Typeschild loeschen", O_TypPlate.DeleteBrcPar(),None,None,None)
print "%s"%self.O_XML_Cmd.BuildRueckgabewert(self.Command,"Rueckgabewert von %s"%self.Command,xml_Wert,0,None)
self.printf("%s"%self.O_XML_Cmd.BuildRueckgabewert(self.Command,"Rueckgabewert von %s"%self.Command,xml_Wert,0,None))
del O_TypPlate
elif self.Command == "exit":
print "%s"%self.O_XML_Cmd.BuildRueckgabewert(self.Command,"Rueckgabewert von %s"%self.Command,None,0,None)
self.printf("%s"%self.O_XML_Cmd.BuildRueckgabewert(self.Command,"Rueckgabewert von %s"%self.Command,None,0,None))
# Offene Pruefthreads jetzt beenden
if self.AktivePrueffunktion !=None:
for t in self.threads:
t.stop()
self.AktivePrueffunktion = None
# Warten bis alle Pruefthread beendet
for t in self.threads:
t.join()
for t in self.threads:
t.__del__()
self.AktivePrueffunktion = None
# endlich
self.__del__()
exit()
elif self.Command == "PwrDwn":
# Achtung zusaetzlich zu Poweroff muss noch ueber serielle Schnittstelle an Leistungselektronik (0) geschickt werden um die Spannungversorgung abzuschalten,
# anssonsten kann nicht mehr ueber Startschalter eingeschalten werden, erst wenn Akku entfernt wurde.
c_String = "%s"%self.O_XML_Cmd.BuildRueckgabewert(self.Command,"Rueckgabewert von %s"%self.Command,None,os.system("poweroff"),None)
print c_String
self.printf(c_String)
print
elif self.Command == "WrFlash":
if self.AktivePrueffunktion == None:
self.printf( "PruefObjekt Instanz anlegen -> Startet Thread SoftwarePruefung")
self.O_Prueffunktion = Software.Prueffunktionen(self,self.O_XML_Cmd,self.AppMode)
self.printf( "PruefObjekt Instanz angelegt !! Funktion starten")
self.O_Prueffunktion.start()
self.AktivePrueffunktion = self.Command
self.threads.append(self.O_Prueffunktion)
elif self.Command == "stop":
if self.AktivePrueffunktion:
self.printf( "PruefObjekt stop Member aufrufen")
self.O_Prueffunktion.stop()
self.Command = ''
elif name == "TypePlate":
if self.Command == "TypPltWr":
if self.aktiv == "TypePlate":
self.printf( "endElement() TypePlate \r\n%s"%(self.Content))
#print( "TypePlate \r\n1%s1"%(self.Content))
O_TypPlate = Typschild.Treiber()
# Typschild schreiben
bResult = O_TypPlate.WriteBrcTypeplateToEeprom(self.Content)
if bResult!=True:
self.printf("Error Gesamttypschild schreiben !")
Return=3000
else:
self.printf("Gesamttypschild schreiben OK")
Return=0
print "%s"%self.O_XML_Cmd.BuildRueckgabewert(self.Command,"Rueckgabewert von %s"%self.Command,None,Return,None)
self.printf("%s"%self.O_XML_Cmd.BuildRueckgabewert(self.Command,"Rueckgabewert von %s"%self.Command,None,0,None))
self.Command = ""
del O_TypPlate
else:
self.printf( "Unbekannter Tag !!!!!")
pass
def characters(self,content):
#self.printf( "characters() command=1%s1 content=2%s2"%(self.Command,content))
if self.aktiv == "Command":
#Whitespaces entfernen
Split=content.split()
WithoutSpace=""
for i in Split:
WithoutSpace = i
if WithoutSpace != "":
self.Command = WithoutSpace
self.aktiv = ""
self.printf( "characters() Command =%s"%(self.Command))
if self.aktiv == "TypePlate":
#Whitespaces entfernen
Split=content.split()
WithoutSpace=""
for i in Split:
WithoutSpace+= (i+"\r\n")
self.Content += WithoutSpace
#self.printf( "characters() Content =%s"%(self.Content))
pass