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

import time
import os
import json
import logging
#import networkx as nx
import numpy as np
import simpy
#from simpy.events import AllOf
#from sim.resources import ASIC, NMS_Queue

log = logging.getLogger(__name__)

def distance(f, t):
    return np.sqrt(np.power(np.abs(f._pos[0] - t._pos[0]), 2) + np.power(np.abs(f._pos[1] - t._pos[1]), 2))

class Warehouse(object):

    def __init__(self, env, args):
        self._env = env
        self._id = args['id']

        self._products = args['products']

        self._pos = (0,0)

        #TODO: Generate availability



class Drone(object):

    def __init__(self, env, args):
        self._env = env
        self._id = args['id']
        self._capacity = args['capacity']

        self._pos = (0,0)

    def load(self, w_id, p_T, count):

        fa = (self._env.now, self._id, w_id, p_T, count)
        print("%.1f: Drone %d loading at warehouse %d product type %d %d times." % fa)
        print("%dL%d%d%d" % fa)

    def unload(self, w_id, p_T, count):

        fa = (self._env.now, self._id, w_id, p_T, count)
        print("%.1f: Drone %d unloading at warehouse %d product type %d %d times." % fa)
        print("%dU%d%d%d" % fa)


class SIM(object):

    def __init__(self):

        self._env = simpy.Environment()

        self._solutionf = open('solution.txt', 'w')

        setattr(self._env, 'solutionf', self._solutionf)

    def setup(self, args):

        self._args = args


    def cleanup(self):
        pass

    def loop(self):

        return

        while True:
            pass
            # wh

            # yield self._env.timeout(self._conf_time)
            #self._env.process(self._nms.create_tunnel(from_id, to_id))

    def run(self):

        start = time.time()

        self._env.process(self.loop())

        self._env.run(until=self._args['until'])
        
        duration = time.time() - start
        
        log.info("Simulation took %.2fs." % duration)



@@ -948,32 +948,33 @@
 mes.%22 %25 fa)%0A
+%0A
         
 print(%22%25dL%25d
@@ -957,29 +957,49 @@
 a)%0A%0A        
-print
+self._env.commands.append
 (%22%25dL%25d%25d%25d%22
@@ -999,24 +999,28 @@
 %25d%25d%25d%22 %25 fa
+%5B1:%5D
 )%0A%0A    def u
@@ -1209,18 +1209,291 @@
     
-print(%22%25dU
+self._env.commands.append(%22%25dU%25d%25d%25d%22 %25 fa%5B1:%5D)%0A%0A    def deliver(self, order_id, p_T, count):%0A%0A        fa = (self._env.now, self._id, order_id, p_T, count)%0A        print(%22%25.1f: Drone %25d delivering for order %25d product type %25d %25d of them.%22 %25 fa)%0A        self._env.commands.append(%22%25dD
 %25d%25d
@@ -1500,16 +1500,20 @@
 %25d%22 %25 fa
+%5B1:%5D
 )%0A%0A%0Aclas
@@ -1612,45 +1612,21 @@
 lf._
-solutionf = open('solution.txt', 'w')
+commands = %5B%5D
 %0A%0A  
@@ -1655,35 +1655,33 @@
 v, '
-solutionf', self._solutionf
+commands', self._commands
 )%0A%0A 
@@ -1758,24 +1758,25 @@
 (self):%0A
+%0A
         
 pass%0A%0A  
@@ -1767,20 +1767,231 @@
         
-pass
+log.debug(%22Saving commands file.%22)%0A%0A        solutionf = open(%22solution.txt%22, 'w')%0A%0A        print(%22%25d%22 %25 len(self._commands), file=solutionf)%0A%0A        for cmd in self._commands:%0A            print(cmd, file=solutionf)
 %0A%0A    de
@@ -2253,32 +2253,116 @@
 time()%0A%0A        
+d = Drone(self._env, %7B'id': 3, 'capacity': 500%7D)%0A%0A        d.load(0, 2, 3)%0A%0A        #
 self._env.proces

commands output