edouard@3979: # mqtt/client.py edouard@3979: edouard@3979: from __future__ import absolute_import edouard@3979: edouard@3979: import os edouard@3979: edouard@3979: from editors.ConfTreeNodeEditor import ConfTreeNodeEditor edouard@3979: from PLCControler import LOCATION_CONFNODE, LOCATION_VAR_INPUT, LOCATION_VAR_OUTPUT edouard@3979: from .mqtt_client_gen import MQTTClientPanel, MQTTClientModel, MQTT_IEC_types, authParams edouard@3979: edouard@3979: import util.paths as paths edouard@3979: edouard@3979: PahoMqttCPath = paths.ThirdPartyPath("MQTT") edouard@3979: PahoMqttCLibraryPath = PahoMqttCPath edouard@3979: PahoMqttCIncludePaths = [PahoMqttCPath] edouard@3979: edouard@3979: class MQTTClientEditor(ConfTreeNodeEditor): edouard@3979: CONFNODEEDITOR_TABS = [ edouard@3979: (_("MQTT Client"), "CreateMQTTClient_UI")] edouard@3979: edouard@3979: def Log(self, msg): edouard@3979: self.Controler.GetCTRoot().logger.write(msg) edouard@3979: edouard@3979: def CreateMQTTClient_UI(self, parent): edouard@3979: return MQTTClientPanel(parent, self.Controler.GetModelData(), self.Log, self.Controler.GetConfig) edouard@3979: edouard@3979: class MQTTClient(object): edouard@3979: XSD = """ edouard@3979: edouard@3979: edouard@3979: edouard@3979: edouard@3979: edouard@3979: edouard@3979: edouard@3979: edouard@3979: edouard@3979: edouard@3979: edouard@3979: edouard@3979: edouard@3979: edouard@3979: edouard@3979: edouard@3979: edouard@3979: edouard@3979: edouard@3979: edouard@3979: edouard@3979: edouard@3979: edouard@3979: edouard@3980: edouard@3979: edouard@3979: edouard@3979: edouard@3979: """ edouard@3979: edouard@3979: EditorType = MQTTClientEditor edouard@3979: edouard@3979: def __init__(self): edouard@3979: self.modeldata = MQTTClientModel(self.Log, self.CTNMarkModified) edouard@3979: edouard@3979: filepath = self.GetFileName() edouard@3979: if os.path.isfile(filepath): edouard@3979: self.modeldata.LoadCSV(filepath) edouard@3979: edouard@3979: def Log(self, msg): edouard@3979: self.GetCTRoot().logger.write(msg) edouard@3979: edouard@3979: def GetModelData(self): edouard@3979: return self.modeldata edouard@3979: edouard@3979: def GetConfig(self): edouard@3979: def cfg(path): edouard@3979: try: edouard@3979: attr=self.GetParamsAttributes("MQTTClient."+path) edouard@3979: except ValueError: edouard@3979: return None edouard@3979: return attr["value"] edouard@3979: edouard@3979: AuthType = cfg("AuthType") edouard@3980: res = dict(URI=cfg("Broker_URI"), AuthType=AuthType, clientID=cfg("Client_ID")) edouard@3979: edouard@3979: paramList = authParams.get(AuthType, None) edouard@3979: if paramList: edouard@3979: for name,default in paramList: edouard@3979: value = cfg("AuthType."+name) edouard@3979: if value == "" or value is None: edouard@3979: value = default edouard@3979: # cryptomaterial is expected to be in project's user provide file directory edouard@3979: if name in ["Certificate","PrivateKey"]: edouard@3979: value = os.path.join(self.GetCTRoot()._getProjectFilesPath(), value) edouard@3979: res[name] = value edouard@3979: edouard@3979: return res edouard@3979: edouard@3979: def GetFileName(self): edouard@3979: return os.path.join(self.CTNPath(), 'selected.csv') edouard@3979: edouard@3979: def OnCTNSave(self, from_project_path=None): edouard@3979: self.modeldata.SaveCSV(self.GetFileName()) edouard@3979: return True edouard@3979: edouard@3979: def CTNGenerate_C(self, buildpath, locations): edouard@3979: current_location = self.GetCurrentLocation() edouard@3979: locstr = "_".join(map(str, current_location)) edouard@3979: c_path = os.path.join(buildpath, "mqtt_client__%s.c" % locstr) edouard@3979: edouard@3979: c_code = '#include "beremiz.h"\n' edouard@3979: c_code += self.modeldata.GenerateC(c_path, locstr, self.GetConfig()) edouard@3979: edouard@3979: with open(c_path, 'w') as c_file: edouard@3979: c_file.write(c_code) edouard@3979: edouard@3979: LDFLAGS = [' "' + os.path.join(PahoMqttCLibraryPath, "libpaho-mqtt3as.a") + '"', '-lcrypto'] edouard@3979: edouard@3979: CFLAGS = ' '.join(['-I"' + path + '"' for path in PahoMqttCIncludePaths]) edouard@3979: edouard@3979: return [(c_path, CFLAGS)], LDFLAGS, True edouard@3979: edouard@3979: def GetVariableLocationTree(self): edouard@3979: current_location = self.GetCurrentLocation() edouard@3979: locstr = "_".join(map(str, current_location)) edouard@3979: name = self.BaseParams.getName() edouard@3979: entries = [] edouard@3979: for direction, data in self.modeldata.iteritems(): edouard@3979: iec_direction_prefix = {"input": "__I", "output": "__Q"}[direction] edouard@3979: for row in data: edouard@3979: dname, ua_nsidx, ua_nodeid_type, _ua_node_id, ua_type, iec_number = row edouard@3979: iec_type, C_type, iec_size_prefix, ua_type_enum, ua_type = MQTT_IEC_types[ua_type] edouard@3979: c_loc_name = iec_direction_prefix + iec_size_prefix + locstr + "_" + str(iec_number) edouard@3979: entries.append({ edouard@3979: "name": dname, edouard@3979: "type": {"input": LOCATION_VAR_INPUT, "output": LOCATION_VAR_OUTPUT}[direction], edouard@3979: "size": {"X":1, "B":8, "W":16, "D":32, "L":64}[iec_size_prefix], edouard@3979: "IEC_type": iec_type, edouard@3979: "var_name": c_loc_name, edouard@3979: "location": iec_size_prefix + ".".join([str(i) for i in current_location]) + "." + str(iec_number), edouard@3979: "description": "", edouard@3979: "children": []}) edouard@3979: return {"name": name, edouard@3979: "type": LOCATION_CONFNODE, edouard@3979: "location": ".".join([str(i) for i in current_location]) + ".x", edouard@3979: "children": entries} edouard@3979: