def fromClient(text:str, sourceSocket) -> Message: try: #log("Factory.fromClient(): %s" % text) array = text.split(',') type = array[0] if type == "phone": return Message( type=TYPES.PHONE_REQUEST, source=TARGETS.CLIENT, destination=TARGETS.PHONE, sourceSocket=sourceSocket, data={"cmd":",".join(array[1:])}) elif type == "phonelog": return Message( type=TYPES.PHONELOG, source=TARGETS.CLIENT, destination=TARGETS.SYSTEM, sourceSocket=sourceSocket) except Exception as e: log("MessageFactory: invalid client request, data: %s" % text) return Message( type=TYPES.PHONE_INVALID_REQUEST, source=TARGETS.BOT, destination=TARGETS.CLIENT, sourceSocket=sourceSocket, data={ "error" : "invalid request"} )
def redirectToSource(message: Message): src, dest, srcS, destS = message.source,\ message.destination,\ message.destinationSocket,\ message.sourceSocket message.destination = dest message.source = src message.sourceSocket = srcS message.destinationSocket = destS
def answer(message: Message): if message.type == TYPES.PHONELOG: return Message(type=TYPES.ANSWER, source=TARGETS.BOT, destination=TARGETS.CLIENT, destinationSocket=message.sourceSocket, data={"phone.log": logRead()})
def parse(data: bytes) -> Message: message = Message(type=TYPES.PHONE_DATA_UNKNOWN, source=TARGETS.PHONE, destination=TARGETS.BOT, data={}) dataArray = data.split(b'\r\n') for part in dataArray: if len(part): PhoneParser._parseData(PhoneParser.sanitizeData(part), message) #log("PhoneParser.parse(): returning: %s" % message) return message
def send_message(self, message: Message, delay=0) -> bool: try: with self.socket_lock: message_bytes = message.to_bytes() data_size = len(message_bytes) data_sent = 0 while data_sent < data_size: send_count = min(data_size - data_sent, self.MAX_DATA_READ) self.socket_connection.send( message_bytes[data_sent:data_sent + send_count]) data_sent += send_count return True except Exception as e: return False
def __init__(self): log("Bot __init__") # self.timebox = Timebox('11:75:58:25:05:41'); self.app = Application([url(r"/", WebSocket)]) ## Websocket port ## self.app.listen(12390) self.msHeartbeat = 1000 ## self.heartbeat sends the activity message ## (and existing clients recieve the answer) self.activityMessage = Message(type=TYPES.PHONE_REQUEST, source=TARGETS.BOT, destination=TARGETS.PHONE, data={"cmd": "+CPAS"}) self.phoneRinging = False self.msTimestamp = 0 IOLoop.current().spawn_callback(BotQueue.consumer, dispatchFunc=self._dispatch) IOLoop.current().spawn_callback(self.heartbeat) self.loop = tornado.ioloop.asyncio.get_event_loop() self.phoneCoroutine = serial_asyncio.create_serial_connection( self.loop, PhoneLLA, '/dev/serial0', baudrate=115200, timeout=5, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, rtscts=True) self.loop.run_until_complete(self.phoneCoroutine)
from Message.ActionButton import ActionButton from Message.ActionMenu import ActionMenu from Message.Message import Message from Message.Attachment import Attachment from Message.Confirm import Confirm from Message.Field import Field from Message.Option import Option from Message.OptionGroup import OptionGroup m = Message.Builder() abtn = ActionButton.Builder() amenu = ActionMenu.Builder() att = Attachment.Builder() cfm = Confirm.Builder() fld = Field.Builder() opt = Option.Builder() optg = OptionGroup.Builder() ob = (m.text('Test message') .attach(att.callback_id('ckckc') .fallback('fbk_ctn') .text('attach txt') .title('att title') .color(Attachment.COLOR_GOOD) .addAction(amenu.name('menu') .text('menu details') .value('value_menu') .addOptionGroup(optg .text('g1') .addOption(opt.text('op1')
def _parseData(text: str, message: Message): log(text) messageData = {} cmdPattern = re.compile(r"\+.+: ") errorPattern = re.compile(r"\+.+ERROR: ") cmdMatch = re.match(cmdPattern, text) cmdError = re.match(errorPattern, text) if cmdMatch: cmd = re.sub('[: ]', '', cmdMatch.group(0)) cmdSplit = re.split(cmdPattern, text) dataRaw = cmdSplit[1] data = re.split(r'[,]', dataRaw) print(data) if cmdError: message.data["ERROR"] = data[0] elif cmd == '+CRING': message.data["RING"] = RP.parse("string", data[0]) elif cmd == '+CR': message.data["service report"] = RP.parse("string", data[0]) elif cmd == '+COPS': message.data["operator selection"] = {} if "(" in data[0]: message.data[ "operator selection"] = PhoneParser._parseOperatorList( dataRaw) else: message.data["operator selection"]["mode"] = RP.parse( "opMode", data[0]) if data.__len__() > 1: message.data["operator selection"][ "format"] = RP.parse("opFormat", data[1]) message.data["operator selection"][ "operator"] = RP.parse("string", data[2]) elif cmd == '+CPAS': message.data["activity"] = RP.parse("pas", data[0]) elif cmd == '+CCLK': message.data["date"] = RP.parse("time", dataRaw) elif cmd == '+CSQ': message.data["signal"] = {} message.data["signal"]["rssi"] = RP.parse("rssi", data[0]) message.data["signal"]["rxqual"] = RP.parse("ber", data[1]) elif cmd == '+CBC': message.data["battery"] = {} message.data["battery"]["status"] = RP.parse("bcs", data[0]) message.data["battery"]["remaining"] = RP.parse("bcl", data[1]) message.data["battery"]["volt"] = RP.parse("voltage", data[2]) elif cmd == '+CSCS': message.data["chset"] = RP.parse("string", dataRaw) elif cmd == '+CLIP': message.writeToLog = True message.data["clip"] = {} message.data["clip"]["number"] = RP.parse('number', data[0]) message.data["clip"]["type"] = RP.parse('number', data[1]) message.data["clip"]["subaddr"] = RP.parse('number', data[2]) message.data["clip"]["satype"] = RP.parse('number', data[3]) message.data["clip"]["entry"] = RP.parse('string', data[4]) message.data["clip"]["validity"] = RP.parse('number', data[5]) elif cmd == '+CMTI': message.writeToLog = True message.data["newSms"] = {} message.data["newSms"]["entry"] = RP.parse('string', data[0]) message.data["newSms"]["num"] = RP.parse('number', data[1]) elif cmd == '+CMGL': message.data["sms"] = {} message.data["sms"]["id"] = RP.parse('number', data[0]) message.data["sms"]["status"] = RP.parse('smsStat', data[1]) message.data["sms"]["sender"] = RP.parse('string', data[2]) message.data["sms"]["entry"] = RP.parse('string', data[3]) message.data["sms"]["date"] = RP.parse('time', data[4] + ',' + data[5]) if 'REC' in data[1] and data.__len__() > 6: message.data["sms"]["sender type"] = RP.parse( 'tooa', data[6]) message.data["sms"]["first octet"] = RP.parse( 'number', data[7]) message.data["sms"]["protocol"] = RP.parse('pid', data[8]) message.data["sms"]["data coding scheme"] = RP.parse( 'dcs', data[9]) message.data["sms"]["sc"] = RP.parse('string', data[10]) message.data["sms"]["sc type"] = RP.parse( 'string', data[11]) message.data["sms"]["length"] = RP.parse( 'string', data[12]) elif cmd == '+CMGR': message.data["sms"] = {} message.data["sms"]["status"] = RP.parse('smsStat', data[0]) message.data["sms"]["sender"] = RP.parse('string', data[1]) message.data["sms"]["entry"] = RP.parse('string', data[2]) message.data["sms"]["date"] = RP.parse('time', data[3] + ',' + data[4]) if 'REC' in data[0] and data.__len__() > 5: message.data["sms"]["sender type"] = RP.parse( 'tooa', data[5]) message.data["sms"]["first octet"] = RP.parse( 'number', data[6]) message.data["sms"]["protocol"] = RP.parse('pid', data[7]) message.data["sms"]["data coding scheme"] = RP.parse( 'dcs', data[8]) message.data["sms"]["sc"] = RP.parse('string', data[9]) message.data["sms"]["sc type"] = RP.parse( 'string', data[10]) message.data["sms"]["length"] = RP.parse( 'string', data[11]) elif cmd == '+CREG': message.data["network registration"] = {} stat = "" lac = "" ci = "" if data.__len__() == 1: message.data["network registration"]["status"] = RP.parse( 'regStat', data[0]) elif data.__len__() == 4: message.data["network registration"]["setting"] = RP.parse( 'string', data[0]) stat = RP.parse('regStat', data[1]) lac = "%s \"%s\"" % (RP.parse( 'string', data[2]), RP.parse('hex', data[2])) ci = "%s \"%s\"" % (RP.parse( 'string', data[3]), RP.parse('hex', data[3])) else: stat = RP.parse('regStat', data[0]) lac = "%s \"%s\"" % (RP.parse( 'string', data[1]), RP.parse('hex', data[1])) ci = "%s \"%s\"" % (RP.parse( 'string', data[2]), RP.parse('hex', data[2])) message.data["network registration"]["status"] = stat message.data["network registration"][ "location area code"] = lac message.data["network registration"]["cell id"] = ci elif cmd == '+CRLP': message.data["radio link protocol"] = {} message.data["radio link protocol"][ "interworking window size"] = RP.parse('number', data[0]) message.data["radio link protocol"][ "mobile window size"] = RP.parse('number', data[1]) message.data["radio link protocol"][ "acknowledgement timer t1"] = RP.parse('T1', data[2]) message.data["radio link protocol"][ "retransmission attempts"] = RP.parse('number', data[3]) message.data["radio link protocol"][ "re-sequencing period"] = RP.parse('T4', data[4]) else: if not cmd in message.data: message.data[cmd] = {} c = 1 if cmd in message.data: while "%d" % c in message.data[cmd]: c += 1 print("%d" % c) message.data[cmd]["%d" % c] = dataRaw else: if text == 'RING': message.writeToLog = True message.type = TYPES.PHONE_DATA_RING elif text == 'NO CARRIER': message.type = TYPES.PHONE_DATA_NO_CARRIER elif text == 'OK': message.type = TYPES.PHONE_ANSWER_OK elif text == 'ERROR': message.type = TYPES.PHONE_ANSWER_ERROR message.data["status"] = "ERROR" else: message.data["text"] = PhoneParser._tryParseText(text)