import json
import redis

from redis_graph_py3.redis_graph_functions import Build_Configuration
from redis_graph_py3.redis_graph_functions import Query_Configuration



class Construct_Farm(Build_Configuration):

   def __init__( self ):
         redis_handle  = redis.StrictRedis( host = "localhost", port=6379, db = 14 )       
         super().__init__(redis_handle )
    

   def construct_system( self,name,properties={}):
   
       self.construct_node( push_namespace = True,  relationship="SYSTEM", label = name,  
          properties=properties)
       
   def end_system( self):
       self.pop_namespace()

   def construct_site( self,name, address, properties={}):

       properties["address"] = address
       self.construct_node(  push_namespace=True,relationship="SITE", label=name,  
               properties =properties)

   def end_site( self ):
      self.pop_namespace()

   def add_redis_data_store( self, name, ip, port=6379, properties = {} ):
       
       properties["ip"] = ip
       properties["port"] = port
       self.construct_node( push_namespace=True,relationship="DATA_STORE", label=name,
                               properties= properties )
   def start_moisture_store( self ):
       self.construct_node( push_namespace=True,relationship="MOISTURE_STORE", label="MOISTURE_STORE",
                               properties= {} )

   def end_moisture_store( self ):
      self.pop_namespace()

      
   def add_moisture_sensor_store( self, name, description, description_map, depth_map, update_time ):
       properties = {}
       properties["description"] = description
       properties["description_map"] = json.dumps(description_map)
       properties["update_time"] = update_time
       properties["depth_map"] = json.dumps(depth_map)
       self.construct_node( push_namespace=True,relationship="MOISTURE_DATA", label=name,
                               properties= properties )
   
   def add_status_store( self, name, queue_name):
       properties = {}
       properties["queue_name"] = queue_name
       self.construct_node( push_namespace=True,relationship="STATUS_STORE", label=name,
                               properties= properties )

   def start_info_store( self ):
       self.construct_node( push_namespace=True,relationship="INFO_STORE", label="INFO_STORE",
                               properties= {} )

  
   def add_eto_store(self ):
       self.construct_node( push_namespace=False,relationship="ETO_STORE", label="ETO_STORE",
                               properties= {} )


   def add_air_temperature_humidity_store(self):
       self.construct_node( push_namespace=False,relationship="TEMP_HUMIDITY", label="TEMP_HUMIDITY",
                               properties= {} )

   def add_air_temperature_humidity_daily_log(self):
       self.construct_node( push_namespace=False,relationship="TEMP_HUMIDITY_DAILY", label="TEMP_HUMIDITY_DAILY",
                               properties= {} )
       self.construct_node( push_namespace=False,relationship="TEMP_HUMIDITY_DAILY_ETO", label="TEMP_HUMIDITY_DAILY_ETO",
                               properties= {} )


   def end_info_store(self):   
       self.pop_namespace()



   def end_redis_data_store( self):
       self.pop_namespace()

   def add_udp_io_sever(self, name, ip,remote_type, port, properties={} ):

       properties["ip"] = ip
       properties["remote_type"] = remote_type
       properties["port"] = port
       return self.construct_node(  push_namespace=True,relationship="UDP_IO_SERVER", 
               label=name, properties = properties )


   def end_udp_io_server(self ):
       self.pop_namespace()


   def add_rtu_interface(self, name ,protocol, baud_rate, properties={} ):

       properties["protocol"]= protocol
       properties["baud_rate"] = baud_rate
       return self.construct_node(  push_namespace=True,relationship="RTU_INTERFACE", 
                 label=name,properties = properties)

   def end_rtu_interface( self ):
       self.pop_namespace()



   def add_remote( self, name,modbus_address,type, function, properties = {}):

 
       properties["modbus_address"] = modbus_address
       properties["type"]           = type
       properties["function"]       = function
       self.construct_node(  push_namespace=True,relationship="REMOTE", label=name, 
               properties = properties )


   def construct_controller( self,name, ip,type,properties={} ):
       properties["name"] = name
       properties["ip"]   = ip
       self.construct_node(  push_namespace=True,relationship="CONTROLLER", label=name, 
               properties = properties)


   def end_controller( self ):
       self.pop_namespace()

   def start_service( self, properties = {} ):
       self.construct_node(  push_namespace=TRUE,relationship="SERVICES", label=name, 
               properties = properties)


   def construct_web_server( self, name,url,properties = {} ):

       properties["url"]   = url
       self.construct_node(  push_namespace=False,relationship="WEB_SERVER", label=name, 
               properties = properties)

   def add_rabbitmq_command_rpc_queue( self,name, properties = {} ):

       
       self.construct_node(  push_namespace=False,relationship="COMMAND_RPC_QUEUE", label=name, 
                                properties = properties)

   def add_rabbitmq_web_rpc_queue( self,name, properties = {} ):
 
       
       self.construct_node(  push_namespace=False,relationship="WEB_RPC_QUEUE", label=name, 
                                properties = properties)

   def add_rabbitmq_event_queue( self,name, properties = {} ):

       
       self.construct_node(  push_namespace=False,relationship="RABBITMQ_EVENT_QUEUE", label=name, 
                                 properties = properties)



   def add_rabbitmq_status_queue( self,name,vhost,queue,port,server  ):
       properties          = {}
  
       properties["vhost"]    = vhost
       properties["queue"]    = queue
       properties["port"]     = port
       properties["server"]   = server
       
       self.construct_node(  push_namespace=False,relationship="RABBITMQ_STATUS_QUEUE", label=name, 
                                 properties = properties)


   def add_ntpd_server( self,name, properties = {} ):
       
       self.construct_node(  push_namespace=False,relationship="NTPD_SERVER", label=name, 
                                properties = properties)


   def start_eto_server( self,name, properties = {} ):
 
       self.construct_node(  push_namespace=False,relationship="ETO_SERVER", label=name, properties = properties)

   def add_eto_setup_code( self, access_codes, altitude , properties = {} ):

       properties["messo_eto"]     = json.dumps( access_codes["messo_eto"] )
       properties["messo_precp"]   = json.dumps( access_codes["messo_precp"] )
       properties["cimis_eto"]     = json.dumps( access_codes["cimis_eto"] )
       properties["cimis_spatial"] = json.dumps( access_codes["cimis_spatial"])