Skip to content

Commit 2a6f5a7

Browse files
Adjust to new version. Introduce mqtt-helper
1 parent 669f576 commit 2a6f5a7

File tree

5 files changed

+51
-196
lines changed

5 files changed

+51
-196
lines changed

.vscode/launch.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Python: Current File",
9+
"type": "python",
10+
"request": "launch",
11+
"program": "${file}",
12+
"console": "integratedTerminal"
13+
}
14+
]
15+
}

remote-access/README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# thin-edge.io remote access
22

3-
To access a device remotely that runs thin edge, `remote_connect.py` can be used. After startup, it will connect to the Cumulocity cloud/edge instance. From that point on you can use the remote access tab in device management to access the device. In addition your can use a local proxy or connect any local port on the device.
3+
To access a device remotely that runs thin edge, `remote_connect.py` can be used. After startup, it will connect to the Cumulocity cloud/edge instance. From that point on you can use the remote access tab in device management to access the device. In addition you can use a local proxy or connect any local port on the device. This plugin demos how operations can be carried out using MQTT only connection (omitting the build in operation handling of thinEdge.io)
44

55
## Requirements
66

77
- Working thin edge installation
88

9-
- Python3 and pip3 installation (will not work on python2)
9+
- >=Python3.7 and pip3 installation (will not work on python2)
1010
1111
- The Cloud Remote Access Feature is assigned to your Tenant. If not ask your Administrator to get it assigned to your Tenant. Please note that the Version must be at least 1007.2.0+
1212

@@ -23,10 +23,13 @@ To access a device remotely that runs thin edge, `remote_connect.py` can be used
2323

2424
## Usage
2525

26-
Make sure thin edge is connected to Cumulocity.
27-
2826
run
27+
```bash
28+
sudo touch /etc/tedge/operations/c8y/c8y_RemoteAccessConnect
29+
```
30+
Make sure thin edge is connected to Cumulocity.
2931

32+
run
3033
```python
3134
python3 remote_connect.py
3235
```

remote-access/c8yMQTT.py

Lines changed: 0 additions & 180 deletions
This file was deleted.

remote-access/remote_connect.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import logging
2+
import sys
3+
from time import time
24
from c8ydp.device_proxy import DeviceProxy, WebSocketFailureException
3-
from threading import Thread
4-
import threading
5-
from c8yMQTT import C8yMQTT
5+
from paho_mqtt_helper import mqtt_helper
66
import concurrent.futures
77
import os
8+
import time
89

910

10-
logging.basicConfig(level=logging.DEBUG,format='%(asctime)s %(name)s %(message)s')
11+
logging.basicConfig(filename='remote-access.log',filemode='a',level=logging.DEBUG,format='%(asctime)s %(name)s %(message)s')
1112
logger = logging.getLogger(__name__)
13+
logger.addHandler(logging.StreamHandler(sys.stdout))
1214

1315
def setCommandExecuting(command):
1416
logger.info('Setting command: '+ command + ' to executing')
@@ -34,31 +36,35 @@ def on_message(client, obj, msg):
3436
tcp_host = fields[2]
3537
tcp_port = int(fields[3])
3638
connection_key = fields[4]
37-
c8y.logger.info('Received Remote Connect.')
39+
logger.info('Received Remote Connect.')
3840
setCommandExecuting('c8y_RemoteAccessConnect')
3941
with concurrent.futures.ThreadPoolExecutor() as executor:
4042
future = executor.submit(remoteConnect,tcp_host,tcp_port,connection_key,'https://'+url )
4143
return_value = future.result()
42-
c8y.logger.info('Remote Connect Result:' + return_value)
44+
logger.info('Remote Connect Result:' + return_value)
4345
if return_value.startswith('success'):
4446
setCommandSuccessfull('c8y_RemoteAccessConnect')
4547
else:
4648
setCommandFailed('c8y_RemoteAccessConnect',return_value)
4749

4850

51+
def on_close_handler(close_status, close_reason):
52+
logger.info('Remote connection closed Reason: ' + str(close_reason) + ' Status:' + str(close_status))
53+
4954

5055

5156
def remoteConnect( tcp_host,tcp_port,connection_key,base_url):
5257
try:
53-
c8y.logger.info('Starting Remote to: ' + str(tcp_host) + ':' + str(tcp_port) + ' Key: ' + str(connection_key) + ' url: ' + str(base_url))
58+
logger.info('Starting Remote to: ' + str(tcp_host) + ':' + str(tcp_port) + ' Key: ' + str(connection_key) + ' url: ' + str(base_url))
5459
devProx = DeviceProxy( tcp_host,
5560
tcp_port,
5661
65536,
5762
connection_key,
5863
base_url,
5964
None,
6065
None,
61-
c8y.token
66+
c8y.token,
67+
on_close_handler
6268
)
6369
devProx.connect()
6470
logger.info('Remote Connection successfull finished')
@@ -67,17 +73,27 @@ def remoteConnect( tcp_host,tcp_port,connection_key,base_url):
6773
logger.error('Remote Connection error:' + str(e))
6874
return str(e)
6975

76+
def refresh_token():
77+
logger.info("Starting refresh token thread ")
78+
while c8y.connected == 0:
79+
logger.info("Refreshing Token")
80+
c8y.client.publish("c8y/s/uat", "",0)
81+
time.sleep(60.0)
82+
#
83+
# Get connection from tedge config
7084
stream = os.popen('sudo tedge config get c8y.url')
7185
url=stream.read().strip()
7286
logger.info('Got tenant URL: '+ url)
73-
c8y = C8yMQTT('remote_connect','localhost',1883,'c8y/s/ds,c8y/s/e,c8y/s/dt,c8y/s/dat')
87+
c8y = mqtt_helper.MQTTHelper('remote_connect','localhost',1883,'c8y/s/ds,c8y/s/e,c8y/s/dt,c8y/s/dat')
7488
connected = c8y.connect(on_message)
7589
logger.info('Connection Result:' + str(connected))
7690
if connected != 0:
7791
logger.error('Connection not possible: ' + str(connected))
7892
exit()
79-
logger.info('Requesting Token' )
80-
c8y.client.publish("c8y/s/uat", "",0)
93+
94+
refresh_token()
95+
96+
8197

8298

8399

remote-access/requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
paho-mqtt
2+
paho-mqtt-helper
23
regex
3-
c8y-device-proxy
4+
c8y-device-proxy

0 commit comments

Comments
 (0)