Compare commits

..

9 Commits

Author SHA1 Message Date
6ab27edbe8 versione definitiva (per adesso) 2021-02-26 15:20:01 +01:00
f9696979cf cosine 2021-02-26 12:19:01 +01:00
476458303b cose 2021-02-26 12:14:40 +01:00
88f9f183bd posizione log 2021-02-26 10:49:50 +01:00
649a31c467 duplicati servizi, riorganizzati file 2021-02-26 09:53:22 +01:00
7110f8fa59 modifiche servizio e messaggi log 2021-02-26 09:31:53 +01:00
d11f3ec243 corretto servizio e altre cose 2021-02-24 16:40:35 +01:00
0d4bcb7413 piccole correzioni 2021-02-24 15:46:19 +01:00
6942686476 funziona e manda le cose su influx 2021-02-24 15:39:09 +01:00
9 changed files with 205 additions and 28 deletions

View File

63
config-grande.json Normal file
View 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"
}

View File

@@ -1,6 +1,6 @@
{
"version": "0.2",
"logFile": "./Nasoscope.log",
"version": "0.3",
"logFile": "/home/briq/logs/nasoscopio-piccola.log",
"logFormat": "%(asctime)s|%(levelname)-7s|%(funcName)-10s|%(lineno)-3d: %(message)-50s",
"logTimeFormat": "%m-%d %H:%M:%S",
"URL": "http://localhost:8081",
@@ -8,7 +8,7 @@
"loopPeriod": 0.5,
"recordTime": 3600,
"fileSaveTime": 360,
"fileSaveDir": "./{}_CL3015HSD.{fType}",
"fileSaveDir": "/tmp/PICCOLA_{}_CL3015HSD.{fType}",
"fileType": "csv",
"variables": [ {"deviceID":1,
"name": "AsseX",
@@ -41,10 +41,23 @@
"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/cnc2recorder",
"mqttReceive": "nasoscope/recorder2cnc",
"sendMqtt": true,
"mqttSendSamples": "nasoscope/samples"
"mqttSend": "nasoscope/piccola/cnc2recorder",
"mqttReceive": "nasoscope/piccola/recorder2cnc",
"sendMqtt": false,
"mqttSendSamples": "nasoscope/piccola/samples",
"sendInflux": true,
"influxHost": "localhost",
"influxPort": 8086,
"influxDB": "axes_piccola",
"influxRetention": "one_day"
}

View File

@@ -17,8 +17,10 @@ import struct
import csv
from paho.mqtt import client
from NasoScope import templates
from datetime import datetime
import templates
from paho.mqtt.client import connack_string
from influxdb import InfluxDBClient
def buildBody():
root = xml.Element("soap:Envelope", templates.XMLNS_ENV)
@@ -112,7 +114,8 @@ def stopSampling(channelID='0'):
def collectSamples(varMap, channelID='0'):
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:
try:
root,body = buildBody()
@@ -198,7 +201,7 @@ def saveSamples(s, fileName = None):
pass
def sendSamples(s):
client.publish(settings['mqttSendSamples']json.dumps(s))
mqtt.publish(settings['mqttSendSamples'],json.dumps(s))
def onMessage(cli, userdata, msg):
@@ -218,7 +221,7 @@ def onMessage(cli, userdata, msg):
elif "EXIT" in msg:
pass
else:
LOGGER.error("Messaggio MQTT sconosciuto")
LOGGER.error(f"Messaggio MQTT sconosciuto: {msg}")
pass
def onConnect(cli, userdata, flags, rc):
@@ -240,11 +243,13 @@ if __name__ == '__main__':
firstLoop = True
stat = "IDLE"
bufferFull = 0
loopcount = 0
variableMap = list()
samples = dict()
try:
fp = open("config.json")
print(f'Config: {sys.argv[1]}')
fp = open(str(sys.argv[1]))
settings = json.load(fp)
fp.close()
except:
@@ -268,7 +273,8 @@ if __name__ == '__main__':
LOGGER.warning("NasoScope {} Started!".format(settings["version"]))
mqtt = client.Client(protocol=client.MQTTv31)
mqtt = client.Client()
mqtt.on_message = onMessage
mqtt.on_connect = onConnect
mqtt.on_disconnect = onDisconnect
@@ -276,6 +282,8 @@ if __name__ == '__main__':
mqtt.subscribe(settings['mqttReceive'])
mqtt.user_data_set(stat)
mqtt.loop_start()
influx = InfluxDBClient(host= settings['influxHost'], port=settings['influxPort'], database = settings['influxDB'])
xmlReqTempl = xml.fromstring(templates.REQ_TEMPLATE)
xmlRespTempl = xml.fromstring(templates.RESP_TEMPLATE)
@@ -320,16 +328,19 @@ if __name__ == '__main__':
tempSamples = collectSamples(variableMap,channelID)
if tempSamples is not False:
if firstCap == True:
firstTS = min([z[templates.SAM_TS] for x in tempSamples \
for y in tempSamples[x] for z in tempSamples[x][y]])
# firstTS = min([z[templates.SAM_TS] for x in tempSamples \
# for y in tempSamples[x] for z in tempSamples[x][y]])
firstCap = False
firstTS = time.time()
LOGGER.warning(f'Inizio cattura: {datetime.fromtimestamp(firstTS).strftime("%c")}')
pass
else:
for ax in tempSamples:
for reg in tempSamples[ax]:
for idx,rec in enumerate(tempSamples[ax][reg]):
tempSamples[ax][reg][idx]=((tempSamples[ax][reg][idx][templates.SAM_TS]-firstTS)/10**6,
tempSamples[ax][reg][idx][templates.SAM_VAL])
if not settings['sendInflux']:
for idx,rec in enumerate(tempSamples[ax][reg]):
tempSamples[ax][reg][idx]=(tempSamples[ax][reg][idx][templates.SAM_TS],
tempSamples[ax][reg][idx][templates.SAM_VAL])
if not settings["sendMqtt"]:
if tempSamples[ax][reg][idx][templates.SAM_TS] > settings["recordTime"]:
samples[ax][reg] = samples[ax][reg][-(len(samples)-len(tempSamples)):]
@@ -345,13 +356,38 @@ if __name__ == '__main__':
samples[ax][reg]+=tempSamples[ax][reg]
else:
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
if time.time() - startTime > settings["fileSaveTime"]:
saveSamples(samples)
startTime = time.time()
LOGGER.info("{}".format(tempSamples))
if not settings['sendMqtt'] and not settings['sendInflux']:
if time.time() - startTime > settings["fileSaveTime"]:
saveSamples(samples)
startTime = time.time()
LOGGER.info("{}".format(tempSamples))
loopcount+=1
if loopcount % 100 == 0:
LOGGER.info(f"Loop Numero, sono vivo: {loopcount}")
else:
LOGGER.error("Disconnesso dal CN")
stat = "STOP"
@@ -359,15 +395,16 @@ if __name__ == '__main__':
elif stat == "STOP":
stopSampling()
closeChannel()
if not settings['sendMqtt']:
if not settings['sendMqtt'] and not settings['sendInflux']:
saveSamples(samples)
LOGGER.warning("Chiudo il Canale")
stat = "IDLE"
firstLoop = True
stat = "IDLE"
pass
elif stat == "IDLE" and firstLoop == True:
LOGGER.info("Pronto a Iniziare Cattura")
firstLoop = False
elif stat == "IDLE":
if firstLoop:
LOGGER.info("Pronto a Iniziare Cattura")
firstLoop = False
else:
pass
time.sleep(settings["loopPeriod"])

16
nasoscopio-grande.service Normal file
View 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

View 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
View 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
View 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