52 explicit JSEngine(
const std::string& jsfile,
53 std::vector<std::string>& searchPaths,
54 std::shared_ptr<UInterface>& ui,
58 inline std::shared_ptr<DebugStream> log()
noexcept
63 inline std::shared_ptr<DebugStream> js_log()
noexcept
68 inline std::shared_ptr<DebugStream> http_log()
noexcept
70 return httpserv->log();
78 void askSensors( UniversalIO::UIOCommand cmd );
86 void initGlobal( JSContext* ctx );
87 void exportAllFunctionsFromTimerModule();
88 void createUInterfaceObject();
89 void createUnisetObject();
90 void createResponsePrototype( JSContext* ctx );
91 void createRequestAtoms(JSContext* ctx);
92 void createRequestPrototype(JSContext* ctx);
96 JSValue jsReqProto_ = { JS_UNDEFINED };
97 JSValue jsResProto_ = { JS_UNDEFINED };
99 bool reqAtomsInited_ =
false;
102 JSAtom method, uri, version, url, path, query, headers, body;
105 std::atomic_bool activated = {
false };
106 std::atomic_bool stopped = {
false };
107 std::shared_ptr<DebugStream> mylog;
109 std::vector<std::string> searchPaths;
110 std::shared_ptr<UInterface> ui;
111 JSRuntime* rt = {
nullptr };
112 JSContext* ctx = {
nullptr };
113 uint8_t* jsbuf = {
nullptr };
115 std::shared_ptr<DebugStream> jslog = {
nullptr };
116 std::shared_ptr<uniset::JHttpServer> httpserv = {
nullptr };
118 std::shared_ptr<uniset::ObjectIndex> oind;
124 bool set( JSContext* ctx, JSValue& global, int64_t v );
127 std::unordered_map<uniset::ObjectId, jsSensor> inputs;
128 std::unordered_map<uniset::ObjectId, jsSensor> outputs;
129 std::list<JSValue> stepFunctions;
130 std::list<JSValue> stopFunctions;
132 JSValue jsFnStep = { JS_UNDEFINED };
133 JSValue jsFnStart = { JS_UNDEFINED };
134 JSValue jsFnStop = { JS_UNDEFINED };
135 JSValue jsFnTimers = { JS_UNDEFINED };
136 JSValue jsFnOnSensor = { JS_UNDEFINED };
137 JSValue jsGlobal = { JS_UNDEFINED };
138 JSValue jsModule = { JS_UNDEFINED };
139 JSValue jsFnHttpRequest = { JS_UNDEFINED };
140 JHttpServer::HandlerFn httpHandleFn;
141 std::shared_ptr<JSModbusClient> modbusClient;
143 JSValue js_ui_getValue(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
144 JSValue js_ui_askSensor(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
145 JSValue js_ui_setValue(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
146 JSValue js_uniset_StepCb(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
147 JSValue js_uniset_StopCb(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
148 JSValue js_uniset_httpStart(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
149 JSValue js_log(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
150 JSValue js_log_level(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
151 JSValue js_modbus_connectTCP(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
152 JSValue js_modbus_disconnect(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
153 JSValue js_modbus_read01(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
154 JSValue js_modbus_read02(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
155 JSValue js_modbus_read03(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
156 JSValue js_modbus_read04(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
157 JSValue js_modbus_write05(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
158 JSValue js_modbus_write06(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
159 JSValue js_modbus_write0F(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
160 JSValue js_modbus_write10(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
161 JSValue js_modbus_diag08(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
162 JSValue js_modbus_read4314(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
163#ifdef JS_OPCUA_ENABLED
164 std::shared_ptr<JSOPCUAClient> opcuaClient;
165 JSValue js_opcua_connect(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
166 JSValue js_opcua_disconnect(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
167 JSValue js_opcua_read(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
168 JSValue js_opcua_write(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
171 static JSValue jsUiGetValue_wrapper(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
172 static JSValue jsUiAskSensor_wrapper(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
173 static JSValue jsUiSetValue_wrapper(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
174 static JSValue jsLog_wrapper(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
175 static JSValue jsLogLevel_wrapper(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
176 static JSValue jsUniSetStepCb_wrapper(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
177 static JSValue jsUniSetStopCb_wrapper(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
178 static JSValue jsUniSetHttpStart_wrapper(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
179 static JSValue jsModbusConnectTCP_wrapper(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
180 static JSValue jsModbusDisconnect_wrapper(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
181 static JSValue jsModbusRead01_wrapper(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
182 static JSValue jsModbusRead02_wrapper(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
183 static JSValue jsModbusRead03_wrapper(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
184 static JSValue jsModbusRead04_wrapper(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
185 static JSValue jsModbusWrite05_wrapper(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
186 static JSValue jsModbusWrite06_wrapper(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
187 static JSValue jsModbusWrite0F_wrapper(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
188 static JSValue jsModbusWrite10_wrapper(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
189 static JSValue jsModbusDiag08_wrapper(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
190 static JSValue jsModbusRead4314_wrapper(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
191#ifdef JS_OPCUA_ENABLED
192 static JSValue jsOpcuaConnect_wrapper(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
193 static JSValue jsOpcuaDisconnect_wrapper(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
194 static JSValue jsOpcuaRead_wrapper(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
195 static JSValue jsOpcuaWrite_wrapper(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst* argv);
206 JSValue jsMakeWriteAck( ModbusRTU::ModbusData start, ModbusRTU::ModbusData count );
213#define myinfo if( log()->debugging(Debug::INFO) ) log()->info()
216#define mywarn if( log()->debugging(Debug::WARN) ) log()->warn()
219#define mycrit if( log()->debugging(Debug::CRIT) ) log()->crit()
222#define mylog1 if( log()->debugging(Debug::LEVEL1) ) log()->level1()
225#define mylog2 if( log()->debugging(Debug::LEVEL2) ) log()->level2()
228#define mylog3 if( log()->debugging(Debug::LEVEL3) ) log()->level3()
231#define mylog4 if( log()->debugging(Debug::LEVEL4) ) log()->level4()
234#define mylog5 if( log()->debugging(Debug::LEVEL5) ) log()->level5()
237#define mylog6 if( log()->debugging(Debug::LEVEL6) ) log()->level6()
240#define mylog7 if( log()->debugging(Debug::LEVEL7) ) log()->level7()
243#define mylog8 if( log()->debugging(Debug::LEVEL8) ) log()->level8()
246#define mylog9 if( log()->debugging(Debug::LEVEL9) ) log()->level9()
249#define mylogany log()->any()