Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import subprocess
- import re
- from itertools import izip
- import sys
- import time
- import os
- # Path file will be saved
- file_path = "/root/"
- # if you want to start a gammu-smsd daemon after file creation
- start_deamon = True
- # if you want the file to be created
- create_file = True
- # prefix name of the configuration file
- file_name = ".gammu_nodo"
- #TODO: create arguments to know the limit of USB modems to delete this ugly if end condition below
- # Execute gammu detect to know all gammu valid modems
- p = subprocess.Popen(['gammu-detect'], stdout=subprocess.PIPE,stderr=subprocess.PIPE)
- out, error = p.communicate()
- config = out.splitlines()
- # Lets group all lines in groups of 3 elements
- def pairwise(iterable):
- a = iter(iterable)
- return izip(a, a, a)
- # valid regex modem list
- modems = []
- # return all /dev/ttyUSB (?:=|\G) ([^[0-9]+)
- # return all /dev/ttyUSB0 (?:=|\G) ([^[0-9]+\d) re.match('(?:=|\G) ([^[0-9]+)', lines, flags=0)
- for lines in config:
- regular_expression = re.match('(?:device|\d) = ([^\n]+)', lines, flags=0)
- if regular_expression:
- modems.append(regular_expression.group(1))
- ports = []
- for port in modems:
- regex = re.match('(\/dev\/ttyUSB)', port, flags=0)
- if regex:
- #print port
- ports.append(port)
- #print ports
- # Create file with usb Settings from group results
- for path in pairwise(ports):
- full_path = "{0}{1}{2}".format(file_path,file_name,path[1][-1:])
- print 'full_file_path: {0}'.format(full_path)
- print "port:{0}".format(path[1])
- string = '''[gammu]
- port = {0}
- model = at
- connection = at
- synchronizetime = yes
- logfile = /root/.log_nodo{1}
- logformat = nothing
- use_locking = no
- gammuloc =
- [smsd]
- service = SQL
- driver = native_mysql
- logfile /root/.log_nodo{2}
- debuglevel = 255
- phoneid = nodo{3}
- maxretries=3
- user = root
- password = 18252245
- pc = localhost
- database = sms
- '''.format(path[1],path[1][-1:],path[1][-1:],path[1][-1:])
- # Write the file on gammu format
- print "file_name: {0}".format(file_name)
- print "file_path: {0}".format(file_path)
- if create_file:
- file = open(file_path+file_name+path[1][-1:],"w")
- file.write(string)
- file.close()
- # Let's give time to the folder to exists on folder
- # Before we go on the next loop modem
- time.sleep(1)
- # Run Daemon for this Device
- command = "gammu-smsd -c {0} -d".format(full_path)
- # Debug print
- print 'command: {0}'.format(command)
- print '++++++++++++++++++++++++++++++++++++++++++'
- if start_deamon:
- # Lets check the file exists before calling it
- if os.path.exists(full_path):
- # Run Gammu-smsd daemon
- c = subprocess.call(['gammu-smsd','-c',full_path,'-d'])
- else:
- print '{0} does not exist'.format(full_path)
- # If you have more than 2 modems loop this way
- # 0,[1],2,3,[4],5,6,[7],8,9
- # if path[1] == "/dev/ttyUSB4":
- # sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement