first commit

This commit is contained in:
Johannes Holzwarth | SE-Inno GmbH
2024-11-26 11:43:38 +01:00
commit 7437702353
8 changed files with 1144 additions and 0 deletions
+41
View File
@@ -0,0 +1,41 @@
{
"elements":
[
{ "type": "Label", "caption": "Main" },
{ "type": "ValidationTextBox", "name": "devName", "caption": "Name" },
{ "type": "NumberSpinner", "name": "devTime", "caption": "Refreshtime" },
{ "type": "Label", "caption": "Mqtt Properties" },
{ "type": "SelectInstance", "name": "mqttGwId", "caption": "Mqtt Gateway", "validModules":["{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}"] },
{ "type": "SelectInstance", "name": "modGwId", "caption": "Modbus Gateway", "validModules":["{A5F663AB-C400-4FE5-B207-4D67CC030564}"] },
{ "type": "ValidationTextBox", "name": "mqttPrefix", "caption": "Mqtt-Prefix e.g. Milesight01 (/uplink/EUI and /downlink/EUI is autm. added)" },
{ "type": "ValidationTextBox", "name": "devEui", "caption": "Device EUI (as Suffix on MQTT Topic)" },
{ "type": "Label", "caption": "Modbus Actualvalus to Controler:" },
{ "type": "NumberSpinner", "name": "modFcWriteActValues", "caption": "Functioncode Write Actual Values", "value": 16},
{ "type": "NumberSpinner", "name": "modFcReadActValues", "caption": "Functioncode Read Actual Values", "value": 3},
{ "type": "NumberSpinner", "name": "modAmbient_Temperature", "caption": "Modbusadress Ambient_Temperature" },
{ "type": "NumberSpinner", "name": "modAmbient_Humidity", "caption": "Modbusadress Ambient_Humidity" },
{ "type": "NumberSpinner", "name": "modV_Bat", "caption": "Modbusadress Voltage_Battery" },
{ "type": "Label", "caption": "Options:" },
{ "type": "NumberSpinner", "name": "CalibrateTemp", "caption": "Calibrate Temp", "value": 0.0},
{ "type": "NumberSpinner", "name": "CalibrateHum", "caption": "Calibrate Hum", "value": 0.0}
],
"actions":
[
],
"status":
[
]
}
+14
View File
@@ -0,0 +1,14 @@
{
"id": "{14A84A97-E74A-66CB-BBDA-6BB47D6D4A2D}",
"name": "LoraNovos3T",
"type": 3,
"vendor": "J.H. Artworks | Thermokon",
"aliases":
[
],
"url": "https://jh-artworks.com",
"parentRequirements": [],
"childRequirements": [],
"implemented": [],
"prefix": "N3T"
}
+306
View File
@@ -0,0 +1,306 @@
<?
// Klassendefinition
class LoraNovos3T extends IPSModule {
// Überschreibt die interne IPS_Create($id) Funktion
public function Create() {
// Diese Zeile nicht löschen.
parent::Create();
$this->RegisterPropertyString("devName","A Small Sensor in a Big World 01");
$this->RegisterPropertyInteger("devTime",10);
$this->RegisterPropertyString("mqttPrefix","milesight01");
$this->RegisterPropertyInteger("mqttGwId",54321);
$this->RegisterPropertyInteger("modGwId",54321);
$this->RegisterPropertyString("devEui","343734344d338150");
$this->RegisterPropertyInteger("modFcWriteActValues",16);
$this->RegisterPropertyInteger("modFcReadActValues",3);
$this->RegisterPropertyInteger("modAmbient_Temperature",12345);
$this->RegisterPropertyInteger("modAmbient_Humidity",12345);
$this->RegisterPropertyInteger("modV_Bat",12345);
$this->RegisterPropertyInteger("modButton",12345);
$this->RegisterPropertyFloat("CalibrateTemp",0.0);
$this->RegisterPropertyFloat("CalibrateHum",0.0);
$this->RegisterTimer("ReadUpdate", 0, 'N3T_ReadActualValues('.$this->InstanceID.');');
}
// Überschreibt die intere IPS_ApplyChanges($id) Funktion
public function ApplyChanges() {
// Diese Zeile nicht löschen
parent::ApplyChanges();
$devName = $this->ReadPropertyString("devName");
$mqttPrefix = $this->ReadPropertyString("mqttPrefix");
$devEui = $this->ReadPropertyString("devEui");
$mqttGwId = $this->ReadPropertyInteger("mqttGwId");
$devTime = $this->ReadPropertyInteger("devTime");
$topicuplink = $mqttPrefix.'/uplink'.'/'.$devEui;
$this->MqttPublish("g".$devEui, $mqttGwId, $devName." ".$devEui." uplink", $topicuplink, "", false, false);
$this->SetTimerInterval("ReadUpdate", $devTime * 1000);
}
/**
* Die folgenden Funktionen stehen automatisch zur Verfügung, wenn das Modul über die "Module Control" eingefügt wurden.
* Die Funktionen werden, mit dem selbst eingerichteten Prefix, in PHP und JSON-RPC wiefolgt zur Verfügung gestellt:
*
*
*/
public function ReadActualValues(){
$devName = $this->ReadPropertyString("devName");
$devEui = $this->ReadPropertyString("devEui");
$modGwId = $this->ReadPropertyInteger("modGwId");
$modFcWriteActValues = $this->ReadPropertyInteger("modFcWriteActValues");
$modFcReadActValues = $this->ReadPropertyInteger("modFcReadActValues");
$idread = @IPS_GetObjectIDByIdent("g".$devEui,$_IPS['SELF']);
// get Value variable and get its Value
//echo "ID READ: ".$idread;
$var_id_read = @IPS_GetChildrenIDs($idread);
$var_id_read = $var_id_read[0];
//echo "var_id_read: ".$var_id_read;
$rawdata = GetValue($var_id_read);
$data = json_decode($rawdata, true);
$temp = "";
if ($data === null) {
$temp = "failure";
return "Fehler: Ungültiges JSON.";
}
$temp = $this->CheckGetArrayValue($data,"TEMP");
if ($temp != "failure") {
$adressmodAmbient_Temperature = $this->ReadPropertyInteger("modAmbient_Temperature");
$this->ModbusPublish($devEui."Ambient_Temperature", $modGwId, $devName." Ambient_Temperature", $adressmodAmbient_Temperature, $temp, $modFcReadActValues, $modFcWriteActValues,7);
$temp = "failure";
}
$temp = $this->CheckGetArrayValue($data,"RHUM");
if ($temp != "failure") {
$adressmodAmbient_Humidity = $this->ReadPropertyInteger("modAmbient_Humidity");
$this->ModbusPublish($devEui."Ambient_Humidity", $modGwId, $devName." Ambient_Humidity", $adressmodAmbient_Humidity, $temp, $modFcReadActValues, $modFcWriteActValues,7);
$temp = "failure";
}
$temp = $this->CheckGetArrayValue($data,"VBAT");
if ($temp != "failure") {
$adressmodV_Bat = $this->ReadPropertyInteger("modV_Bat");
$this->ModbusPublish($devEui."V_Bat", $modGwId, $devName." V_Bat", $adressmodV_Bat, $temp/1000.0, $modFcReadActValues, $modFcWriteActValues,7);
$temp = "failure";
}
$temp = $this->CheckGetArrayValue($data,"Button");
if ($temp != "failure") {
$adressmodmodButton = $this->ReadPropertyInteger("modButton");
$this->ModbusPublish($devEui."Button", $modGwId, $devName." Button", $adressmodmodButton, $temp, $modFcReadActValues, $modFcWriteActValues,0);
$temp = "failure";
}
}
private function CheckGetArrayValue($data,$index){
if (array_key_exists($index, $data)) {
return $data[$index];
}else
{
return "failure";
}
}
public function MqttPublish($ident, $server_id, $name, $topic, $payload, $retain, $write) {
// ensure server instance exists
if(!IPS_InstanceExists($server_id)) {
return false;
}
// convert array structure to json string
if(is_array($payload)) $payload = json_encode($payload);
// determine data type
if(is_string($payload)) {
$ips_var_type = 3;
} else if(is_float($payload)) {
$ips_var_type = 2;
} else if(is_int($payload)) {
$ips_var_type = 1;
} else if(is_bool($payload)) {
$ips_var_type = 0;
} else { // unsupported
return false;
}
$module_id = "{01C00ADD-D04E-452E-B66A-D253278743FE}" /* Module ID of MQTT Server Device */;
// enter semaphore to ensure the temporary device gets used by one thread at a time
if(IPS_SemaphoreEnter($ident, 100)) {
// get temporary MQTT Server Device or create if needed
$id = @IPS_GetObjectIDByIdent($ident, $_IPS['SELF']);
if($id === false) {
$id = @IPS_CreateInstance($module_id);
if($id === false) {
return false;
}
IPS_SetParent($id, $_IPS['SELF']);
IPS_SetIdent($id, $ident);
}
// ensure the specified server instance is actually compatible
if(!IPS_IsInstanceCompatible($id, $server_id)) {
return false;
}
// ensure that the temporary device is actually connected to the correct server instance
$inst_config = IPS_GetInstance($id);
if($inst_config["ConnectionID"] != $server_id) {
IPS_DisconnectInstance($id);
if(!@IPS_ConnectInstance($id, $server_id)) {
return false;
IPS_SemaphoreLeave($ident);
}
}
// name object to help with debugging
IPS_SetName($id, $name);
// configure temporary device
$config_arr = array(
"Retain" => $retain,
"Topic" => $topic,
"Type" => $ips_var_type
);
$config_str = json_encode($config_arr);
IPS_SetConfiguration($id, $config_str);
IPS_SetHidden($id,true);
IPS_ApplyChanges($id);
if($write == true){
// get Value variable and use it to publish the payload
$var_id = @IPS_GetChildrenIDs($id);
RequestAction($var_id[0], $payload);
}
IPS_SemaphoreLeave($ident); // !!! IMPORTANT !!!
} else { // semaphore timeout
return false;
IPS_SemaphoreLeave($ident);
}
return true;
IPS_SemaphoreLeave($ident);
} // MQTT_Publish
public function ModbusPublish($ident, $server_id, $name, $adress, $payload, $fcread, $fcwrite, $vartype) {
// ensure server instance exists
if(!IPS_InstanceExists($server_id)) {
return false;
}
// convert array structure to json string
if(is_array($payload)) $payload = json_encode($payload);
$ips_var_type = $vartype;
// determine data type
$module_id = "{CB197E50-273D-4535-8C91-BB35273E3CA5}" /* Module ID of Modbus Device */;
// enter semaphore to ensure the temporary device gets used by one thread at a time
if(IPS_SemaphoreEnter($ident, 100)) {
// get temporary MQTT Server Device or create if needed
$id = @IPS_GetObjectIDByIdent($ident, $_IPS['SELF']);
if($id === false) {
$id = @IPS_CreateInstance($module_id);
if($id === false) {
IPS_SemaphoreLeave($ident);
return false;
}
IPS_SetParent($id, $_IPS['SELF']);
IPS_SetIdent($id, $ident);
if ($ips_var_type == 0){
$fcread = 1;
$fcwrite = 5;
}
// name object to help with debugging
IPS_SetName($id, $name);
// configure temporary device
//{"ByteOrder":0,"DataType":0,"EmulateStatus":true,"Factor":0.0,"Length":0,"Poller":5000,"ReadAddress":0,"ReadFunctionCode":1,"WriteAddress":0,"WriteFunctionCode":5}
$config_arr = array(
"ByteOrder" => 3,
"DataType" => $ips_var_type,
"EmulateStatus" => true,
"Factor" => 0.0,
"Length" => 0,
"Poller" => 5000,
"ReadAddress" => $adress,
"ReadFunctionCode" => $fcread,
"WriteAddress" => $adress,
"WriteFunctionCode" => $fcwrite
);
$config_str = json_encode($config_arr);
IPS_SetConfiguration($id, $config_str);
IPS_SetHidden($id,true);
IPS_ApplyChanges($id);
}
// ensure that the temporary device is actually connected to the correct server instance
$inst_config = IPS_GetInstance($id);
if($inst_config["ConnectionID"] != $server_id) {
IPS_DisconnectInstance($id);
if(!@IPS_ConnectInstance($id, $server_id)) {
return false;
IPS_SemaphoreLeave($ident);
}
}
$var_id = @IPS_GetChildrenIDs($id);
$var_id = $var_id[0];
if($fcwrite > 0){
// get Value variable and use it to publish the payload
if ($payload == 0 & $ips_var_type = 0){
$payload = false;
}
if ($payload == 1 & $ips_var_type = 0){
$payload = true;
}
RequestAction($var_id, $payload);
IPS_SemaphoreLeave($ident);
return true;
}else{
IPS_SemaphoreLeave($ident);
return GetValue($var_id);
}
IPS_SemaphoreLeave($ident); // !!! IMPORTANT !!!
} else { // semaphore timeout
return false;
}
//return true;
} // Modbus_Publish
}
?>