#!/usr/bin/env python
#
# Copyright (c) 2014,2017 - Adjacent Link LLC, Bridgewater, New Jersey
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
#   notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in
#   the documentation and/or other materials provided with the
#   distribution.
# * Neither the name of Adjacent Link LLC nor the names of its
#   contributors may be used to endorse or promote products derived
#   from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

from __future__ import absolute_import, division, print_function
import sys
from optparse import OptionParser
from emane.events import EventService
from emane.events import LocationEvent

usage = """emaneevent-location [OPTION]... NEMID[:NEMID] LATITUDE \\
         LONGITUDE ALTITUDE [VELOCITY] [ORIENTATION]

       LATITUDE := 'latitude'=DEGREES

       LONGITUDE := 'longitude'=DEGREES

       ALTITUDE := 'altitude'=MPS

       VELOCITY := 'azimuth'=DEGREES 'elevation'=DEGREES 'magnitude'=MPS

       ORIENTATION := 'pitch'=DEGREES 'roll'=DEGREES  'yaw'=DEGREES

       DEGREES := degrees (float)

       MPS := Meters/Second (float)
"""

description="Publish a location event containing NEMs in a specified \
range. Unless one or more targets are specified the location event will \
be sent to the 'all NEMs' event address.  This is always what you want \
to do since all NEMs must be aware of all other NEM locations."

epilog="""
The NEM range specification creates a list of NEMs that will have the
specified location information.

For example

  emaneevent-location 1:10 latitude=40.031075 longitude=-74.523518 \\
   altitude=3.000000

Will create a single location event that contains 10 entries for NEM
1 to 10, all with the same location: 40.031075 -74.52351 3.0

Velocity and orientation are optional. When missing NEMs will you any
previously received values.

You can use the '--target' option and the '--reference' to specifically
target one or more NEMs in the range with one of more of the
location values. The NEMs specified with '--target' do not have to be
in the range.

For example

  emaneevent-location 1:10 latitude=40.031075 longitude=-74.523518 \\
    altitude=3.000000 -t 3 -t 4 -r 8 -r 9 -r 10

will send location events to NEM 3 and 4 containing the location information
for NEMs 8, 9 and 10.

  emaneevent-location 13 latitude=40.031075 longitude=-74.523518 \\
    altitude=3.000000 -t 7 -t 8

will send a location event to NEM 7 and 8 with the location of NEM 13.

  emaneevent-location 15 -t 0 latitude=40.031075 longitude=-74.523518 \\
    altitude=3.000000

will send a location event to all NEMs with the location of NEM 15.

"""

class LocalParser(OptionParser):
    def format_epilog(self, formatter):
        return self.epilog

optionParser = LocalParser(usage=usage,
                           description=description,
                           epilog=epilog)

optionParser.add_option("-p",
                        "--port",
                        action="store",
                        type="int",
                        dest="port",
                        default=45703,
                        help="Event channel listen port [default: %default]")

optionParser.add_option("-g",
                        "--group",
                        action="store",
                        type="string",
                        dest="group",
                        default="224.1.2.8",
                        help="Event channel multicast group [default: %default]")

optionParser.add_option("-i",
                        "--device",
                        action="store",
                        type="string",
                        dest="device",
                        help="Event channel multicast device")

optionParser.add_option("-t",
                        "--target",
                        action="append",
                        type="int",
                        dest="target",
                        help="Only send an event to the target")

optionParser.add_option("-r",
                        "--reference",
                        action="append",
                        type="int",
                        dest="reference",
                        help="Send events to NEMs in the range but only include information for the reference NEM.")


(options, args) = optionParser.parse_args()

service = EventService((options.group,options.port,options.device))

if len(args) < 2:
    print("missing arguments", file=sys.stderr)
    exit(1)

nems = args[0].split(':')

if len(nems) == 0 or len(nems) > 2:
    print("invalid NEMID format:",args[0], file=sys.stderr)
    exit(1)

try:
    nems = [int(x) for x in nems]
except:
    print("invalid NEMID format:",args[0], file=sys.stderr)
    exit(1)

if len(nems) > 1:
    nems = list(range(nems[0],nems[1]+1))

if not nems:
    print("invalid target format:",args[0], file=sys.stderr)
    exit(1)

kwargs  = {}

for component in args[1:]:
    try:
        (param,value) = component.split("=")
    except:
       print("invalid component format:",component, file=sys.stderr)
       exit(1)

    if param == 'latitude' or \
       param == 'longitude' or \
       param == 'altitude' or \
       param == 'azimuth' or \
       param == 'elevation' or \
       param == 'magnitude' or \
       param == 'pitch' or \
       param == 'roll' or \
       param == 'yaw':
        try:
            kwargs[param] = float(value)
        except:
            print("invalid %s format:"% param,value, file=sys.stderr)
            exit(1)
    else:
        print("invalid location component:",param, file=sys.stderr)
        exit(1)

if nems[0] == 0:
    print("0 is not a valid NEMID", file=sys.stderr)
    exit(1)

if options.target:
    targets = options.target
else:
    targets = [0]

if options.reference:
    references = options.reference
else:
    references = nems

for i in targets:
    event = LocationEvent()
    for j in nems:
        if j in references:
            try:
                event.append(j,**kwargs);
            except Exception as exp:
                print("error:",exp, file=sys.stderr)
                exit(1)


    service.publish(i,event)
