# -*- coding: utf-8 -*-

import scrapy
import re
from itertools import groupby
from scrapy.conf import settings
from scraper.items import ScraperItem
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor

class Rhprofiler(CrawlSpider):
    name = "rhprofiler"
    allowed_domains = [
                 'recrutement.groupe-ldlc.com',
                 'cerfranceseinenormandie.sitederecrutement.com',
                 'recrutement-cassous.com',
                 'solucesrh.fr',
                 'recrutement.foselev.com',
                 'recrutement.acces-rh.eu',
                 'recrutement.5-sante.fr',
                 'www.verdie-recrutement.com',
                 'emploi.stg-logistique.fr',
                 'careers-teknys.com',
                 'emploi.terresdestempliers.fr',
                 'www.recrutement.foncieredesregions.fr',
                 'recrutement.eccfrance.fr',
                 'emploi.basystemes.com',
                 'recrutement.eternit.fr'
    ]
    start_urls = [
        "http://recrutement.groupe-ldlc.com/listeoffre.asp?alsess=",
        "http://cerfranceseinenormandie.sitederecrutement.com/listeoffre.asp?alsess=",
        "http://recrutement-cassous.com/listeoffre.asp?alsess=",
        "http://solucesrh.fr/listeoffre.asp?alsess=",
        "http://recrutement.foselev.com/listeoffre.asp?alsess=",
        "http://recrutement.acces-rh.eu/listeoffre.asp?alsess=",
        "http://recrutement.5-sante.fr/listeoffre.asp?alsess=",
        "http://www.verdie-recrutement.com/listeoffre.asp?alsess=",
        "http://emploi.stg-logistique.fr/listeoffre.asp?alsess=",
        "http://careers-teknys.com/listeoffre.asp?alsess=",
        "http://emploi.terresdestempliers.fr/listeoffre.asp?alsess=",
        "http://www.recrutement.foncieredesregions.fr/listeoffre.asp?alsess=",
        "http://recrutement.eccfrance.fr/listeoffre.asp?alsess=",
        "http://emploi.basystemes.com/listeoffre.asp?alsess=",
        "http://recrutement.eternit.fr/listeoffre.asp?alsess="
    ]

    rules = (
        # # Suivre les pages suivantes
        # Rule(LinkExtractor(allow=('/search/index/page/[0-9]+', ))),

        # Extraire les liens d'offres d'emploi et les passer à la méthode parse_item
        Rule(LinkExtractor(allow=('/consulteroffre\.asp\?idannonce=.*', )), callback='parse_item'),
    )

    def parse_item(self, response):

        item = ScraperItem()

        # Nom de l'ats
        item['ats'] = 'rhprofiler'

        # URL de l'annonce à utiliser pour si en réponse URL
        item['RJ_url'] = response.url

        # Recherche d'une adresse email pour candidater
        item['RJ_email'] = re.findall("[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z]+", response.body)
        while "exemple@exemple.fr" in item['RJ_email']:
            item['RJ_email'].remove("exemple@exemple.fr")
        item['RJ_email'] = [k for k, g in groupby(item['RJ_email'])]

        # Référence du poste si elle existe
        item['RJ_reference'] = None

        # Intitulé et intégralité du texte de l'annonce
        item['RJ_intitulePoste'] = ''.join(response.xpath('//h1/text()').extract()).strip()
        item['RJ_texteoffre'] = []
        RJ_texteoffre = response.xpath('//table//text()').extract()
        for textoffre in RJ_texteoffre:
            textoffre = textoffre.strip()
            item['RJ_texteoffre'].append(textoffre)
        item['RJ_texteoffre'] = [k for k, g in groupby(item['RJ_texteoffre'])]
        item['RJ_texteoffre'] = '\r\n'.join(item['RJ_texteoffre']).strip()

        # Critères non normalisés si ils existent
        criterias = response.xpath('//table[@id="descr"]/tbody/tr')
        for criteria in criterias:
            item['RJ_contrat'] = ''.join(criteria.xpath('//td[span/text()="Type de contrat :"]/following-sibling::td//text()').extract()).strip()
            item['RJ_salaire'] = ''.join(criteria.xpath('//td[span/text()='+u'"Rémunération :"'+']/following-sibling::td//text()').extract()).strip()
            item['RJ_niveauExperience'] = ''.join(criteria.xpath('//td[span/text()='+u'"Précision sur l\'expérience demandée :"'+']/following-sibling::td//text()').extract()).strip()
            item['RJ_niveauQualif'] = ''.join(criteria.xpath('//td[span/text()='+u'"Formation :"'+']/following-sibling::td//text()').extract()).strip()
            item['RJ_locCollection'] = criteria.xpath('//td[span/text()='+u'"Région :"'+']/following-sibling::td//text()').extract()

        # je supprime tous les CP ou code dept entre parenthèses
        item['RJ_locCollection'][:] = [re.sub(r'\([0-9]+\)', '', s).strip() for s in item['RJ_locCollection']]

        # Trouver le nom de l'entreprise avec la première partie de l'url
        entreprise = item['RJ_url'].split('/')[2]
        item['RJ_entreprise'] = settings['RHPROFILER_ENTREPRISES'][entreprise]

        yield item


#!/usr/bin/env python3

VERSION="""grrv2_ocu_color.py
use with grrv2_rover_controller_2017-12-12.py
"""

LANC_COMMAND_HELP = """
LANC COMMAND HELP

KEYBOARD COMMANDS
?: this help message
P: PHOTO    //Doesn't work
R, r: REC   //To confirm if RECORDING, select 'M' for Menu. REC/STBY status is displayed
z: ZOOM_OUT_4
Z: ZOOM_IN_4
f: FOCUS_NEAR //WORKS, IN MANUAL FOCUS MODE ONLY (Set Manual/Auto via menu).
F: FOCUS_FAR  //WORKS, IN MANUAL FOCUS MODE ONLY

CAMCORDER MENU COMMANDS
M, m: MENU
<CR>: SELECT / EXECUTE
8: MENU_UP
2: MENU_DOWN
4: MENU_LEFT or PREV;
6: MENU_RIGHT or NEXT;
O: CAMERA POWER ON
o: CAMERA POWER OFF
L: VIDEO LIGHTS ON
l: VIDEO LIGHTS OFF

RAW LANC COMMAND MODE
X, x: rawHexCommandMenu(); Enter header byte, then raw hex number command byte.
	If you don't know what this means, don't mess with it. :-)
"""

import pygame
from pygame import *

import getopt
import math
import os
import string
import sys

from pygame.locals import *
from socket import *
from time import *
from time import sleep

import colorama
from colorama import Fore, Back, Style

colorama.init()

# Joystick Structure info:
# 8 floats. Up to 6 real axes and 2 virtual axes (from buttons). joystickx[0][0] to [0][7]
# 13 ints.  Up to 12 buttons. joystickx[1][0] to [1][12]. [1][0]
# is ignored since joysticks label 12 buttons 1-12, not 0-11
# 1 string. Joystick Name as pulled from Pygame joystickx[2]

jstick = {
	"axes": [0,0,0,0],
	"scale": [125, -125, 125, -125],	# Corrects native joystick orientation, motor polarity, etc
	"buttons": [0,] * 13,  				# 13 bytes for 12 buttons: 0 is ignored since joysticks number 1-12 not 0-11
	"hat": [0,0], 						# joystick hat x and y, -1, 0, 1
}

class UDPSocket:
	"""UDPSocket tracks a remote and simplifies handling multiple targets"""
	def __init__(self, remoteIP, remotePort):
		"""Opens a new socket with a default destination of remoteIP, remotePort

        Args:
            remoteIP (str): Default remote IP.
            remotePort (int): Default remote Port
        """

		self.remoteIP = remoteIP
		self.remotePort = remotePort
		self.sockobj = socket(AF_INET, SOCK_DGRAM)
		self.sockobj.settimeout(1.0)
		self.last_message = None

	def sendto(self, message, target = None):
		"""Send a message to target"""
		msg = bytes(message)
		if target is None:
			target = (self.remoteIP, self.remotePort)

		try:
			self.sockobj.sendto(msg, target)
		except Exception as e:
			print(e)

		if msg != self.last_message:
			with open("command.log", "wb+") as f:
				f.write(msg)
			self.last_message = msg

	def recvfrom(self, size):
		"""receives a message

		Returns: (data, remote_address)
		"""
		data, remote_addr = self.sockobj.recvfrom(size)
		self.last_remote_addr = remote_addr
		return data, remote_addr

def initalize_joysticks():
	"""Walks through the list of joysticks and ensures PYGame has initilized them"""
	grab_input()

	for x in range(joystick.get_count()): 			# inits however many joysticks are attached, usually 1 or 2
		j = joystick.Joystick(x) 					# Logitech Dual Action is 1 joystick w/ 2 sticks with 1 XY axis pair (4 axes).
		j.init()                       				# Most elaborate joysticks are 1 joystick 1 stick with 2 axes (x, y)
		txt = 'Enable Joystick: ' + j.get_name()
		# jstick['name'] = j[x].get_name()
		print(txt)

	release_input()

def setup_window():
	"""sets the x,y position of the window and required video mode flags
	we don't actually need to run the window to run the program
	"""
	os.environ['SDL_VIDEO_WINDOW_POS'] = "%d, %d" % (1600, 40)
	video_flags = OPENGL|DOUBLEBUF
	display.set_mode((128,96), video_flags)

def main():
	print(VERSION)

	# pygame initialization
	setup_window()
	pygame.init()
	clock, ticks = pygame.time.Clock(), pygame.time.get_ticks()
	initalize_joysticks()

	# a socket to talk to the rover
	rover_socket = UDPSocket('192.168.1.99', 60615)

	print('Starting' + "\n"*4)

	pilt_upcounter = 0
	pilt_downcounter = 255
	loops=1

	motor_enable_flag = 0

	last_keydown = -1
	keyboard_keys = [0,0]

	while True:
		clock.tick(300)  # clock.tick(x) x = Hertz of control loop. 100=100Hz, 20 = 20 Hz Works

		for e in event.get():
			# print e.type,'  ',

			if e.type == pygame.JOYBUTTONDOWN:
				button = e.dict['button'] + 1
				# print("button down")
				jstick['buttons'][0] = button
				if button == 9:
					grab_input()
				if button == 10:
					release_input()
				# print button, ' ',jstick['buttons'][button]

			if e.type == pygame.JOYBUTTONUP:
				# print("button up")
				button = e.dict['button'] + 1
				jstick['buttons'][0] = 0
				# print button, ' ',jstick['buttons'][button]

			if e.type == pygame.KEYDOWN:
				# print(e)
				key, mod, uni = e.dict['key'], e.dict['mod'], e.unicode
				if len(uni) > 0:
					uni = ord(uni)
				else:
					uni = 0
				# key = e.dict['key'] - 32 * e.dict['mod'] # mod = 1 is shift

				# check for control keys
				if mod & pygame.KMOD_CTRL:
					if key == pygame.K_l:		# lock mouse and keyboard to game window
						grab_input()

					if key in (pygame.K_c, pygame.K_d):
						end_program()

				if key == pygame.K_ESCAPE:		# release mouse and keyboard from game window
					release_input()

				if key == pygame.K_SPACE: 		# ALL STOP  SPACE BAR (Big, easy to hit in a panic for faux E-STOP)
					motor_enable_flag = 0

				if key == pygame.K_e and mod & pygame.KMOD_SHIFT: # 'E', ENABLE MOTORS
					motor_enable_flag = 1

				# map arrows, pygame's 273..276 (up, down, right, left) => 128..131
				if key in (pygame.K_LEFT, pygame.K_RIGHT, pygame.K_UP, pygame.K_DOWN):
					map = {pygame.K_LEFT:131, pygame.K_RIGHT:130, pygame.K_UP:128, pygame.K_DOWN:129}
					keyboard_keys[0] = map[key]

				if key == 47:
					print(LANC_COMMAND_HELP)

				# Only send non-continuous commands once
				continuous_key_commands = ('f','F','z','Z')
				if uni not in continuous_key_commands and uni == last_keydown:
					uni = -1
					keyboard_keys[0] = 0

				# The above, including local commands & esc, space, and E all fall through to here and will be sent to the remote. Intentional?
				if uni >= 0 and uni < 256:
					keyboard_keys[0] = uni

			if e.type == pygame.KEYUP:
				key, mod = e.dict['key'], e.dict['mod']

				if key >= 0 and key < 256:
					keyboard_keys[0] = 0

				if key in (pygame.K_LEFT, pygame.K_RIGHT, pygame.K_UP, pygame.K_DOWN):
					keyboard_keys[0] = 0

				last_keydown = -1

			if e.type == pygame.JOYAXISMOTION:
				axis = e.dict['axis']
				# print("joy motion")
				jstick['axes'][axis] = int(e.dict['value'] * jstick['scale'][axis]) # -1.0 to 0 to 1.0 * +/-125

			if e.type == pygame.JOYHATMOTION: # HAT MOTION Use hat +/-y for LANC zoom? And +/-x for focus?
				# print("joy hat motion")
				jstick['hat'] = e.dict['value'] # 0,1 are x,y
				# print("\njstick hat: ", jstick['hat'])
				# since all LANC commands must be sent down the serial LANC wire
				# it's best to treat the Hat as keystrokes so they never compete
				# and the keystroke SwitchCase can prioritize them

				focus_commands = { 1: ord('F'), -1: ord('f') }
				zoom_commands =  { 1: ord('Z'), -1: ord('z') }

				# look it up in the dictionary. If it's not there, use '0'
				key = focus_commands.get(jstick['hat'][0], 0)
				# look up zoom state, if any, and allow that to override focus
				key = zoom_commands.get(jstick['hat'][1], key)
				# print('jstick hat to key', key)
				keyboard_keys[0] = key

			# if e.type == pygame.JOYBALLMOTION:
			# 	pass
			# if e.type in (pygame.MOUSEBUTTONDOWN, pygame.MOUSEBUTTONUP, pygame.MOUSEMOTION):
			# 	pass

		# send the joystick and keyboard commands to the rover
		motions = stick_to_motions(jstick['axes'])
		# print(motions, "\n")
		msg = build_rover_command_byte_packet(motions, joystick, keyboard_keys, motor_enable_flag)
		print('cmd_chr', *msg, end = ' ')
		rover_socket.sendto(msg)

		# RECEIVE DATA FROM ROBOT
		try:
			rover_packet, remote_address = rover_socket.recvfrom(1024)
			rover_status, pilt_status, rover_message = unpack_rover_message(rover_packet)

			pilt_upcounter = (pilt_upcounter + 1) % 255
			pilt_downcounter = 255 - pilt_upcounter

			print_rover_pilt_status(rover_status, pilt_status, rover_message, pilt_upcounter, pilt_downcounter, loops)
		except timeout:
			print('NO ROVER STATUS PACKET RECEIVED.  Last known ', end=' ')

		print("                \r", end=' ')
		sys.stdout.flush()

		loops = loops + 1
		if loops == 101:
			loops = 1

def pilt_indicator_hue(x):
	"""return a color format for printing based on the range of the value"""

	# Well within the limits of travel of pan and tilt
	if 96 <= x <= 160:
		return Style.BRIGHT + Fore.GREEN

	# Approaching the limits of travel of pan and tilt
	if 64 <= x <= 192:
		return Style.BRIGHT + Fore.YELLOW

	# AT OR EXCEEDING the limits of travel of pan and tilt
	return Style.BRIGHT + Fore.RED

def pilt_indicator(x):
	"""Convert a PILT reading to a colorized string"""
	return pilt_indicator_hue(x) + str(x) + Style.RESET_ALL

def print_rover_pilt_status(rover_status, pilt_status, rover_message, pilt_upcounter, pilt_downcounter, loops):
	"""Prints one line corresponding to the rover status. Multiple calls will overwrite previous lines."""
	print(' rover_status', *rover_status, end=' ')

	print('   pilt_status', end=' ')

	for i in range(min(14, len(pilt_status))):
		if i==2:
			print(pilt_indicator(pilt_upcounter), end = ' ')
		elif i==5:
			print(pilt_indicator(pilt_downcounter), end = ' ')
		else:
			print(pilt_status[i], end=' ')

	print('  %s   %d            \r' % (rover_message, loops), end=' ')

	sys.stdout.flush()

def build_rover_command_byte_packet(motions, joystick, keyboard_keys, motor_enable_flag):
	"""Builds a rover command message from parameters"""

	def wire_signed(x):
		"""Converts (-1,1) to (0,1) for PILT"""
		if x == -1:
			return 0
		return 1

	msg = [
		motions['pwm_left'],						# Left Stick (X-Y mixed): Left PWM abs value
		wire_signed(motions['sign_left']),			# Left Stick (X-Y mixed): Left PWM boolean sign map(1=1, -1=0)
		motions['pwm_right'],						# Left Stick (X-Y mixed): Right PWM abs value
		wire_signed(motions['sign_right']),			# Left Stick (X-Y mixed): Right PWM boolean sign map(1=1, -1=0)
		abs(motions['stick_pan']),					# Right Stick,PWM, Pan
		wire_signed(sign(motions['stick_pan'])),	# Right Stick,DIR, Pan
		abs(motions['stick_tilt']),					# Right Stick,PWM, Tilt
		wire_signed(sign(motions['stick_tilt'])),	# Right Stick,DIR, Pan
		jstick['buttons'][0],
		keyboard_keys[0], 				# commands for lanc, lights, whatever to Rover
		jstick['hat'][0] + 1,	 		# JS Hat values are -1, 0, +1.
		jstick['hat'][1] + 1, 			# be unsigned: like joystick axes, it must be converted
		motor_enable_flag,
		0, 								# Spare
		0, 								# Spare
		0, 								# Spare
	] + [45]
	# [45] is an end of message marker

	assert len(msg) == 17
	return msg

def unpack_rover_message(rover_packet):
	"""break apart the received packet and convert from array of bytes to native python types
	Returns:
		rover_status (:obj:`list` of :obj:`int`))
		pilt_status  (:obj:`list` of :obj:`int`))
		rover_message (str)
	"""

	rover_status  = [x for x in rover_packet[0:16]]	# 16 bytes rover
	pilt_status   = [x for x in rover_packet[16:30]]	# 14 bytes Arduino status
	rover_message = rover_packet[32:48]						# Arduino puts.
	if not isinstance(rover_message, str):
		# In Python 3 strings are no longer equivalent to byte arrays, so go through a decode step
		rover_message = rover_message.decode('ascii')
	return rover_status, pilt_status, rover_message

def stick_to_motions(joystick):
	"""Translates joystick to PWMs, signs, octant, pan, and tilt"""
	pwm_left, pwm_right, sign_left, sign_right, octant = map_x_y_to_left_right_pwms(jstick['axes'])
	stick_pan = jstick['axes'][2]
	stick_tilt = jstick['axes'][3]
	motions = {
		"pwm_left": pwm_left,
		"pwm_right": pwm_right,
		"sign_left": sign_left,
		"sign_right": sign_right,
		"stick_pan": stick_pan,
		"stick_tilt": stick_tilt,
		"octant": octant,
	}
	return motions

def get_octant(joystick):
	"""get_octant() handles left gamepad joystick mixing for the tank skid-steering
	As normal cartesian grid has 4 quadrants where X and Y are + /-, we need 8 divisions
	So 'octants'. Based on 3 conditions:
		abs_x => abs_y; sgn_x >0; sgn_y >0. from these are derived the left and right
		'tank track' pwm mixing for single stick skid steer used in map_x_y_to_left_right()
	"""

	stick_x = jstick['axes'][0]
	stick_y = jstick['axes'][1]

	octant = 0
	if abs(stick_y) >= abs(stick_x):
		octant += 4

	if stick_y < 0:
		octant += 2

	if stick_x < 0:
		octant += 1

	return octant

def map_x_y_to_left_right_pwms(joystick):
	"""basically, left joystick positions within XY octants defined as:
	stick fwd, left right tracks fwd: back = bkwd
	stick 90 left or right = pivot lft or rt (one track fwd opposite track rev)
	stick 45 deg = soft turn; one track moving, other track stopped.
	intermediate angles smooth continuum of translate rotated between the above
	"""

	octant = get_octant(jstick['axes'])

	stick_x, stick_y = jstick['axes'][0], jstick['axes'][1]
	abs_x, abs_y     = abs(stick_x), abs(stick_y)
	sign_x, sign_y   = sign(stick_x), sign(stick_y)

	if octant in (0, 3):
		pwm_left = abs_y
		pwm_right = abs_y - abs_x
		sign_left = sign_y
		sign_right = sign_y

	if octant in (1, 2):
		pwm_left = abs_y - abs_x
		pwm_right = abs_y
		sign_left = sign_y
		sign_right = sign_y

	if octant in (4, 7):
		pwm_left = abs_x
		pwm_right = abs_x - abs_y
		sign_left = sign_x
		sign_right = -sign_x

	if octant in (5, 6):
		pwm_left = abs_x - abs_y
		pwm_right = abs_x
		sign_left = sign_x
		sign_right = -sign_x

	dead_boundary = 12

	if pwm_left < dead_boundary:
		pwm_left = 0
	if pwm_right < dead_boundary:
		pwm_right = 0

	return (pwm_left, pwm_right, sign_left, sign_right, octant)

def sign(number):
	"""Returns 1 if the number >= 0, otherwise -1"""
	if number >= 0:
		return 1
	return -1

def grab_input():
	"""Lock mouse and keyboard to window"""
	event.set_grab(True)

def release_input():
	"""Release mouse and keyboard from being locked to window"""
	event.set_grab(False)

def end_program():
	release_input()
	print("ALL STOP")
	print("EXITING OCU PROGRAM")
	sys.exit(0) # exits program

if __name__ == '__main__':
	main()


from rest_framework import permissions

class UpdateOwnProfile(permissions.BasePermission):
    """allow user to edit own profile"""

    def has_object_permission(self, request, view, obj):
        """check user is trying to edit profiel"""
        if request.method in permissions.SAFE_METHODS:
            return True

        return obj.id == request.user.id

class UpdateOwnStatus(permissions.BasePermission):
    """allow usr to upadate their own status"""

    def has_object_permission(self, request, view, obj):
        """used to check user is trying to upadte their own stsaus"""
        if request.method in permissions.SAFE_METHODS:
            return True
        return obj.user_profile.id == request.user.id


#######################################################
#
# TAKFreeServer.py
# Original author: naman108
# This code is Open Source, made available under the EPL 2.0 license.
# https://www.eclipse.org/legal/eplfaq.php
# credit to Harshini73 for base code
#
#######################################################
import argparse
import datetime
import logging
import os
import socket
import sqlite3
import sys
import threading
import time
import traceback
import uuid
import xml.etree.ElementTree as ET
from logging.handlers import RotatingFileHandler

import constants
import SQLcommands
from Controllers.RequestCOTController import RequestCOTController
from Controllers.serializer import Serializer

const = constants.vars()
sql = SQLcommands.sql()

def newHandler(filename, log_level, log_format):
    handler = RotatingFileHandler(
        filename,
        maxBytes=const.MAXFILESIZE,
        backupCount=const.BACKUPCOUNT
    )
    handler.setFormatter(log_format)
    handler.setLevel(log_level)
    return handler

log_format = logging.Formatter(const.LOGFORMAT)
logger = logging.getLogger(const.LOGNAME)
logger.setLevel(logging.DEBUG)
logger.addHandler(newHandler(const.DEBUGLOG, logging.DEBUG, log_format))
logger.addHandler(newHandler(const.WARNINGLOG, logging.WARNING, log_format))
logger.addHandler(newHandler(const.INFOLOG, logging.INFO, log_format))
console = logging.StreamHandler(sys.stdout)
console.setFormatter(log_format)
console.setLevel(logging.DEBUG)
logger.addHandler(console)

''' Server class '''
class ThreadedServer(object):
    def __init__(self, host=const.IP, port=const.PORT):
        # change from string
        self.host = host
        self.port = port
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.sock.bind((self.host, self.port))
        self.client_dict = {}
        logger.info(f"Server IP: {host}, server port: {port}")
        self.emergencyDict = {}
        # configure sql database
        with sqlite3.connect(const.DATABASE) as db:
            cursor = db.cursor()
            cursor.execute(sql.CREATEUSERSTABLE)
            cursor.close()
            db.commit()
        self.bandaidUID = ''

    def listen(self):
        '''
        listen for client connections and begin thread if found
        '''
        threading.Thread(target=self.bandaid, args=(), daemon=True).start()
        self.sock.listen(1000)
        while True:
            try:
                client, address = self.sock.accept()
                threading.Thread(target=self.listenToClient, args=(client, address), daemon=True).start()
            except:
                logger.error(traceback.format_exc())
                logger.error('Error in listen()')

    def bandaid(self):
        while True:
            try:
                start = datetime.datetime.now()
                end = start + datetime.timedelta(minutes=const.RENEWTIME)
                while datetime.datetime.now() < end:
                    time.sleep(10)
                self.bandaidUID = uuid.uuid1()
                mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                mysock.connect(('127.0.0.1', const.PORT))
                mysock.send(Serializer().serializerRoot(RequestCOTController().ping(eventuid=self.bandaidUID)).encode())
                mysock.recv(2048)
                mysock.shutdown(socket.SHUT_RDWR)
                mysock.close()
                logger.info('finished bandaid keepalive')
                logger.debug(f"Currently running {len(threading.enumerate())} threads")
            except ConnectionRefusedError:
                logger.warning("Bandaid listening socket was closed")
            except:
                logger.error(traceback.format_exc())
                logger.error("Error in bandaid()")

    def check_xml(self, xml_string, current_id):
        '''
        check xml type or class
        '''
        data_value = ''
        try:
            if xml_string == const.EMPTY_BYTE:
                logger.info('client disconnected via empty byte response')
                self.client_dict[current_id]['alive'] = 0
                logger.info(str(self.client_dict[current_id]['uid'])+' disconnected')
                return const.FAIL
            elif xml_string == None:
                logger.info('client disconnected via none response')
                self.client_dict[current_id]['alive'] = 0
                logger.info(str(self.client_dict[current_id]['uid'])+' disconnected')
                return const.FAIL

            tree = ET.fromstring(xml_string)
            uid = tree.get('uid')
            logger.debug('parsing data uid is ' + str(uid))
            cot_type = tree.get('type')
            if cot_type == "a-f-G-U-C":
                self.client_dict[current_id]['id_data'] = xml_string
            elif cot_type == 'b-f-t-a':
                destination = tree.find('detail').find('marti').find('dest').attrib['callsign']
                connData = self.client_dict[current_id]["id_data"]
                for x in self.client_dict:
                    if self.client_dict[x]["callsign"] == destination:
                        self.client_dict[x]["main_data"].append(connData)
                        logger.info('adding conn data to '+str(x))
                        logger.info(f"Now adding the following connection data: {str(connData)}")
            if uid.endswith("9-1-1"):
                for x in tree.iter('emergency'):
                    if x.get('cancel') != 'true':
                        self.emergencyDict[uid] = xml_string
                    else:
                        del self.emergencyDict[uid]
            elif uid.endswith(const.PING):
                data_value = const.PING
                logger.debug(f"Received a ping: {xml_string}")
            elif uid.startswith(const.GEOCHAT):
                data_value = const.GEOCHAT
                logger.debug(f"Received a GeoChat: {xml_string}")
            else:
                logger.debug(f"Received CoT: {xml_string}")

            # adds data to all connected client data list except sending client
            for detail in tree.findall('detail'):
                marti = detail.find('marti')
                if marti != None:
                    dest = marti.find('dest')
                    callsign = dest.attrib['callsign']
                    for client_id in self.client_dict:
                        if self.client_dict[client_id]['callsign'] == callsign:
                            self.client_dict[client_id]['main_data'].append(xml_string)
                else:
                    for client_id in self.client_dict:
                        if client_id != current_id:
                            self.client_dict[client_id]['main_data'].append(xml_string)
            return data_value

        except:
            logger.error(traceback.format_exc())
            logger.error(f"Error in check_xml for: {xml_string}")

    def connectionSetup(self, client, address):
        db = sqlite3.connect(const.DATABASE)
        try:
            cursor = db.cursor()
            first_run = 1
            # Create client dictionary within main dictionary containing arrays for data and chat also other stuff for client initial connection
            total_clients_connected = 0
            total_clients_connected += 1
            id_data = client.recv(const.STARTBUFFER)
            logger.debug(f"id_data = {id_data}")
            tree = ET.fromstring(id_data)
            uid = tree.get('uid')
            if uid == self.bandaidUID:
                return 'Bandaid'
            callsign = tree.find('detail').find('contact').attrib['callsign']
            current_id = uuid.uuid1().int

            # add identifying information
            self.client_dict[current_id] = {
                'id_data': id_data,
                'main_data': [],
                'alive': 1,
                'uid': uid,
                'client': client,
                'callsign': callsign
            }
            cursor.execute(sql.INSERTNEWUSER, (str(current_id), str(uid), str(callsign)))
            cursor.close()
            db.commit()
            logger.info(f"Client connected, initial information for current_id={current_id}: {self.client_dict[current_id]}")
            return str(first_run)+' ? '+str(total_clients_connected)+' ? '+str(id_data)+' ? '+str(current_id)
        except:
            logger.error(traceback.format_exc())
            logger.error('Error in connection setup')
            return "error"
        finally:
            db.close()

    def recieveAll(self, client):
        try:
            total_data = []
            while True:
                data = client.recv(const.BUFFER)
                logger.debug(f"Received {sys.getsizeof(data)} bytes from {client}")
                if sys.getsizeof(data) == const.BUFFER+33:
                    total_data.append(data)
                elif sys.getsizeof(data) < const.BUFFER+33:
                    total_data.append(data)
                    break
            total_data = b''.join(total_data)
            return total_data
        except:
            logger.error(traceback.format_exc())
            logger.error(f"Error in recieveAll() from {client}")
            return None

    def listenToClient(self, client, address):
        '''
        Function to receive data from the client. this must be long as everything
        '''
        try:
            defaults = self.connectionSetup(client, address)
            if defaults == 'error':
                client.shutdown(socket.SHUT_RDWR)
                client.close()
                return
            elif defaults == 'Bandaid':
                self.sock.shutdown(socket.SHUT_RDWR)
                client.close()
                return
            else:
                defaults = defaults.split(' ? ')
                logger.debug(defaults)
                first_run = int(defaults[0])
                id_data = bytes(defaults[2], 'utf-8')
                current_id = int(defaults[3])
                # main connection loop
                while True:
                    # Receive data
                    try:
                        if first_run == 0:
                            data = self.recieveAll(client)
                            logger.debug(f"Received data from client: {str(data)}")
                            working = self.check_xml(data, current_id)
                            # checking if check_xml detected client disconnect
                            if working == const.FAIL:
                                timeoutInfo = Serializer().serializerRoot(RequestCOTController().timeout(
                                    eventhow='h-g-i-g-o',
                                    eventuid=uuid.uuid1(),
                                    linkuid=self.client_dict[current_id]['uid']
                                ))
                                logger.debug(f"Sending timeout: {timeoutInfo.encode()}")
                                for client_id in self.client_dict:
                                    if client_id != current_id:
                                        self.client_dict[client_id]['client'].send(timeoutInfo.encode())
                                uid = self.client_dict[current_id]['uid']
                                del self.client_dict[current_id]
                                with sqlite3.connect(const.DATABASE) as db:
                                    cursor = db.cursor()
                                    cursor.execute(sql.DELETEBYUID, (uid,))
                                    cursor.close()
                                    db.commit()
                                    client.shutdown(socket.SHUT_RDWR)
                                    client.close()
                                return
                            elif working == const.PING:
                                logger.debug('Received ping')

                        elif first_run == 1:
                            for client_id in self.client_dict:
                                client = self.client_dict[client_id]['client']
                                if client != self.client_dict[current_id]['client']:
                                    logger.info('Sending '+str(id_data))
                                    client.send(self.client_dict[current_id]['id_data'])
                            for client_id in self.client_dict:
                                data = self.client_dict[client_id]['id_data']
                                logger.debug('Sending conn data to '+str(client))
                                client.send(data)
                            threading.Thread(
                                target=self.sendClientData,
                                args=(client, address, current_id),
                                daemon=True).start()

                        # just some debug stuff
                        first_run = 0
                    except:
                        logger.error(traceback.format_exc())
                        logger.error('Error in listenToClient() main loop')
                        client.close()
                        return
        except Exception as e:
            logger.error(traceback.format_exc())
            logging.error("Unknown error in listenToClient")
            client.close()

    def sendClientData(self, client, address, current_id):
        try:
            while True:
                time.sleep(const.DELAY)
                for uid in self.emergencyDict:
                    client.send(self.emergencyDict[uid])
                    logger.info(f"Emergency activated: {uid}")

                if len(self.client_dict[current_id]['main_data']) > 0:
                    for x in self.client_dict[current_id]['main_data']:
                        logger.debug(self.client_dict[current_id]['main_data'])
                        client.send(x)
                        logger.info('Sent ' + str(x) + ' to ' + str(address))
                        self.client_dict[current_id]['main_data'].remove(x)
                else:
                    client.send(Serializer().serializerRoot(RequestCOTController().ping(eventuid=uuid.uuid1())).encode())
        except:
            logger.error(traceback.format_exc())
            logger.warning('Error in sendClientData')
        finally:
            client.close()

def startup():
    logger.info('starting windows service')
    ThreadedServer(host=const.IP, port=const.PORT).listen()

if __name__ == "__main__":
    try:
        parser = argparse.ArgumentParser()
        parser.add_argument("-p", type=int)
        args = parser.parse_args()
        port = args.p
    except:
        ThreadedServer(host='', port=const.PORT).listen()
        logger.error(f"Failed to read port number from command arguments, defaulting to {const.PORT}")
        port = const.PORT
    ThreadedServer(host=const.IP, port=port).listen()


import unittest
import requests
import json
import mgr_functions

class TestQueryOrder(unittest.TestCase):
    def setUp(self):
        self.url = "http://172.16.17.148:8080/hzhotel/mgrweb-api/order/queryOrder"

    def testQueryOrder(self):
        token = mgr_functions.get_token()
        order_date = json.dumps({"head": {"token": token, "channel": "", "clientId": ""},
                                "currentPage": 1, "contactMobile": "", "orderNo": "", "pageSize": 10})
        headers = {
            'Content-Type': "application/json"
        }

        response = requests.request('POST', self.url, data=order_date, headers=headers)
        self.result = response.json()
        self.head = self.result['head']
        print(self.result['msg'])
        self.assertEqual(0, self.result['code'])

if __name__ == '__main__':
    unittest.main()


#Ora;  Perc;   AdasDb;   Nev
#  0     1        2        3
#6;0;2;Laci

class Cb:
    def __init__(self, sor):
        s = sor.strip().split(';')
        self.ora  = int(s[0])
        self.perc = int(s[1])
        self.db   = int(s[2])
        self.nev  =     s[3]

#Előkészületek
with open("cb.txt", 'r', encoding='UTF-8-sig') as f:
    fejlec = f.readline()
    matrix = [Cb(sor) for sor in f]

#3. feladat
print(f' 3. feladat: Bejegyzések száma: {len(matrix)} db.')

# 4. feladat
for sor in matrix:
    if sor.db == 4:
        print(f' 4. feladat: Volt 4 adást indító sofőr!')
        break

#5. feladat
sofor = input(f' 5. feladat: Kérek egy nevet: ')
adas = 0
van = False

for sor in matrix:
    if sor.nev == sofor:
        adas = adas + sor.db
        van = True
if van:
    print(    f'         {sofor} {adas}x használta a CB rádiót.')
else:
    print(    f'         Nincs ilyen nevű sofőr!')

#6. feladat
def AtszamolPercre(ora, perc):
    return ora * 60 + perc

#7.  feladat

cb2fejlec = 'Kezdes;Nev;AdasDb\n'
with open('cb2.txt', 'w', encoding='UTF-8') as fout:
#    fout.write(cb2fejlec)
    print(cb2fejlec, end='', file=fout)
    for sor in matrix:
        perc   = AtszamolPercre(sor.ora, sor.perc)
        nev    = sor.nev
        darab  = sor.db
        szoveg = f'{perc};{nev};{darab}\n'
#       fout.write(szoveg)
        print(szoveg, end='', file=fout)

#8. feladat

halmaz = set()
for sor in matrix:
    halmaz.add(sor.nev)
print(f' 8. feladat: Sofőrök száma: {len(halmaz)} fő.')

#9. feladat

szotar = {}
for sor in matrix:
    szotar[sor.nev] = 0

for sor in matrix:
    szotar[sor.nev] += sor.db

nev, db = sorted(szotar.items(), key=lambda x:x[1], reverse=True)[0]
print(f' 9. feladat: A legtöbb adást indító sofőr:')
print(f'         Név: {nev}')
print(f'         Adások száma: {db} alkalom')


from qiskit import QuantumCircuit,QuantumRegister,ClassicalRegister
from qiskit import Aer,IBMQ
from qiskit import execute
from qiskit.tools import visualization
from qiskit.tools.visualization import circuit_drawer, plot_histogram
import matplotlib.pyplot as plt
from executeCircuit import execute_locally,extractClassical
import random as r
from qAlgorithms import QMF
import math
import random
import numpy as np
import sys
from qiskit.aqua.components.initial_states import Custom

#######################################################################
############ QMF TEST FOR A UNIFORM SUPERPOSITION STATE ###############
########################## FUNCTIONAL #################################
#######################################################################

n = 4 # quantum state will have 2**n superposition states
superpositions = 2**n
maximum_expected = superpositions-1

#IN THIS CASE THE MAXIMUM WILL BE |1111> = 15

qstate = QuantumRegister(n,name="qstate") #quantum register that contains the superposition
new_qr = QuantumRegister(n,name="new_qr") #qantum register for qmf measuring test
qc = QuantumCircuit(qstate,new_qr)

qc.h(qstate)
qc.h(new_qr) #new_qr will be measured as |0001> = 1

'''
	INIT PARAMS FOR THE QUANTUM MAXIMUM FINDING ALG

__init__(self, circuit=None,search_register=None,num_iterations=None, max_index=None, extra_registers=None,size=None):

#############################################################################################
	IF WITH A UNIFORM SUPERPOSITION STATE LIKE qstate WE CHOOSE INITIAL index = 0 or 1,
	WHAT HAPPENS IS THAT THE ORACLE WILL MARK ABOUT N-1 OR N-2 TERMS IN THE SUPERPOSITION
	AND SO, THE AVERAGE WILL BECOME NEGATIVE, AND THE INVERSION ABOUT MEAN OPERATOR WILL
	NOT WORK PROPERLY, MEANING THAT THE PROBABILITY OF COLLAPSING INTO 0 OR 1 INSTEAD OF
	OF AN ACTUAL SOLUTION WILL BECOME LARGER AND LARGER MAKING REACHING THE MAXIMUM IMPOSSIBLE
	IN THESE CASES RANDOMLY CHOOSING AN INTIAL GUESS, IS THE BEST STRATEGY FOR ACHIEVING
	THE MAXIMUM.
##############################################################################################
'''

index = int(sys.argv[1])
alg = QMF(circuit=qc,search_register=qstate,size=superpositions,max_index=index,extra_registers=[new_qr])

Backend = Aer.get_backend('qasm_simulator')
maximum, top_measurement = alg.run(backend=Backend,shots=1024)

#measured_val = result['result']
#maximum = result['top_measurement']

print("MAXIMUM SHOULD BE |",np.binary_repr(maximum_expected,n),"> = %d" % maximum_expected)
print("MAXIMUM ACHIEVED - %d\n" % maximum)
print("TOP MEASUREMENT\n",top_measurement)

#######################################################################
############ QMF TEST FOR AN ARBITRAY CUSTOM INTIAL STATE #############
########################## FUNCTIONAL #################################
#######################################################################
'''
state vector array - > numpy array [[Real,imaginary], ... ] for all superposition state
For n=4 qubits the first term encodes the amplitude of the state |0000> the second term encodes |0001> and so one.

For n=4 i'll create the state = |s> = 1/sqrt(2) ( |0100> + |0010> ) and QMF should return
|0100> = 4 as the maximum value within the state.
'''

state=[]
for i in range(2**n):
	if i == 4 or i == 2:
		state.append(complex(1/math.sqrt(2),0.0))
	else:
		state.append(complex(0.0,0.0))

state = np.asarray(state)

quantum_state = Custom(n,state_vector=state)

index = int(sys.argv[1])
alg = QMF(initial_state=quantum_state,search_register=qstate,size=superpositions,max_index=index)

Backend = Aer.get_backend('qasm_simulator')
maximum, top_measurement = alg.run(backend=Backend,shots=1024)

#measured_val = result['result']
#maximum = result['top_measurement']

print("MAXIMUM SHOULD BE |0100> = 4\n")
print("MAXIMUM ACHIEVED - %d\n" % maximum)
print("TOP MEASUREMENT\n",top_measurement)

###########################################################################
############ QMF TEST FOR A STATE THATS NOT IN SUPERPOSITION ##############
########################## FUNCTIONAL #####################################
###########################################################################
'''
Test if the quantum maximum finding algorithm can find the maximum in a register
tht's not in superposition, qstate, however it is conditioned by another register, h_state, that's in superposition.

qc has 2 CNOT gates , the result should be |psi> = |11>|11> , giving the maximum on the qstate register of |qstate> = |11> = 3
'''
h_state = QuantumRegister(2,name="hstate")
qstate = QuantumRegister(2,name="qstate")
qc= QuantumCircuit(h_state,qstate)

h_size = 4

qc.h(h_state)
qc.cx(h_state[0],qstate[0])
qc.cx(h_state[1],qstate[1])

alg = QMF(circuit=qc,search_register=qstate,size=h_size,extra_registers=[h_state],draw_circuit=True)

Backend = Aer.get_backend('qasm_simulator')
maximum, top_measurement = alg.run(backend=Backend,shots=1024)

#measured_val = result['result']
#maximum = result['top_measurement']

print("MAXIMUM SHOULD BE |1> = 1\n")
print("MAXIMUM ACHIEVED - %d\n" % maximum)
print("TOP MEASUREMENT\n",top_measurement)


# basic packages #
import os
import numpy as np
import tensorflow as tf
tf.logging.set_verbosity(tf.logging.ERROR)
import pickle
# trial generation and network building #

import sys
sys.path.append('.')
from utils import tools

from task_and_network.task import generate_trials
from task_and_network.network import Model

def gen_task_info(hp,
                log,
                model_dir,
                rules,
                return_trial_store=False,):
    task_info_file = model_dir+'/task_info.pkl'
    if os.path.isfile(task_info_file):
        with open(task_info_file,'rb') as tinfr:
            task_info = pickle.load(tinfr)
    else:
        task_info = dict()

    trial_store = dict()

    print("Epoch information:")
    for rule in rules:
        task_info[rule] = dict()
        trial_store[rule] = generate_trials(rule, hp, 'test', noise_on=False)

        n_stims = len([ep for ep in trial_store[rule].epochs.keys() if 'stim' in ep])
        stim_loc_log_len = int(len(trial_store[rule].input_loc)/n_stims)
        task_info[rule]['in_loc'] = np.array([np.argmax(i) for i in trial_store[rule].input_loc[:stim_loc_log_len]])
        if n_stims != 1:
            for nst in range(2,n_stims+1):
                task_info[rule]['in_loc_'+str(nst)] = \
                    np.array([np.argmax(i) for i in trial_store[rule].input_loc[(nst-1)*stim_loc_log_len:nst*stim_loc_log_len]])

        task_info[rule]['in_loc_set'] = sorted(set(task_info[rule]['in_loc']))
        task_info[rule]['epoch_info'] = trial_store[rule].epochs
        print('\t'+rule+':')
        for e_name, e_time in task_info[rule]['epoch_info'].items():
            print('\t\t'+e_name+':',e_time)

    with open(task_info_file,'wb') as tinf:
        pickle.dump(task_info, tinf)

    if return_trial_store:
        return trial_store

def compute_H_(hp, model_dir, rule, trial_num, trial=None, task_mode='test'):

    if trial is None:
        trial = generate_trials(rule, hp, task_mode, noise_on=False)

    sub_dir = model_dir+'/'+str(trial_num)+'/'
    model = Model(sub_dir, hp=hp)
    with tf.Session() as sess:
        model.restore()
        feed_dict = tools.gen_feed_dict(model, trial, hp)
        h = sess.run(model.h, feed_dict=feed_dict)
    return h

def compute_H(
            hp,
            log,
            model_dir,
            rules=None,
            trial_list=None,
            recompute=False,
            save_H_pickle=True,
            ):

    if rules is not None:
        rules = rules
    else:
        rules = hp['rule_trains']

    if trial_list is None:
        trial_list = log['trials']
    elif isinstance(trial_list, list):
        trial_list = trial_list

    trial_store = gen_task_info(hp,log,model_dir,rules,True,)

    for rule in rules:
        if isinstance(trial_list, dict):
            temp_list = list()
            for value in trial_list[rule].values():
                temp_list += value
            temp_list = sorted(set(temp_list))
        elif isinstance(trial_list, list):
            temp_list = trial_list

        for trial_num in temp_list:
            H_file = model_dir+'/'+str(trial_num)+'/H_'+rule+'.pkl'
            if recompute or not os.path.isfile(H_file):
                H_ = compute_H_(hp, model_dir, rule, trial_num, trial = trial_store[rule])
                with open(H_file,'wb') as wh:
                    pickle.dump(H_,wh)

def Get_H(hp,model_dir,trial_num,rule,save_H=False,task_mode='test',):

    H_file = model_dir+'/'+str(trial_num)+'/H_'+rule+'.pkl'

    if os.path.isfile(H_file):
        with open(H_file,'rb') as hf:
            H = pickle.load(hf)

    else:
        H = compute_H_(hp, model_dir, rule, trial_num, trial=None, task_mode=task_mode)
        if save_H:
            with open(H_file,'wb') as wh:
                pickle.dump(H,wh)
    return H


from utils import Logger
import scipy.signal
import signal
import numpy as np

"""

    Adapted from code written by Patrick Coady (pat-coady.github.io)

"""

class Agent(object):
    def __init__(self,policy,val_func,env,input_normalizer,logger,policy_episodes=20,policy_steps=10,gamma1=0.0,gamma2=9.995,lam=0.98,
                    normalize_advantages=True,use_tdlam=False,use_timestep=False,monitor=None, animate=False):
        self.env = env
        self.monitor = monitor
        self.policy_steps = policy_steps
        self.logger = logger
        self.use_tdlam = use_tdlam
        self.use_timestep = use_timestep
        self.policy = policy
        self.val_func = val_func
        self.input_normalizer = input_normalizer
        self.policy_episodes = policy_episodes
        self.animate = animate
        self.normalize_advantages = normalize_advantages
        self.gamma1 = gamma1
        self.gamma2 = gamma2

        self.lam = lam
        self.global_steps = 0

        """

            Args:
                policy:                 policy object with update() and sample() methods
                val_func:               value function object with fit() and predict() methods
                env:                    environment
                input_normalizer:       scaler object with apply(), reverse(), and update() methods
                logger:                 Logger object

                policy_episodes:        number of episodes collected before update
                policy_steps:           minimum number of steps before update
                    (will update when either episodes > policy_episodes or steps > policy_steps)

                gamma:                  discount rate
                lam:                    lambda for GAE calculation
                normalize_advantages:   boolean, normalizes advantages if True
                use_tdlam:              boolean, True uses TD lambda target for value function, else Monte Carlo
                use_timestep:           boolean, True enables time step feature which sometimes works better than a
                                        low discount rate for continuing tasks with per-step rewards (like Mujoco envs)
                monitor:                A monitor object like RL_stats to plot interesting stats as learning progresses
                                        Monitor object implements update_episode() and show() methods
                animate:                boolean, True uses env.render() method to animate episode

        """

    def run_episode(self):
        """

        Returns: 4-tuple of NumPy arrays
            observes: shape = (episode len, obs_dim)
            actions: shape = (episode len, act_dim)
            rewards: shape = (episode len,)
            unscaled_obs: useful for training scaler, shape = (episode len, obs_dim)
        """
        obs = self.env.reset()
        observes, actions, rewards1, rewards2, unscaled_obs  =  [], [], [], [], []
        done = False
        step = 0.0
        while not done:
            if self.animate:
                self.env.render()
            obs = obs.astype(np.float64).reshape((1, -1))
            unscaled_obs.append(obs.copy())
            if self.input_normalizer is not None:
                obs = self.input_normalizer.apply(obs)
            if self.use_timestep:
                obs = np.append(obs, [[step]], axis=1)  # add time step feature
            observes.append(obs)
            action, env_action = self.policy.sample(obs)# .reshape((1, -1)).astype(np.float64) #[:,0:-1])
            actions.append(action)
            obs, reward, done, reward_info = self.env.step(env_action)
            reward1 = reward[0]
            reward2 = reward[1]
            if not isinstance(reward1, float):
                reward1 = np.asscalar(reward1)
            if not isinstance(reward1, float):
                reward2 = np.asscalar(reward2)
            rewards1.append(reward1)
            rewards2.append(reward2)
            step += 1e-3  # increment time step feature
        #logger.log({'Score': sum_rewards})
        return (np.concatenate(observes), np.concatenate(actions), np.array(rewards1, dtype=np.float64),
                np.array(rewards2, dtype=np.float64), np.concatenate(unscaled_obs))

    def run_policy(self,episode_cnt,warmup=False):
        """ Run policy and collect data for a minimum of min_steps and min_episodes
        Args:
            episode_cnt: current episode number, used for logging stats

        Returns: list of trajectory dictionaries, list length = number of episodes
            'observes' : NumPy array of states from episode
            'actions' : NumPy array of actions from episode
            'rewards' : NumPy array of (un-discounted) rewards from episode
            'unscaled_obs' : NumPy array of (un-discounted) rewards from episode
        """
        total_steps = 0
        e_cnt = 0
        trajectories = []
        #for e in range(self.policy_episodes):
        while e_cnt <= self.policy_episodes or total_steps < self.policy_steps:
            observes, actions, rewards1, rewards2, unscaled_obs  = self.run_episode()
            if self.monitor is not None and not warmup:
                self.monitor.update_episode(np.sum(rewards1) + np.sum(rewards2),  observes.shape[0])
            total_steps += observes.shape[0]
            trajectory = {'observes': observes,
                          'actions': actions,
                          'rewards1': rewards1,
                          'rewards2': rewards2,
                          'unscaled_obs': unscaled_obs}
            trajectories.append(trajectory)
            e_cnt += 1
        unscaled = np.concatenate([t['unscaled_obs'] for t in trajectories])
        if self.input_normalizer is not None:
            self.input_normalizer.update(unscaled)  # update running statistics for scaling observations

        self.add_value(trajectories)  # add estimated values to episodes
        self.add_disc_sum_rew(trajectories, self.gamma1, self.gamma2)  # calculated discounted sum of Rs
        self.add_gae(trajectories, self.gamma1, self.gamma2, self.lam)  # calculate advantage
        # concatenate all episodes into single NumPy arrays
        observes, actions, advantages, disc_sum_rew = self.build_train_set(trajectories)

        if not warmup:
            self.policy.update(observes, actions, advantages, self.logger)  # update policy
            self.val_func.fit(observes, disc_sum_rew, self.logger)  # update value function
            self.log_batch_stats(observes, actions, advantages, disc_sum_rew, episode_cnt)
            self.global_steps += total_steps
            self.logger.log({'_MeanReward': np.mean([t['rewards1'].sum() + t['rewards2'].sum() for t in trajectories]),
                         '_StdReward': np.std([t['rewards1'].sum() + t['rewards2'].sum() for t in trajectories]),
                         '_MinReward': np.min([t['rewards1'].sum() + t['rewards2'].sum()  for t in trajectories]),
                         'Steps': total_steps,
                         'TotalSteps' : self.global_steps})
            if self.monitor is not None:
                self.monitor.show()
        return trajectories

    def train(self,train_episodes, train_samples=None):
        _ = self.run_policy(-1,warmup=True)
        print('*** SCALER WARMUP COMPLETE *** ')
        print(np.sqrt(self.input_normalizer.vars))
        episode = 0

        if train_samples is not None:
            while self.global_steps < train_samples:
                trajectories = self.run_policy(episode)
                self.logger.write(display=True)
                episode += len(trajectories)
        else:
            while episode < train_episodes:
                trajectories = self.run_policy(episode)
                self.logger.write(display=True)
                episode += len(trajectories)

    def discount(self,x, gamma):
        """ Calculate discounted forward sum of a sequence at each point """
        return scipy.signal.lfilter([1.0], [1.0, -gamma], x[::-1])[::-1]

    def add_disc_sum_rew(self,trajectories, gamma1, gamma2):
        """ Adds discounted sum of rewards to all time steps of all trajectories

        Args:
            trajectories: as returned by run_policy()
            gamma: discount

        Returns:
            None (mutates trajectories dictionary to add 'disc_sum_rew')
        """
        for trajectory in trajectories:
            if gamma1 < 0.999:  # don't scale for gamma ~= 1
                rewards1 = trajectory['rewards1'] * (1 - gamma1)
            else:
                rewards1 = trajectory['rewards1']

            if gamma2 < 0.999:  # don't scale for gamma ~= 1
                rewards2 = trajectory['rewards2'] * (1 - gamma2)
            else:
                rewards2 = trajectory['rewards2']

            disc_sum_rew1 = self.discount(rewards1, gamma1)
            disc_sum_rew2 = self.discount(rewards2, gamma2)

            trajectory['disc_sum_rew'] = disc_sum_rew1 + disc_sum_rew2

    def add_value(self,trajectories):
        """ Adds estimated value to all time steps of all trajectories

        Args:
            trajectories: as returned by run_policy()
            val_func: object with predict() method, takes observations
                and returns predicted state value

        Returns:
            None (mutates trajectories dictionary to add 'values')
        """
        for trajectory in trajectories:
            observes = trajectory['observes']
            values = self.val_func.predict(observes)
            trajectory['values'] = values

    def add_gae(self,trajectories, gamma1, gamma2, lam):
        """ Add generalized advantage estimator.
        https://arxiv.org/pdf/1506.02438.pdf

        Args:
            trajectories: as returned by run_policy(), must include 'values'
                key from add_value().
            gamma: reward discount
            lam: lambda (see paper).
                lam=0 : use TD residuals
                lam=1 : A =  Sum Discounted Rewards - V_hat(s)

        Returns:
            None (mutates trajectories dictionary to add 'advantages')
        """
        for trajectory in trajectories:
            advantages = trajectory['disc_sum_rew'] - trajectory['values']
            trajectory['advantages'] = advantages

    def build_train_set(self,trajectories):
        """

        Args:
            trajectories: trajectories after processing by add_disc_sum_rew(),
                add_value(), and add_gae()

        Returns: 4-tuple of NumPy arrays
            observes: shape = (N, obs_dim)
            actions: shape = (N, act_dim)
            advantages: shape = (N,)
            disc_sum_rew: shape = (N,)
        """
        observes = np.concatenate([t['observes'] for t in trajectories])
        actions = np.concatenate([t['actions'] for t in trajectories])
        disc_sum_rew = np.concatenate([t['disc_sum_rew'] for t in trajectories])
        advantages = np.concatenate([t['advantages'] for t in trajectories])
        # normalize advantages
        if self.normalize_advantages:
            advantages = (advantages - advantages.mean()) / (advantages.std() + 1e-6)
        else:
            advantages = advantages - advantages.mean()

        return observes, actions, advantages, disc_sum_rew

    def log_batch_stats(self,observes, actions, advantages, disc_sum_rew, episode):
        """ Log various batch statistics """
        self.logger.log({'_mean_obs': np.mean(observes),
                '_min_obs': np.min(observes),
                '_max_obs': np.max(observes),
                '_std_obs': np.mean(np.var(observes, axis=0)),
                '_mean_act': np.mean(actions),
                '_min_act': np.min(actions),
                '_max_act': np.max(actions),
                '_std_act': np.mean(np.var(actions, axis=0)),
                '_mean_adv': np.mean(advantages),
                '_min_adv': np.min(advantages),
                '_max_adv': np.max(advantages),
                '_std_adv': np.var(advantages),
                '_mean_discrew': np.mean(disc_sum_rew),
                '_min_discrew': np.min(disc_sum_rew),
                '_max_discrew': np.max(disc_sum_rew),
                '_std_discrew': np.var(disc_sum_rew),
                '_Episode': episode
            })


from __future__ import division

from math import ceil
from time import time
from itertools import groupby
from collections import deque

import six
from numpy import arange, mean
import matplotlib.pyplot as plt

from neupy.utils import format_data, is_row1d
from neupy.helpers import preformat_value
from neupy.core.base import BaseSkeleton
from neupy.core.properties import (Property, FuncProperty, NumberProperty,
                                   BoolProperty)
from neupy.layers import BaseLayer, OutputLayer
from neupy.functions import normilize_error_output, mse
from .utils import iter_until_converge, shuffle
from .connections import (FAKE_CONNECTION, LayerConnection,
                          NetworkConnectionError)

__all__ = ('BaseNetwork',)

def show_training_summary(network):
    network.logs.data("""
        Epoch {epoch}
        Train error:  {error}
        Validation error: {error_out}
        Epoch time: {epoch_time} sec
    """.format(
        epoch=network.epoch,
        error=network.last_error_in() or '-',
        error_out=network.last_error_out() or '-',
        epoch_time=round(network.train_epoch_time, 5)
    ))

def show_epoch_summary(network, show_epoch):
    delay_limit = 1  # in seconds
    prev_summary_time = None
    delay_history_length = 10
    terminal_output_delays = deque(maxlen=delay_history_length)

    while True:
        now = time()

        if prev_summary_time is not None:
            time_delta = now - prev_summary_time
            terminal_output_delays.append(time_delta)

        show_training_summary(network)
        prev_summary_time = now

        if len(terminal_output_delays) == delay_history_length:
            prev_summary_time = None
            average_delay = mean(terminal_output_delays)

            if average_delay < delay_limit:
                show_epoch *= ceil(delay_limit / average_delay)
                network.logs.warning(
                    "Too many outputs in a terminal. Set "
                    "up logging after each {} epoch"
                    "".format(show_epoch)
                )
                terminal_output_delays.clear()

        yield show_epoch

def shuffle_train_data(input_train, target_train):
    if target_train is None:
        return shuffle(input_train), None
    return shuffle(input_train, target_train)

def clean_layers(connection):
    """ Clean layers connections and format transform them into one format.
    Also this function validate layers connections.

    Parameters
    ----------
    connection : list, tuple or object
        Layers connetion in different formats.

    Returns
    -------
    object
        Cleaned layers connection.
    """
    if connection == FAKE_CONNECTION:
        return connection

    if isinstance(connection, tuple):
        connection = list(connection)

    islist = isinstance(connection, list)

    if islist and isinstance(connection[0], BaseLayer):
        chain_connection = connection.pop()
        for layer in reversed(connection):
            chain_connection = LayerConnection(layer, chain_connection)
        connection = chain_connection

    elif islist and isinstance(connection[0], LayerConnection):
        pass

    if not isinstance(connection.output_layer, OutputLayer):
        raise NetworkConnectionError("Final layer must be OutputLayer class "
                                     "instance.")

    return connection

def parse_show_epoch_property(value, n_epochs):
    if isinstance(value, int):
        return value

    number_end_position = value.index('time')
    # Ignore grammar mistakes like `2 time`, this error could be
    # really annoying
    n_epochs_to_check = int(value[:number_end_position].strip())

    if n_epochs <= n_epochs_to_check:
        return 1

    return int(round(n_epochs / n_epochs_to_check))

class ShowEpochProperty(Property):
    """ Class helps validate specific syntax for `show_epoch`
    property from ``BaseNetwork`` class.
    """
    expected_type = tuple([int] + [six.string_types])

    def validate(self, value):
        if not isinstance(value, six.string_types):
            if value < 1:
                raise ValueError("Property `{}` value should be integer "
                                 "greater than zero or string. See the "
                                 "documentation for more information."
                                 "".format(self.name))
            return

        if 'time' not in value:
            raise ValueError("`{}` value has invalid string format."
                             "".format(self.name))

        valid_endings = ('times', 'time')
        number_end_position = value.index('time')
        number_part = value[:number_end_position].strip()

        if not value.endswith(valid_endings) or not number_part.isdigit():
            valid_endings_formated = ', '.join(valid_endings)
            raise ValueError(
                "Property `{}` in string format should be a positive number "
                "with one of those endings: {}. For example: `10 times`."
                "".format(self.name, valid_endings_formated)
            )

        if int(number_part) < 1:
            raise ValueError("Part that related to the number in `{}` "
                             "property should be an integer greater or "
                             "equal to one.".format(self.name))

class BaseNetwork(BaseSkeleton):
    """ Base class Network algorithms.

    Parameters
    ----------
    {full_params}

    Methods
    -------
    {plot_errors}
    {last_error}
    """
    error = FuncProperty(default=mse)
    use_bias = BoolProperty(default=True)
    step = NumberProperty(default=0.1)

    # Training settings
    show_epoch = ShowEpochProperty(min_size=1, default='10 times')
    shuffle_data = BoolProperty(default=False)

    # Signals
    train_epoch_end_signal = FuncProperty()
    train_end_signal = FuncProperty()

    def __init__(self, connection, **options):
        self.connection = clean_layers(connection)

        self.errors_in = []
        self.errors_out = []
        self.train_epoch_time = None

        self.layers = list(self.connection)
        self.input_layer = self.layers[0]
        self.output_layer = self.layers[-1]
        self.train_layers = self.layers[:-1]

        super(BaseNetwork, self).__init__(**options)
        self.setup_defaults()

        if self.verbose:
            self.show_network_options(highlight_options=options)

        self.init_layers()

    def show_network_options(self, highlight_options=None):
        available_classes = [cls.__name__ for cls in self.__class__.__mro__]
        logs = self.logs

        if highlight_options is None:
            highlight_options = {}

        def classname_grouper(option):
            classname = option[1].class_name
            class_priority = -available_classes.index(classname)
            return (class_priority, classname)

        # Sort and group options by classes
        grouped_options = groupby(
            sorted(self.options.items(), key=classname_grouper),
            key=classname_grouper
        )

        if isinstance(self.connection, LayerConnection):
            logs.header("Network structure")
            logs.log("LAYERS", self.connection)

        # Just display in terminal all network options.
        logs.header("Network options")
        for (_, clsname), class_options in grouped_options:
            if not class_options:
                # When in some class we remove all available attributes
                # we just skip it.
                continue

            logs.simple("{}:".format(clsname))

            for key, data in sorted(class_options):
                if key in highlight_options:
                    logger = logs.log
                    value = highlight_options[key]
                else:
                    logger = logs.gray_log
                    value = data.value

                logger("OPTION", "{} = {}".format(
                    key, preformat_value(value))
                )
            logs.empty()

    def setup_defaults(self):
        """ Setup default values before populate the options.
        """

    # ----------------- Neural Network Layers ---------------- #

    def init_layers(self):
        """ Initialize layers.
        """
        if self.connection == FAKE_CONNECTION:
            return

        for layer in self.train_layers:
            layer.initialize(with_bias=self.use_bias)

    # ----------------- Neural Network Train ---------------- #

    def _train(self, input_train, target_train=None, input_test=None,
               target_test=None, epochs=100, epsilon=None):
        """ Main method for the Neural Network training.
        """

        # ----------- Pre-format target data ----------- #

        input_row1d = is_row1d(self.input_layer)
        input_train = format_data(input_train, row1d=input_row1d)

        target_row1d = is_row1d(self.output_layer)
        target_train = format_data(target_train, row1d=target_row1d)

        if input_test is not None:
            input_test = format_data(input_test, row1d=input_row1d)

        if target_test is not None:
            target_test = format_data(target_test, row1d=target_row1d)

        # ----------- Validate input values ----------- #

        if epsilon is not None and epochs <= 2:
            raise ValueError("Network should train at teast 3 epochs before "
                             "check the difference between errors")

        # ----------- Predefine parameters ----------- #

        self.epoch = 1
        show_epoch = self.show_epoch
        logs = self.logs
        compute_error_out = (input_test is not None and
                             target_test is not None)
        predict = self.predict
        last_epoch_shown = 0

        if epsilon is not None:
            iterepochs = iter_until_converge(self, epsilon, max_epochs=epochs)

            if isinstance(show_epoch, six.string_types):
                show_epoch = 100
                logs.warning("Can't use `show_epoch` value in converging "
                             "mode. Set up 100 to `show_epoch` property "
                             "by default.")

        else:
            iterepochs = range(1, epochs + 1)
            show_epoch = parse_show_epoch_property(show_epoch, epochs)

        epoch_summary = show_epoch_summary(self, show_epoch)

        # ----------- Train process ----------- #

        logs.header("Start train")
        logs.log("TRAIN", "Train data size: {}".format(input_train.shape[0]))

        if input_test is not None:
            logs.log("TRAIN", "Validation data size: {}"
                              "".format(input_test.shape[0]))

        if epsilon is None:
            logs.log("TRAIN", "Total epochs: {}".format(epochs))
        else:
            logs.log("TRAIN", "Max epochs: {}".format(epochs))

        logs.empty()

        # Optimizations for long loops
        errors = self.errors_in
        errors_out = self.errors_out
        shuffle_data = self.shuffle_data

        error_func = self.error
        train_epoch = self.train_epoch
        train_epoch_end_signal = self.train_epoch_end_signal
        train_end_signal = self.train_end_signal

        self.input_train = input_train
        self.target_train = target_train

        for epoch in iterepochs:
            self.epoch = epoch
            epoch_start_time = time()

            if shuffle_data:
                input_train, target_train = shuffle_train_data(input_train,
                                                               target_train)
                self.input_train = input_train
                self.target_train = target_train

            try:
                error = train_epoch(input_train, target_train)

                if compute_error_out:
                    predicted_test = predict(input_test)
                    error_out = error_func(predicted_test, target_test)
                    errors_out.append(error_out)

                errors.append(error)
                self.train_epoch_time = time() - epoch_start_time

                if epoch % show_epoch == 0 or epoch == 1:
                    show_epoch = next(epoch_summary)
                    last_epoch_shown = epoch

                if train_epoch_end_signal is not None:
                    train_epoch_end_signal(self)

            except StopIteration as err:
                logs.log("TRAIN", "Epoch #{} stopped. {}"
                                  "".format(epoch, str(err)))
                break

        if epoch != last_epoch_shown:
            show_training_summary(self)

        if train_end_signal is not None:
            train_end_signal(self)

        logs.log("TRAIN", "End train")

    # ----------------- Errors ----------------- #

    def _last_error(self, errors):
        if errors and errors[-1] is not None:
            return normilize_error_output(errors[-1])

    def last_error_in(self):
        return self._last_error(self.errors_in)

    def last_error(self):
        return self._last_error(self.errors_in)

    def last_error_out(self):
        return self._last_error(self.errors_out)

    def previous_error(self):
        errors = self.errors_in
        return normilize_error_output(errors[-2]) if len(errors) > 2 else None

    def _normalized_errors(self, errors):
        if not len(errors) or isinstance(errors[0], float):
            return errors

        self.logs.warning("Your errors bad formated for plot output. "
                          "They will be normilized.")

        normilized_errors = []
        for error in errors:
            normilized_errors.append(normilize_error_output(error))

        return normilized_errors

    def normalized_errors_in(self):
        return self._normalized_errors(self.errors_in)

    def normalized_errors_out(self):
        return self._normalized_errors(self.errors_out)

    def plot_errors(self, logx=False):
        if not self.errors_in:
            return

        errors_in = self.normalized_errors_in()
        errors_out = self.normalized_errors_out()
        errors_range = arange(len(errors_in))
        plot_function = plt.semilogx if logx else plt.plot

        line_error_in, = plot_function(errors_range, errors_in)
        title_text = 'Learning error after each epoch'

        if errors_out:
            line_error_out, = plot_function(errors_range, errors_out)
            plt.legend(
                [line_error_in, line_error_out],
                ['Train error', 'Validation error']
            )
            title_text = 'Learning errors after each epoch'

        plt.title(title_text)
        plt.xlim(0)

        plt.ylabel('Error')
        plt.xlabel('Epoch')

        plt.show()

    # ----------------- Representations ----------------- #

    def get_class_name(self):
        return self.__class__.__name__

    def __repr__(self):
        classname = self.get_class_name()
        options_repr = self._repr_options()

        if self.connection != FAKE_CONNECTION:
            return "{}({}, {})".format(classname, self.connection,
                                       options_repr)
        return "{}({})".format(classname, options_repr)


"""
使用numpy实现卷积核的运算
"""
import numpy as np
import cv2

def concolve(datMat, kernel):
    """
    :param datMat: 要处理的数据
    :param kernel: 卷积核
    :return:处理完的数据
    """
    # 获取形状
    m = datMat.shape[0]
    n = datMat.shape[1]
    km = kernel.shape[0]
    kn = kernel.shape[1]
    # 创建全“1”数组
    newMat = np.ones((m, n), dtype=np.uint8)
    tempMat = np.ones((km, kn), dtype=np.uint8)

    for row in range(m - km + 1):
        for col in range(n - kn + 1):
            for m_k in range(km):
                for n_k in range(kn):
                    tempMat[m_k, n_k] = datMat[(row + m_k), (col + n_k)] * kernel[m_k, n_k]
            newMat[row, col] = np.sum(tempMat)

    return newMat

Core = np.array(((-0.1, 2, 0.1),
                (-0.2, 0, 0.2),
                (1, -2, -1)), dtype=np.uint8)
imgCore = np.array(((-0.1, 2, 0.1, 0),
                    (-0.2, 0, 0.2, 0),
                    (1, -2, -1, 0),
                    (0, 0, 0, 0)), dtype=np.uint8)

img = cv2.imread('./picture.jpg')
img = cv2.resize(img, (640, 480))
# cv2.imshow('Demo_0', img)
img_nov = cv2.filter2D(img, -1, Core)
cv2.imshow('Demo_1', img_nov)
img_nov = concolve(imgCore, Core)

cv2.waitKey(0)
cv2.destroyAllWindows()


"""Verify that a climate file is well formed."""
import sys
import datetime

def main(argv):
    """Run for a given file."""
    fn = argv[1]
    lines = open(fn).readlines()
    tokens = lines[4].strip().split()
    syear = int(tokens[4])
    # simyears = int(tokens[5])
    linenum = 15
    yesterday = datetime.date(syear - 1, 12, 31)
    while linenum < len(lines):
        tokens = lines[linenum].split()
        if len(tokens) < 4:
            print("linenum: %s has len(tokens): %s" % (linenum, len(tokens)))
            sys.exit()
        thisdate = datetime.date(
            int(tokens[2]), int(tokens[1]), int(tokens[0])
        )
        if (thisdate - yesterday) != datetime.timedelta(days=1):
            print(
                "linenum: %s has date: %s, not %s"
                % (linenum, thisdate, yesterday + datetime.timedelta(days=1))
            )
            sys.exit()
        yesterday = thisdate
        lastprecip = -1
        for _ in range(int(tokens[3])):
            linenum += 1
            tokens = lines[linenum].split()
            if len(tokens) != 2:
                print("linenum: %s has bad token count" % (linenum,))
                sys.exit()
            tm = float(tokens[0])
            if tm < 0 or tm >= 24:
                print("linenum: %s has bad time: %s" % (linenum, tokens[0]))
                sys.exit()
            precip = float(tokens[1])
            if precip < 0 or precip >= 350:
                print("linenum: %s has bad precip: %s" % (linenum, tokens[1]))
                sys.exit()
            if precip <= lastprecip:
                print(
                    "linenum: %s has decreasing precip: %s, last %s"
                    % (linenum, tokens[1], lastprecip)
                )
                sys.exit()
            lastprecip = precip
        linenum += 1

if __name__ == "__main__":
    main(sys.argv)