Skip to content

Instantly share code, notes, and snippets.

@tckb
Last active February 5, 2016 20:30
Show Gist options
  • Save tckb/f58959f62724f1ee7d08 to your computer and use it in GitHub Desktop.
Save tckb/f58959f62724f1ee7d08 to your computer and use it in GitHub Desktop.
A python client to simulate device pushes into GeoLocator service
#!//usr/local/bin/python
# Author: tckb <[email protected]>
# A python client to simulate device pushes into GeoLocator service
# Usage:
# ./GeoLocator-sim.py --show (-s) [devices/clusters]: shows devices or clusters available in server
# ./GeoLocator-sim.py --reset (-r) : resets the simulation devices
# ./GeoLocator-sim.py --addDevice : registers new device
# ./GeoLocator-sim.py --start (-S) : starts simulation
# ./GeoLocator-sim.py --help (-h) : prints this message
import osa
import random
import time
import sys,getopt
import os
geoServiceAdaptor = None
bDevice = None
lDevice = None
mDevice = None
pDevice = None
mdDevice = None
paris = None
london = None
milan = None
madrid = None
berlin = None
def Main(cmdArgs):
try:
doInit();
except Exception as e:
print "!! Error initializing service!!"
print e
sys.exit(1)
print "// GeoLocation Simulation \n"
try:
if(len(cmdArgs) == 0):
printUsage()
sys.exit(0)
opts, args = getopt.getopt(cmdArgs,"hrSs:",["reset","start","show=","help","addDevice"])
for opt, arg in opts:
if opt in ("-h", "--help"):
printUsage()
elif opt in ("-r", "--reset"):
setupMockDevices()
resetMockLocation()
registerMockDevices()
elif opt in ("-S", "--start"):
doSimulation()
elif opt == "--addDevice":
addDevice()
elif opt in ("-s", "--show"):
if arg in "devices":
printAllDevices()
elif arg in "clusters":
printAllClusters()
else:
print "!! Invalid options !!"
printUsage()
sys.exit(2)
except getopt.GetoptError:
print "!! Invalid options !!"
printUsage()
sys.exit(2)
def doInit():
global geoServiceAdaptor
global bDevice
global lDevice
global mDevice
global pDevice
global mdDevice
global paris
global london
global milan
global madrid
global berlin
# initiate devices
geoServiceAdaptor= osa.Client("http://webapps-tckb.rhcloud.com/GeoLocator/service/Locator?wsdl");
bDevice = geoServiceAdaptor.types.device()
lDevice = geoServiceAdaptor.types.device()
mDevice = geoServiceAdaptor.types.device()
pDevice = geoServiceAdaptor.types.device()
mdDevice = geoServiceAdaptor.types.device()
#create mock locations
paris = geoServiceAdaptor.types.location()
paris.latitude=48.856614
paris.longitude=2.3522219000000177
berlin = geoServiceAdaptor.types.location()
berlin.latitude=52.52000659999999
berlin.longitude=13.404953999999975
london = geoServiceAdaptor.types.location()
london.latitude=51.5073509
london.longitude=-0.12775829999998223
milan = geoServiceAdaptor.types.location()
milan.latitude=45.4654219
milan.longitude=9.18592430000001
madrid= geoServiceAdaptor.types.location()
madrid.latitude=40.4167754
madrid.longitude=-3.7037901999999576
def setupMockDevices():
print "// Setting up mock devices"
# register london and berlin to cluster 1
bDevice.clustID = "cluster-1"
bDevice.devID = "dev-1"
bDevice.name = "Berliner Device "
bDevice.weight = 1.0
####
lDevice.clustID = "cluster-1"
lDevice.devID = "dev-2"
lDevice.name = "Londoner Device "
lDevice.weight = 1.0
# register milan, madrid and paris to cluster 2
mDevice.clustID = "cluster-2"
mDevice.devID = "dev-3"
mDevice.name = "Milan Device "
mDevice.weight = 1.0
pDevice.devID = "dev-4"
pDevice.clustID = "cluster-2"
pDevice.name = "Paris Device "
pDevice.weight = 1.0
mdDevice.devID = "dev-5"
mdDevice.clustID = "cluster-3"
mdDevice.name = "Milan Device "
mdDevice.weight = 1.0
def resetMockLocation():
bDevice.location = berlin
lDevice.location = london
mDevice.location = milan
pDevice.location = paris
mdDevice.location = milan
def registerMockDevices():
geoServiceAdaptor.service.registerDevice(bDevice)
geoServiceAdaptor.service.registerDevice(lDevice)
geoServiceAdaptor.service.registerDevice(mDevice)
geoServiceAdaptor.service.registerDevice(mdDevice)
geoServiceAdaptor.service.registerDevice(pDevice)
def doSimulation():
setupMockDevices()
resetMockLocation()
print "// Press any key to start simulation ( Cntrl+C to terminate )"
raw_input()
try:
while True:
print "// Updating locations..."
bDevice.location.latitude=bDevice.location.latitude+random.random()
bDevice.location.longitude=bDevice.location.longitude-random.random()
lDevice.location.latitude=lDevice.location.latitude-random.random()
lDevice.location.longitude=lDevice.location.longitude+random.random()
mDevice.location.latitude=mDevice.location.latitude+random.random()
mDevice.location.longitude=mDevice.location.longitude-random.random()
pDevice.location.latitude=pDevice.location.latitude-random.random()
pDevice.location.longitude=pDevice.location.longitude+random.random()
registerMockDevices()
time.sleep(1)
except KeyboardInterrupt:
print "\n// Exiting simulation..."
def printAllDevices():
devices = geoServiceAdaptor.service.getAllDevices();
print "// Number of available device: ",len(devices)
print devices
def printAllClusters():
clusters = geoServiceAdaptor.service.getAllClusters();
print "// Number of available clusters: ",len(clusters)
def printUsage():
scriptName = os.path.basename(sys.argv[0])
print "Usage:"
print "./"+scriptName+" --show (-s) [devices/clusters]: shows devices or clusters available in server"
print "./"+scriptName+" --reset (-r) : resets the simulation devices"
print "./"+scriptName+" --addDevice : registers new device"
print "./"+scriptName+" --start (-S) : starts simulation"
print "./"+scriptName+" --help (-h) : prints this message"
def addDevice():
print "// Enter device details"
try:
devId = raw_input("Enter device id: ")
devName = raw_input("Enter device name: ")
clustID = raw_input("Enter cluster id: ")
lat = float(raw_input("Enter device latitude: "))
lng = float(raw_input("Enter device longitude: "))
print "// Registering device"
loc = geoServiceAdaptor.types.location()
loc.latitude = lat
loc.longitude = lng
device = geoServiceAdaptor.types.device()
device.clustID = clustID
device.devID = devId
device.name = devName
device.location = loc
device.weight = 1.0
geoServiceAdaptor.service.registerDevice(device)
print "// done"
except ValueError:
print "!! Only float variables are accepted, please try again !!"
sys.exit(2)
except Exception as e:
print "!! Cannot register device !!"
print e
sys.exit(2)
if __name__ == "__main__":
Main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment