# Path: settings.py
# NETWORK_ADAPTATER = {
#     0: "Ethernet 802.3",
#     1: "Token Ring 802.5",
#     2: "Fiber Distributed Data Interface (FDDI)",
#     3: "Wide Area Network (WAN)",
#     4: "LocalTalk",
#     5: "Ethernet using DIX header format",
#     6: "ARCNET",
#     7: "ARCNET (878.2)",
#     8: "ATM",
#     9: "Wireless",
#     10: "Infrared Wireless",
#     11: "Bpc",
#     13: "CoWan",
#     14: "1394",
#     15: "Tunnel"
# }
# 
# Path: utils/utils.py
# def write_to_output(str_to_write, output, logger):
#     """Writes content to a file, encoding the string in UTF-8 for compatibility issues"""
#     try:
#         output.write(str_to_write.encode("utf-8"))
#     except UnicodeError:
#         logger.error(traceback.format_exc())
# 
# def get_csv_writer(csvfile):
#     return UnicodeWriter(csvfile, quoting=csv.QUOTE_ALL)
# 
# def write_to_json(header, arr_data, json_writer):
#     """Write data in json files using UTF-8"""
#     try:
#         json_writer.dump_json({
#             h: arr_data[index] for index, h in enumerate(header)
#         })
#     except IndexError:
#         json_writer.dump_json({
#             header[index]: d for index, d in enumerate(arr_data)
#         })
# 
# def close_json_writer(json_writer):
#     json_writer.close_current()
# 
# def get_json_writer(json_file):
#     return UnicodeJsonDumper(json_file)
# 
# def write_list_to_json(arr_data, json_writer):
#     """Write list data in json files using UTF-8"""
#     json_writer.list_to_json(arr_data)
# 
# def write_to_csv(arr_data, csv_writer):
#     """Writes contents to a CSV file using UTF-8"""
#     csv_writer.writerow(arr_data)
# 
# def get_terminal_decoded_string(string):
#     return string.decode(sys.stdout.encoding, "ignore")
# 
# def record_sha256_logs(fr, fw):
#     with open(fw, "a") as hash_file:
#         m = process_sha256(fr)
#         hash_file.write(fr + "," + m + "\n")
#         hash_file.close()
# 
# def process_md5(path):
#     with open(path, "rb") as f:
#         return hashlib.md5(f.read()).hexdigest()
# 
# def process_sha1(path):
#     with open(path, "rb") as f:
#         return hashlib.sha1(f.read()).hexdigest()


import os
import subprocess
import traceback
import psutil
import win32process
import re
import wmi
import datetime
from settings import NETWORK_ADAPTATER
from utils.utils import write_to_output, get_csv_writer, write_to_json, close_json_writer, get_json_writer,\
     write_list_to_json, write_to_csv, get_terminal_decoded_string, record_sha256_logs, process_md5, process_sha1

            if net_enabled:
                nic = self.wmi.Win32_NetworkAdapterConfiguration(MACAddress=mac_address)
                for nc in nic:
                    database_path = nc.DatabasePath
                    if nc.IPAddress:
                        try:
                            IPv4 = nc.IPAddress[0]
                            IPv6 = nc.IPAddress[1]
                        except IndexError:
                            self.logger.error('Error to catch IP Address %s ' % str(nc.IPAddress))
                    if IPv4:
                        nbtstat = 'nbtstat -A ' + IPv4
                        p = subprocess.Popen(nbtstat, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                        output, errors = p.communicate()
                        # output=utils.decode_output_cmd(output)
                        output = get_terminal_decoded_string(output)
                        nbtstat_value = output.split('\r\n')
                        nbtstat_value = ' '.join([n.replace('\n', '') for n in nbtstat_value])
                    if nc.DNSServerSearchOrder:
                        DNS_server = nc.DNSServerSearchOrder[0]
                    if nc.DHCPEnabled:
                        if nc.DHCPServer:
                            DHCP_server = nc.DHCPServer
            yield netcard, adapter_type, description, mac_address, product_name, physical_adapter, product_name, speed,\
                  IPv4, IPv6, DHCP_server, DNS_server, database_path, nbtstat_value

    def _list_arp_table(self):
        cmd = "arp -a"
        p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        output, errors = p.communicate()

# Path: settings.py
# NETWORK_ADAPTATER = {
#     0: "Ethernet 802.3",
#     1: "Token Ring 802.5",
#     2: "Fiber Distributed Data Interface (FDDI)",
#     3: "Wide Area Network (WAN)",
#     4: "LocalTalk",
#     5: "Ethernet using DIX header format",
#     6: "ARCNET",
#     7: "ARCNET (878.2)",
#     8: "ATM",
#     9: "Wireless",
#     10: "Infrared Wireless",
#     11: "Bpc",
#     13: "CoWan",
#     14: "1394",
#     15: "Tunnel"
# }
# 
# Path: utils/utils.py
# def write_to_output(str_to_write, output, logger):
#     """Writes content to a file, encoding the string in UTF-8 for compatibility issues"""
#     try:
#         output.write(str_to_write.encode("utf-8"))
#     except UnicodeError:
#         logger.error(traceback.format_exc())
# 
# def get_csv_writer(csvfile):
#     return UnicodeWriter(csvfile, quoting=csv.QUOTE_ALL)
# 
# def write_to_json(header, arr_data, json_writer):
#     """Write data in json files using UTF-8"""
#     try:
#         json_writer.dump_json({
#             h: arr_data[index] for index, h in enumerate(header)
#         })
#     except IndexError:
#         json_writer.dump_json({
#             header[index]: d for index, d in enumerate(arr_data)
#         })
# 
# def close_json_writer(json_writer):
#     json_writer.close_current()
# 
# def get_json_writer(json_file):
#     return UnicodeJsonDumper(json_file)
# 
# def write_list_to_json(arr_data, json_writer):
#     """Write list data in json files using UTF-8"""
#     json_writer.list_to_json(arr_data)
# 
# def write_to_csv(arr_data, csv_writer):
#     """Writes contents to a CSV file using UTF-8"""
#     csv_writer.writerow(arr_data)
# 
# def get_terminal_decoded_string(string):
#     return string.decode(sys.stdout.encoding, "ignore")
# 
# def record_sha256_logs(fr, fw):
#     with open(fw, "a") as hash_file:
#         m = process_sha256(fr)
#         hash_file.write(fr + "," + m + "\n")
#         hash_file.close()
# 
# def process_md5(path):
#     with open(path, "rb") as f:
#         return hashlib.md5(f.read()).hexdigest()
# 
# def process_sha1(path):
#     with open(path, "rb") as f:
#         return hashlib.sha1(f.read()).hexdigest()


# Path: health/statemachine.py
import os
import subprocess
import traceback
import psutil
import win32process
import re
import wmi
import datetime
from settings import NETWORK_ADAPTATER
from utils.utils import write_to_output, get_csv_writer, write_to_json, close_json_writer, get_json_writer,\
     write_list_to_json, write_to_csv, get_terminal_decoded_string, record_sha256_logs, process_md5, process_sha1

            if net_enabled:
                nic = self.wmi.Win32_NetworkAdapterConfiguration(MACAddress=mac_address)
                for nc in nic:
                    database_path = nc.DatabasePath
                    if nc.IPAddress:
                        try:
                            IPv4 = nc.IPAddress[0]
                            IPv6 = nc.IPAddress[1]
                        except IndexError:
                            self.logger.error('Error to catch IP Address %s ' % str(nc.IPAddress))
                    if IPv4:
                        nbtstat = 'nbtstat -A ' + IPv4
                        p = subprocess.Popen(nbtstat, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                        output, errors = p.communicate()
                        # output=utils.decode_output_cmd(output)
                        output = get_terminal_decoded_string(output)
                        nbtstat_value = output.split('\r\n')
                        nbtstat_value = ' '.join([n.replace('\n', '') for n in nbtstat_value])
                    if nc.DNSServerSearchOrder:
                        DNS_server = nc.DNSServerSearchOrder[0]
                    if nc.DHCPEnabled:
                        if nc.DHCPServer:
                            DHCP_server = nc.DHCPServer
            yield netcard, adapter_type, description, mac_address, product_name, physical_adapter, product_name, speed,\
                  IPv4, IPv6, DHCP_server, DNS_server, database_path, nbtstat_value

    def _list_arp_table(self):
        cmd = "arp -a"
        p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        output, errors = p.communicate()

        output = get_terminal_decoded_string(output)
