package cn.stylefeng.guns.plcserver.server.OP120B.callback; import cn.stylefeng.guns.plcserver.opc.GlobalVariable; import cn.stylefeng.guns.plcserver.opc.OPCComm; import cn.stylefeng.guns.plcserver.opc.unit.OPCUnit; import org.jinterop.dcom.common.JIException; import org.jinterop.dcom.core.JIVariant; import org.openscada.opc.lib.da.DataCallback; import org.openscada.opc.lib.da.Item; import org.openscada.opc.lib.da.ItemState; public class DataCallBack_OP120B_F_HEART_BEAT implements DataCallback { private OPCComm opc = OPCComm.getInstance(); private int count = 0; @Override public void changed(Item item, ItemState itemState) { // 读取心跳变量的值 try { if (itemState.getValue().getType() == JIVariant.VT_BOOL) { boolean value = itemState.getValue().getObjectAsBoolean(); setHeartbeat(value); } } catch (JIException e) { e.printStackTrace(); } } /** * 设置心跳标志位,保存到全局变量 */ public void setHeartbeat(boolean value) { if (!value) { OPCUnit.write(opc.getOP120B_F_HEART_BEAT(), "true"); GlobalVariable.OP120B_F_HEART_BEAT = true; count = 0; } else { count++; } // 循环读取频率是:500ms一次,如果超过6次,也就是3s认定超时 if (count > 6) { GlobalVariable.OP120B_F_HEART_BEAT = false; } } }