class RedgeStateMachine extends RedgeFront{state={duh:123};remote=!1;logs=[];hide_logs=["event","state_tick"];clr_log(){$(`#${this.id}_LOG tr`).remove(),$(`#${this.id}_LOGH tr`).remove();var t,s,e=(e="")+"event"+"state";for([t,s]of Object.entries($$$("NHMI").state.sm.v))-1==this.hide_logs.indexOf(t)&&"params"!=s.domain&&s.show&&(e+=`${t}`);e+="",$(`#${this.id}_LOGH`).append(e)}prev_log_vars={};add_log(t){var s,e,a,i="";i+=""+t.event+"",this.prev_log_vars.state!=t.new_state?i+=""+t.new_state+"":i+=""+t.new_state+"";for([s,e]of Object.entries($$$("NHMI").state.sm.v))-1==this.hide_logs.indexOf(s)&&("params"!=e.domain&&e.show&&(a="",this.state.sm.remote?t.vars&&s in t.vars&&(a="*"+t.vars[s]):a=$$$("NHMI").state.sm.v[s].get(),this.prev_log_vars[s]!=a?i+=`${a}`:i+=`${a}`,this.prev_log_vars[s]=a),this.prev_log_vars.state=t.new_state);i+="",$(`#${this.id}_LOG`).append(i),23<$(`#${this.id}_LOG tr`).length&&$(`#${this.id}_LOG tr:first`).remove(),t.fatal&&(i=(i="")+""+t.fatal+"",$(`#${this.id}_LOG`).append(i),30<$(`#${this.id}_LOG tr`).length)&&$(`#${this.id}_LOG tr:first`).remove()}cb_event(t,s,e){console.log("cb_event",s,e);var a=$$$("NHMI").state.sm.current_state;switch($$$("NHMI").add_log({event:s,tick:$$$("NHMI").state.sm.v.tick.get(),state_tick:$$$("NHMI").state.sm.v.state_tick.get(),old_state:a,new_state:a,info:JSON.stringify(e)}),s){case"B_SET":switch(a){case"M_REF":$$$("NHMI").state.sm.v.v_ref.set($$$("NHMI").state.sm.v.value.get());break;case"M_CTRL":$$$("NHMI").state.sm.v.v_ctrl.set($$$("NHMI").state.sm.v.value.get());break;case"M_DISP":$$$("NHMI").state.sm.v.v_disp.set($$$("NHMI").state.sm.v.value.get());break;case"M_UNIT":$$$("NHMI").state.sm.v.v_unit.set($$$("NHMI").state.sm.v.value.get());break;case"M_CONF":$$$("NHMI").state.sm.v.v_conf.set($$$("NHMI").state.sm.v.value.get())}break;case"to_ref":$$$("NHMI").state.sm.v.value.set($$$("NHMI").state.sm.v.v_ref.get()),$$$("NHMI").state.sm.v.old.set($$$("NHMI").state.sm.v.value.get());break;case"to_ctrl":$$$("NHMI").state.sm.v.value.set($$$("NHMI").state.sm.v.v_ctrl.get()),$$$("NHMI").state.sm.v.old.set($$$("NHMI").state.sm.v.value.get());break;case"to_disp":$$$("NHMI").state.sm.v.value.set($$$("NHMI").state.sm.v.v_disp.get()),$$$("NHMI").state.sm.v.old.set($$$("NHMI").state.sm.v.value.get());break;case"to_unit":$$$("NHMI").state.sm.v.value.set($$$("NHMI").state.sm.v.v_unit.get()),$$$("NHMI").state.sm.v.old.set($$$("NHMI").state.sm.v.value.get());break;case"to_conf":$$$("NHMI").state.sm.v.value.set($$$("NHMI").state.sm.v.v_conf.get()),$$$("NHMI").state.sm.v.old.set($$$("NHMI").state.sm.v.value.get());break;case"B_MINUS":console.log("minus in state",a),"IDLE"!=a&&($$$("NHMI").state.sm.v.last_button.set(1),$$$("NHMI").state.sm.v.value.set($$$("NHMI").state.sm.v.value.get()-1));break;case"B_PLUS":"IDLE"!=a&&($$$("NHMI").state.sm.v.last_button.set(1),$$$("NHMI").state.sm.v.value.set($$$("NHMI").state.sm.v.value.get()+1));break;case"CAN_RX_1":$$$("NHMI").state.sm.v.last_rx.set(1);break;case"BUS_OFF":e.state?($$$("NHMI").state.sm.v.last_online.set(0),console.log("BUS_OFF, STOP last_online")):($$$("NHMI").state.sm.v.last_online.set(1),console.log("BUS_OFF, START last_online"))}}row={sm_tick:0,cols:[]};sm_show_row(){var t,s="-1",e=this.row.to_state_index,a=window[`json_sm_${window.args.sm}_lookup`],i=(this.row.event_index in a.events&&(s=a.events[this.row.event_index].key),this.row.transition_index in a.transitions&&(s=a.transitions[this.row.transition_index].key),this.row.to_state_index in a.states&&(e=a.states[this.row.to_state_index].key),0),o={};for(t of this.row.cols){var _=a.variables[i++];o[_.key]=t,this.state.sm.remote_vars[_.key]=t}o.tick=this.row.sm_tick,-1==s&&-1==e||this.state.sm.remote&&(this.state.sm.remote_state=e,this.add_log({event:s,sm_tick:this.row.sm_tick,state_tick:"",new_state:e,vars:o,cols:this.row.cols}))}sm_do_ind(t){var s=1;"NHMI_UI"==window.args.sm&&(s=2),t.sm_id==s&&(delete t.indication,delete t.received,delete t.sent,delete t.seq,delete t.device,this.row.sm_tick!=t.sm_tick&&(this.row.sm_tick&&this.sm_show_row(),this.row.sm_tick=t.sm_tick,this.row.event_index=-1,this.row.from_state_index=-1,this.row.to_state_index=-1,this.row.transition_index=-1),"sm_var_change_ind"==t.topic?(63==t.var_index?(this.sm_show_row(),this.row.sm_tick=0,this.row.event_index=-1,this.row.from_state_index=-1,this.row.to_state_index=-1,this.row.transition_index=-1):this.row.cols[t.var_index]=t.val,this.row.stamp=t.stamp):"sm_state_change_ind"==t.topic?(this.row.from_state_index=t.from_state_index,this.row.to_state_index=t.to_state_index,this.row.transition_index=t.transition_index,this.row.stamp=t.stamp):"sm_event_ind"==t.topic&&(this.row.event_index=t.event_index,this.row.to_state_index=t.state_index,this.row.from_state_index=t.state_index,this.row.stamp=t.stamp))}constructor(t,s,e,a){super(t,s,e,a),console.log("SM construction"),this.state.sm=new fsm(window.args.sm,this.cb_event);for(var[i,o]of Object.entries(this.state.sm.v))o.ui="none",o.show=!0,-1!=i.indexOf("__")&&(o.show=!1),"timers"==o.domain&&(o.unit="ms"),"timers"!=o.domain&&"funcs"!=o.domain&&"output"!=o.domain&&("bool"==o.ui_type?o.ui="toggler":o.ui="setter");this.r=t.req_ind("+","log_write","log_write",async(t,s)=>{console.log("log:",s.data)}),this.r=t.req_ind("+","sm_state_change_ind","sm_state_change_ind",async(t,s)=>{this.sm_do_ind(s)}),this.r=t.req_ind("+","sm_var_change_ind","sm_var_change_ind",async(t,s)=>{this.sm_do_ind(s)}),this.r=t.req_ind("+","sm_event_ind","sm_event_ind",async(t,s)=>{this.sm_do_ind(s)}),register_button_handler("SET","",(t,s)=>{var e=$(`#${this.id}__UPDATE_`+t.fld).val();console.log("SET",t.fld,e),$$$("NHMI").state.sm.v[t.fld].set(e),this.update_vars()}),register_button_handler("TOG","",(t,s)=>{$$$("NHMI").state.sm.v[t.fld].set(!$$$("NHMI").state.sm.v[t.fld].get()),this.update_vars()}),register_button_handler("ETOG","",(t,s)=>{console.log("ETOG",t.fld,o),$("#"+t.fld).val(0==parseInt($("#"+t.fld).val())?1:0),this.update_vars()}),register_button_handler("SEND_EVENT","",(t,s)=>{console.log("SEND_EVENT",t,s);var e,a,s=$$$("NHMI").state.sm.fsm.events[t.fld],i={},o=[];for([e,a]of Object.entries(s.args))console.log("ARG",e,a,`#${this.id}__UPDATE_${t.fld}__`+e),i[e]=parseInt($(`#${this.id}__UPDATE_${t.fld}__`+e).val()),o.push(parseInt($(`#${this.id}__UPDATE_${t.fld}__`+e).val()));this.state.sm.remote?(console.log("SEND_EVENTn",o),$$("X000000")[t.fld](...o)):(console.log("SEND_EVENTa",i),$$$("NHMI").state.sm.push_event(t.fld,i)),this.update_vars()}),register_button_handler("RESET","",(t,s)=>{this.reset()}),register_button_handler("RUN","",(t,s)=>{this.state.sm.running?this.stop():this.start()}),register_button_handler("REMOTE","",(t,s)=>{this.state.sm.remote=!this.state.sm.remote,this.old_state="",this.state.sm.remote?$(`#${this.id}_REMOTE`).html("REMOTE"):$(`#${this.id}_REMOTE`).html("LOCAL")}),setTimeout(()=>{this.start(),this.clr_log},10)}stop(){this.state.sm.running=!1,$(`#${this.id}_RUN`).html("RUN")}start(){this.clr_log(),this.state.sm.running=!0,$(`#${this.id}_RUN`).html("STOP")}reset(){this.state.sm.remote?$$("X000000").reset(1,[1]):window.location="/?sm="+window.args.sm}old_vars={};old_state="";update_vars(){for(var[t,s]of Object.entries(this.state.sm.v)){var e,a,i=s.gett()||"",o=i;try{this.old_vars[t]!=i?("ms"==s.unit&&(e=Math.floor(i/1e3),a=i%1e3,o=e<60?sprintf("%d.%03d",e,a):e<3600?sprintf("%d:%02d.%03d",e/60,e%60,a):e<86400?sprintf("%d:%02d:%02d.%03d",e/3600,Math.floor(e/60)%60,e%60,a):sprintf("%d %02d:%02d:%02d.%03d",e/86400,Math.floor(e/3600)%24,Math.floor(e/60)%60,e%60,a)),-1==$(`#${this.id}__`+t).attr("class").indexOf("changed")&&$(`#${this.id}__`+t).addClass("changed"),$(`#${this.id}__`+t).html(o),"bool"===s.ui_type?$(".v_b_"+t).attr("fill",i?"green":"red"):$(".v_n_"+t).html(o)):-1!=$(`#${this.id}__`+t).attr("class").indexOf("changed")&&$(`#${this.id}__`+t).removeClass("changed")}catch(t){console.log("duh",t)}this.old_vars[t]=i}var _=$$$("NHMI").state.sm.current_state;(_=this.state.sm.remote?$$$("NHMI").state.sm.remote_state:_)!=this.old_state&&(console.log("STATE CHANGED",this.old_state,"=>",_),$(`#${this.id}_STATE`).html(_),$(`.s_${this.old_state}_dash`).removeClass("active"),$(".vec_"+this.old_state).removeClass("active"),$(`.s_${_}_dash`).addClass("active"),$(".vec_"+_).addClass("active"),this.old_state=_),(t=Object.keys($_.devices).find(t=>"X000000"==$_.devices[t].serno))!=this.state.sm.remote_key&&(console.log("REMOTE_DEV",t),this.state.sm.remote_key=t)}onEvent(t){switch(super.onEvent(t),t.event){case"focusout":break;case"click":console.log("click",t),"BUTTON"==$(t.target).prop("nodeName")&&$(t.target)[0].blur(),emit_button_event(t.data.event,{button:t.key,fld:t.data.fld,name:t.key},this.id)}t.e.preventDefault()}ticker=0;tick(){$$$("NHMI").state.sm.do_event(null),5