Skip to content

Instantly share code, notes, and snippets.

@y-ookuma
Last active September 20, 2018 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save y-ookuma/2722d34889c11baa7959c81fd7249f5e to your computer and use it in GitHub Desktop.
Save y-ookuma/2722d34889c11baa7959c81fd7249f5e to your computer and use it in GitHub Desktop.
#-------------------------------------------------------------------------------
# Name: wifi_signal.py
# Purpose: WIFI Signal level SEND CCM
# Author: ookuma yousuke
#
# Created:
# Copyright: (c) ookuma 2018
# Licence: MIT License(X11 License)
#-------------------------------------------------------------------------------
#!/usr/bin python3
# -*- coding: utf-8 -*-
import sys
import subprocess
import sqlite3
from contextlib import closing
from socket import *
def run_capture(cmd):
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout_data, stderr_data = p.communicate()
print(stdout_data.decode('utf-8'))
#Cell count
f = stdout_data.decode('utf-8')
f = f.lstrip()
cnt = f.count("Cell")
wlan_cnt=[]
for i in range(0,cnt):
s = '00' + str(i+1)
s_start= f.find('Cell ' + s[-2:])
wlan_cnt.append(s_start)
wlan_cnt.append(len(f))
wlan = []
for i in range(0,cnt):
s = f[wlan_cnt[i] : wlan_cnt[i+1]]
#address
address = (s[s.find('Address:') : s.find('\n')].replace('Address:','')).replace(':','')
#Quality
Quality = s[s.find('Quality=') : s.find(' Signal')].replace('Quality=','')
#Signal level
Signal = s[s.find('Signal level=') : s.find(' dBm')].replace('Signal level=','')
#ESSID
ESSID = (s[s.find('ESSID:') : s.rfind('\n')].replace('"','')).replace('ESSID:','')
dict = {'Address':address, 'Quality':Quality, 'Signal':Signal, 'ESSID':ESSID}
wlan.append(dict)
return wlan
def get_config():
DB = '/opt/jetty/webapps/root/WEB-INF/db/UecsNode.db'
config ={}
with closing(sqlite3.connect(DB)) as conn:
c = conn.cursor()
# for s in ['Room','Region','Order','Priority','NodeType']:
# select_sql = 'select distinct value from node_config where key=\'%s\'' % (s)
# for row in c.execute(select_sql):
# config[s]=row[0]
rows = c.execute('select * from node_config').fetchall()
for r in rows:
config[r[0]]=r[1]
cmd = 'ifconfig |egrep \'wlan0|inet addr\' |tr -d \'\n\'| awk -F\'wlan0\' \'{print $2}\' |awk -F\'addr:\' \'{print $2}\'|awk \'{print $1}\''
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout_data, stderr_data = p.communicate()
ip_add = stdout_data.decode('utf-8')
# print(config)
return config['Room'],config['Region'],config['Order'],config['Priority'],config['NodeType'],ip_add
def send_CCM(msg):
HOST = ''
PORT = 16520
ADDRESS = "255.255.255.255"
s =socket(AF_INET,SOCK_DGRAM)
# s.bind((HOST, PORT))
s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
s.sendto(msg.encode('utf-8'), (ADDRESS, PORT))
if __name__ == '__main__':
Room,Region,Order,Priority,NodeType,ip_add = get_config()
wlan =[]
wlan = run_capture("sudo iwlist wlan0 scan | egrep \"Cell|ESSID|Signal\"")
# print(wlan)
CCM_data=[]
for x in wlan:
if x['Signal'].find('/100') != -1:
x['Signal'] = '-' + str(x['Signal']).replace('/100','')
# print(x['Signal'])
# CCM SEND
type = x['Address'].replace(' ','') + '.' + NodeType
msg = "<?xml version=\"1.0\"?><UECS ver=\"1.00-E10\">"
msg = msg + "<DATA type=\"" + type + "\""
msg = msg + " room=" + "\"" + Room + "\""
msg = msg + " region=" + "\"" + Region + "\""
msg = msg + " order=" + "\"" + Order + "\""
msg = msg + " priority=" + "\"" + Priority + "\">"
msg = msg + x['Signal'] + "</DATA>"
msg = msg + "<IP>" + ip_add + "</IP></UECS>"
print(msg)
send_CCM(msg)
#!/bin/sh
echo "----------------------------------"
echo " ...WIFI SIGNAL ..."
echo "----------------------------------"
cd
DIR_NAME=`pwd`
script=$DIR_NAME"/wifi_signal.py"
echo "sudo python3 "$script
sudo python3 $script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment