#!/usr/bin/python

# Copyright (c) 2013 Paolo Patruno <p.patruno@iperbole.bologna.it>
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 
# 1. Redistributions of source code must retain the above copyright notice,
#   this list of conditions and the following disclaimer.
# 2. 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.
# 3. Neither the name of mosquitto 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.

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'rmap.settings'
from django.conf import settings

#disable unwanted initialization and error management of kivy
os.environ['KIVY_DOC_INCLUDE'] = "1"

import time
from rmap import rabbitshovel
from django.core.exceptions import ObjectDoesNotExist
from rmap.utils import nint
from rmap import jsonrpc

from rmap import __version__
import rmap.settings
from rmap.stations.models import StationMetadata
import argparse
import rmap.rmap_core

import django
django.setup()

parser = argparse.ArgumentParser(description='Configure rmap boards.')
parser.add_argument('--version', action='version', version="%(prog)s "+__version__)

parser.add_argument('--list_stations',action="store_true", help="list all stations, board and transports")
parser.add_argument('--list_boards',action="store_true", help="list all board on station and transports")
parser.add_argument('--wizard',action="store_true", help="wizard configuration tool (simple set user, password, location on station")
parser.add_argument('--config_station',action="store_true", help="configure board connected to station")

parser.add_argument('--station_slug',  default=rmap.settings.stationslug, help="work on station defined by this slug")
parser.add_argument('--board_slug',  default=None, help="work on board defined by this slug")
parser.add_argument('--device',  default=None, help="use this device for serial transport")
parser.add_argument('--baudrate',  default=None, help="use this baudrate for serial transport")
parser.add_argument('--transport',  default="serial",choices=["serial","tcpip","amqp"],help="work on this transport")
parser.add_argument('--host',  default="master",choices=['master', 'master2', 'master3', 'master4'],help="work on this named board only")
parser.add_argument('--lon',  default=None, help="longitude of station")
parser.add_argument('--lat',  default=None, help="latitude of station")
parser.add_argument('--height',  default=None, help="height of station in meters")
parser.add_argument('--stationname',  default=None, help="name of station")
parser.add_argument('--username',  default=None, help="username for authentication")
parser.add_argument('--password',  default=None, help="password for authentication")
parser.add_argument('--server',  default="rmap.cc", help="rmap server")
parser.add_argument('--samplerate',  default=5, help="samplerate for sensors")
parser.add_argument('--queue',  default="rmap", help="queue for amqp broker")
parser.add_argument('--exchange',  default="rmap", help="exchange for amqp broker")
parser.add_argument('--bluetoothname',  default="HC-05", help="name of the bluetooth device")

args = parser.parse_args()


if args.wizard:

    if (args.username is None or
        args.password is None or
        args.lon is None or
        args.lat is None or
        args.board_slug is None):
        print " selected wizard without username or password or lat or lon or board"
        raise SystemExit(1)


    constantdata={}
    if not args.stationname is None:
        constantdata["B01019"]=args.stationname
    if not args.height is None:
        constantdata["B07030"]=str(int(float(args.height)*10))

    rmap.rmap_core.configdb(username=args.username,password=args.password,
                            station=args.station_slug,lat=args.lat,lon=args.lon,constantdata=constantdata,
                            mqttusername=args.username,
                            mqttpassword=args.password,
                            mqttserver=args.server,
                            mqttsamplerate=args.samplerate,
                            bluetoothname=args.bluetoothname,
                            amqpusername=args.username,
                            amqppassword=args.password,
                            amqpserver=args.server,
                            queue=args.queue,
                            exchange=args.exchange,
                            board=args.board_slug,
                            activate=True)

    print "END of wizard configuration"

if args.list_stations:

    for mystation in StationMetadata.objects.all():
        print "STATION:", mystation
        print "slug=",mystation.slug

    print "END of station list"
    #raise SystemExit(0)

if args.list_boards:


    if (args.username is None or
        args.station_slug is None):
        print " selected list_boards without username or station_slug"
        raise SystemExit(1)


    mystation=StationMetadata.objects.get(slug=args.station_slug,ident__username=args.username)

    print "STATION:", mystation

    for board in mystation.board_set.all():
        print ">board: ", board.name," slug="+board.slug," active=",board.active
        try:
            if ( board.transportserial.active):
                print "\tSerial Transport device=",board.transportserial.device," baudrate=",board.transportserial.baudrate
                
        except ObjectDoesNotExist:
            print "\ttransport serial not present for this board"


        try:
            if ( board.transporttcpip.active):
                print "\tTCP/IP Transport", " hostname=",board.transporttcpip.name

        except ObjectDoesNotExist:

            print "\ttransport TCPIP not present for this board"

        try:
            if ( board.transportamqp.active):
                print "\tAMQP Transport", " amqpserver=",board.transportamqp.amqpserver,
                " exchange=",board.transportamqp.exchange, "queue=",board.transportamqp.queue

        except ObjectDoesNotExist:

            print "\ttransport AMQP not present for this board"


    print "END of board list"
    #raise SystemExit(0)


if args.config_station:

    if (args.username is None or
        args.station_slug is None):
        print " selected config_station without username or station_slug"
        raise SystemExit(1)

    rmap.rmap_core.configstation(transport_name=args.transport,station_slug=args.station_slug,
                                 board_slug=args.board_slug,
                                 device=args.device,baudrate=args.baudrate,host=args.host,username=args.username)

    print "END of station configuration"
