No Description

run.py 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #!/usr/bin/env python
  2. import RPi.GPIO as GPIO
  3. import time
  4. import os
  5. import sys
  6. import re
  7. import subprocess
  8. GPIO.setmode(GPIO.BCM)
  9. GPIO.setup(26,GPIO.IN, pull_up_down = GPIO.PUD_UP)
  10. GPIO.setup(21,GPIO.OUT)
  11. GPIO.setup(6,GPIO.IN, pull_up_down = GPIO.PUD_UP)
  12. GPIO.setup(20,GPIO.OUT)
  13. GPIO.setmode(GPIO.BCM)
  14. GPIO.setup(17, GPIO.OUT)
  15. os.system('linphonecsh exit')
  16. time.sleep(1)
  17. os.system('linphonecsh init')
  18. time.sleep(1)
  19. os.system('linphonecsh soundcard playback')
  20. time.sleep(1)
  21. os.system('linphonecsh soundcard ring')
  22. time.sleep(1)
  23. os.system('linphonecsh register --username <Nummer> --host hg.eventphone.de --password <password>')
  24. def wecker():
  25. GPIO.output(17, 1)
  26. time.sleep(0.04)
  27. GPIO.output(17, 0)
  28. time.sleep(0.04)
  29. GPIO.output(17, 1)
  30. time.sleep(0.04)
  31. GPIO.output(17, 0)
  32. time.sleep(0.04)
  33. GPIO.output(17, 1)
  34. time.sleep(0.04)
  35. GPIO.output(17, 0)
  36. time.sleep(0.04)
  37. GPIO.output(17, 1)
  38. time.sleep(0.04)
  39. GPIO.output(17, 0)
  40. time.sleep(0.04)
  41. GPIO.output(17, 1)
  42. time.sleep(0.04)
  43. GPIO.output(17, 0)
  44. time.sleep(0.04)
  45. GPIO.output(17, 1)
  46. time.sleep(0.04)
  47. GPIO.output(17, 0)
  48. time.sleep(0.04)
  49. GPIO.output(17, 1)
  50. time.sleep(0.04)
  51. GPIO.output(17, 0)
  52. time.sleep(0.04)
  53. GPIO.output(17, 1)
  54. time.sleep(0.04)
  55. GPIO.output(17, 0)
  56. time.sleep(0.04)
  57. def hangup():
  58. FORK = GPIO.input(6)
  59. if FORK == 1:
  60. os.system('linphonecsh generic terminate')
  61. time.sleep(0.0001)
  62. else:
  63. GPIO.input(6)
  64. def answer():
  65. FORK = GPIO.input(6)
  66. if FORK == 1:
  67. os.system("linphonecsh generic \"answer $(linphonecsh generic 'calls' | sed -n 4p | awk '{print $1}')\"")
  68. time.sleep(0.0001)
  69. else:
  70. GPIO.input(6)
  71. def dialnumber():
  72. DIAL = GPIO.input(26)
  73. NOM = 0
  74. timeout = False
  75. countdown = 100
  76. while countdown > 0:
  77. if timeout:
  78. countdown = countdown -1
  79. if DIAL != 1:
  80. if NOM == 0:
  81. DIAL = GPIO.input(26)
  82. else:
  83. if NOM == 10:
  84. print("0", end='')
  85. else:
  86. print(NOM, end='')
  87. NOM = 0
  88. time.sleep(0.01)
  89. elif DIAL == 1:
  90. NOM = NOM +1
  91. time.sleep(0.109)
  92. DIAL = GPIO.input(26)
  93. countdown = 500
  94. timeout = True
  95. def CALL():
  96. FORK = GPIO.input(6)
  97. time.sleep(0.1)
  98. if FORK == 1:
  99. FORK = GPIO.input(6)
  100. time.sleep(0.0001)
  101. RINGCHECK = 'linphonecsh generic \'calls\' | sed -n 4p | awk \'{print $5}\''
  102. RINGVALUE = subprocess.check_output(['bash', '-c', RINGCHECK ]).decode().strip()
  103. if RINGVALUE == 'IncomingReceived':
  104. wecker()
  105. FORK = GPIO.input(6)
  106. time.sleep(0.0001)
  107. else:
  108. FORK = GPIO.input(6)
  109. time.sleep(0.0001)
  110. else:
  111. FORK = GPIO.input(6)
  112. CMD = 'linphonecsh generic "answer $(linphonecsh generic \'calls\' | sed -n 4p | awk \'{print $1}\')"'
  113. VALUE = subprocess.check_output(['bash', '-c', CMD ]).decode().strip()
  114. if VALUE == 'There are no calls to answer.':
  115. orig_stdout = sys.stdout
  116. f = open('dial.txt', 'w')
  117. sys.stdout = f
  118. dialnumber()
  119. time.sleep(0.0001)
  120. sys.stdout = orig_stdout
  121. f.close()
  122. os.system('linphonecsh dial $(cat dial.txt)')
  123. else:
  124. FORK = GPIO.input(6)
  125. answer()
  126. while FORK == 0:
  127. FORK = GPIO.input(6)
  128. time.sleep(0.001)
  129. else:
  130. FORK = GPIO.input(6)
  131. hangup()
  132. while True:
  133. CALL()