00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 import socket
00021 import sys
00022 import re
00023
00024 import roslib.message
00025 import rostopic
00026 import rosmsg
00027 import rospy
00028 import genpy
00029 import string
00030 import os,sys,threading
00031 from datetime import datetime
00032 from std_msgs.msg import String
00033
00034 toRetalisPub_ = rospy.Publisher('retalisInputEvents',String, queue_size=5000)
00035
00036
00037
00038
00039
00040 def sendRosMessageToRetalis(rawMsg):
00041
00042 event = convertRosToEvent(rawMsg)
00043
00044 try:
00045 secs = rawMsg.header.stamp.secs
00046 nsecs = rawMsg.header.stamp.nsecs
00047 except:
00048 now = rospy.get_rostime()
00049 secs = now.secs
00050 nsecs = now.nsecs
00051
00052 event = 'event('+event+',['+datetime.fromtimestamp(secs).strftime('datime(%Y,%m,%d,%H,%M,%S,')+str(nsecs)+'),'+datetime.fromtimestamp(secs).strftime('datime(%Y,%m,%d,%H,%M,%S,')+str(nsecs)+')])'
00053
00054
00055 global toRetalisPub_
00056 toRetalisPub_.publish(event)
00057
00058
00059 """def sendSMCQueryMessageToEtalis(smcQueryMsg):
00060 event = smcQueryMsg.winRegQuery #+'\\'+str(smcQueryMsg.header.stamp.secs)+'-'+str(smcQueryMsg.header.stamp.nsecs)
00061 sendRosMessageToRetalis(event)
00062 """
00063
00064 def convertRosToEvent(rawMsg):
00065 msg = ""
00066 if hasattr(rawMsg, "_type"):
00067 msg_type = rawMsg._type
00068 msg_type = msg_type.replace("/","__0__")
00069 temp_msg = []
00070
00071 try:
00072 msg+=(msg_type + "(")
00073 for slot in rawMsg.__slots__:
00074 x = convertRosToEvent(getattr(rawMsg, slot))
00075 msg = msg+x+", "
00076 msg = msg[:-2]+")"
00077 except:
00078 print "[ERROR] Couldn't extract information from ROS message"
00079 assert(Fail)
00080 else:
00081 temp_msg = []
00082 if (type(rawMsg) == list) or (type(rawMsg) == tuple):
00083 if(len(rawMsg) == 0):
00084 msg+="[]"
00085 else:
00086 msg+="["
00087 for i in range(len(rawMsg)) :
00088 x = convertRosToEvent(rawMsg[i])
00089 msg = msg + x + ", "
00090 msg = msg[:-2]+"]"
00091 elif (isinstance(rawMsg, genpy.rostime.Time)):
00092 msg+=("["+str(rawMsg.secs)+", "+str(rawMsg.nsecs)+"]")
00093 else:
00094 if(type(rawMsg) is str):
00095 printset = set(string.printable)
00096 isprintable = set(rawMsg).issubset(printset)
00097 if(isprintable):
00098 msg = "'\"" + rawMsg + "\"'"
00099 else:
00100 msg = "'\"FFFFFFFFFF"+ rawMsg.encode('base64','strict').replace("\n", "////") + "\"'"
00101 else:
00102 msg = str(rawMsg)
00103 return msg
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113 def extractInfoFromSMC(message):
00114 outerPar = re.compile("\((.+)\)")
00115 m = outerPar.search(message)
00116 stringToAnalyze = m.group(1)
00117 parts = stringToAnalyze.split(",")
00118 etalis_msg = ""
00119 for i in range(len(parts) - 3) :
00120 etalis_msg=etalis_msg+parts[i]+","
00121 etalis_msg = etalis_msg+parts[len(parts)-3]
00122 topic = parts[len(parts)-2]
00123 msg_id = parts[len(parts)-1]
00124 return etalis_msg, topic, msg_id
00125
00126
00127 def convertEventToRos(event, rosMsg, msgClass):
00128
00129 outerPar = re.compile("\((.+)\)")
00130 try:
00131 m = outerPar.search(event)
00132 event = m.group(1)
00133 except:
00134 True
00135 parts= event.split(",")
00136 i = 0
00137 slot_counter = -1;
00138 for slot in rosMsg.__slots__:
00139 slot_counter+=1
00140 slotAttr = getattr(rosMsg, slot)
00141 if hasattr(slotAttr, "_type"):
00142 paranthesis_count= parts[i].count("(") - parts[i].count(")")
00143 stringPart = str(parts[i])
00144 while(paranthesis_count>0):
00145 i += 1
00146 paranthesis_count = paranthesis_count + parts[i].count("(")
00147 paranthesis_count = paranthesis_count - parts[i].count(")")
00148 stringPart += "," + parts[i]
00149 convertEventToRos(stringPart,slotAttr,type(slotAttr))
00150 i+=1
00151 elif (type(slotAttr) == list) or (type(slotAttr) == tuple):
00152 if (parts[i][1:] == "[]") or (parts[i] == "[]") or (parts[i][:-1] == "[]") :
00153 True
00154 i+=1
00155 else:
00156 list_type = rosMsg._slot_types[slot_counter]
00157 list_type = list_type[:list_type.find('[')]
00158 if(re.match('bool|int|long|float|string|str',list_type)):
00159 array_counter = 0
00160 parts[i]=parts[i][parts[i].find('[')+1:]
00161 while(parts[i].find("]")==-1):
00162 if(re.match('bool',list_type)):
00163 if (slotAttr[array_counter] is None):
00164 if(parts[i] in ("True","true","1")):
00165 slotAttr.append(True)
00166 else:
00167 slotAttr.append(False)
00168 else:
00169 if(parts[i] in ("True","true","1")):
00170 slotAttr[array_counter] = True
00171 else:
00172 slotAttr[array_counter] = False
00173 elif(re.match('int',list_type)):
00174 if (slotAttr[array_counter] is None):
00175 slotAttr.append(int(parts[i]))
00176 else:
00177 slotAttr[array_counter] = int(parts[i])
00178 elif(re.match('long',list_type)):
00179 if (slotAttr[array_counter] is None):
00180 slotAttr.append(long(parts[i]))
00181 else:
00182 slotAttr[array_counter] = long(parts[i])
00183 elif(re.match('float',list_type)):
00184 if (slotAttr[array_counter] is None):
00185 slotAttr.append(float(parts[i]))
00186 else:
00187 slotAttr[array_counter] = float(parts[i])
00188 elif(re.match('string|str',list_type)):
00189 if (slotAttr[array_counter] is None):
00190 if(parts[i].find("\"")>1):
00191 slotAttr.append(parts[i][parts[i].find("\"")+1:parts[i].rfind("\"")])
00192 else:
00193 slotAttr.append(parts[i])
00194 else:
00195 if(parts[i].find("\"")>1):
00196 slotAttr[array_counter] = parts[i][parts[i].find("\"")+1:parts[i].rfind("\"")]
00197 else:
00198 slotAttr[array_counter] = parts[i]
00199 array_counter+=1
00200 i+=1
00201 parts[i]=parts[i][:parts[i].find(']')]
00202 if(re.match('bool',list_type)):
00203 if (slotAttr[array_counter] is None):
00204 if(parts[i] in ("True","true","1")):
00205 slotAttr.append(True)
00206 else:
00207 slotAttr.append(False)
00208 else:
00209 if(parts[i] in ("True","true","1")):
00210 slotAttr[array_counter] = True
00211 else:
00212 slotAttr[array_counter] = False
00213 elif(re.match('int',list_type)):
00214 if (slotAttr[array_counter] is None):
00215 slotAttr.append(int(parts[i]))
00216 else:
00217 slotAttr[array_counter] = int(parts[i])
00218 elif(re.match('long',list_type)):
00219 if (slotAttr[array_counter] is None):
00220 slotAttr.append(long(parts[i]))
00221 else:
00222 slotAttr[array_counter] = long(parts[i])
00223 elif(re.match('float',list_type)):
00224 if (slotAttr[array_counter] is None):
00225 slotAttr.append(float(parts[i]))
00226 else:
00227 slotAttr[array_counter] = float(parts[i])
00228 elif(re.match('string|str',list_type)):
00229 if (slotAttr[array_counter] is None):
00230 if(parts[i].find("\"")>1):
00231 slotAttr.append(parts[i][parts[i].find("\"")+1:parts[i].rfind("\"")])
00232 else:
00233 slotAttr.append(parts[i])
00234 else:
00235 if(parts[i].find("\"")>1):
00236 slotAttr[array_counter] = parts[i][parts[i].find("\"")+1:parts[i].rfind("\"")]
00237 else:
00238 slotAttr[array_counter] = parts[i]
00239 i+=1
00240 else:
00241 if( (parts[i].count("[") - parts[i].count("]")) == 0 ):
00242 partRosMsg = roslib.message.get_message_class(list_type)()
00243 convertEventToRos(parts[i][parts[i].find('[')+1:parts[i].rfind(']')],partRosMsg,roslib.message.get_message_class(list_type))
00244 slotAttr.append(partRosMsg)
00245 i+=1
00246 else:
00247 brak_count= 0
00248 paranthesis_count = 0
00249 stringPart = ""
00250 firstTime = True
00251 contWhile = True
00252 firstTimeWhile = True
00253 stringPart = ""
00254 while(contWhile):
00255 if(firstTimeWhile):
00256 stringPart += parts[i]
00257 firstTimeWhile = False
00258 else:
00259 stringPart += ", "+parts[i]
00260 paranthesis_count += parts[i].count("(") - parts[i].count(")")
00261 brak_count += parts[i].count("[") - parts[i].count("]")
00262 if(paranthesis_count==0):
00263 if(firstTime):
00264 stringPart = stringPart[stringPart.find('[')+1:]
00265 firstTime = False
00266 if(brak_count==0):
00267 stringPart = stringPart[:stringPart.rfind(']')]
00268 partRosMsg = roslib.message.get_message_class(list_type)()
00269 convertEventToRos(stringPart,partRosMsg,roslib.message.get_message_class(list_type))
00270 slotAttr.append(partRosMsg)
00271 stringPart=""
00272 if(brak_count==0):
00273 contWhile = False
00274 i+=1
00275
00276 elif (isinstance(slotAttr, genpy.rostime.Time)):
00277 setattr(rosMsg.stamp, "secs", long(parts[i][parts[i].find("[")+1:]))
00278 setattr(rosMsg.stamp, "nsecs", long(parts[i+1][1:parts[i+1].find("]")]))
00279 i+=2
00280 else:
00281 if( isinstance(slotAttr,bool)):
00282 if(parts[i] in ("True","true","1")):
00283 slotAttr.append(True)
00284 else:
00285 slotAttr.append(False)
00286 elif( isinstance(slotAttr,int)):
00287 setattr(rosMsg,slot,int(parts[i]))
00288 elif( isinstance(slotAttr,long)):
00289 setattr(rosMsg,slot,long(parts[i]))
00290 elif( isinstance(slotAttr,float)):
00291 setattr(rosMsg,slot,float(parts[i]))
00292 elif( isinstance(slotAttr,str)):
00293 if parts[i].startswith("'\"") or parts[i].startswith("\""):
00294 tempStr = parts[i][parts[i].find('"')+1:parts[i].rfind('"')]
00295 else:
00296 tempStr = parts[i]
00297 if(tempStr.startswith("FFFFFFFFFF")):
00298 y=tempStr[tempStr.find("FFFFFFFFFF")+10:].replace("////","\n")
00299 setattr(rosMsg,slot,y.decode('base64','strict'))
00300 else:
00301 setattr(rosMsg,slot,tempStr)
00302 i+=1
00303
00304 def decodec(c):
00305
00306 return '\n' if c == '/n' else c[0]
00307
00308
00309 def ByteToHex( byteStr ):
00310 """
00311 Convert a byte string to it's hex string representation e.g. for output.
00312 """
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323 return ''.join( [ "%02X " % ord( x ) for x in byteStr ] ).strip()
00324
00325 def HexToByte( hexStr ):
00326 """
00327 Convert a string hex byte values into a byte string. The Hex Byte values may
00328 or may not be space separated.
00329 """
00330
00331
00332
00333
00334
00335
00336 bytes = []
00337
00338 hexStr = ''.join( hexStr.split(" ") )
00339
00340 for i in range(0, len(hexStr), 2):
00341 bytes.append( chr( int (hexStr[i:i+2], 16 ) ) )
00342
00343 return ''.join( bytes )
00344
00345
00346
00347
00348
00349