funziona e manda le cose su influx

This commit is contained in:
2021-02-24 15:39:09 +01:00
parent c7b9dcadad
commit 6942686476
4 changed files with 25210 additions and 15 deletions

903
NasoScope/Nasoscope.log Normal file

File diff suppressed because one or more lines are too long

View File

@@ -3,7 +3,7 @@
"logFile": "./Nasoscope.log", "logFile": "./Nasoscope.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:8080",
"samplePeriod": 10, "samplePeriod": 10,
"loopPeriod": 0.5, "loopPeriod": 0.5,
"recordTime": 3600, "recordTime": 3600,
@@ -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/cnc2recorder",
"mqttReceive": "nasoscope/recorder2cnc", "mqttReceive": "nasoscope/recorder2cnc",
"sendMqtt": true, "sendMqtt": true,
"mqttSendSamples": "nasoscope/samples" "mqttSendSamples": "nasoscope/samples",
"sendInflux": true,
"influxHost": "localhost",
"influxPort": 8086,
"influxDB": "axes",
"influxRetention": "one_day"
} }

24244
NasoScope/get-pip.py Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -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):
@@ -240,6 +243,7 @@ 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()
@@ -268,7 +272,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 +281,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 +327,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.info(f'Inizio cattura TS: {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 +355,37 @@ if __name__ == '__main__':
samples[ax][reg]+=tempSamples[ax][reg] samples[ax][reg]+=tempSamples[ax][reg]
else: else:
sendSamples(tempSamples) sendSamples(tempSamples)
else:
influxMeas = list()
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"
@@ -364,6 +398,7 @@ if __name__ == '__main__':
LOGGER.warning("Chiudo il Canale") LOGGER.warning("Chiudo il Canale")
stat = "IDLE" stat = "IDLE"
firstLoop = True firstLoop = True
loopcount = 0
pass pass
elif stat == "IDLE" and firstLoop == True: elif stat == "IDLE" and firstLoop == True:
LOGGER.info("Pronto a Iniziare Cattura") LOGGER.info("Pronto a Iniziare Cattura")