Ejemplo n.º 1
0
    def __init__(self, dllLoader, logger):
        """
        Constructor
        :type dllLoader: DllLoader
        :param dllLoader: the object that will load JWorks driver dll
        :type logger: Logger
        :param logger: the logger of the relay card instance
        """
        JWorksWrapper.__init__(self, logger)

        # load the driver dll
        dllLoader.load_driver()
        self.__dll = dllLoader.get_dll()

        # ---- get serial number ----
        # get method from dll
        funcProto = ctypes.WINFUNCTYPE(ctypes.c_int, ctypes.c_byte,
                                       wintypes.LPSTR, ctypes.c_int)
        jwRelaySerialNumber = funcProto(("jwRelaySerialNumber", self.__dll))
        # call the method
        moduleNum = ctypes.c_byte(1)
        strReturn = wintypes.LPSTR("--------")
        sizeString = ctypes.c_int(len(strReturn.value))
        jwRelaySerialNumber(moduleNum, strReturn, sizeString)
        self._serial_number = strReturn.value

        # ---- get number of relay ----
        # get method from dll
        funcProto = ctypes.WINFUNCTYPE(ctypes.c_short)
        jwRelayNumberOfModules = funcProto(
            ("jwRelayNumberOfModules", self.__dll))
        # call the method
        self._number_of_relay = jwRelayNumberOfModules()
Ejemplo n.º 2
0
    def _set_all_relays_state(self, state):
        """
        Set all relays state at once
        :type state: int
        :param state: the state to set (can be RELAY_STATE_OFF or RELAY_STATE_ON)
        :rtype: bool
        :return: True if all relays are set to "state", False otherwise
        """
        # we want to address all the relays at once, so we create a field of bit
        # all set to 0 for state OFF, and all set to 1 (depending nb of relays) if ON
        relaysToSetBitValue = 0 if state == RELAY_STATE_OFF else (
            (2 << self.number_of_relay) - 1)

        # get method from dll
        funcProto = ctypes.WINFUNCTYPE(ctypes.c_void_p, wintypes.LPSTR,
                                       ctypes.c_uint)
        jwRelaySetBitPattern = funcProto(("jwRelaySetBitPattern", self.__dll))
        # call the method
        strSerial = wintypes.LPSTR(self._serial_number)
        bRelaysToSetBitValue = ctypes.c_uint(relaysToSetBitValue)
        jwRelaySetBitPattern(strSerial, bRelaysToSetBitValue)
        # check that operation succeeded. IE: if state is ON every relay bit should be 1, else should be 0
        state_table = self._get_all_relays_state()
        is_relay_set = ((state == RELAY_STATE_ON
                         and state_table == relaysToSetBitValue)
                        or (state == RELAY_STATE_OFF and state_table == 0))
        return is_relay_set
Ejemplo n.º 3
0
 def _set_relay_state(self, relay_num, state):
     """
     Set the given relay's state
     :type relay_num: int
     :param relay_num: the number of the relay to activate (starting at 1)
     :type state: int
     :param state: the state to set (can be RELAY_STATE_OFF or RELAY_STATE_ON)
     :rtype: bool
     :return: True if relay is set to "state", False otherwise
     """
     # get method from dll
     funcProto = ctypes.WINFUNCTYPE(ctypes.c_void_p, wintypes.LPSTR,
                                    ctypes.c_byte, ctypes.c_byte)
     jwRelaySetRelayState = funcProto(("jwRelaySetRelayState", self.__dll))
     # call the method
     strSerial = wintypes.LPSTR(self._serial_number)
     bRelayNum = ctypes.c_byte(relay_num)
     bState = ctypes.c_byte(state)
     jwRelaySetRelayState(strSerial, bRelayNum, bState)
     # check that operation succeeded
     is_relay_on = self.is_relay_on(relay_num)
     is_relay_set = (state == RELAY_STATE_ON
                     and is_relay_on) or (state == RELAY_STATE_OFF
                                          and not is_relay_on)
     return is_relay_set
Ejemplo n.º 4
0
 def _get_all_relays_state(self):
     """
     :rtype: int
     :return: the relays state as a bit field
     """
     # get method from dll
     funcProto = ctypes.WINFUNCTYPE(ctypes.c_uint, wintypes.LPSTR)
     jwRelayBitPattern = funcProto(("jwRelayBitPattern", self.__dll))
     # call the method
     strSerial = wintypes.LPSTR(self._serial_number)
     relays_state = jwRelayBitPattern(strSerial)
     return relays_state
Ejemplo n.º 5
0
    def __str_to_winastr__(self, in_str_u):
        """
		In: Python unicode string
		Out: ANSI_STRING
		"""

        in_str_a = in_str_u.encode('utf-8')
        in_astr = ANSI_STRING()
        in_astr.Length = len(in_str_a)
        in_astr.MaximumLength = len(in_str_a) + 2
        in_astr.Buffer = wintypes.LPSTR(in_str_a)

        return in_astr
Ejemplo n.º 6
0
def open_siu():
    xfs_version_open = WFSVERSION()
    sp_version = WFSVERSION()
    hService = ctypes.c_ushort()

    sensores = wintypes.LPSTR("Sensores".encode('utf-8'))
    BRXFSTEST = wintypes.LPSTR("BRXFSTEST".encode('utf-8'))

    WFS_DEFAULT_HAPP = ctypes.c_int(0)
    traceLevel = wintypes.DWORD(0)
    timeout = wintypes.DWORD(5000)
    versionOpen = wintypes.DWORD(0x00030303)

    hResult = lib_msxfs.WFSOpen(sensores, WFS_DEFAULT_HAPP, BRXFSTEST,
                                traceLevel, timeout, versionOpen,
                                ctypes.byref(xfs_version_open),
                                ctypes.byref(sp_version),
                                ctypes.byref(hService))

    ShwWFSOpenResult(hResult, hService, xfs_version_open, sp_version)

    lib_msxfs.WFSClose(hService)
    return hResult
Ejemplo n.º 7
0
 def is_relay_on(self, relay_num):
     """
     Check if the given relay's state is ON
     :type relay_num: int
     :param relay_num: the number of the relay to check (starting at 1)
     :rtype: bool
     :return: True if relay is ON, False otherwise
     """
     # get method from dll
     funcProto = ctypes.WINFUNCTYPE(ctypes.c_bool, wintypes.LPSTR,
                                    ctypes.c_byte)
     jwRelayIsRelayOn = funcProto(("jwRelayIsRelayOn", self.__dll))
     # call the method
     strSerial = wintypes.LPSTR(self._serial_number)
     bRelayNum = ctypes.c_byte(relay_num)
     is_relay_on = jwRelayIsRelayOn(strSerial, bRelayNum)
     return is_relay_on
Ejemplo n.º 8
0

class CERT_KEY_CONTEXT(ctypes.Structure):
    _fields_ = [
        ('cbSize', wintypes.DWORD),
        ('hNCryptKey', wintypes.HANDLE),
        ('dwKeySpec', wintypes.DWORD),
    ]


AT_KEYEXCHANGE = 1
AT_SIGNATURE = 2
CERT_NAME_UPN_TYPE = 8
CERT_SHA1_HASH_PROP_ID = 3
CERT_STORE_ADD_REPLACE_EXISTING = 3
CERT_STORE_PROV_MEMORY = wintypes.LPSTR(2)
CERT_STORE_PROV_SYSTEM = wintypes.LPSTR(10)
CERT_SYSTEM_STORE_CURRENT_USER = 0x10000
CERT_SYSTEM_STORE_LOCAL_MACHINE = 0x20000
CERT_X500_NAME_STR = 3
CERT_STORE_ADD_NEW = 1
CERT_STORE_OPEN_EXISTING_FLAG = 0x4000
CERT_STORE_CREATE_NEW_FLAG = 0x2000
CRYPT_SILENT = 64
CRYPT_MACHINE_KEYSET = 32
CRYPT_NEWKEYSET = 8
CRYPT_STRING_BASE64 = 1
PKCS_7_ASN_ENCODING = 0x10000
PROV_RSA_FULL = 1
X509_ASN_ENCODING = 1
CERT_FIND_ANY = 0
Ejemplo n.º 9
0
    _fields_ = [
        ('pwszContainerName', wintypes.LPWSTR),
        ('pwszProvName', wintypes.LPWSTR),
        ('dwProvType', wintypes.DWORD),
        ('dwFlags', wintypes.DWORD),
        ('cProvParam', wintypes.DWORD),
        ('cProvParam', ctypes.POINTER(CRYPT_KEY_PROV_PARAM)),
        ('dwKeySpec', wintypes.DWORD),
    ]


AT_SIGNATURE = 2
CERT_NAME_UPN_TYPE = 8
CERT_SHA1_HASH_PROP_ID = 3
CERT_STORE_ADD_REPLACE_EXISTING = 3
CERT_STORE_PROV_SYSTEM = wintypes.LPSTR(10)
CERT_SYSTEM_STORE_CURRENT_USER = 65536
CERT_SYSTEM_STORE_LOCAL_MACHINE = 131072
CERT_X500_NAME_STR = 3
CRYPT_MACHINE_KEYSET = 32
CRYPT_NEWKEYSET = 8
CRYPT_STRING_BASE64 = 1
PKCS_7_ASN_ENCODING = 65536
PROV_RSA_FULL = 1
X509_ASN_ENCODING = 1
szOID_PKIX_KP_SERVER_AUTH = b"1.3.6.1.5.5.7.3.1"
szOID_RSA_SHA1RSA = b"1.2.840.113549.1.1.5"

advapi32 = windll.advapi32
crypt32 = windll.crypt32
kernel32 = windll.kernel32