funziona e manda le cose su influx
This commit is contained in:
903
NasoScope/Nasoscope.log
Normal file
903
NasoScope/Nasoscope.log
Normal file
File diff suppressed because one or more lines are too long
@@ -3,7 +3,7 @@
|
||||
"logFile": "./Nasoscope.log",
|
||||
"logFormat": "%(asctime)s|%(levelname)-7s|%(funcName)-10s|%(lineno)-3d: %(message)-50s",
|
||||
"logTimeFormat": "%m-%d %H:%M:%S",
|
||||
"URL": "http://localhost:8081",
|
||||
"URL": "http://localhost:8080",
|
||||
"samplePeriod": 10,
|
||||
"loopPeriod": 0.5,
|
||||
"recordTime": 3600,
|
||||
@@ -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"
|
||||
"mqttSendSamples": "nasoscope/samples",
|
||||
"sendInflux": true,
|
||||
"influxHost": "localhost",
|
||||
"influxPort": 8086,
|
||||
"influxDB": "axes",
|
||||
"influxRetention": "one_day"
|
||||
}
|
||||
24244
NasoScope/get-pip.py
Normal file
24244
NasoScope/get-pip.py
Normal file
File diff suppressed because it is too large
Load Diff
@@ -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):
|
||||
@@ -240,6 +243,7 @@ if __name__ == '__main__':
|
||||
firstLoop = True
|
||||
stat = "IDLE"
|
||||
bufferFull = 0
|
||||
loopcount = 0
|
||||
variableMap = list()
|
||||
samples = dict()
|
||||
|
||||
@@ -268,7 +272,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 +281,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 +327,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.info(f'Inizio cattura TS: {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 +355,37 @@ if __name__ == '__main__':
|
||||
samples[ax][reg]+=tempSamples[ax][reg]
|
||||
else:
|
||||
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
|
||||
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"
|
||||
@@ -364,6 +398,7 @@ if __name__ == '__main__':
|
||||
LOGGER.warning("Chiudo il Canale")
|
||||
stat = "IDLE"
|
||||
firstLoop = True
|
||||
loopcount = 0
|
||||
pass
|
||||
elif stat == "IDLE" and firstLoop == True:
|
||||
LOGGER.info("Pronto a Iniziare Cattura")
|
||||
|
||||
Reference in New Issue
Block a user