Compare commits
9 Commits
c7b9dcadad
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 6ab27edbe8 | |||
| f9696979cf | |||
| 476458303b | |||
| 88f9f183bd | |||
| 649a31c467 | |||
| 7110f8fa59 | |||
| d11f3ec243 | |||
| 0d4bcb7413 | |||
| 6942686476 |
63
config-grande.json
Normal file
63
config-grande.json
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
{
|
||||||
|
"version": "0.3",
|
||||||
|
"logFile": "/home/briq/logs/nasoscopio-grande.log",
|
||||||
|
"logFormat": "%(asctime)s|%(levelname)-7s|%(funcName)-10s|%(lineno)-3d: %(message)-50s",
|
||||||
|
"logTimeFormat": "%m-%d %H:%M:%S",
|
||||||
|
"URL": "http://localhost:8080",
|
||||||
|
"samplePeriod": 10,
|
||||||
|
"loopPeriod": 0.5,
|
||||||
|
"recordTime": 3600,
|
||||||
|
"fileSaveTime": 360,
|
||||||
|
"fileSaveDir": "/tmp/GRANDE_{}_CL3015HSD.{fType}",
|
||||||
|
"fileType": "csv",
|
||||||
|
"variables": [ {"deviceID":1,
|
||||||
|
"name": "AsseX",
|
||||||
|
"codes": [1000,1002,1017,1021,1022,1038],
|
||||||
|
"descr": ["ActualFeed","FollowingError","MeasuredPosition","CalculatedAcceleration","Jerk","ServoPoint"]
|
||||||
|
},
|
||||||
|
{"deviceID":2,
|
||||||
|
"name": "AsseY",
|
||||||
|
"codes": [1000,1002,1017,1021,1022,1038],
|
||||||
|
"descr": ["ActualFeed","FollowingError","MeasuredPosition","CalculatedAcceleration","Jerk","ServoPoint"]
|
||||||
|
},
|
||||||
|
{"deviceID":3,
|
||||||
|
"name": "AsseZ",
|
||||||
|
"codes": [1000,1002,1017,1021,1022,1038],
|
||||||
|
"descr": ["ActualFeed","FollowingError","MeasuredPosition","CalculatedAcceleration","Jerk","ServoPoint"]
|
||||||
|
},
|
||||||
|
{"deviceID":4,
|
||||||
|
"name": "AsseC",
|
||||||
|
"codes": [1000,1002,1017,1021,1022,1038],
|
||||||
|
"descr": ["ActualFeed","FollowingError","MeasuredPosition","CalculatedAcceleration","Jerk","ServoPoint"]
|
||||||
|
},
|
||||||
|
{"deviceID":5,
|
||||||
|
"name": "AsseB",
|
||||||
|
"codes": [1000,1002,1017,1021,1022,1038],
|
||||||
|
"descr": ["ActualFeed","FollowingError","MeasuredPosition","CalculatedAcceleration","Jerk","ServoPoint"]
|
||||||
|
},
|
||||||
|
{"deviceID":6,
|
||||||
|
"name": "AsseXs",
|
||||||
|
"codes": [1000,1002,1017,1021,1022,1038],
|
||||||
|
"descr": ["ActualFeed","FollowingError","MeasuredPosition","CalculatedAcceleration","Jerk","ServoPoint"]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"variableNames": {
|
||||||
|
"1000": "ActualFeed",
|
||||||
|
"1002": "FollowingError",
|
||||||
|
"1017": "MeasuredPosition",
|
||||||
|
"1021": "CalculatedAcceleration",
|
||||||
|
"1022": "Jerk",
|
||||||
|
"1038": "ServoPoint"
|
||||||
|
},
|
||||||
|
"mqttHost": "localhost",
|
||||||
|
"mqttPort": 1883,
|
||||||
|
"mqttSend": "nasoscope/grande/cnc2recorder",
|
||||||
|
"mqttReceive": "nasoscope/grande/recorder2cnc",
|
||||||
|
"sendMqtt": false,
|
||||||
|
"mqttSendSamples": "nasoscope/grande/samples",
|
||||||
|
"sendInflux": true,
|
||||||
|
"influxHost": "localhost",
|
||||||
|
"influxPort": 8086,
|
||||||
|
"influxDB": "axes_grande",
|
||||||
|
"influxRetention": "one_day"
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"version": "0.2",
|
"version": "0.3",
|
||||||
"logFile": "./Nasoscope.log",
|
"logFile": "/home/briq/logs/nasoscopio-piccola.log",
|
||||||
"logFormat": "%(asctime)s|%(levelname)-7s|%(funcName)-10s|%(lineno)-3d: %(message)-50s",
|
"logFormat": "%(asctime)s|%(levelname)-7s|%(funcName)-10s|%(lineno)-3d: %(message)-50s",
|
||||||
"logTimeFormat": "%m-%d %H:%M:%S",
|
"logTimeFormat": "%m-%d %H:%M:%S",
|
||||||
"URL": "http://localhost:8081",
|
"URL": "http://localhost:8081",
|
||||||
@@ -8,7 +8,7 @@
|
|||||||
"loopPeriod": 0.5,
|
"loopPeriod": 0.5,
|
||||||
"recordTime": 3600,
|
"recordTime": 3600,
|
||||||
"fileSaveTime": 360,
|
"fileSaveTime": 360,
|
||||||
"fileSaveDir": "./{}_CL3015HSD.{fType}",
|
"fileSaveDir": "/tmp/PICCOLA_{}_CL3015HSD.{fType}",
|
||||||
"fileType": "csv",
|
"fileType": "csv",
|
||||||
"variables": [ {"deviceID":1,
|
"variables": [ {"deviceID":1,
|
||||||
"name": "AsseX",
|
"name": "AsseX",
|
||||||
@@ -41,10 +41,23 @@
|
|||||||
"descr": ["ActualFeed","FollowingError","MeasuredPosition","CalculatedAcceleration","Jerk","ServoPoint"]
|
"descr": ["ActualFeed","FollowingError","MeasuredPosition","CalculatedAcceleration","Jerk","ServoPoint"]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
"variableNames": {
|
||||||
|
"1000": "ActualFeed",
|
||||||
|
"1002": "FollowingError",
|
||||||
|
"1017": "MeasuredPosition",
|
||||||
|
"1021": "CalculatedAcceleration",
|
||||||
|
"1022": "Jerk",
|
||||||
|
"1038": "ServoPoint"
|
||||||
|
},
|
||||||
"mqttHost": "localhost",
|
"mqttHost": "localhost",
|
||||||
"mqttPort": 1883,
|
"mqttPort": 1883,
|
||||||
"mqttSend": "nasoscope/cnc2recorder",
|
"mqttSend": "nasoscope/piccola/cnc2recorder",
|
||||||
"mqttReceive": "nasoscope/recorder2cnc",
|
"mqttReceive": "nasoscope/piccola/recorder2cnc",
|
||||||
"sendMqtt": true,
|
"sendMqtt": false,
|
||||||
"mqttSendSamples": "nasoscope/samples"
|
"mqttSendSamples": "nasoscope/piccola/samples",
|
||||||
|
"sendInflux": true,
|
||||||
|
"influxHost": "localhost",
|
||||||
|
"influxPort": 8086,
|
||||||
|
"influxDB": "axes_piccola",
|
||||||
|
"influxRetention": "one_day"
|
||||||
}
|
}
|
||||||
@@ -17,8 +17,10 @@ import struct
|
|||||||
import csv
|
import csv
|
||||||
|
|
||||||
from paho.mqtt import client
|
from paho.mqtt import client
|
||||||
from NasoScope import templates
|
from datetime import datetime
|
||||||
|
import templates
|
||||||
from paho.mqtt.client import connack_string
|
from paho.mqtt.client import connack_string
|
||||||
|
from influxdb import InfluxDBClient
|
||||||
|
|
||||||
def buildBody():
|
def buildBody():
|
||||||
root = xml.Element("soap:Envelope", templates.XMLNS_ENV)
|
root = xml.Element("soap:Envelope", templates.XMLNS_ENV)
|
||||||
@@ -112,7 +114,8 @@ def stopSampling(channelID='0'):
|
|||||||
|
|
||||||
def collectSamples(varMap, channelID='0'):
|
def collectSamples(varMap, channelID='0'):
|
||||||
dataDict = {}
|
dataDict = {}
|
||||||
for var in varMap: dataDict[var[templates.VAR_NAME]] = dict()
|
for var in varMap:
|
||||||
|
dataDict[var[templates.VAR_NAME]] = dict()
|
||||||
for var in varMap:
|
for var in varMap:
|
||||||
try:
|
try:
|
||||||
root,body = buildBody()
|
root,body = buildBody()
|
||||||
@@ -198,7 +201,7 @@ def saveSamples(s, fileName = None):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
def sendSamples(s):
|
def sendSamples(s):
|
||||||
client.publish(settings['mqttSendSamples']json.dumps(s))
|
mqtt.publish(settings['mqttSendSamples'],json.dumps(s))
|
||||||
|
|
||||||
|
|
||||||
def onMessage(cli, userdata, msg):
|
def onMessage(cli, userdata, msg):
|
||||||
@@ -218,7 +221,7 @@ def onMessage(cli, userdata, msg):
|
|||||||
elif "EXIT" in msg:
|
elif "EXIT" in msg:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
LOGGER.error("Messaggio MQTT sconosciuto")
|
LOGGER.error(f"Messaggio MQTT sconosciuto: {msg}")
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def onConnect(cli, userdata, flags, rc):
|
def onConnect(cli, userdata, flags, rc):
|
||||||
@@ -240,11 +243,13 @@ if __name__ == '__main__':
|
|||||||
firstLoop = True
|
firstLoop = True
|
||||||
stat = "IDLE"
|
stat = "IDLE"
|
||||||
bufferFull = 0
|
bufferFull = 0
|
||||||
|
loopcount = 0
|
||||||
variableMap = list()
|
variableMap = list()
|
||||||
samples = dict()
|
samples = dict()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
fp = open("config.json")
|
print(f'Config: {sys.argv[1]}')
|
||||||
|
fp = open(str(sys.argv[1]))
|
||||||
settings = json.load(fp)
|
settings = json.load(fp)
|
||||||
fp.close()
|
fp.close()
|
||||||
except:
|
except:
|
||||||
@@ -268,7 +273,8 @@ if __name__ == '__main__':
|
|||||||
|
|
||||||
LOGGER.warning("NasoScope {} Started!".format(settings["version"]))
|
LOGGER.warning("NasoScope {} Started!".format(settings["version"]))
|
||||||
|
|
||||||
mqtt = client.Client(protocol=client.MQTTv31)
|
|
||||||
|
mqtt = client.Client()
|
||||||
mqtt.on_message = onMessage
|
mqtt.on_message = onMessage
|
||||||
mqtt.on_connect = onConnect
|
mqtt.on_connect = onConnect
|
||||||
mqtt.on_disconnect = onDisconnect
|
mqtt.on_disconnect = onDisconnect
|
||||||
@@ -276,6 +282,8 @@ if __name__ == '__main__':
|
|||||||
mqtt.subscribe(settings['mqttReceive'])
|
mqtt.subscribe(settings['mqttReceive'])
|
||||||
mqtt.user_data_set(stat)
|
mqtt.user_data_set(stat)
|
||||||
mqtt.loop_start()
|
mqtt.loop_start()
|
||||||
|
|
||||||
|
influx = InfluxDBClient(host= settings['influxHost'], port=settings['influxPort'], database = settings['influxDB'])
|
||||||
|
|
||||||
xmlReqTempl = xml.fromstring(templates.REQ_TEMPLATE)
|
xmlReqTempl = xml.fromstring(templates.REQ_TEMPLATE)
|
||||||
xmlRespTempl = xml.fromstring(templates.RESP_TEMPLATE)
|
xmlRespTempl = xml.fromstring(templates.RESP_TEMPLATE)
|
||||||
@@ -320,16 +328,19 @@ if __name__ == '__main__':
|
|||||||
tempSamples = collectSamples(variableMap,channelID)
|
tempSamples = collectSamples(variableMap,channelID)
|
||||||
if tempSamples is not False:
|
if tempSamples is not False:
|
||||||
if firstCap == True:
|
if firstCap == True:
|
||||||
firstTS = min([z[templates.SAM_TS] for x in tempSamples \
|
# firstTS = min([z[templates.SAM_TS] for x in tempSamples \
|
||||||
for y in tempSamples[x] for z in tempSamples[x][y]])
|
# for y in tempSamples[x] for z in tempSamples[x][y]])
|
||||||
firstCap = False
|
firstCap = False
|
||||||
|
firstTS = time.time()
|
||||||
|
LOGGER.warning(f'Inizio cattura: {datetime.fromtimestamp(firstTS).strftime("%c")}')
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
for ax in tempSamples:
|
for ax in tempSamples:
|
||||||
for reg in tempSamples[ax]:
|
for reg in tempSamples[ax]:
|
||||||
for idx,rec in enumerate(tempSamples[ax][reg]):
|
if not settings['sendInflux']:
|
||||||
tempSamples[ax][reg][idx]=((tempSamples[ax][reg][idx][templates.SAM_TS]-firstTS)/10**6,
|
for idx,rec in enumerate(tempSamples[ax][reg]):
|
||||||
tempSamples[ax][reg][idx][templates.SAM_VAL])
|
tempSamples[ax][reg][idx]=(tempSamples[ax][reg][idx][templates.SAM_TS],
|
||||||
|
tempSamples[ax][reg][idx][templates.SAM_VAL])
|
||||||
if not settings["sendMqtt"]:
|
if not settings["sendMqtt"]:
|
||||||
if tempSamples[ax][reg][idx][templates.SAM_TS] > settings["recordTime"]:
|
if tempSamples[ax][reg][idx][templates.SAM_TS] > settings["recordTime"]:
|
||||||
samples[ax][reg] = samples[ax][reg][-(len(samples)-len(tempSamples)):]
|
samples[ax][reg] = samples[ax][reg][-(len(samples)-len(tempSamples)):]
|
||||||
@@ -345,13 +356,38 @@ if __name__ == '__main__':
|
|||||||
samples[ax][reg]+=tempSamples[ax][reg]
|
samples[ax][reg]+=tempSamples[ax][reg]
|
||||||
else:
|
else:
|
||||||
sendSamples(tempSamples)
|
sendSamples(tempSamples)
|
||||||
|
else:
|
||||||
|
influxMeas = list()
|
||||||
|
LOGGER.debug("Invio Influx")
|
||||||
|
for idx,record in enumerate(tempSamples[ax][reg]):
|
||||||
|
ts = tempSamples[ax][reg][idx][templates.SAM_TS]+firstTS*1000000
|
||||||
|
influxMeas.append({
|
||||||
|
"measurement" : ax,
|
||||||
|
"tags": {
|
||||||
|
"parameter": str(settings['variableNames'][str(reg)])
|
||||||
|
},
|
||||||
|
"time": int(ts),
|
||||||
|
"fields":{
|
||||||
|
"value": tempSamples[ax][reg][idx][templates.SAM_VAL]
|
||||||
|
}
|
||||||
|
})
|
||||||
|
try:
|
||||||
|
influx.write_points(points=influxMeas, time_precision='u', retention_policy=settings['influxRetention'])
|
||||||
|
except Exception as e:
|
||||||
|
LOGGER.error(f'Impossibile scrivere nel database: {e}')
|
||||||
|
LOGGER.error(f'TS da controllo: {datetime.fromtimestamp(ts).strftime("%d/%m/%y_%H:%M:%S,%f")}')
|
||||||
|
stat='STOP'
|
||||||
pass
|
pass
|
||||||
pass
|
pass
|
||||||
pass
|
pass
|
||||||
if time.time() - startTime > settings["fileSaveTime"]:
|
if not settings['sendMqtt'] and not settings['sendInflux']:
|
||||||
saveSamples(samples)
|
if time.time() - startTime > settings["fileSaveTime"]:
|
||||||
startTime = time.time()
|
saveSamples(samples)
|
||||||
LOGGER.info("{}".format(tempSamples))
|
startTime = time.time()
|
||||||
|
LOGGER.info("{}".format(tempSamples))
|
||||||
|
loopcount+=1
|
||||||
|
if loopcount % 100 == 0:
|
||||||
|
LOGGER.info(f"Loop Numero, sono vivo: {loopcount}")
|
||||||
else:
|
else:
|
||||||
LOGGER.error("Disconnesso dal CN")
|
LOGGER.error("Disconnesso dal CN")
|
||||||
stat = "STOP"
|
stat = "STOP"
|
||||||
@@ -359,15 +395,16 @@ if __name__ == '__main__':
|
|||||||
elif stat == "STOP":
|
elif stat == "STOP":
|
||||||
stopSampling()
|
stopSampling()
|
||||||
closeChannel()
|
closeChannel()
|
||||||
if not settings['sendMqtt']:
|
if not settings['sendMqtt'] and not settings['sendInflux']:
|
||||||
saveSamples(samples)
|
saveSamples(samples)
|
||||||
LOGGER.warning("Chiudo il Canale")
|
LOGGER.warning("Chiudo il Canale")
|
||||||
stat = "IDLE"
|
|
||||||
firstLoop = True
|
firstLoop = True
|
||||||
|
stat = "IDLE"
|
||||||
pass
|
pass
|
||||||
elif stat == "IDLE" and firstLoop == True:
|
elif stat == "IDLE":
|
||||||
LOGGER.info("Pronto a Iniziare Cattura")
|
if firstLoop:
|
||||||
firstLoop = False
|
LOGGER.info("Pronto a Iniziare Cattura")
|
||||||
|
firstLoop = False
|
||||||
else:
|
else:
|
||||||
pass
|
pass
|
||||||
time.sleep(settings["loopPeriod"])
|
time.sleep(settings["loopPeriod"])
|
||||||
16
nasoscopio-grande.service
Normal file
16
nasoscopio-grande.service
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=Nasoscopio - Letture Movimento Assi Materia-CL4020
|
||||||
|
Requires=network.target mosquitto.service docker.service sshtunnel-grande.service
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
RestartSec=15s
|
||||||
|
Type=simple
|
||||||
|
User=briq
|
||||||
|
Group=briq
|
||||||
|
WorkingDirectory=/home/briq/NasoScopio/
|
||||||
|
ExecStart=/usr/bin/python3 nasomain.py config-grande.json
|
||||||
|
PIDFile=/run/nasoscopio-grande.pid
|
||||||
|
Restart=always
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
16
nasoscopio-piccola.service
Normal file
16
nasoscopio-piccola.service
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=Nasoscopio - Letture Movimento Assi Materia-CL3015
|
||||||
|
Requires=network.target mosquitto.service docker.service sshtunnel-piccola.service
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
RestartSec=15s
|
||||||
|
Type=simple
|
||||||
|
User=briq
|
||||||
|
Group=briq
|
||||||
|
WorkingDirectory=/home/briq/NasoScopio/
|
||||||
|
ExecStart=/usr/bin/python3 nasomain.py config-piccola.json
|
||||||
|
PIDFile=/run/nasoscopio-piccola.pid
|
||||||
|
Restart=always
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
16
sshtunnel-grande.service
Normal file
16
sshtunnel-grande.service
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=SSH Tunnel - Letture Movimento Assi Materia-CL4020
|
||||||
|
Requires=network.target mosquitto.service docker.service
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
RestartSec=15s
|
||||||
|
Type=simple
|
||||||
|
User=briq
|
||||||
|
Group=briq
|
||||||
|
WorkingDirectory=/home/briq/NasoScopio/
|
||||||
|
ExecStart=/usr/bin/autossh -Nf -M 0 -L 8880:192.168.0.1:8080 cnc@192.168.30.50
|
||||||
|
PIDFile=/run/sshtunnel-grande.pid
|
||||||
|
Restart=always
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
16
sshtunnel-piccola.service
Normal file
16
sshtunnel-piccola.service
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=SSH Tunnel - Letture Movimento Assi Materia-CL3015
|
||||||
|
Requires=network.target mosquitto.service docker.service
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
RestartSec=15s
|
||||||
|
Type=simple
|
||||||
|
User=briq
|
||||||
|
Group=briq
|
||||||
|
WorkingDirectory=/home/briq/NasoScopio/
|
||||||
|
ExecStart=/usr/bin/autossh -Nf -M 0 -L 8881:192.168.0.1:8080 cnc@192.168.30.51
|
||||||
|
PIDFile=/run/sshtunnel-piccola.pid
|
||||||
|
Restart=always
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
Reference in New Issue
Block a user