from cpppo.server.enip.get_attribute import proxy_simple
import logging

class VFD_COMM(proxy_simple):
    '''A simple (non-routing) CIP device with one parameter with a
       shortcut name: 'A Sensor Parameter' '''
    PARAMETERS = dict(proxy_simple.PARAMETERS,
        at_reference = proxy_simple.parameter('@0x2A/1/3', 'BOOL', "I/O"),
        net_ref = proxy_simple.parameter('@0x2A/1/4', 'BOOL', "I/O"),
        drive_mode = proxy_simple.parameter('@0x2A/1/6', 'SINT', "mode"),
        speed_actual = proxy_simple.parameter('@0x2A/1/7', 'INT', "Hz"),
        speed_ref = proxy_simple.parameter('@0x2A/1/8', 'INT', "Hz"),
        torque_actual = proxy_simple.parameter('@0x2A/1/0x0B', 'INT', "Nm"),
        torque_ref = proxy_simple.parameter('@0x2A/1/0x0C', 'INT', "Nm"),
        speed_scale = proxy_simple.parameter('@0x2A/1/0x16', 'SINT', "Nm"),
        torque_scale = proxy_simple.parameter('@0x2A/1/0x18', 'SINT', "Nm"),
        ref_from_net = proxy_simple.parameter('@0x2A/1/0x1D', 'BOOL', "I/O")
    )


class Vacon100:
    def __init__(self, ip_address):
        self.ip_address = ip_address
        self.cpppo_proxy = VFD_COMM(ip_address)

    def read_ACDCDriveOBj(self):
        try:
            vals_wanted = [
                "At Reference",
                "Net Ref",
                "Drive Mode",
                "Speed Actual",
                "Speed Ref",
                "Torque Actual",
                "Torque Ref",
                "Speed Scale",
                "Torque Scale",
                "Ref From Net"
            ]
            params = self.cpppo_proxy.parameter_substitution(vals_wanted)
            vals = self.cpppo_proxy.read(params)
            val_list = list(vals)
            for x in range(0, len(vals_wanted)):
                print("{}: {}".format(vals_wanted[x], val_list[x][0]))
            return(vals)
        except Exception as exc:
            logging.warning("Access to remote CIP device failed: %s", exc)
            self.cpppo_proxy.close_gateway(exc=exc)
            raise


def main():
    vacon_100 = Vacon100("10.20.4.27")
    print(vacon_100.read_ACDCDriveOBj())

if __name__ == '__main__':
    import sys
    sys.exit(int(main() or 0))

