Initial commit

This commit is contained in:
Quentin WEPHRE
2024-06-18 14:59:46 +02:00
parent 13549fe46e
commit fe82b3962f
23 changed files with 1832 additions and 1 deletions

37
Python/azure_iot_hub_1.py Normal file
View File

@@ -0,0 +1,37 @@
from azure.iot.hub import IoTHubRegistryManager
from azure.iot.hub.models import QuerySpecification
from azure.iot.hub.protocol.models.device_py3 import Device
import json
# Install the Azure IoT Hub SDK:
# pip install azure-iot-hub
# Authenticate to your Azure account
CONNECTION_STRING = ""
if CONNECTION_STRING == "":
print("Provide a connection string for the Iot Hub before running the script!")
exit(13)
registry_manager = IoTHubRegistryManager(CONNECTION_STRING)
sorted_list = []
def custom_sort(item):
return (item[1], int(item[2]))
for i in registry_manager.get_devices():
if "DIGIT" in i.device_id:
twin = registry_manager.get_twin(i.device_id)
site = twin.tags['site']
number = twin.tags['number']
status = i.connection_state
time = twin.last_activity_time
if "RUAKAKA" in site:
sorted_list.append((i.device_id, site, number, status, time))
sorted_list = sorted(sorted_list, key=custom_sort)
column_sizes = (15, 5, 25, 15, 25)
# Print the sorted list in a tabular format
for item in sorted_list:
print("{:<{}} #{:<{}} {:<{}} {:<{}} {:<{}}".format(item[1], column_sizes[0], item[2], column_sizes[1], item[0], column_sizes[2], item[3], column_sizes[3], item[4], column_sizes[4]))

144
Python/danish_D1_api.py Normal file
View File

@@ -0,0 +1,144 @@
import datetime
import time
import pandas as pd
import requests
from urllib3.exceptions import InsecureRequestWarning
import jq
import json
# Function to authenticate and get token
def authenticate(device_ip, payload):
auth_url = f"https://{device_ip}:8443/api/v1/auth"
response = requests.post(auth_url, json=payload, verify=False)
if response.status_code == 200:
token = response.json()["data"]["token"]
#print(f"Authentication successful. Token received: {token}")
print(" authenticated!")
return token
else:
print(f"Authentication failed. Status code: {response.status_code}")
return None
# Function to send PATCH request
def send_patch_request(device_ip, token, connection_string):
headers = {
"mx-api-token": token
}
payload = {
"provisioning": {
"source": "manual",
"connectionString": connection_string,
"enable": True
}
}
patch_url = f"https://{device_ip}:8443/api/v1/azure-iotedge"
response = requests.patch(patch_url, json=payload, headers=headers, verify=False)
if response.status_code == 200:
print(f"PATCH request successful for device {device_ip}")
else:
print(f"Failed to send PATCH request to device {device_ip}. Status code: {response.status_code}")
# Function to send UPGRADE request
def send_upgrade_request(device_ip, token, upgrade_url):
headers = {
"mx-api-token": token
}
payload = {
"deleteFileAfterInstallComplete": True,
"install": True,
"url": upgrade_url
}
patch_url = f"https://{device_ip}:8443/api/v1/upgrades"
response = requests.post(patch_url, json=payload, headers=headers, verify=False)
if response.status_code == 200:
print(f"POST request successful for device {device_ip}")
else:
print(f"Failed to send POST request to device {device_ip}. Status code: {response.status_code}")
print(response.content)
# Function to send UPGRADE request
def get_upgrade_jobs(device_ip, token):
headers = {
"mx-api-token": token
}
payload = {
}
patch_url = f"https://{device_ip}:8443/api/v1/upgrades/7"
response = requests.get(patch_url, json=payload, headers=headers, verify=False)
if response.status_code == 200:
#print(f"GET request successful for device {device_ip}")
json_data = json.loads(response.content.decode())
json_str = json.dumps(json_data, indent=4)
#print(json_str)
print(jq.compile('.data.tasks[0].progress').input(json.loads(json_str)).first(), end="% speed: ")
print(jq.compile('.data.tasks[0].speed').input(json.loads(json_str)).first())
else:
print(f"Failed to send GET request to device {device_ip}. Status code: {response.status_code}")
print(response.content)
# Function to send UPGRADE request
def get_API_1(device_ip, token):
headers = {
"mx-api-token": token
}
payload = {
}
patch_url = f"https://{device_ip}:8443/api/v1/azure-iotedge/messages"
response = requests.get(patch_url, json=payload, headers=headers, verify=False)
if response.status_code == 200:
#print(f"GET request successful for device {device_ip}")
#json_data = json.loads(response.content.decode())
#json_str = json.dumps(json_data)
#print(jq.compile('.data.completedAt').input(json.loads(json_str)).first())
parsed = json.loads(response.content.decode())
print(json.dumps(parsed, indent=4))
else:
print(f"Failed to send GET request to device {device_ip}. Status code: {response.status_code}")
print(response.content.decode())
# Read the Excel file
# df = pd.read_excel("")
# df = df[df["device_name"].notnull()]
# Iterate over each row in the DataFrame
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
payload_auth = {
"acceptEULA": True,
"name": "",
"password": ""
}
if payload_auth["name"] == "" or payload_auth["password"] == "":
print("Provide the credentials before running the script!")
exit(10)
ip_address = "10.197.35.116"
token = authenticate(ip_address, payload_auth)
upgrade_url = ""#"https://files.thingsprocloud.com/package/moxa-aig-301-series-includes-security-patch-firmware-v1.0.deb.yaml"
if upgrade_url == "":
print("Provide upgrade URL before running the script!")
exit(12)
if token:
while True:
print(datetime.datetime.now().strftime("%H:%M:%S"), end=" ")
get_upgrade_jobs(ip_address, token)
time.sleep(60)
# for index, row in df.iterrows():
# device_name = row['device_name']
# device_ip_address_https = row['device_ip_address_http']
# connection_string = row['connection_string']
# upgrade_url = "https://files.thingsprocloud.com/package/moxa-aig-301-series-includes-security-patch-firmware-v1.0.deb.yaml"
# # Authenticate and get token
# payload_auth = {
# "acceptEULA": True,
# "name": "",
# "password": ""
# }
# print(device_name, end="")
# token = authenticate(device_ip_address_https, payload_auth)
# if token:
# get_API(device_ip_address_https, token)
# print("\n")

147
Python/danish_batch_api.py Normal file
View File

@@ -0,0 +1,147 @@
import pandas as pd
import requests
from urllib3.exceptions import InsecureRequestWarning
import jq
import json
# Function to authenticate and get token
def authenticate(device_ip, payload):
auth_url = f"https://{device_ip}:8443/api/v1/auth"
response = requests.post(auth_url, json=payload, verify=False)
if response.status_code == 200:
token = response.json()["data"]["token"]
#print(f"Authentication successful. Token received: {token}")
print(" authenticated!")
return token
else:
print(f"Authentication failed. Status code: {response.status_code}")
return None
# Function to send PATCH request
def send_patch_request(device_ip, token, connection_string):
headers = {
"mx-api-token": token
}
payload = {
"provisioning": {
"source": "manual",
"connectionString": connection_string,
"enable": True
}
}
patch_url = f"https://{device_ip}:8443/api/v1/azure-iotedge"
response = requests.patch(patch_url, json=payload, headers=headers, verify=False)
if response.status_code == 200:
print(f"PATCH request successful for device {device_ip}")
else:
print(f"Failed to send PATCH request to device {device_ip}. Status code: {response.status_code}")
# Function to send UPGRADE request
def send_upgrade_request(device_ip, token, upgrade_url):
headers = {
"mx-api-token": token
}
payload = {
"deleteFileAfterInstallComplete": True,
"install": True,
"url": upgrade_url
}
patch_url = f"https://{device_ip}:8443/api/v1/upgrades"
response = requests.post(patch_url, json=payload, headers=headers, verify=False)
if response.status_code == 200:
print(f"POST request successful for device {device_ip}")
else:
print(f"Failed to send POST request to device {device_ip}. Status code: {response.status_code}")
print(response.content)
# Function to send UPGRADE request
def get_upgrade_jobs(device_ip, token):
headers = {
"mx-api-token": token
}
payload = {
}
patch_url = f"https://{device_ip}:8443/api/v1/upgrades/2"
response = requests.get(patch_url, json=payload, headers=headers, verify=False)
if response.status_code == 200:
#print(f"GET request successful for device {device_ip}")
json_data = json.loads(response.content.decode())
json_str = json.dumps(json_data)
print(jq.compile('.data.completedAt').input(json.loads(json_str)).first())
else:
print(f"Failed to send GET request to device {device_ip}. Status code: {response.status_code}")
print(response.content)
# Function to send UPGRADE request
def put_API(device_ip, token):
headers = {
"mx-api-token": token
}
payload = {
}
patch_url = f"https://{device_ip}:8443/api/v1/azure-iotedge/reset"
response = requests.put(patch_url, json=payload, headers=headers, verify=False)
if response.status_code == 200:
#print(f"GET request successful for device {device_ip}")
#json_data = json.loads(response.content.decode())
#json_str = json.dumps(json_data)
#print(jq.compile('.data.completedAt').input(json.loads(json_str)).first())
print(response.content.decode())
else:
print(f"Failed to send GET request to device {device_ip}. Status code: {response.status_code}")
print(response.content.decode())
def get_API(device_ip, token):
headers = {
"mx-api-token": token
}
payload = {
}
patch_url = f"https://{device_ip}:8443/api/v1/azure-iotedge"
response = requests.get(patch_url, json=payload, headers=headers, verify=False)
if response.status_code == 200:
#print(f"GET request successful for device {device_ip}")
#json_data = json.loads(response.content.decode())
#json_str = json.dumps(json_data)
#print(jq.compile('.data.completedAt').input(json.loads(json_str)).first())
print(response.content.decode())
else:
print(f"Failed to send GET request to device {device_ip}. Status code: {response.status_code}")
print(response.content.decode())
# Read the Excel file
excel_file_path = ""
if excel_file_path == "":
print("Provide Excel file path before running the script!")
exit(11)
df = pd.read_excel(excel_file_path)
df = df[df["device_name"].notnull()]
# Iterate over each row in the DataFrame
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
for index, row in df.iterrows():
device_name = row['device_name']
device_ip_address_https = row['device_ip_address_http']
connection_string = row['connection_string']
upgrade_url = "" #https://files.thingsprocloud.com/package/moxa-aig-301-series-includes-security-patch-firmware-v1.0.deb.yaml
if upgrade_url == "":
print("Provide upgrade URL before running the script!")
exit(12)
# Authenticate and get token
payload_auth = {
"acceptEULA": True,
"name": "",
"password": ""
}
if payload_auth["name"] == "" or payload_auth["password"] == "":
print("Provide the credentials before running the script!")
exit(10)
print(device_name, end="")
token = authenticate(device_ip_address_https, payload_auth)
if token:
get_API(device_ip_address_https, token)
print("\n")

104
Python/danish_batch_scp.py Normal file
View File

@@ -0,0 +1,104 @@
import pandas as pd
import requests
from urllib3.exceptions import InsecureRequestWarning
import jq
import json
import scp
import paramiko
def scp_file(local_path, remote_path, hostname, username, password):
try:
# Create a new SSH client
ssh_client = paramiko.SSHClient()
# Automatically add the server's host key
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Connect to the server
ssh_client.connect(hostname, username=username, password=password)
# Use SCP to transfer the file
with scp.SCPClient(ssh_client.get_transport()) as scp_client:
scp_client.put(local_path, remote_path)
print("File transferred successfully")
except Exception as e:
print(f"Error: {e}")
finally:
# Close the SSH connection
ssh_client.close()
def ssh_execute_command_with_password(hostname, username, password, command):
try:
# Create a new SSH client
ssh_client = paramiko.SSHClient()
# Automatically add the server's host key
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Connect to the server
ssh_client.connect(hostname, username=username, password=password)
# Execute the command with sudo -S to read password from stdin
stdin, stdout, stderr = ssh_client.exec_command('sudo -k -S ' + command)
stdin.write(password + '\n')
stdin.flush()
# Read the output
output = stdout.read().decode('utf-8')
error = stderr.read().decode('utf-8')
# Print output and errors, if any
if output:
print("Command output:")
print(output)
if error:
print("Command error:")
print(error)
print("Command executed successfully")
except Exception as e:
print(f"Error: {e}")
finally:
# Close the SSH connection
ssh_client.close()
# Read the Excel file
excel_file_path = ""
if excel_file_path == "":
print("Provide Excel file path before running the script!")
exit(11)
df = pd.read_excel(excel_file_path)
df = df[df["device_name"].notnull()]
# Iterate over each row in the DataFrame
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
local_file_path = ""#"./azureiotedge_2.4.0-2697_armhf.mpkg"
if local_file_path == "":
print("Provide upgrade file path before running the script!")
exit(12)
remote_file_path = "./."
username = ""
password = ""
if username == "" or password == "":
print("Provide credentials before running the script!")
exit(10)
command = ""#"appman app install azureiotedge_2.4.0-2697_armhf.mpkg"
if command == "":
print("Provide a command to execute before running the script!")
exit(11)
for index, row in df.iterrows():
device_name = row['device_name']
device_ip_address_https = row['device_ip_address_http']
print(device_name)
ssh_execute_command_with_password(device_ip_address_https, username, password, command)
print("\n")

View File

@@ -0,0 +1,87 @@
import paramiko
import pandas as pd
import getpass
import re
import time
import json
def read_excel(filename, column_name):
df = pd.read_excel(filename)
df = df[df["device_name"].notnull()]
global names
global ips
global connections
names = df["device_name"].tolist()
ips = df["device_ip_address_http"].tolist()
connections = df["connection_string"].tolist()
return ips
def ssh_execute_commands(device_ip, username, password, commands, connection):
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(device_ip, username=username, password=password)
transport = ssh_client.get_transport()
session = transport.open_session()
session.set_combine_stderr(True)
session.get_pty()
session.exec_command("sudo cat /var/thingspro/data/mx-api-token")
stdin = session.makefile('wb', -1)
stdout = session.makefile('rb', -1)
stdin.write(password + '\n')
stdin.flush()
for line in stdout.read().splitlines():
if not re.search('[Pp]assword', line.decode()):
token = line.decode()
# session = transport.open_session()
# session.set_combine_stderr(True)
# session.get_pty()
# session.exec_command('curl -k -X PUT https://127.0.0.1:8443/api/v1/azure-iotedge/reset -H "Content-Type: application/json" -H "mx-api-token: ' + token + '"')
# stdout = session.makefile('rb', -1)
# for line in stdout.read().splitlines():
# print(line.decode())
print("\n" + connection + "\n")
print(token + "\n")
jq_data = {}
jq_data_2 = {}
jq_data_2["source"] = "manual"
jq_data_2["connectionString"] = connection
jq_data_2["enable"] = True
jq_data["provisioning"] = jq_data_2
json_object = json.dumps(jq_data)
print(json_object + "\n")
session = transport.open_session()
session.set_combine_stderr(True)
session.get_pty()
# -H "Content-Type: application/json" -H "mx-api-token:$(token)'
session.exec_command('curl -k -X PATCH https://127.0.0.1:8443/api/v1/azure-iotedge -H "Content-Type: application/json" -H "mx-api-token: ' + token + '" -d "' + str(json_object) + '"')
stdout = session.makefile('rb', -1)
for line in stdout.read().splitlines():
print(line.decode())
def main():
filename = ""
if filename == "":
print("Provide Excel file path before running the script!")
exit(11)
column_name = "device_ip_address_http"
global names
global ips
global connections
devices = read_excel(filename, column_name)
username = input("Enter SSH username: ")
password = input("Enter SSH password: ")
print(names)
commands = ["sudo bash"] # Add your commands here
for i, device in enumerate(names):
print(f"Connecting to gateway #{i} {device} ({ips[i]})...")
ssh_execute_commands(ips[i], username, password, commands, connections[i])
if __name__ == "__main__":
names = []
ips = []
main()

83
Python/server_updating.py Normal file
View File

@@ -0,0 +1,83 @@
#!/usr/bin/env python3
import asyncio
import logging
import server_async
import time
import math
from pymodbus.payload import BinaryPayloadBuilder
from pymodbus.constants import Endian
from pymodbus.datastore import (
ModbusSequentialDataBlock,
ModbusServerContext,
ModbusSlaveContext,
)
_logger = logging.getLogger(__name__)
async def updating_task(context):
# parameters
fc_as_hex = 3
slave_id = 0x00
address = 0x00
count = 100
# initialization: set values to zero
values = context[slave_id].getValues(fc_as_hex, address, count=count)
values = [0 for v in values]
# infinite loop updating every register to its sine value (period 1 hour, amplitude according to register number)
while True:
await asyncio.sleep(1)
current_time = time.time()
sine_value = sin_wave(current_time)
# BinaryPayloadBuilder is used to convert a float to two Modbus registers
builder = BinaryPayloadBuilder(byteorder=Endian.LITTLE)
for i in range(address, count):
builder.add_32bit_float((i+1) * sine_value)
payload = builder.to_registers()
# once the registers of all the sine waves are created, update the values in the simulator
context[slave_id].setValues(fc_as_hex, address, payload)
# sin wave value calculator
period = 3600
def sin_wave(time_elapsed):
return math.sin(2 * math.pi * time_elapsed / period)
# server start setup function
def setup_updating_server(cmdline=None):
# define the registers
num_floats = 100
float_values = [float(i + 0.5) for i in range(num_floats)]
builder = BinaryPayloadBuilder(byteorder=Endian.LITTLE)
for value in float_values:
builder.add_32bit_float(value)
payload = builder.to_registers()
# create a Modbus simulator with the previously generated registers
datablock = ModbusSequentialDataBlock(0x01, payload)
context = ModbusSlaveContext(di=datablock, co=datablock, hr=datablock, ir=datablock)
context = ModbusServerContext(slaves=context, single=True)
return server_async.setup_server(
description="Run asynchronous server.", context=context, cmdline=cmdline
)
# asynchronous function that will call the values updating function with the arguments (modbus registers layout) of the created server
async def run_updating_server(args):
task = asyncio.create_task(updating_task(args.context))
await server_async.run_async_server(args) # start the server
task.cancel()
# main function that will setup the server, start it and run the function what will update the values of the registers
async def main(cmdline=None):
run_args = setup_updating_server(cmdline=cmdline)
await run_updating_server(run_args)
if __name__ == "__main__":
asyncio.run(main(), debug=True)

View File

@@ -0,0 +1,77 @@
import os
import json
import pandas as pd
import pprint
import matplotlib.pyplot as plt
from datetime import datetime
import numpy as np
def process_folder(folder_path, table):
"""
Recursively process each file in the folder and its subfolders.
"""
for root, dirs, files in os.walk(folder_path):
for file in files:
file_path = os.path.join(root, file)
process_file(file_path, table)
def process_file(file_path, table):
"""
Read each line of JSON data from the file and store it in the table.
"""
with open(file_path, 'r') as f:
for line in f:
try:
json_data = json.loads(line)
table.append(json_data)
except json.JSONDecodeError:
print(f"Error decoding JSON in file: {file_path}")
def main(folder_path):
table = []
process_folder(folder_path, table)
# Convert table to pandas DataFrame for easy manipulation
df = pd.DataFrame(table)
index_table = []
enqueued_table = []
emission_table = []
for index, row in df.iterrows():
print(index, end=' ')
index_table.append(index)
print(row['EnqueuedTimeUtc'], end=' ')
enqueued_table.append(row['EnqueuedTimeUtc'])
body_table = []
body_table.append(row['Body'])
body_df = pd.DataFrame(body_table)
for body_index, body_row in body_df.iterrows():
print(body_row['emissionDate'])
emission_table.append(body_row['emissionDate'])
emission_dates = [datetime.strptime(date, '%Y-%m-%dT%H:%M:%SZ') for date in emission_table]
enqueued_dates = [datetime.strptime(date[:19] + date[-1], '%Y-%m-%dT%H:%M:%SZ') for date in enqueued_table]
plt.figure(figsize=(10, 6))
plt.plot(enqueued_dates, index_table, label='Enqueued')
plt.plot(emission_dates, index_table, label='Emission')
plt.xlabel('Time')
plt.ylabel('Index')
plt.title('Index vs Time')
plt.legend()
plt.grid(True)
plt.xticks(rotation=45)
parts = folder_path.split('/')[-4:]
result = '_'.join(parts)
figurename = "index_" + result + ".png"
plt.savefig(figurename, bbox_inches='tight')
if __name__ == "__main__":
folder_path = '/mnt/c/Users/QWPHR/Downloads/JSON_BUFFER_7'
main(folder_path)