# ===============================================================================
# Copyright 2019 ross
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ===============================================================================
from pychron.hardware.labjack.base_labjack import BaseLabjack


class BaseT4(BaseLabjack):

    def load(self, *args, **kw):


        config = self.get_configuration()
        if config:
            return self.load_additional_args(config)

    def open(self, *args, **kw):
        try:
            self._device = self._create_device()
        except NullHandleException:
            return

        return True

    def load_additional_args(self, config):

        return True

    def initialize(self, *args, **kw):

        chs = [v for v in self._dio_mapping.values() if v not in (u3.CIO0, u3.CIO1, u3.CIO2, u3.CIO3)]
        self._device.configDigital(*chs)

        return True

    def set_channel_state(self, ch, state):
        """

        @param ch:
        @param state: bool True == channel on,  False == channel off
        @return:
        """
        pin = self._get_pin(ch)
        if pin is not None:
            self._device.setDOState(pin, int(not state))

    def get_channel_state(self, ch):
        pin = self._get_pin(ch)
        if pin is not None:
            return self._device.getDIOState(pin)

    def read_dac_channel(self, ch):
        v = self._device.getFIOState(ch)
        return v

    # private
    def _get_pin(self, ch):
        try:
            return self._dio_mapping[str(ch)]
        except KeyError:
            self.warning('Invalid channel {}'.format(ch))
            self.warning('DIOMapping {}'.format(self._dio_mapping))

# ============= EOF =============================================

@@ -787,36 +787,185 @@
 ack%0A
-%0A%0Aclass BaseT4(BaseLabjack):
+from traits.api import Enum, Str%0A%0Afrom labjack import ljm%0A%0A%0Aclass BaseT4(BaseLabjack):%0A%0A    connection_type = Enum('ANY', 'USB', 'TCP', 'ETHERNET',  'WIFI')%0A    identifier = Str
 %0A%0A  
@@ -995,18 +995,16 @@
  **kw):%0A
-%0A%0A
         
@@ -1143,23 +1143,104 @@
  **kw):%0A
+%0A
        
+ self._device = ljm.openS('T4', self.connection_type, self.identifier)%0A        #
  try:%0A  
@@ -1236,32 +1236,34 @@
   # try:%0A       
+ #
      self._devic
@@ -1287,32 +1287,34 @@
 device()%0A       
+ #
  except NullHand
@@ -1330,28 +1330,30 @@
 on:%0A        
+#
     
+ 
 return%0A%0A    
@@ -1413,25 +1413,24 @@
 onfig):%0A
-%0A
         
 return T
@@ -1425,205 +1425,232 @@
     
-return True%0A%0A    def initialize(self, *args, **kw):%0A%0A        chs = %5Bv for v in self._dio_mapping.values() if v not in (u3.CIO0, u3.CIO1, u3.CIO2, u3.CIO3)%5D%0A        self._device.configDigital(*chs)%0A
+self.set_attribute(config, 'connection_type', 'Communications', 'connection_type')%0A        self.set_attribute(config, 'identifier', 'Communications', 'identifier')%0A%0A        return True%0A%0A    def initialize(self, *args, **kw):
 %0A   
@@ -1853,109 +1853,45 @@
     
-pin = self._get_pin(ch)%0A        if pin is not None:%0A            self._device.setDOState(pin
+ljm.eWriteName(self._device, ch
 , int(
-not 
 stat
@@ -1944,457 +1944,135 @@
     
-pin = self._get_pin(ch)%0A        if pin is not None:%0A            return self._device.getDIOState(pin)%0A%0A    def read_dac_channel(self, ch):%0A        v = self._device.getFIOState(ch)%0A        return v%0A%0A    # private%0A    def _get_pin(self, ch):%0A        try:%0A            return self._dio_mapping%5Bstr(ch)%5D%0A        except KeyError:%0A            self.warning('Invalid channel %7B%7D'.format(ch))%0A            self.warning('DIOMapping %7B%7D'.format(self._dio_mapping))%0A
+result = ljm.eReadName(self._device, ch)%0A        return result%0A%0A    def read_dac_channel(self, ch):%0A        pass%0A%0A    # private
 %0A# =

update t4