Guest User

Untitled

a guest
Jan 26th, 2019
294
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2.  
  3. import time
  4. import sys
  5. import subprocess
  6. import os
  7. import requests
  8. import json
  9. from datetime import datetime
  10. from dateutil import tz
  11. from termcolor import colored
  12.  
  13. def curtime():
  14.     return time.strftime("%H:%M:%S %Y-%m-%d")
  15.  
  16. with open('error.txt','a') as file:
  17.     file.write(('#' * 20) + '\n' + curtime() + '\n')
  18.  
  19. multimon_ng = subprocess.Popen("rtl_fm -f 169.65M -M fm -s 22050 -g 20 | multimon-ng -a FLEX -t raw -",
  20.                                stdout=subprocess.PIPE,
  21.                                stderr=open('error.txt','a'),
  22.                                shell=True)
  23.  
  24. try:
  25.     while True:
  26.         line = multimon_ng.stdout.readline()
  27.         multimon_ng.poll()
  28.  
  29.         if line.__contains__('ALN'):
  30.  
  31.             if line.startswith('FLEX'):
  32.  
  33.         discord = []
  34.         del discord[:]
  35.  
  36.                 flex = line[0:5]
  37.                 timestamp = line[6:25]
  38.                 melding = line[60:]
  39.                 meldinglow = melding.lower()
  40.                 prioriteit = line[60:63]
  41.                 groupid = line[37:43]
  42.                 capcode = line[45:54]
  43.  
  44.                 utc = datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S')
  45.                 utc = utc.replace(tzinfo=tz.tzutc())
  46.                 local = utc.astimezone(tz.tzlocal())
  47.                 local = local.strftime('%d-%m-%Y %H:%M:%S')
  48.                 time_discord = utc.astimezone(tz.tzlocal())
  49.                 time_discord = time_discord.strftime('%H:%M:%S')
  50.  
  51.         prio_check = prioriteit == ('P 1') and melding.find('OMS-') == -1 and melding.find('PAC-') == -1 or meldinglow.find('opschaling') != -1 or meldinglow.find(' grip') != -1
  52.                 vianen_check = meldinglow.find(' vianen ') != -1 or meldinglow.find(' hagestein ') != -1
  53.                 cap_vianen = capcode == '000708906'
  54.                 cap_vc2 = capcode == '000706587' or capcode == '000706952'
  55.                 cap_persalarm = capcode == '000736090'
  56.                 cap_regio = capcode == '000706001'
  57.                 cap_lifeliners = capcode == '000120901' or capcode == '001420059' or capcode == '000923993'
  58.  
  59.                 cap_dict = {
  60.                     '000706001': 'Monitorcode RAC',
  61.                     '000708906': 'Monitorcode Vianen',
  62.                     '000706587': 'VC-2 Bilthoven',
  63.                 '000706952': 'VC-2 Houten',
  64.                 '000736090': 'Persalarm',
  65.                 '000120901': 'Lifeliner 1',
  66.                 '001420059': 'Lifeliner 2',
  67.                 '000923993': 'Lifeliner 3',
  68.                 }
  69.  
  70.             if capcode in cap_dict:
  71.             cap_discord = cap_dict[capcode]
  72.  
  73.             else:
  74.             cap_discord = capcode
  75.  
  76.             if cap_vianen or cap_vc2 or cap_regio:
  77.                 priokleur = 'red'
  78.         color_discord = '16711680'
  79.  
  80.             if cap_lifeliners:
  81.                 priokleur = 'yellow'
  82.         color_discord = '16776960'
  83.  
  84.             if cap_persalarm:
  85.                 priokleur = 'blue'
  86.         color_discord = '255'
  87.  
  88.             if cap_regio or cap_persalarm or cap_vianen or cap_vc2 and vianen_check or cap_lifeliners and vianen_check:
  89.                 print ' '
  90.                 print colored(local,'blue', attrs=['bold']), colored(capcode, 'white', attrs=['bold']), colored(melding, priokleur, attrs=['bold']),
  91.  
  92.             if cap_vianen:
  93.  
  94.                 if vianen_check:
  95.  
  96.                     if prio_check:
  97.                         discord.append('prio1')
  98.  
  99.                     else:
  100.                         discord.append('prio2')
  101.  
  102.                 else:
  103.                     discord.apend('prio2')
  104.  
  105.             if cap_vc2 and vianen_check:
  106.                 discord.append('prio1')
  107.  
  108.             if cap_lifeliners and vianen_check:
  109.                 discord.append('prio1')
  110.  
  111.             if cap_persalarm:
  112.                 discord.append('regio')
  113.  
  114.         if vianen_check:
  115.             discord.append('prio1')
  116.  
  117.             if cap_regio:
  118.         discord.append('regio')
  119.  
  120.             if 'prio1' in discord:
  121.                 url = ""
  122.  
  123.                 data = {}
  124.                 data["username"] = "Prio 1"
  125.  
  126.                 data["embeds"] = []
  127.                 embed = {}
  128.                 embed["color"] = color_discord
  129.                 embed["fields"] = []
  130.  
  131.                 field_alarm = {}
  132.                 field_alarm["name"] = "Melding"
  133.                 field_alarm["value"] = melding
  134.                 embed["fields"].append(field_alarm)
  135.  
  136.                 field_description = {}
  137.                 field_description["name"] = "Cap"
  138.                 field_description["value"] = cap_discord
  139.                 field_description["inline"] = "true"
  140.                 embed["fields"].append(field_description)
  141.  
  142.                 field_time = {}
  143.                 field_time["name"] = "Tijd"
  144.                 field_time["value"] = time_discord
  145.                 field_time["inline"] = "true"
  146.                 embed["fields"].append(field_time)
  147.  
  148.                 data["embeds"].append(embed)
  149.  
  150.                 result = requests.post(url, data=json.dumps(data), headers={"Content-Type": "application/json"})
  151.  
  152.                 try:
  153.                     result.raise_for_status()
  154.                 except requests.exceptions.HTTPError as err:
  155.                     print(err)
  156.                 else:
  157.                     print("Discord: delivered successfully, code {}.".format(result.status_code))
  158.  
  159.             if 'prio2' in discord:
  160.                 url = ""
  161.  
  162.                 data = {}
  163.                 data["username"] = "Prio 2"
  164.  
  165.                 data["embeds"] = []
  166.                 embed = {}
  167.                 embed["color"] = color_discord
  168.                 embed["fields"] = []
  169.  
  170.                 field_alarm = {}
  171.                 field_alarm["name"] = "Melding"
  172.                 field_alarm["value"] = melding
  173.                 embed["fields"].append(field_alarm)
  174.  
  175.                 field_description = {}
  176.                 field_description["name"] = "Cap"
  177.                 field_description["value"] = cap_discord
  178.                 field_description["inline"] = "true"
  179.                 embed["fields"].append(field_description)
  180.  
  181.                 field_time = {}
  182.                 field_time["name"] = "Tijd"
  183.                 field_time["value"] = time_discord
  184.                 field_time["inline"] = "true"
  185.                 embed["fields"].append(field_time)
  186.  
  187.                 data["embeds"].append(embed)
  188.  
  189.                 result = requests.post(url, data=json.dumps(data), headers={"Content-Type": "application/json"})
  190.  
  191.                 try:
  192.                     result.raise_for_status()
  193.                 except requests.exceptions.HTTPError as err:
  194.                     print(err)
  195.                 else:
  196.                     print("Discord: delivered successfully, code {}.".format(result.status_code))
  197.  
  198.             if 'regio' in discord:
  199.                 url = ""
  200.  
  201.                 data = {}
  202.                 data["username"] = "Regio"
  203.  
  204.                 data["embeds"] = []
  205.                 embed = {}
  206.                 embed["color"] = color_discord
  207.                 embed["fields"] = []
  208.  
  209.                 field_alarm = {}
  210.                 field_alarm["name"] = "Melding"
  211.                 field_alarm["value"] = melding
  212.                 embed["fields"].append(field_alarm)
  213.  
  214.                 field_description = {}
  215.                 field_description["name"] = "Cap"
  216.                 field_description["value"] = cap_discord
  217.                 field_description["inline"] = "true"
  218.                 embed["fields"].append(field_description)
  219.  
  220.                 field_time = {}
  221.                 field_time["name"] = "Tijd"
  222.                 field_time["value"] = time_discord
  223.                 field_time["inline"] = "true"
  224.                 embed["fields"].append(field_time)
  225.  
  226.                 data["embeds"].append(embed)
  227.  
  228.                 result = requests.post(url, data=json.dumps(data), headers={"Content-Type": "application/json"})
  229.  
  230.                 try:
  231.                     result.raise_for_status()
  232.                 except requests.exceptions.HTTPError as err:
  233.                     print(err)
  234.                 else:
  235.                     print("Discord: delivered successfully, code {}.".format(result.status_code))
  236.  
  237. except KeyboardInterrupt:
  238.     os.kill(multimon_ng.pid, 9)
RAW Paste Data