# “Machine Utilization Example” - Machine Connectivity

This this file, Modbus TCP based machine connectivity has been handled. Additionally, data has been pre-processed using Connectware Rules.

Download:

{% file src="<https://3008235441-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FshLLdPEhD7SHVDYSquU1%2Fuploads%2Fgit-blob-566a1684e7019066efd0a1d382993e19fd3f5b2d%2Futilization-connectivity.yml?alt=media>" %}

{% hint style="info" %}
The example below uses an additional Docker image provided by Cybus that requires a suitable license. You can check the current capabilities and permissions of your Connectware license in the Cybus Portal ([https://portal.cybus.io](https://portal.cybus.io/)). If your license is not eligible to use the example Docker image, please contact Cybus Sales ([sales@cybus.io](mailto:sales%40cybus.io)).
{% endhint %}

{% code lineNumbers="true" %}

```yaml
description: >
    Machine connectivity and data mapping

metadata:
    name: ModBus Machine Connectivity
    icon: https://www.cybus.io/wp-content/uploads/2019/03/Cybus-logo-Claim-lang.svg
    provider: cybus
    homepage: https://www.cybus.io
    version: 0.0.1

parameters:
    modbus_connection_ip:
        type: string
        default: 172.17.0.1

    modbus_connection_port:
        type: integer
        default: 10502

resources:
    # Modbus simulator service for this example
    machineSimulator:
        type: Cybus::Container
        properties:
            image: registry.cybus.io/cybus-testing/modbus-machine-simulator:0.0.1
            ports:
                - !sub '${modbus_connection_port}:10502/tcp'

    modbusConnection:
        type: Cybus::Connection
        properties:
            protocol: Modbus
            targetState: connected
            connection:
                host: !ref modbus_connection_ip
                port: !ref modbus_connection_port

    powerLevel:
        type: Cybus::Endpoint
        properties:
            protocol: Modbus
            connection: !ref modbusConnection
            subscribe:
                fc: 3
                length: 2
                interval: 1000
                address: 0
                dataType: floatBE

    mapping:
        type: Cybus::Mapping
        properties:
            mappings:
                - subscribe:
                      endpoint: !ref powerLevel
                  publish:
                      topic: !sub '${Cybus::MqttRoot}/power-level'
                - subscribe:
                      endpoint: !ref powerLevel
                  rules:
                      - transform:
                            expression: |
                                (
                                    $stateMap := [
                                        {
                                            "lower": 0,
                                            "upper": 0,
                                            "state": "OFF"
                                        },
                                        {
                                            "lower": 1,
                                            "upper": 10,
                                            "state": "IDLE"
                                        },
                                        {
                                            "lower": 11,
                                            "upper": 40,
                                            "state": "JOG"
                                        },
                                        {
                                            "lower": 41,
                                            "upper": 100,
                                            "state": "RUNNING"
                                        }
                                    ];
                                    $findState := function($stateRaw) {
                                        (
                                        $result := $filter($stateMap, function($v) {
                                            ($stateRaw >= $v.lower and $stateRaw <= $v.upper)
                                        });
                                        $result ? $result[0].state : "ERROR"
                                        )
                                    };
                                    {
                                        "powerlevel": value,
                                        "state": $findState(value),
                                        "timestamp": timestamp
                                    }
                                )
                  publish:
                      topic: !sub '${Cybus::MqttRoot}/machine-state'
```

{% endcode %}
