File: smsd-testing.py

package info (click to toggle)
gammu 1.28.0-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 21,972 kB
  • ctags: 8,504
  • sloc: ansic: 104,965; pascal: 7,209; cpp: 4,023; python: 3,639; php: 1,622; sh: 1,534; sql: 450; cs: 260; makefile: 192; perl: 107; asm: 31
file content (111 lines) | stat: -rwxr-xr-x 3,266 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# vim: expandtab sw=4 ts=4 sts=4:
'''
python-gammu - Test script to test several Gammu SMSD operations (usually using dummy driver, but it depends on config)
'''
__author__ = 'Michal Čihař'
__email__ = 'michal@cihar.com'
__license__ = '''
Copyright © 2003 - 2010 Michal Čihař

This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License version 2 as published by
the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
'''

SQLITE_BIN='@SQLITE_BIN@'
DATA_DIR='@CMAKE_CURRENT_BINARY_DIR@'
GAMMU_SRC_DIR='@Gammu_SOURCE_DIR@'

if SQLITE_BIN == '' or SQLITE_BIN[0] == '@':
    raise Exception('Please configure this script!')

import gammu.smsd
import sys
import os
import threading
import time
import traceback

def GenerateSMSDRC(path, gammurc):
    gammu_config = file(gammurc).read()
    out = file(path, 'w')
    out.write('''
[smsd]
commtimeout = 1
debuglevel = 255
logfile = stderr
service = dbi
driver = sqlite3
database = smsd.db
dbdir = %s

%s
    ''' % (DATA_DIR, gammu_config))

def CreateSQLiteDB():
    smsddb = os.path.join(DATA_DIR, 'smsd.db')
    if os.path.exists(smsddb):
        os.unlink(smsddb)
    os.system('%s %s < %s' % (
        SQLITE_BIN,
        smsddb,
        os.path.join(GAMMU_SRC_DIR, 'docs', 'sql', 'sqlite.sql')))

if __name__ == '__main__':
    try:
        smsdrc = os.path.join(DATA_DIR, '.smsdrc')

        # Prepare test environment
        GenerateSMSDRC(smsdrc, os.path.join(DATA_DIR, '.gammurc'))
        CreateSQLiteDB()

        # Load configuration
        smsd = gammu.smsd.SMSD(smsdrc)

        # Inject SMS messages
        # Please note that SMSD is not thread safe, so you can not use inject and main loop from different threads
        message = {'Text': 'python-gammu testing message', 'SMSC': {'Location': 1}, 'Number': '1234567890'}
        smsd.InjectSMS([message])
        message = {'Text': 'python-gammu second testing message', 'SMSC': {'Location': 1}, 'Number': '1234567890'}
        smsd.InjectSMS([message])

        # Start SMSD thread
        smsd_thread = threading.Thread(target = smsd.MainLoop)
        smsd_thread.start()
        # We need to let it run for some time here to finish initialization
        time.sleep(10)

        # Show SMSD status
        retries = 0
        while retries < 60:
            status = smsd.GetStatus()
            for k in status.keys():
                print '%s: %s' % (k, status[k])
            print
            if status['Sent'] >= 2:
                break
            time.sleep(10)
            retries += 1

        if status['Sent'] != 2:
            raise Exception('Messages were not sent!')

        time.sleep(1)

    finally:
        # Signal SMSD to stop
        smsd.Shutdown()

        # Wait for it
        smsd_thread.join()